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