goat3d

view goatview/src/goatview.cc @ 88:7941e89798e5

selections
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 15 May 2014 06:52:01 +0300
parents 91e3aa1a60c3
children 8b156bc5205b
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_node(goat3d_node *node);
10 static void draw_mesh(goat3d_mesh *mesh);
12 goat3d *scene;
13 static SceneModel *sdata;
14 static GoatViewport *glview;
16 static long anim_time;
17 static float cam_theta, cam_phi, cam_dist = 8;
18 static float fov = 60.0;
19 static bool use_nodes = true;
20 static bool use_lighting = true;
22 void post_redisplay()
23 {
24 glview->updateGL();
25 }
28 GoatView::GoatView()
29 {
30 glview = 0;
31 scene_model = 0;
33 QSettings settings;
34 resize(settings.value("main/size", QSize(1024, 768)).toSize());
35 move(settings.value("main/pos", QPoint(100, 100)).toPoint());
36 use_nodes = settings.value("use_nodes", true).toBool();
37 use_lighting = settings.value("use_lighting", true).toBool();
39 make_center(); // must be first
40 make_menu();
41 make_dock();
43 statusBar();
45 setWindowTitle("GoatView");
46 }
48 GoatView::~GoatView()
49 {
50 delete scene_model;
51 sdata = 0;
52 }
54 void GoatView::closeEvent(QCloseEvent *ev)
55 {
56 QSettings settings;
57 settings.setValue("main/size", size());
58 settings.setValue("main/pos", pos());
59 settings.setValue("use_nodes", use_nodes);
60 settings.setValue("use_lighting", use_lighting);
61 }
64 bool GoatView::load_scene(const char *fname)
65 {
66 if(scene) {
67 goat3d_free(scene);
68 }
69 if(!(scene = goat3d_create()) || goat3d_load(scene, fname) == -1) {
70 return false;
71 }
73 float bmin[3], bmax[3];
74 if(goat3d_get_bounds(scene, bmin, bmax) != -1) {
75 float bsize = (Vector3(bmax[0], bmax[1], bmax[2]) - Vector3(bmin[0], bmin[1], bmin[2])).length();
76 cam_dist = bsize / tan(DEG_TO_RAD(fov) / 2.0);
77 printf("bounds size: %f, cam_dist: %f\n", bsize, cam_dist);
78 }
80 scene_model->set_scene(scene);
81 treeview->expandAll();
82 treeview->resizeColumnToContents(0);
84 sdata = scene_model; // set the global sdata ptr
85 return true;
86 }
88 bool GoatView::make_menu()
89 {
90 // file menu
91 QMenu *menu_file = menuBar()->addMenu("&File");
93 QAction *act_open_sce = new QAction("&Open Scene", this);
94 act_open_sce->setShortcuts(QKeySequence::Open);
95 connect(act_open_sce, &QAction::triggered, this, &GoatView::open_scene);
96 menu_file->addAction(act_open_sce);
98 QAction *act_open_anm = new QAction("Open &Animation", this);
99 connect(act_open_anm, &QAction::triggered, this, &GoatView::open_anim);
100 menu_file->addAction(act_open_anm);
102 QAction *act_quit = new QAction("&Quit", this);
103 act_quit->setShortcuts(QKeySequence::Quit);
104 connect(act_quit, &QAction::triggered, [&](){qApp->quit();});
105 menu_file->addAction(act_quit);
107 // view menu
108 QMenu *menu_view = menuBar()->addMenu("&View");
110 QAction *act_use_nodes = new QAction("use nodes", this);
111 act_use_nodes->setCheckable(true);
112 act_use_nodes->setChecked(use_nodes);
113 connect(act_use_nodes, &QAction::triggered, this,
114 [&](){ use_nodes = !use_nodes; post_redisplay(); });
115 menu_view->addAction(act_use_nodes);
117 QAction *act_use_lighting = new QAction("lighting", this);
118 act_use_lighting->setCheckable(true);
119 act_use_lighting->setChecked(use_lighting);
120 connect(act_use_lighting, &QAction::triggered, glview, &GoatViewport::toggle_lighting);
121 menu_view->addAction(act_use_lighting);
123 // help menu
124 QMenu *menu_help = menuBar()->addMenu("&Help");
126 QAction *act_about = new QAction("&About", this);
127 connect(act_about, &QAction::triggered, this, &GoatView::show_about);
128 menu_help->addAction(act_about);
129 return true;
130 }
132 bool GoatView::make_dock()
133 {
134 // ---- side-dock ----
135 QWidget *dock_cont = new QWidget;
136 QVBoxLayout *dock_vbox = new QVBoxLayout;
137 dock_cont->setLayout(dock_vbox);
139 QDockWidget *dock = new QDockWidget("Scene graph", this);
140 dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
141 dock->setWidget(dock_cont);
142 addDockWidget(Qt::LeftDockWidgetArea, dock);
144 // make the tree view widget
145 treeview = new QTreeView;
146 treeview->setAlternatingRowColors(true);
147 treeview->setSelectionMode(QAbstractItemView::SingleSelection);
148 dock_vbox->addWidget(treeview);
150 scene_model = new SceneModel;
151 connect(scene_model, &SceneModel::dataChanged, [&](){ post_redisplay(); });
152 treeview->setModel(scene_model);
154 connect(treeview->selectionModel(), &QItemSelectionModel::selectionChanged,
155 [&](){ scene_model->selchange(treeview->selectionModel()->selectedIndexes()); });
157 // misc
158 QPushButton *bn_quit = new QPushButton("quit");
159 dock_vbox->addWidget(bn_quit);
160 connect(bn_quit, &QPushButton::clicked, [&](){ qApp->quit(); });
162 // ---- bottom dock ----
163 dock_cont = new QWidget;
164 QHBoxLayout *dock_hbox = new QHBoxLayout;
165 dock_cont->setLayout(dock_hbox);
167 QSlider *slider_time = new QSlider(Qt::Orientation::Horizontal);
168 slider_time->setDisabled(true);
169 dock_hbox->addWidget(slider_time);
171 dock = new QDockWidget("Animation", this);
172 dock->setAllowedAreas(Qt::BottomDockWidgetArea);
173 dock->setWidget(dock_cont);
174 addDockWidget(Qt::BottomDockWidgetArea, dock);
176 return true;
177 }
179 bool GoatView::make_center()
180 {
181 glview = ::glview = new GoatViewport(this);
182 setCentralWidget(glview);
183 return true;
184 }
186 void GoatView::open_scene()
187 {
188 std::string fname = QFileDialog::getOpenFileName(this, "Open scene file", "",
189 "Goat3D Scene (*.goatsce);;All Files (*)").toStdString();
190 if(fname.empty()) {
191 statusBar()->showMessage("Abort: No file selected!");
192 return;
193 }
195 statusBar()->showMessage("opening scene file");
196 if(!load_scene(fname.c_str())) {
197 statusBar()->showMessage("failed to load scene file");
198 }
199 }
201 void GoatView::open_anim()
202 {
203 statusBar()->showMessage("opening animation...");
204 }
207 // ---- OpenGL viewport ----
208 GoatViewport::GoatViewport(QWidget *main_win)
209 : QGLWidget(QGLFormat(QGL::DepthBuffer))
210 {
211 this->main_win = main_win;
212 initialized = false;
213 }
215 GoatViewport::~GoatViewport()
216 {
217 }
219 QSize GoatViewport::sizeHint() const
220 {
221 return QSize(800, 600);
222 }
224 #define CRITICAL(error, detail) \
225 do { \
226 fprintf(stderr, "%s: %s\n", error, detail); \
227 QMessageBox::critical(main_win, error, detail); \
228 abort(); \
229 } while(0)
231 void GoatViewport::initializeGL()
232 {
233 if(initialized) return;
234 initialized = true;
236 init_opengl();
238 if(!GLEW_ARB_transpose_matrix) {
239 CRITICAL("OpenGL initialization failed", "ARB_transpose_matrix extension not found!");
240 }
242 glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
244 glEnable(GL_DEPTH_TEST);
245 glEnable(GL_CULL_FACE);
246 if(use_lighting) {
247 glEnable(GL_LIGHTING);
248 }
249 glEnable(GL_LIGHT0);
251 float ldir[] = {-1, 1, 2, 0};
252 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
253 }
255 void GoatViewport::resizeGL(int xsz, int ysz)
256 {
257 glViewport(0, 0, xsz, ysz);
258 glMatrixMode(GL_PROJECTION);
259 glLoadIdentity();
260 gluPerspective(60.0, (float)xsz / (float)ysz, 0.5, 5000.0);
261 }
263 void GoatViewport::paintGL()
264 {
265 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
267 glMatrixMode(GL_MODELVIEW);
268 glLoadIdentity();
269 glTranslatef(0, 0, -cam_dist);
270 glRotatef(cam_phi, 1, 0, 0);
271 glRotatef(cam_theta, 0, 1, 0);
273 if(!scene) return;
275 if(use_nodes) {
276 int node_count = goat3d_get_node_count(scene);
277 for(int i=0; i<node_count; i++) {
278 goat3d_node *node = goat3d_get_node(scene, i);
279 if(!goat3d_get_node_parent(node)) {
280 draw_node(node); // only draw root nodes, the rest will be drawn recursively
281 }
282 }
283 } else {
284 int mesh_count = goat3d_get_mesh_count(scene);
285 for(int i=0; i<mesh_count; i++) {
286 goat3d_mesh *mesh = goat3d_get_mesh(scene, i);
287 draw_mesh(mesh);
288 }
289 }
290 }
292 void GoatViewport::toggle_lighting()
293 {
294 use_lighting = !use_lighting;
295 if(use_lighting) {
296 glEnable(GL_LIGHTING);
297 } else {
298 glDisable(GL_LIGHTING);
299 }
300 updateGL();
301 }
303 #ifndef GLEW_ARB_transpose_matrix
304 #error "GLEW_ARB_transpose_matrix undefined?"
305 #endif
307 static void draw_node(goat3d_node *node)
308 {
309 SceneNodeData *data = sdata ? sdata->get_node_data(node) : 0;
310 if(!data) return;
312 float xform[16];
313 goat3d_get_node_matrix(node, xform, anim_time);
315 glPushMatrix();
316 glMultTransposeMatrixf(xform);
318 if(data->visible) {
319 if(goat3d_get_node_type(node) == GOAT3D_NODE_MESH) {
320 goat3d_mesh *mesh = (goat3d_mesh*)goat3d_get_node_object(node);
322 draw_mesh(mesh);
324 if(data->selected) {
325 float bmin[3], bmax[3];
326 goat3d_get_mesh_bounds(mesh, bmin, bmax);
328 glPushAttrib(GL_ENABLE_BIT);
329 glDisable(GL_LIGHTING);
331 glColor3f(0.3, 1, 0.2);
333 glBegin(GL_LINE_LOOP);
334 glVertex3f(bmin[0], bmin[1], bmin[2]);
335 glVertex3f(bmax[0], bmin[1], bmin[2]);
336 glVertex3f(bmax[0], bmin[1], bmax[2]);
337 glVertex3f(bmin[0], bmin[1], bmax[2]);
338 glEnd();
340 glBegin(GL_LINE_LOOP);
341 glVertex3f(bmin[0], bmax[1], bmin[2]);
342 glVertex3f(bmax[0], bmax[1], bmin[2]);
343 glVertex3f(bmax[0], bmax[1], bmax[2]);
344 glVertex3f(bmin[0], bmax[1], bmax[2]);
345 glEnd();
347 glBegin(GL_LINES);
348 glVertex3f(bmin[0], bmin[1], bmin[2]);
349 glVertex3f(bmin[0], bmax[1], bmin[2]);
350 glVertex3f(bmin[0], bmin[1], bmax[2]);
351 glVertex3f(bmin[0], bmax[1], bmax[2]);
352 glVertex3f(bmax[0], bmin[1], bmin[2]);
353 glVertex3f(bmax[0], bmax[1], bmin[2]);
354 glVertex3f(bmax[0], bmin[1], bmax[2]);
355 glVertex3f(bmax[0], bmax[1], bmax[2]);
356 glEnd();
358 glPopAttrib();
359 }
360 }
361 }
363 int num_child = goat3d_get_node_child_count(node);
364 for(int i=0; i<num_child; i++) {
365 draw_node(goat3d_get_node_child(node, i));
366 }
368 glPopMatrix();
369 }
371 static void draw_mesh(goat3d_mesh *mesh)
372 {
373 static const float white[] = {1, 1, 1, 1};
374 static const float black[] = {0, 0, 0, 1};
376 const float *color;
377 goat3d_material *mtl = goat3d_get_mesh_mtl(mesh);
379 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_DIFFUSE))) {
380 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
381 } else {
382 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, white);
383 }
384 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_SPECULAR))) {
385 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
386 } else {
387 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
388 }
389 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_SHININESS))) {
390 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, color[0]);
391 } else {
392 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
393 }
394 // TODO texture
397 int num_faces = goat3d_get_mesh_face_count(mesh);
398 int num_verts = goat3d_get_mesh_attrib_count(mesh, GOAT3D_MESH_ATTR_VERTEX);
400 glEnableClientState(GL_VERTEX_ARRAY);
401 glVertexPointer(3, GL_FLOAT, 0, goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX));
403 float *data;
404 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL))) {
405 glEnableClientState(GL_NORMAL_ARRAY);
406 glNormalPointer(GL_FLOAT, 0, data);
407 }
408 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
409 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
410 glTexCoordPointer(2, GL_FLOAT, 0, data);
411 }
413 int *indices;
414 if((indices = goat3d_get_mesh_faces(mesh))) {
415 glDrawElements(GL_TRIANGLES, num_faces * 3, GL_UNSIGNED_INT, indices);
416 } else {
417 glDrawArrays(GL_TRIANGLES, 0, num_verts * 3);
418 }
420 glDisableClientState(GL_VERTEX_ARRAY);
421 glDisableClientState(GL_NORMAL_ARRAY);
422 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
423 }
426 static float prev_x, prev_y;
427 void GoatViewport::mousePressEvent(QMouseEvent *ev)
428 {
429 prev_x = ev->x();
430 prev_y = ev->y();
431 }
433 void GoatViewport::mouseMoveEvent(QMouseEvent *ev)
434 {
435 int dx = ev->x() - prev_x;
436 int dy = ev->y() - prev_y;
437 prev_x = ev->x();
438 prev_y = ev->y();
440 if(!dx && !dy) return;
442 if(ev->buttons() & Qt::LeftButton) {
443 cam_theta += dx * 0.5;
444 cam_phi += dy * 0.5;
446 if(cam_phi < -90) cam_phi = -90;
447 if(cam_phi > 90) cam_phi = 90;
448 }
449 if(ev->buttons() & Qt::RightButton) {
450 cam_dist += dy * 0.1;
452 if(cam_dist < 0.0) cam_dist = 0.0;
453 }
454 updateGL();
455 }
457 static const char *about_str =
458 "GoatView - Goat3D scene file viewer<br>"
459 "Copyright (C) 2014 John Tsiombikas &lt;<a href=\"mailto:nuclear@mutantstargoat.com\">nuclear@mutantstargoat.com</a>&gt;<br>"
460 "<br>"
461 "This program is free software: you can redistribute it and/or modify<br>"
462 "it under the terms of the GNU General Public License as published by<br>"
463 "the Free Software Foundation, either version 3 of the License, or<br>"
464 "(at your option) any later version.<br>"
465 "<br>"
466 "This program is distributed in the hope that it will be useful,<br>"
467 "but WITHOUT ANY WARRANTY; without even the implied warranty of<br>"
468 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>"
469 "GNU General Public License for more details.<br>"
470 "<br>"
471 "You should have received a copy of the GNU General Public License<br>"
472 "along with this program. If not, see <a href=\"http://www.gnu.org/licenses/gpl\">http://www.gnu.org/licenses/gpl</a>.";
474 void GoatView::show_about()
475 {
476 QMessageBox::information(this, "About GoatView", about_str);
477 }