goat3d

view goatview/src/goatview.cc @ 94:21319e71117f

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