goat3d

view 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
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 }
237 }
239 void GoatView::open_anim()
240 {
241 statusBar()->showMessage("opening animation...");
242 }
245 // ---- OpenGL viewport ----
246 GoatViewport::GoatViewport(QWidget *main_win)
247 : QGLWidget(QGLFormat(QGL::DepthBuffer | QGL::SampleBuffers))
248 {
249 this->main_win = main_win;
250 initialized = false;
251 }
253 GoatViewport::~GoatViewport()
254 {
255 }
257 QSize GoatViewport::sizeHint() const
258 {
259 return QSize(800, 600);
260 }
262 #define CRITICAL(error, detail) \
263 do { \
264 fprintf(stderr, "%s: %s\n", error, detail); \
265 QMessageBox::critical(main_win, error, detail); \
266 abort(); \
267 } while(0)
269 void GoatViewport::initializeGL()
270 {
271 if(initialized) return;
272 initialized = true;
274 init_opengl();
276 if(!GLEW_ARB_transpose_matrix) {
277 CRITICAL("OpenGL initialization failed", "ARB_transpose_matrix extension not found!");
278 }
280 glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
282 glEnable(GL_DEPTH_TEST);
283 glEnable(GL_CULL_FACE);
284 if(use_lighting) {
285 glEnable(GL_LIGHTING);
286 }
287 glEnable(GL_LIGHT0);
289 float ldir[] = {-1, 1, 2, 0};
290 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
292 glEnable(GL_MULTISAMPLE);
293 }
295 void GoatViewport::resizeGL(int xsz, int ysz)
296 {
297 glViewport(0, 0, xsz, ysz);
298 glMatrixMode(GL_PROJECTION);
299 glLoadIdentity();
300 gluPerspective(60.0, (float)xsz / (float)ysz, 0.5, 5000.0);
301 }
303 void GoatViewport::paintGL()
304 {
305 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
307 glMatrixMode(GL_MODELVIEW);
308 glLoadIdentity();
309 glTranslatef(0, 0, -cam_dist);
310 glRotatef(cam_phi, 1, 0, 0);
311 glRotatef(cam_theta, 0, 1, 0);
313 draw_grid();
315 if(!scene) return;
317 if(use_nodes) {
318 int node_count = goat3d_get_node_count(scene);
319 for(int i=0; i<node_count; i++) {
320 goat3d_node *node = goat3d_get_node(scene, i);
321 if(!goat3d_get_node_parent(node)) {
322 draw_node(node); // only draw root nodes, the rest will be drawn recursively
323 }
324 }
325 } else {
326 int mesh_count = goat3d_get_mesh_count(scene);
327 for(int i=0; i<mesh_count; i++) {
328 goat3d_mesh *mesh = goat3d_get_mesh(scene, i);
329 draw_mesh(mesh);
330 }
331 }
332 }
334 void GoatViewport::toggle_lighting()
335 {
336 use_lighting = !use_lighting;
337 if(use_lighting) {
338 glEnable(GL_LIGHTING);
339 } else {
340 glDisable(GL_LIGHTING);
341 }
342 updateGL();
343 }
345 #ifndef GLEW_ARB_transpose_matrix
346 #error "GLEW_ARB_transpose_matrix undefined?"
347 #endif
349 static void draw_grid()
350 {
351 float viewsz_orig = cam_dist * tan(DEG_TO_RAD(fov) / 2.0);
352 float sz1 = next_pow2((int)viewsz_orig);
353 float sz0 = sz1 / 2.0;
354 float t = (viewsz_orig - sz0) / (sz1 - sz0);
355 float alpha = t < 0.333333 ? 0.0 : (t > 0.666666 ? 1.0 : (t - 0.333333) / 0.333333);
357 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BITS);
359 glEnable(GL_BLEND);
360 glDepthFunc(GL_ALWAYS);
362 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
363 draw_grid(sz0 * 2.0, 10, 1.0 - alpha);
364 draw_grid(sz1 * 2.0, 10, alpha);
366 glPopAttrib();
367 }
369 static void draw_grid(float sz, int nlines, float alpha)
370 {
371 float hsz = sz / 2.0;
372 float offs = sz / (float)nlines;
374 glPushAttrib(GL_ENABLE_BIT);
375 glDisable(GL_LIGHTING);
376 glDisable(GL_TEXTURE_2D);
378 glLineWidth(2.0);
379 glBegin(GL_LINES);
380 glColor4f(1, 0, 0, alpha);
381 glVertex3f(-hsz, 0, 0);
382 glVertex3f(hsz, 0, 0);
383 glColor4f(0, 0, 1, alpha);
384 glVertex3f(0, 0, -hsz);
385 glVertex3f(0, 0, hsz);
386 glEnd();
388 glLineWidth(1.0);
389 glBegin(GL_LINES);
390 glColor4f(0.5, 0.5, 0.5, alpha);
391 for(int i=0; i<nlines / 2; i++) {
392 float dist = (float)(i + 1) * offs;
393 for(int j=0; j<2; j++) {
394 float sign = j > 0 ? -1.0 : 1.0;
395 glVertex3f(-hsz, 0, dist * sign);
396 glVertex3f(hsz, 0, dist * sign);
397 glVertex3f(dist * sign, 0, -hsz);
398 glVertex3f(dist * sign, 0, hsz);
399 }
400 }
401 glEnd();
403 glPopAttrib();
404 }
406 static void draw_node(goat3d_node *node)
407 {
408 SceneNodeData *data = sdata ? sdata->get_node_data(node) : 0;
409 if(!data) return;
411 float xform[16];
412 goat3d_get_node_matrix(node, xform, anim_time);
414 glPushMatrix();
415 glMultTransposeMatrixf(xform);
417 if(data->visible) {
418 if(goat3d_get_node_type(node) == GOAT3D_NODE_MESH) {
419 goat3d_mesh *mesh = (goat3d_mesh*)goat3d_get_node_object(node);
421 draw_mesh(mesh);
423 if(data->selected) {
424 float bmin[3], bmax[3];
425 goat3d_get_mesh_bounds(mesh, bmin, bmax);
427 glPushAttrib(GL_ENABLE_BIT);
428 glDisable(GL_LIGHTING);
430 glColor3f(0.3, 1, 0.2);
432 glBegin(GL_LINE_LOOP);
433 glVertex3f(bmin[0], bmin[1], bmin[2]);
434 glVertex3f(bmax[0], bmin[1], bmin[2]);
435 glVertex3f(bmax[0], bmin[1], bmax[2]);
436 glVertex3f(bmin[0], bmin[1], bmax[2]);
437 glEnd();
438 glBegin(GL_LINE_LOOP);
439 glVertex3f(bmin[0], bmax[1], bmin[2]);
440 glVertex3f(bmax[0], bmax[1], bmin[2]);
441 glVertex3f(bmax[0], bmax[1], bmax[2]);
442 glVertex3f(bmin[0], bmax[1], bmax[2]);
443 glEnd();
444 glBegin(GL_LINES);
445 glVertex3f(bmin[0], bmin[1], bmin[2]);
446 glVertex3f(bmin[0], bmax[1], bmin[2]);
447 glVertex3f(bmin[0], bmin[1], bmax[2]);
448 glVertex3f(bmin[0], bmax[1], bmax[2]);
449 glVertex3f(bmax[0], bmin[1], bmin[2]);
450 glVertex3f(bmax[0], bmax[1], bmin[2]);
451 glVertex3f(bmax[0], bmin[1], bmax[2]);
452 glVertex3f(bmax[0], bmax[1], bmax[2]);
453 glEnd();
455 glPopAttrib();
456 }
457 }
458 }
460 int num_child = goat3d_get_node_child_count(node);
461 for(int i=0; i<num_child; i++) {
462 draw_node(goat3d_get_node_child(node, i));
463 }
465 glPopMatrix();
466 }
468 static void draw_mesh(goat3d_mesh *mesh)
469 {
470 static const float white[] = {1, 1, 1, 1};
471 static const float black[] = {0, 0, 0, 1};
473 const float *color;
474 goat3d_material *mtl = goat3d_get_mesh_mtl(mesh);
476 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_DIFFUSE))) {
477 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
478 glColor3fv(color);
479 } else {
480 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, white);
481 glColor3fv(white);
482 }
483 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_SPECULAR))) {
484 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
485 } else {
486 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
487 }
488 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_SHININESS))) {
489 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, color[0]);
490 } else {
491 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
492 }
493 // TODO texture
496 int num_faces = goat3d_get_mesh_face_count(mesh);
497 int num_verts = goat3d_get_mesh_attrib_count(mesh, GOAT3D_MESH_ATTR_VERTEX);
499 glEnableClientState(GL_VERTEX_ARRAY);
500 glVertexPointer(3, GL_FLOAT, 0, goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX));
502 float *data;
503 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL))) {
504 glEnableClientState(GL_NORMAL_ARRAY);
505 glNormalPointer(GL_FLOAT, 0, data);
506 }
507 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
508 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
509 glTexCoordPointer(2, GL_FLOAT, 0, data);
510 }
512 int *indices;
513 if((indices = goat3d_get_mesh_faces(mesh))) {
514 glDrawElements(GL_TRIANGLES, num_faces * 3, GL_UNSIGNED_INT, indices);
515 } else {
516 glDrawArrays(GL_TRIANGLES, 0, num_verts * 3);
517 }
519 glDisableClientState(GL_VERTEX_ARRAY);
520 glDisableClientState(GL_NORMAL_ARRAY);
521 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
522 }
525 static float prev_x, prev_y;
526 void GoatViewport::mousePressEvent(QMouseEvent *ev)
527 {
528 prev_x = ev->x();
529 prev_y = ev->y();
530 }
532 void GoatViewport::mouseMoveEvent(QMouseEvent *ev)
533 {
534 int dx = ev->x() - prev_x;
535 int dy = ev->y() - prev_y;
536 prev_x = ev->x();
537 prev_y = ev->y();
539 if(!dx && !dy) return;
541 if(ev->buttons() & Qt::LeftButton) {
542 cam_theta += dx * 0.5;
543 cam_phi += dy * 0.5;
545 if(cam_phi < -90) cam_phi = -90;
546 if(cam_phi > 90) cam_phi = 90;
547 }
548 if(ev->buttons() & Qt::RightButton) {
549 cam_dist += dy * 0.1;
551 if(cam_dist < 0.0) cam_dist = 0.0;
552 }
553 updateGL();
554 }
556 static const char *about_str =
557 "GoatView - Goat3D scene file viewer<br>"
558 "Copyright (C) 2014 John Tsiombikas &lt;<a href=\"mailto:nuclear@mutantstargoat.com\">nuclear@mutantstargoat.com</a>&gt;<br>"
559 "<br>"
560 "This program is free software: you can redistribute it and/or modify<br>"
561 "it under the terms of the GNU General Public License as published by<br>"
562 "the Free Software Foundation, either version 3 of the License, or<br>"
563 "(at your option) any later version.<br>"
564 "<br>"
565 "This program is distributed in the hope that it will be useful,<br>"
566 "but WITHOUT ANY WARRANTY; without even the implied warranty of<br>"
567 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>"
568 "GNU General Public License for more details.<br>"
569 "<br>"
570 "You should have received a copy of the GNU General Public License<br>"
571 "along with this program. If not, see <a href=\"http://www.gnu.org/licenses/gpl\">http://www.gnu.org/licenses/gpl</a>.";
573 void GoatView::show_about()
574 {
575 QMessageBox::information(this, "About GoatView", about_str);
576 }
579 static int next_pow2(int x)
580 {
581 x--;
582 x = (x >> 1) | x;
583 x = (x >> 2) | x;
584 x = (x >> 4) | x;
585 x = (x >> 8) | x;
586 x = (x >> 16) | x;
587 return x + 1;
588 }