goat3d

annotate goatview/src/goatview.cc @ 102:0ea2b355bc96

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 12 Sep 2015 17:32:12 +0300
parents a7c7fbeef93b
children 45a9d493e98c
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@100 337
nuclear@100 338 grp_anim_ctl->setDisabled(true);
nuclear@100 339 grp_anim_time->setDisabled(true);
nuclear@98 340 }
nuclear@98 341
nuclear@73 342 void GoatView::open_anim()
nuclear@73 343 {
nuclear@94 344 std::string fname = QFileDialog::getOpenFileName(this, "Open animation file", "",
nuclear@94 345 "Goat3D Animation (*.goatanm);;All Files (*)").toStdString();
nuclear@94 346 if(fname.empty()) {
nuclear@98 347 statusBar()->showMessage("Abort: No file selected!");
nuclear@94 348 return;
nuclear@94 349 }
nuclear@94 350
nuclear@94 351 statusBar()->showMessage("opening animation file");
nuclear@94 352 if(!load_anim(fname.c_str())) {
nuclear@94 353 statusBar()->showMessage("failed to load animation file");
nuclear@94 354 return;
nuclear@94 355 }
nuclear@95 356 statusBar()->showMessage("Successfully loaded animation: " + QString(fname.c_str()));
nuclear@73 357 }
nuclear@73 358
nuclear@73 359
nuclear@73 360 // ---- OpenGL viewport ----
nuclear@82 361 GoatViewport::GoatViewport(QWidget *main_win)
nuclear@92 362 : QGLWidget(QGLFormat(QGL::DepthBuffer | QGL::SampleBuffers))
nuclear@73 363 {
nuclear@82 364 this->main_win = main_win;
nuclear@82 365 initialized = false;
nuclear@73 366 }
nuclear@73 367
nuclear@73 368 GoatViewport::~GoatViewport()
nuclear@73 369 {
nuclear@73 370 }
nuclear@73 371
nuclear@73 372 QSize GoatViewport::sizeHint() const
nuclear@73 373 {
nuclear@73 374 return QSize(800, 600);
nuclear@73 375 }
nuclear@73 376
nuclear@82 377 #define CRITICAL(error, detail) \
nuclear@82 378 do { \
nuclear@82 379 fprintf(stderr, "%s: %s\n", error, detail); \
nuclear@82 380 QMessageBox::critical(main_win, error, detail); \
nuclear@82 381 abort(); \
nuclear@82 382 } while(0)
nuclear@82 383
nuclear@73 384 void GoatViewport::initializeGL()
nuclear@73 385 {
nuclear@82 386 if(initialized) return;
nuclear@82 387 initialized = true;
nuclear@82 388
nuclear@79 389 init_opengl();
nuclear@79 390
nuclear@82 391 if(!GLEW_ARB_transpose_matrix) {
nuclear@82 392 CRITICAL("OpenGL initialization failed", "ARB_transpose_matrix extension not found!");
nuclear@82 393 }
nuclear@82 394
nuclear@79 395 glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
nuclear@78 396
nuclear@78 397 glEnable(GL_DEPTH_TEST);
nuclear@78 398 glEnable(GL_CULL_FACE);
nuclear@83 399 if(use_lighting) {
nuclear@83 400 glEnable(GL_LIGHTING);
nuclear@83 401 }
nuclear@83 402 glEnable(GL_LIGHT0);
nuclear@83 403
nuclear@83 404 float ldir[] = {-1, 1, 2, 0};
nuclear@83 405 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
nuclear@92 406
nuclear@92 407 glEnable(GL_MULTISAMPLE);
nuclear@73 408 }
nuclear@73 409
nuclear@73 410 void GoatViewport::resizeGL(int xsz, int ysz)
nuclear@73 411 {
nuclear@73 412 glViewport(0, 0, xsz, ysz);
nuclear@75 413 glMatrixMode(GL_PROJECTION);
nuclear@75 414 glLoadIdentity();
nuclear@75 415 gluPerspective(60.0, (float)xsz / (float)ysz, 0.5, 5000.0);
nuclear@73 416 }
nuclear@73 417
nuclear@73 418 void GoatViewport::paintGL()
nuclear@73 419 {
nuclear@75 420 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@75 421
nuclear@75 422 glMatrixMode(GL_MODELVIEW);
nuclear@75 423 glLoadIdentity();
nuclear@75 424 glTranslatef(0, 0, -cam_dist);
nuclear@75 425 glRotatef(cam_phi, 1, 0, 0);
nuclear@75 426 glRotatef(cam_theta, 0, 1, 0);
nuclear@75 427
nuclear@101 428 if(scene) {
nuclear@101 429 if(use_nodes) {
nuclear@101 430 int node_count = goat3d_get_node_count(scene);
nuclear@101 431 for(int i=0; i<node_count; i++) {
nuclear@101 432 goat3d_node *node = goat3d_get_node(scene, i);
nuclear@101 433 if(!goat3d_get_node_parent(node)) {
nuclear@101 434 draw_node(node); // only draw root nodes, the rest will be drawn recursively
nuclear@101 435 }
nuclear@101 436 }
nuclear@101 437 } else {
nuclear@101 438 int mesh_count = goat3d_get_mesh_count(scene);
nuclear@101 439 for(int i=0; i<mesh_count; i++) {
nuclear@101 440 goat3d_mesh *mesh = goat3d_get_mesh(scene, i);
nuclear@101 441 draw_mesh(mesh);
nuclear@83 442 }
nuclear@82 443 }
nuclear@75 444 }
nuclear@101 445
nuclear@101 446 draw_grid();
nuclear@73 447 }
nuclear@75 448
nuclear@83 449 void GoatViewport::toggle_lighting()
nuclear@83 450 {
nuclear@83 451 use_lighting = !use_lighting;
nuclear@83 452 if(use_lighting) {
nuclear@83 453 glEnable(GL_LIGHTING);
nuclear@83 454 } else {
nuclear@83 455 glDisable(GL_LIGHTING);
nuclear@83 456 }
nuclear@102 457 post_redisplay();
nuclear@83 458 }
nuclear@83 459
nuclear@82 460 #ifndef GLEW_ARB_transpose_matrix
nuclear@82 461 #error "GLEW_ARB_transpose_matrix undefined?"
nuclear@82 462 #endif
nuclear@82 463
nuclear@90 464 static void draw_grid()
nuclear@90 465 {
nuclear@90 466 float viewsz_orig = cam_dist * tan(DEG_TO_RAD(fov) / 2.0);
nuclear@90 467 float sz1 = next_pow2((int)viewsz_orig);
nuclear@90 468 float sz0 = sz1 / 2.0;
nuclear@90 469 float t = (viewsz_orig - sz0) / (sz1 - sz0);
nuclear@90 470 float alpha = t < 0.333333 ? 0.0 : (t > 0.666666 ? 1.0 : (t - 0.333333) / 0.333333);
nuclear@90 471
nuclear@90 472 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BITS);
nuclear@90 473
nuclear@90 474 glEnable(GL_BLEND);
nuclear@101 475 glDepthMask(0);
nuclear@90 476
nuclear@90 477 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
nuclear@90 478 draw_grid(sz0 * 2.0, 10, 1.0 - alpha);
nuclear@90 479 draw_grid(sz1 * 2.0, 10, alpha);
nuclear@90 480
nuclear@101 481 glDepthMask(1);
nuclear@90 482 glPopAttrib();
nuclear@90 483 }
nuclear@90 484
nuclear@90 485 static void draw_grid(float sz, int nlines, float alpha)
nuclear@90 486 {
nuclear@90 487 float hsz = sz / 2.0;
nuclear@90 488 float offs = sz / (float)nlines;
nuclear@90 489
nuclear@90 490 glPushAttrib(GL_ENABLE_BIT);
nuclear@90 491 glDisable(GL_LIGHTING);
nuclear@90 492 glDisable(GL_TEXTURE_2D);
nuclear@90 493
nuclear@90 494 glLineWidth(2.0);
nuclear@90 495 glBegin(GL_LINES);
nuclear@90 496 glColor4f(1, 0, 0, alpha);
nuclear@90 497 glVertex3f(-hsz, 0, 0);
nuclear@90 498 glVertex3f(hsz, 0, 0);
nuclear@90 499 glColor4f(0, 0, 1, alpha);
nuclear@90 500 glVertex3f(0, 0, -hsz);
nuclear@90 501 glVertex3f(0, 0, hsz);
nuclear@90 502 glEnd();
nuclear@90 503
nuclear@90 504 glLineWidth(1.0);
nuclear@90 505 glBegin(GL_LINES);
nuclear@90 506 glColor4f(0.5, 0.5, 0.5, alpha);
nuclear@90 507 for(int i=0; i<nlines / 2; i++) {
nuclear@90 508 float dist = (float)(i + 1) * offs;
nuclear@90 509 for(int j=0; j<2; j++) {
nuclear@90 510 float sign = j > 0 ? -1.0 : 1.0;
nuclear@90 511 glVertex3f(-hsz, 0, dist * sign);
nuclear@90 512 glVertex3f(hsz, 0, dist * sign);
nuclear@90 513 glVertex3f(dist * sign, 0, -hsz);
nuclear@90 514 glVertex3f(dist * sign, 0, hsz);
nuclear@90 515 }
nuclear@90 516 }
nuclear@90 517 glEnd();
nuclear@90 518
nuclear@90 519 glPopAttrib();
nuclear@90 520 }
nuclear@90 521
nuclear@75 522 static void draw_node(goat3d_node *node)
nuclear@75 523 {
nuclear@85 524 SceneNodeData *data = sdata ? sdata->get_node_data(node) : 0;
nuclear@85 525 if(!data) return;
nuclear@85 526
nuclear@75 527 float xform[16];
nuclear@75 528 goat3d_get_node_matrix(node, xform, anim_time);
nuclear@78 529
nuclear@78 530 glPushMatrix();
nuclear@82 531 glMultTransposeMatrixf(xform);
nuclear@75 532
nuclear@85 533 if(data->visible) {
nuclear@85 534 if(goat3d_get_node_type(node) == GOAT3D_NODE_MESH) {
nuclear@85 535 goat3d_mesh *mesh = (goat3d_mesh*)goat3d_get_node_object(node);
nuclear@75 536
nuclear@85 537 draw_mesh(mesh);
nuclear@88 538
nuclear@88 539 if(data->selected) {
nuclear@88 540 float bmin[3], bmax[3];
nuclear@88 541 goat3d_get_mesh_bounds(mesh, bmin, bmax);
nuclear@88 542
nuclear@88 543 glPushAttrib(GL_ENABLE_BIT);
nuclear@88 544 glDisable(GL_LIGHTING);
nuclear@88 545
nuclear@88 546 glColor3f(0.3, 1, 0.2);
nuclear@88 547
nuclear@88 548 glBegin(GL_LINE_LOOP);
nuclear@88 549 glVertex3f(bmin[0], bmin[1], bmin[2]);
nuclear@88 550 glVertex3f(bmax[0], bmin[1], bmin[2]);
nuclear@88 551 glVertex3f(bmax[0], bmin[1], bmax[2]);
nuclear@88 552 glVertex3f(bmin[0], bmin[1], bmax[2]);
nuclear@88 553 glEnd();
nuclear@88 554 glBegin(GL_LINE_LOOP);
nuclear@88 555 glVertex3f(bmin[0], bmax[1], bmin[2]);
nuclear@88 556 glVertex3f(bmax[0], bmax[1], bmin[2]);
nuclear@88 557 glVertex3f(bmax[0], bmax[1], bmax[2]);
nuclear@88 558 glVertex3f(bmin[0], bmax[1], bmax[2]);
nuclear@88 559 glEnd();
nuclear@88 560 glBegin(GL_LINES);
nuclear@88 561 glVertex3f(bmin[0], bmin[1], bmin[2]);
nuclear@88 562 glVertex3f(bmin[0], bmax[1], bmin[2]);
nuclear@88 563 glVertex3f(bmin[0], bmin[1], bmax[2]);
nuclear@88 564 glVertex3f(bmin[0], bmax[1], bmax[2]);
nuclear@88 565 glVertex3f(bmax[0], bmin[1], bmin[2]);
nuclear@88 566 glVertex3f(bmax[0], bmax[1], bmin[2]);
nuclear@88 567 glVertex3f(bmax[0], bmin[1], bmax[2]);
nuclear@88 568 glVertex3f(bmax[0], bmax[1], bmax[2]);
nuclear@88 569 glEnd();
nuclear@88 570
nuclear@88 571 glPopAttrib();
nuclear@88 572 }
nuclear@85 573 }
nuclear@75 574 }
nuclear@75 575
nuclear@83 576 int num_child = goat3d_get_node_child_count(node);
nuclear@75 577 for(int i=0; i<num_child; i++) {
nuclear@75 578 draw_node(goat3d_get_node_child(node, i));
nuclear@83 579 }
nuclear@78 580
nuclear@78 581 glPopMatrix();
nuclear@78 582 }
nuclear@78 583
nuclear@82 584 static void draw_mesh(goat3d_mesh *mesh)
nuclear@82 585 {
nuclear@83 586 static const float white[] = {1, 1, 1, 1};
nuclear@83 587 static const float black[] = {0, 0, 0, 1};
nuclear@83 588
nuclear@83 589 const float *color;
nuclear@83 590 goat3d_material *mtl = goat3d_get_mesh_mtl(mesh);
nuclear@83 591
nuclear@83 592 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_DIFFUSE))) {
nuclear@83 593 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@90 594 glColor3fv(color);
nuclear@83 595 } else {
nuclear@83 596 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, white);
nuclear@90 597 glColor3fv(white);
nuclear@83 598 }
nuclear@83 599 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_SPECULAR))) {
nuclear@83 600 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
nuclear@83 601 } else {
nuclear@83 602 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
nuclear@83 603 }
nuclear@83 604 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_SHININESS))) {
nuclear@83 605 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, color[0]);
nuclear@83 606 } else {
nuclear@83 607 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
nuclear@83 608 }
nuclear@83 609 // TODO texture
nuclear@83 610
nuclear@83 611
nuclear@82 612 int num_faces = goat3d_get_mesh_face_count(mesh);
nuclear@82 613 int num_verts = goat3d_get_mesh_attrib_count(mesh, GOAT3D_MESH_ATTR_VERTEX);
nuclear@82 614
nuclear@82 615 glEnableClientState(GL_VERTEX_ARRAY);
nuclear@82 616 glVertexPointer(3, GL_FLOAT, 0, goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX));
nuclear@82 617
nuclear@82 618 float *data;
nuclear@82 619 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL))) {
nuclear@82 620 glEnableClientState(GL_NORMAL_ARRAY);
nuclear@82 621 glNormalPointer(GL_FLOAT, 0, data);
nuclear@82 622 }
nuclear@82 623 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
nuclear@82 624 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@82 625 glTexCoordPointer(2, GL_FLOAT, 0, data);
nuclear@82 626 }
nuclear@82 627
nuclear@82 628 int *indices;
nuclear@82 629 if((indices = goat3d_get_mesh_faces(mesh))) {
nuclear@82 630 glDrawElements(GL_TRIANGLES, num_faces * 3, GL_UNSIGNED_INT, indices);
nuclear@82 631 } else {
nuclear@82 632 glDrawArrays(GL_TRIANGLES, 0, num_verts * 3);
nuclear@82 633 }
nuclear@82 634
nuclear@82 635 glDisableClientState(GL_VERTEX_ARRAY);
nuclear@82 636 glDisableClientState(GL_NORMAL_ARRAY);
nuclear@82 637 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@82 638 }
nuclear@82 639
nuclear@78 640
nuclear@78 641 static float prev_x, prev_y;
nuclear@78 642 void GoatViewport::mousePressEvent(QMouseEvent *ev)
nuclear@78 643 {
nuclear@78 644 prev_x = ev->x();
nuclear@78 645 prev_y = ev->y();
nuclear@78 646 }
nuclear@78 647
nuclear@78 648 void GoatViewport::mouseMoveEvent(QMouseEvent *ev)
nuclear@78 649 {
nuclear@78 650 int dx = ev->x() - prev_x;
nuclear@78 651 int dy = ev->y() - prev_y;
nuclear@80 652 prev_x = ev->x();
nuclear@80 653 prev_y = ev->y();
nuclear@78 654
nuclear@78 655 if(!dx && !dy) return;
nuclear@78 656
nuclear@78 657 if(ev->buttons() & Qt::LeftButton) {
nuclear@78 658 cam_theta += dx * 0.5;
nuclear@78 659 cam_phi += dy * 0.5;
nuclear@78 660
nuclear@78 661 if(cam_phi < -90) cam_phi = -90;
nuclear@78 662 if(cam_phi > 90) cam_phi = 90;
nuclear@78 663 }
nuclear@78 664 if(ev->buttons() & Qt::RightButton) {
nuclear@78 665 cam_dist += dy * 0.1;
nuclear@78 666
nuclear@78 667 if(cam_dist < 0.0) cam_dist = 0.0;
nuclear@78 668 }
nuclear@102 669 post_redisplay();
nuclear@78 670 }
nuclear@83 671
nuclear@83 672 static const char *about_str =
nuclear@83 673 "GoatView - Goat3D scene file viewer<br>"
nuclear@83 674 "Copyright (C) 2014 John Tsiombikas &lt;<a href=\"mailto:nuclear@mutantstargoat.com\">nuclear@mutantstargoat.com</a>&gt;<br>"
nuclear@83 675 "<br>"
nuclear@83 676 "This program is free software: you can redistribute it and/or modify<br>"
nuclear@83 677 "it under the terms of the GNU General Public License as published by<br>"
nuclear@83 678 "the Free Software Foundation, either version 3 of the License, or<br>"
nuclear@83 679 "(at your option) any later version.<br>"
nuclear@83 680 "<br>"
nuclear@83 681 "This program is distributed in the hope that it will be useful,<br>"
nuclear@83 682 "but WITHOUT ANY WARRANTY; without even the implied warranty of<br>"
nuclear@83 683 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>"
nuclear@83 684 "GNU General Public License for more details.<br>"
nuclear@83 685 "<br>"
nuclear@83 686 "You should have received a copy of the GNU General Public License<br>"
nuclear@83 687 "along with this program. If not, see <a href=\"http://www.gnu.org/licenses/gpl\">http://www.gnu.org/licenses/gpl</a>.";
nuclear@83 688
nuclear@83 689 void GoatView::show_about()
nuclear@83 690 {
nuclear@83 691 QMessageBox::information(this, "About GoatView", about_str);
nuclear@83 692 }
nuclear@90 693
nuclear@93 694
nuclear@90 695 static int next_pow2(int x)
nuclear@90 696 {
nuclear@90 697 x--;
nuclear@90 698 x = (x >> 1) | x;
nuclear@90 699 x = (x >> 2) | x;
nuclear@90 700 x = (x >> 4) | x;
nuclear@90 701 x = (x >> 8) | x;
nuclear@90 702 x = (x >> 16) | x;
nuclear@90 703 return x + 1;
nuclear@90 704 }