goat3d

annotate goatview/src/goatview.cc @ 99:d7ab4f13f5af

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