goat3d

annotate goatview/src/goatview.cc @ 93:07c0ec4a410d

[goatview] line endings fix and missing post_redisplay in load_scene
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 18 May 2014 06:20:20 +0300
parents ae6c5941faac
children 21319e71117f
rev   line source
nuclear@78 1 #include <stdio.h>
nuclear@85 2 #include <map>
nuclear@82 3 #include "opengl.h"
nuclear@76 4 #include <QtOpenGL/QtOpenGL>
nuclear@76 5 #include <vmath/vmath.h>
nuclear@73 6 #include "goatview.h"
nuclear@74 7 #include "goat3d.h"
nuclear@74 8
nuclear@90 9 static void draw_grid();
nuclear@90 10 static void draw_grid(float sz, int nlines, float alpha = 1.0f);
nuclear@78 11 static void draw_node(goat3d_node *node);
nuclear@82 12 static void draw_mesh(goat3d_mesh *mesh);
nuclear@90 13 static int next_pow2(int x);
nuclear@78 14
nuclear@74 15 goat3d *scene;
nuclear@85 16 static SceneModel *sdata;
nuclear@88 17 static GoatViewport *glview;
nuclear@75 18
nuclear@75 19 static long anim_time;
nuclear@90 20 static float cam_theta, cam_phi = 25, cam_dist = 8;
nuclear@76 21 static float fov = 60.0;
nuclear@82 22 static bool use_nodes = true;
nuclear@83 23 static bool use_lighting = true;
nuclear@92 24 static bool use_textures = true;
nuclear@75 25
nuclear@88 26 void post_redisplay()
nuclear@88 27 {
nuclear@90 28 if(glview) {
nuclear@90 29 glview->updateGL();
nuclear@90 30 }
nuclear@88 31 }
nuclear@88 32
nuclear@82 33
nuclear@82 34 GoatView::GoatView()
nuclear@82 35 {
nuclear@82 36 glview = 0;
nuclear@85 37 scene_model = 0;
nuclear@82 38
nuclear@92 39 QGLFormat glfmt = QGLFormat::defaultFormat();
nuclear@92 40 glfmt.setSampleBuffers(true);
nuclear@92 41 QGLFormat::setDefaultFormat(glfmt);
nuclear@92 42
nuclear@84 43 QSettings settings;
nuclear@84 44 resize(settings.value("main/size", QSize(1024, 768)).toSize());
nuclear@84 45 move(settings.value("main/pos", QPoint(100, 100)).toPoint());
nuclear@84 46 use_nodes = settings.value("use_nodes", true).toBool();
nuclear@84 47 use_lighting = settings.value("use_lighting", true).toBool();
nuclear@92 48 use_textures = settings.value("use_textures", true).toBool();
nuclear@83 49
nuclear@83 50 make_center(); // must be first
nuclear@82 51 make_menu();
nuclear@82 52 make_dock();
nuclear@82 53
nuclear@82 54 statusBar();
nuclear@82 55
nuclear@82 56 setWindowTitle("GoatView");
nuclear@82 57 }
nuclear@82 58
nuclear@82 59 GoatView::~GoatView()
nuclear@82 60 {
nuclear@85 61 delete scene_model;
nuclear@85 62 sdata = 0;
nuclear@82 63 }
nuclear@82 64
nuclear@83 65 void GoatView::closeEvent(QCloseEvent *ev)
nuclear@83 66 {
nuclear@84 67 QSettings settings;
nuclear@84 68 settings.setValue("main/size", size());
nuclear@84 69 settings.setValue("main/pos", pos());
nuclear@84 70 settings.setValue("use_nodes", use_nodes);
nuclear@84 71 settings.setValue("use_lighting", use_lighting);
nuclear@83 72 }
nuclear@83 73
nuclear@83 74
nuclear@82 75 bool GoatView::load_scene(const char *fname)
nuclear@76 76 {
nuclear@76 77 if(scene) {
nuclear@76 78 goat3d_free(scene);
nuclear@76 79 }
nuclear@76 80 if(!(scene = goat3d_create()) || goat3d_load(scene, fname) == -1) {
nuclear@76 81 return false;
nuclear@76 82 }
nuclear@76 83
nuclear@76 84 float bmin[3], bmax[3];
nuclear@82 85 if(goat3d_get_bounds(scene, bmin, bmax) != -1) {
nuclear@82 86 float bsize = (Vector3(bmax[0], bmax[1], bmax[2]) - Vector3(bmin[0], bmin[1], bmin[2])).length();
nuclear@83 87 cam_dist = bsize / tan(DEG_TO_RAD(fov) / 2.0);
nuclear@82 88 printf("bounds size: %f, cam_dist: %f\n", bsize, cam_dist);
nuclear@82 89 }
nuclear@76 90
nuclear@85 91 scene_model->set_scene(scene);
nuclear@85 92 treeview->expandAll();
nuclear@85 93 treeview->resizeColumnToContents(0);
nuclear@85 94
nuclear@85 95 sdata = scene_model; // set the global sdata ptr
nuclear@93 96 post_redisplay();
nuclear@76 97 return true;
nuclear@76 98 }
nuclear@73 99
nuclear@73 100 bool GoatView::make_menu()
nuclear@73 101 {
nuclear@82 102 // file menu
nuclear@73 103 QMenu *menu_file = menuBar()->addMenu("&File");
nuclear@73 104
nuclear@73 105 QAction *act_open_sce = new QAction("&Open Scene", this);
nuclear@73 106 act_open_sce->setShortcuts(QKeySequence::Open);
nuclear@73 107 connect(act_open_sce, &QAction::triggered, this, &GoatView::open_scene);
nuclear@73 108 menu_file->addAction(act_open_sce);
nuclear@73 109
nuclear@73 110 QAction *act_open_anm = new QAction("Open &Animation", this);
nuclear@73 111 connect(act_open_anm, &QAction::triggered, this, &GoatView::open_anim);
nuclear@73 112 menu_file->addAction(act_open_anm);
nuclear@73 113
nuclear@73 114 QAction *act_quit = new QAction("&Quit", this);
nuclear@73 115 act_quit->setShortcuts(QKeySequence::Quit);
nuclear@90 116 connect(act_quit, &QAction::triggered, [&](){ qApp->quit(); });
nuclear@73 117 menu_file->addAction(act_quit);
nuclear@82 118
nuclear@82 119 // view menu
nuclear@82 120 QMenu *menu_view = menuBar()->addMenu("&View");
nuclear@82 121
nuclear@82 122 QAction *act_use_nodes = new QAction("use nodes", this);
nuclear@82 123 act_use_nodes->setCheckable(true);
nuclear@82 124 act_use_nodes->setChecked(use_nodes);
nuclear@83 125 connect(act_use_nodes, &QAction::triggered, this,
nuclear@88 126 [&](){ use_nodes = !use_nodes; post_redisplay(); });
nuclear@82 127 menu_view->addAction(act_use_nodes);
nuclear@83 128
nuclear@83 129 QAction *act_use_lighting = new QAction("lighting", this);
nuclear@83 130 act_use_lighting->setCheckable(true);
nuclear@83 131 act_use_lighting->setChecked(use_lighting);
nuclear@83 132 connect(act_use_lighting, &QAction::triggered, glview, &GoatViewport::toggle_lighting);
nuclear@83 133 menu_view->addAction(act_use_lighting);
nuclear@83 134
nuclear@83 135 // help menu
nuclear@83 136 QMenu *menu_help = menuBar()->addMenu("&Help");
nuclear@83 137
nuclear@83 138 QAction *act_about = new QAction("&About", this);
nuclear@83 139 connect(act_about, &QAction::triggered, this, &GoatView::show_about);
nuclear@83 140 menu_help->addAction(act_about);
nuclear@73 141 return true;
nuclear@73 142 }
nuclear@73 143
nuclear@73 144 bool GoatView::make_dock()
nuclear@73 145 {
nuclear@73 146 // ---- side-dock ----
nuclear@73 147 QWidget *dock_cont = new QWidget;
nuclear@73 148 QVBoxLayout *dock_vbox = new QVBoxLayout;
nuclear@73 149 dock_cont->setLayout(dock_vbox);
nuclear@73 150
nuclear@90 151 QDockWidget *dock = new QDockWidget(this);
nuclear@73 152 dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
nuclear@73 153 dock->setWidget(dock_cont);
nuclear@73 154 addDockWidget(Qt::LeftDockWidgetArea, dock);
nuclear@73 155
nuclear@82 156 // make the tree view widget
nuclear@85 157 treeview = new QTreeView;
nuclear@85 158 treeview->setAlternatingRowColors(true);
nuclear@88 159 treeview->setSelectionMode(QAbstractItemView::SingleSelection);
nuclear@85 160 dock_vbox->addWidget(treeview);
nuclear@82 161
nuclear@85 162 scene_model = new SceneModel;
nuclear@88 163 connect(scene_model, &SceneModel::dataChanged, [&](){ post_redisplay(); });
nuclear@85 164 treeview->setModel(scene_model);
nuclear@82 165
nuclear@88 166 connect(treeview->selectionModel(), &QItemSelectionModel::selectionChanged,
nuclear@88 167 [&](){ scene_model->selchange(treeview->selectionModel()->selectedIndexes()); });
nuclear@88 168
nuclear@82 169 // misc
nuclear@82 170 QPushButton *bn_quit = new QPushButton("quit");
nuclear@82 171 dock_vbox->addWidget(bn_quit);
nuclear@85 172 connect(bn_quit, &QPushButton::clicked, [&](){ qApp->quit(); });
nuclear@82 173
nuclear@73 174 // ---- bottom dock ----
nuclear@73 175 dock_cont = new QWidget;
nuclear@73 176 QHBoxLayout *dock_hbox = new QHBoxLayout;
nuclear@73 177 dock_cont->setLayout(dock_hbox);
nuclear@73 178
nuclear@90 179 // animation control box
nuclear@90 180 QGridLayout *anim_ctl_box = new QGridLayout;
nuclear@90 181 dock_hbox->addLayout(anim_ctl_box);
nuclear@90 182
nuclear@90 183 anim_ctl_box->addWidget(new QLabel("Animation"), 0, 0);
nuclear@90 184 cbox_anims = new QComboBox;
nuclear@90 185 cbox_anims->setDisabled(true);
nuclear@90 186 anim_ctl_box->addWidget(cbox_anims, 0, 1);
nuclear@90 187
nuclear@90 188 chk_loop = new QCheckBox("loop");
nuclear@90 189 chk_loop->setChecked(false);
nuclear@90 190 anim_ctl_box->addWidget(chk_loop, 1, 0);
nuclear@90 191
nuclear@90 192 QToolBar *toolbar_ctl = new QToolBar;
nuclear@90 193 anim_ctl_box->addWidget(toolbar_ctl, 1, 1);
nuclear@90 194
nuclear@90 195 act_rewind = new QAction(style()->standardIcon(QStyle::SP_MediaSkipBackward), "Rewind", this);
nuclear@90 196 act_rewind->setDisabled(true);
nuclear@90 197 toolbar_ctl->addAction(act_rewind);
nuclear@90 198 act_play = new QAction(style()->standardIcon(QStyle::SP_MediaPlay), "Play", this);
nuclear@90 199 act_play->setDisabled(true);
nuclear@90 200 toolbar_ctl->addAction(act_play);
nuclear@90 201
nuclear@90 202 // timeline slider
nuclear@90 203 slider_time = new QSlider(Qt::Orientation::Horizontal);
nuclear@73 204 slider_time->setDisabled(true);
nuclear@90 205 connect(slider_time, &QSlider::valueChanged,
nuclear@90 206 [&](){ anim_time = slider_time->value(); post_redisplay(); });
nuclear@73 207 dock_hbox->addWidget(slider_time);
nuclear@73 208
nuclear@90 209 dock = new QDockWidget(this);
nuclear@73 210 dock->setAllowedAreas(Qt::BottomDockWidgetArea);
nuclear@73 211 dock->setWidget(dock_cont);
nuclear@73 212 addDockWidget(Qt::BottomDockWidgetArea, dock);
nuclear@73 213
nuclear@73 214 return true;
nuclear@73 215 }
nuclear@73 216
nuclear@73 217 bool GoatView::make_center()
nuclear@73 218 {
nuclear@88 219 glview = ::glview = new GoatViewport(this);
nuclear@82 220 setCentralWidget(glview);
nuclear@73 221 return true;
nuclear@73 222 }
nuclear@73 223
nuclear@73 224 void GoatView::open_scene()
nuclear@73 225 {
nuclear@74 226 std::string fname = QFileDialog::getOpenFileName(this, "Open scene file", "",
nuclear@74 227 "Goat3D Scene (*.goatsce);;All Files (*)").toStdString();
nuclear@74 228 if(fname.empty()) {
nuclear@74 229 statusBar()->showMessage("Abort: No file selected!");
nuclear@74 230 return;
nuclear@74 231 }
nuclear@74 232
nuclear@74 233 statusBar()->showMessage("opening scene file");
nuclear@76 234 if(!load_scene(fname.c_str())) {
nuclear@74 235 statusBar()->showMessage("failed to load scene file");
nuclear@74 236 }
nuclear@73 237 }
nuclear@73 238
nuclear@73 239 void GoatView::open_anim()
nuclear@73 240 {
nuclear@73 241 statusBar()->showMessage("opening animation...");
nuclear@73 242 }
nuclear@73 243
nuclear@73 244
nuclear@73 245 // ---- OpenGL viewport ----
nuclear@82 246 GoatViewport::GoatViewport(QWidget *main_win)
nuclear@92 247 : QGLWidget(QGLFormat(QGL::DepthBuffer | QGL::SampleBuffers))
nuclear@73 248 {
nuclear@82 249 this->main_win = main_win;
nuclear@82 250 initialized = false;
nuclear@73 251 }
nuclear@73 252
nuclear@73 253 GoatViewport::~GoatViewport()
nuclear@73 254 {
nuclear@73 255 }
nuclear@73 256
nuclear@73 257 QSize GoatViewport::sizeHint() const
nuclear@73 258 {
nuclear@73 259 return QSize(800, 600);
nuclear@73 260 }
nuclear@73 261
nuclear@82 262 #define CRITICAL(error, detail) \
nuclear@82 263 do { \
nuclear@82 264 fprintf(stderr, "%s: %s\n", error, detail); \
nuclear@82 265 QMessageBox::critical(main_win, error, detail); \
nuclear@82 266 abort(); \
nuclear@82 267 } while(0)
nuclear@82 268
nuclear@73 269 void GoatViewport::initializeGL()
nuclear@73 270 {
nuclear@82 271 if(initialized) return;
nuclear@82 272 initialized = true;
nuclear@82 273
nuclear@79 274 init_opengl();
nuclear@79 275
nuclear@82 276 if(!GLEW_ARB_transpose_matrix) {
nuclear@82 277 CRITICAL("OpenGL initialization failed", "ARB_transpose_matrix extension not found!");
nuclear@82 278 }
nuclear@82 279
nuclear@79 280 glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
nuclear@78 281
nuclear@78 282 glEnable(GL_DEPTH_TEST);
nuclear@78 283 glEnable(GL_CULL_FACE);
nuclear@83 284 if(use_lighting) {
nuclear@83 285 glEnable(GL_LIGHTING);
nuclear@83 286 }
nuclear@83 287 glEnable(GL_LIGHT0);
nuclear@83 288
nuclear@83 289 float ldir[] = {-1, 1, 2, 0};
nuclear@83 290 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
nuclear@92 291
nuclear@92 292 glEnable(GL_MULTISAMPLE);
nuclear@73 293 }
nuclear@73 294
nuclear@73 295 void GoatViewport::resizeGL(int xsz, int ysz)
nuclear@73 296 {
nuclear@73 297 glViewport(0, 0, xsz, ysz);
nuclear@75 298 glMatrixMode(GL_PROJECTION);
nuclear@75 299 glLoadIdentity();
nuclear@75 300 gluPerspective(60.0, (float)xsz / (float)ysz, 0.5, 5000.0);
nuclear@73 301 }
nuclear@73 302
nuclear@73 303 void GoatViewport::paintGL()
nuclear@73 304 {
nuclear@75 305 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@75 306
nuclear@75 307 glMatrixMode(GL_MODELVIEW);
nuclear@75 308 glLoadIdentity();
nuclear@75 309 glTranslatef(0, 0, -cam_dist);
nuclear@75 310 glRotatef(cam_phi, 1, 0, 0);
nuclear@75 311 glRotatef(cam_theta, 0, 1, 0);
nuclear@75 312
nuclear@90 313 draw_grid();
nuclear@90 314
nuclear@82 315 if(!scene) return;
nuclear@82 316
nuclear@82 317 if(use_nodes) {
nuclear@75 318 int node_count = goat3d_get_node_count(scene);
nuclear@75 319 for(int i=0; i<node_count; i++) {
nuclear@75 320 goat3d_node *node = goat3d_get_node(scene, i);
nuclear@83 321 if(!goat3d_get_node_parent(node)) {
nuclear@83 322 draw_node(node); // only draw root nodes, the rest will be drawn recursively
nuclear@83 323 }
nuclear@82 324 }
nuclear@82 325 } else {
nuclear@82 326 int mesh_count = goat3d_get_mesh_count(scene);
nuclear@82 327 for(int i=0; i<mesh_count; i++) {
nuclear@82 328 goat3d_mesh *mesh = goat3d_get_mesh(scene, i);
nuclear@82 329 draw_mesh(mesh);
nuclear@75 330 }
nuclear@75 331 }
nuclear@73 332 }
nuclear@75 333
nuclear@83 334 void GoatViewport::toggle_lighting()
nuclear@83 335 {
nuclear@83 336 use_lighting = !use_lighting;
nuclear@83 337 if(use_lighting) {
nuclear@83 338 glEnable(GL_LIGHTING);
nuclear@83 339 } else {
nuclear@83 340 glDisable(GL_LIGHTING);
nuclear@83 341 }
nuclear@83 342 updateGL();
nuclear@83 343 }
nuclear@83 344
nuclear@82 345 #ifndef GLEW_ARB_transpose_matrix
nuclear@82 346 #error "GLEW_ARB_transpose_matrix undefined?"
nuclear@82 347 #endif
nuclear@82 348
nuclear@90 349 static void draw_grid()
nuclear@90 350 {
nuclear@90 351 float viewsz_orig = cam_dist * tan(DEG_TO_RAD(fov) / 2.0);
nuclear@90 352 float sz1 = next_pow2((int)viewsz_orig);
nuclear@90 353 float sz0 = sz1 / 2.0;
nuclear@90 354 float t = (viewsz_orig - sz0) / (sz1 - sz0);
nuclear@90 355 float alpha = t < 0.333333 ? 0.0 : (t > 0.666666 ? 1.0 : (t - 0.333333) / 0.333333);
nuclear@90 356
nuclear@90 357 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BITS);
nuclear@90 358
nuclear@90 359 glEnable(GL_BLEND);
nuclear@90 360 glDepthFunc(GL_ALWAYS);
nuclear@90 361
nuclear@90 362 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
nuclear@90 363 draw_grid(sz0 * 2.0, 10, 1.0 - alpha);
nuclear@90 364 draw_grid(sz1 * 2.0, 10, alpha);
nuclear@90 365
nuclear@90 366 glPopAttrib();
nuclear@90 367 }
nuclear@90 368
nuclear@90 369 static void draw_grid(float sz, int nlines, float alpha)
nuclear@90 370 {
nuclear@90 371 float hsz = sz / 2.0;
nuclear@90 372 float offs = sz / (float)nlines;
nuclear@90 373
nuclear@90 374 glPushAttrib(GL_ENABLE_BIT);
nuclear@90 375 glDisable(GL_LIGHTING);
nuclear@90 376 glDisable(GL_TEXTURE_2D);
nuclear@90 377
nuclear@90 378 glLineWidth(2.0);
nuclear@90 379 glBegin(GL_LINES);
nuclear@90 380 glColor4f(1, 0, 0, alpha);
nuclear@90 381 glVertex3f(-hsz, 0, 0);
nuclear@90 382 glVertex3f(hsz, 0, 0);
nuclear@90 383 glColor4f(0, 0, 1, alpha);
nuclear@90 384 glVertex3f(0, 0, -hsz);
nuclear@90 385 glVertex3f(0, 0, hsz);
nuclear@90 386 glEnd();
nuclear@90 387
nuclear@90 388 glLineWidth(1.0);
nuclear@90 389 glBegin(GL_LINES);
nuclear@90 390 glColor4f(0.5, 0.5, 0.5, alpha);
nuclear@90 391 for(int i=0; i<nlines / 2; i++) {
nuclear@90 392 float dist = (float)(i + 1) * offs;
nuclear@90 393 for(int j=0; j<2; j++) {
nuclear@90 394 float sign = j > 0 ? -1.0 : 1.0;
nuclear@90 395 glVertex3f(-hsz, 0, dist * sign);
nuclear@90 396 glVertex3f(hsz, 0, dist * sign);
nuclear@90 397 glVertex3f(dist * sign, 0, -hsz);
nuclear@90 398 glVertex3f(dist * sign, 0, hsz);
nuclear@90 399 }
nuclear@90 400 }
nuclear@90 401 glEnd();
nuclear@90 402
nuclear@90 403 glPopAttrib();
nuclear@90 404 }
nuclear@90 405
nuclear@75 406 static void draw_node(goat3d_node *node)
nuclear@75 407 {
nuclear@85 408 SceneNodeData *data = sdata ? sdata->get_node_data(node) : 0;
nuclear@85 409 if(!data) return;
nuclear@85 410
nuclear@75 411 float xform[16];
nuclear@75 412 goat3d_get_node_matrix(node, xform, anim_time);
nuclear@78 413
nuclear@78 414 glPushMatrix();
nuclear@82 415 glMultTransposeMatrixf(xform);
nuclear@75 416
nuclear@85 417 if(data->visible) {
nuclear@85 418 if(goat3d_get_node_type(node) == GOAT3D_NODE_MESH) {
nuclear@85 419 goat3d_mesh *mesh = (goat3d_mesh*)goat3d_get_node_object(node);
nuclear@75 420
nuclear@85 421 draw_mesh(mesh);
nuclear@88 422
nuclear@88 423 if(data->selected) {
nuclear@88 424 float bmin[3], bmax[3];
nuclear@88 425 goat3d_get_mesh_bounds(mesh, bmin, bmax);
nuclear@88 426
nuclear@88 427 glPushAttrib(GL_ENABLE_BIT);
nuclear@88 428 glDisable(GL_LIGHTING);
nuclear@88 429
nuclear@88 430 glColor3f(0.3, 1, 0.2);
nuclear@88 431
nuclear@88 432 glBegin(GL_LINE_LOOP);
nuclear@88 433 glVertex3f(bmin[0], bmin[1], bmin[2]);
nuclear@88 434 glVertex3f(bmax[0], bmin[1], bmin[2]);
nuclear@88 435 glVertex3f(bmax[0], bmin[1], bmax[2]);
nuclear@88 436 glVertex3f(bmin[0], bmin[1], bmax[2]);
nuclear@88 437 glEnd();
nuclear@88 438 glBegin(GL_LINE_LOOP);
nuclear@88 439 glVertex3f(bmin[0], bmax[1], bmin[2]);
nuclear@88 440 glVertex3f(bmax[0], bmax[1], bmin[2]);
nuclear@88 441 glVertex3f(bmax[0], bmax[1], bmax[2]);
nuclear@88 442 glVertex3f(bmin[0], bmax[1], bmax[2]);
nuclear@88 443 glEnd();
nuclear@88 444 glBegin(GL_LINES);
nuclear@88 445 glVertex3f(bmin[0], bmin[1], bmin[2]);
nuclear@88 446 glVertex3f(bmin[0], bmax[1], bmin[2]);
nuclear@88 447 glVertex3f(bmin[0], bmin[1], bmax[2]);
nuclear@88 448 glVertex3f(bmin[0], bmax[1], bmax[2]);
nuclear@88 449 glVertex3f(bmax[0], bmin[1], bmin[2]);
nuclear@88 450 glVertex3f(bmax[0], bmax[1], bmin[2]);
nuclear@88 451 glVertex3f(bmax[0], bmin[1], bmax[2]);
nuclear@88 452 glVertex3f(bmax[0], bmax[1], bmax[2]);
nuclear@88 453 glEnd();
nuclear@88 454
nuclear@88 455 glPopAttrib();
nuclear@88 456 }
nuclear@85 457 }
nuclear@75 458 }
nuclear@75 459
nuclear@83 460 int num_child = goat3d_get_node_child_count(node);
nuclear@75 461 for(int i=0; i<num_child; i++) {
nuclear@75 462 draw_node(goat3d_get_node_child(node, i));
nuclear@83 463 }
nuclear@78 464
nuclear@78 465 glPopMatrix();
nuclear@78 466 }
nuclear@78 467
nuclear@82 468 static void draw_mesh(goat3d_mesh *mesh)
nuclear@82 469 {
nuclear@83 470 static const float white[] = {1, 1, 1, 1};
nuclear@83 471 static const float black[] = {0, 0, 0, 1};
nuclear@83 472
nuclear@83 473 const float *color;
nuclear@83 474 goat3d_material *mtl = goat3d_get_mesh_mtl(mesh);
nuclear@83 475
nuclear@83 476 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_DIFFUSE))) {
nuclear@83 477 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@90 478 glColor3fv(color);
nuclear@83 479 } else {
nuclear@83 480 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, white);
nuclear@90 481 glColor3fv(white);
nuclear@83 482 }
nuclear@83 483 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_SPECULAR))) {
nuclear@83 484 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
nuclear@83 485 } else {
nuclear@83 486 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
nuclear@83 487 }
nuclear@83 488 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_SHININESS))) {
nuclear@83 489 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, color[0]);
nuclear@83 490 } else {
nuclear@83 491 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
nuclear@83 492 }
nuclear@83 493 // TODO texture
nuclear@83 494
nuclear@83 495
nuclear@82 496 int num_faces = goat3d_get_mesh_face_count(mesh);
nuclear@82 497 int num_verts = goat3d_get_mesh_attrib_count(mesh, GOAT3D_MESH_ATTR_VERTEX);
nuclear@82 498
nuclear@82 499 glEnableClientState(GL_VERTEX_ARRAY);
nuclear@82 500 glVertexPointer(3, GL_FLOAT, 0, goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX));
nuclear@82 501
nuclear@82 502 float *data;
nuclear@82 503 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL))) {
nuclear@82 504 glEnableClientState(GL_NORMAL_ARRAY);
nuclear@82 505 glNormalPointer(GL_FLOAT, 0, data);
nuclear@82 506 }
nuclear@82 507 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
nuclear@82 508 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@82 509 glTexCoordPointer(2, GL_FLOAT, 0, data);
nuclear@82 510 }
nuclear@82 511
nuclear@82 512 int *indices;
nuclear@82 513 if((indices = goat3d_get_mesh_faces(mesh))) {
nuclear@82 514 glDrawElements(GL_TRIANGLES, num_faces * 3, GL_UNSIGNED_INT, indices);
nuclear@82 515 } else {
nuclear@82 516 glDrawArrays(GL_TRIANGLES, 0, num_verts * 3);
nuclear@82 517 }
nuclear@82 518
nuclear@82 519 glDisableClientState(GL_VERTEX_ARRAY);
nuclear@82 520 glDisableClientState(GL_NORMAL_ARRAY);
nuclear@82 521 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@82 522 }
nuclear@82 523
nuclear@78 524
nuclear@78 525 static float prev_x, prev_y;
nuclear@78 526 void GoatViewport::mousePressEvent(QMouseEvent *ev)
nuclear@78 527 {
nuclear@78 528 prev_x = ev->x();
nuclear@78 529 prev_y = ev->y();
nuclear@78 530 }
nuclear@78 531
nuclear@78 532 void GoatViewport::mouseMoveEvent(QMouseEvent *ev)
nuclear@78 533 {
nuclear@78 534 int dx = ev->x() - prev_x;
nuclear@78 535 int dy = ev->y() - prev_y;
nuclear@80 536 prev_x = ev->x();
nuclear@80 537 prev_y = ev->y();
nuclear@78 538
nuclear@78 539 if(!dx && !dy) return;
nuclear@78 540
nuclear@78 541 if(ev->buttons() & Qt::LeftButton) {
nuclear@78 542 cam_theta += dx * 0.5;
nuclear@78 543 cam_phi += dy * 0.5;
nuclear@78 544
nuclear@78 545 if(cam_phi < -90) cam_phi = -90;
nuclear@78 546 if(cam_phi > 90) cam_phi = 90;
nuclear@78 547 }
nuclear@78 548 if(ev->buttons() & Qt::RightButton) {
nuclear@78 549 cam_dist += dy * 0.1;
nuclear@78 550
nuclear@78 551 if(cam_dist < 0.0) cam_dist = 0.0;
nuclear@78 552 }
nuclear@78 553 updateGL();
nuclear@78 554 }
nuclear@83 555
nuclear@83 556 static const char *about_str =
nuclear@83 557 "GoatView - Goat3D scene file viewer<br>"
nuclear@83 558 "Copyright (C) 2014 John Tsiombikas &lt;<a href=\"mailto:nuclear@mutantstargoat.com\">nuclear@mutantstargoat.com</a>&gt;<br>"
nuclear@83 559 "<br>"
nuclear@83 560 "This program is free software: you can redistribute it and/or modify<br>"
nuclear@83 561 "it under the terms of the GNU General Public License as published by<br>"
nuclear@83 562 "the Free Software Foundation, either version 3 of the License, or<br>"
nuclear@83 563 "(at your option) any later version.<br>"
nuclear@83 564 "<br>"
nuclear@83 565 "This program is distributed in the hope that it will be useful,<br>"
nuclear@83 566 "but WITHOUT ANY WARRANTY; without even the implied warranty of<br>"
nuclear@83 567 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>"
nuclear@83 568 "GNU General Public License for more details.<br>"
nuclear@83 569 "<br>"
nuclear@83 570 "You should have received a copy of the GNU General Public License<br>"
nuclear@83 571 "along with this program. If not, see <a href=\"http://www.gnu.org/licenses/gpl\">http://www.gnu.org/licenses/gpl</a>.";
nuclear@83 572
nuclear@83 573 void GoatView::show_about()
nuclear@83 574 {
nuclear@83 575 QMessageBox::information(this, "About GoatView", about_str);
nuclear@83 576 }
nuclear@90 577
nuclear@93 578
nuclear@90 579 static int next_pow2(int x)
nuclear@90 580 {
nuclear@90 581 x--;
nuclear@90 582 x = (x >> 1) | x;
nuclear@90 583 x = (x >> 2) | x;
nuclear@90 584 x = (x >> 4) | x;
nuclear@90 585 x = (x >> 8) | x;
nuclear@90 586 x = (x >> 16) | x;
nuclear@90 587 return x + 1;
nuclear@90 588 }