goat3d

view goatview/src/goatview.cc @ 85:91e3aa1a60c3

more powerful treeview
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 May 2014 05:34:18 +0300
parents 022b13ed975b
children 7941e89798e5
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;
15 static long anim_time;
16 static float cam_theta, cam_phi, cam_dist = 8;
17 static float fov = 60.0;
18 static bool use_nodes = true;
19 static bool use_lighting = true;
22 GoatView::GoatView()
23 {
24 glview = 0;
25 scene_model = 0;
27 QSettings settings;
28 resize(settings.value("main/size", QSize(1024, 768)).toSize());
29 move(settings.value("main/pos", QPoint(100, 100)).toPoint());
30 use_nodes = settings.value("use_nodes", true).toBool();
31 use_lighting = settings.value("use_lighting", true).toBool();
33 make_center(); // must be first
34 make_menu();
35 make_dock();
37 statusBar();
39 setWindowTitle("GoatView");
40 }
42 GoatView::~GoatView()
43 {
44 delete scene_model;
45 sdata = 0;
46 }
48 void GoatView::closeEvent(QCloseEvent *ev)
49 {
50 QSettings settings;
51 settings.setValue("main/size", size());
52 settings.setValue("main/pos", pos());
53 settings.setValue("use_nodes", use_nodes);
54 settings.setValue("use_lighting", use_lighting);
55 }
58 bool GoatView::load_scene(const char *fname)
59 {
60 if(scene) {
61 goat3d_free(scene);
62 }
63 if(!(scene = goat3d_create()) || goat3d_load(scene, fname) == -1) {
64 return false;
65 }
67 float bmin[3], bmax[3];
68 if(goat3d_get_bounds(scene, bmin, bmax) != -1) {
69 float bsize = (Vector3(bmax[0], bmax[1], bmax[2]) - Vector3(bmin[0], bmin[1], bmin[2])).length();
70 cam_dist = bsize / tan(DEG_TO_RAD(fov) / 2.0);
71 printf("bounds size: %f, cam_dist: %f\n", bsize, cam_dist);
72 }
74 scene_model->set_scene(scene);
75 treeview->expandAll();
76 treeview->resizeColumnToContents(0);
78 sdata = scene_model; // set the global sdata ptr
79 return true;
80 }
82 bool GoatView::make_menu()
83 {
84 // file menu
85 QMenu *menu_file = menuBar()->addMenu("&File");
87 QAction *act_open_sce = new QAction("&Open Scene", this);
88 act_open_sce->setShortcuts(QKeySequence::Open);
89 connect(act_open_sce, &QAction::triggered, this, &GoatView::open_scene);
90 menu_file->addAction(act_open_sce);
92 QAction *act_open_anm = new QAction("Open &Animation", this);
93 connect(act_open_anm, &QAction::triggered, this, &GoatView::open_anim);
94 menu_file->addAction(act_open_anm);
96 QAction *act_quit = new QAction("&Quit", this);
97 act_quit->setShortcuts(QKeySequence::Quit);
98 connect(act_quit, &QAction::triggered, [&](){qApp->quit();});
99 menu_file->addAction(act_quit);
101 // view menu
102 QMenu *menu_view = menuBar()->addMenu("&View");
104 QAction *act_use_nodes = new QAction("use nodes", this);
105 act_use_nodes->setCheckable(true);
106 act_use_nodes->setChecked(use_nodes);
107 connect(act_use_nodes, &QAction::triggered, this,
108 [&](){ use_nodes = !use_nodes; glview->updateGL(); });
109 menu_view->addAction(act_use_nodes);
111 QAction *act_use_lighting = new QAction("lighting", this);
112 act_use_lighting->setCheckable(true);
113 act_use_lighting->setChecked(use_lighting);
114 connect(act_use_lighting, &QAction::triggered, glview, &GoatViewport::toggle_lighting);
115 menu_view->addAction(act_use_lighting);
117 // help menu
118 QMenu *menu_help = menuBar()->addMenu("&Help");
120 QAction *act_about = new QAction("&About", this);
121 connect(act_about, &QAction::triggered, this, &GoatView::show_about);
122 menu_help->addAction(act_about);
123 return true;
124 }
126 bool GoatView::make_dock()
127 {
128 // ---- side-dock ----
129 QWidget *dock_cont = new QWidget;
130 QVBoxLayout *dock_vbox = new QVBoxLayout;
131 dock_cont->setLayout(dock_vbox);
133 QDockWidget *dock = new QDockWidget("Scene graph", this);
134 dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
135 dock->setWidget(dock_cont);
136 addDockWidget(Qt::LeftDockWidgetArea, dock);
138 // make the tree view widget
139 treeview = new QTreeView;
140 /*
141 scntree->setColumnCount(1);
142 QStringList hdrstr;
143 hdrstr << "Node";// << "Type";
144 scntree->setHeaderItem(new QTreeWidgetItem((QTreeWidget*)0, hdrstr));
145 */
146 treeview->setAlternatingRowColors(true);
147 dock_vbox->addWidget(treeview);
149 scene_model = new SceneModel;
150 connect(scene_model, &SceneModel::dataChanged, [&](){ glview->updateGL(); });
151 treeview->setModel(scene_model);
153 // misc
154 QPushButton *bn_quit = new QPushButton("quit");
155 dock_vbox->addWidget(bn_quit);
156 connect(bn_quit, &QPushButton::clicked, [&](){ qApp->quit(); });
158 // ---- bottom dock ----
159 dock_cont = new QWidget;
160 QHBoxLayout *dock_hbox = new QHBoxLayout;
161 dock_cont->setLayout(dock_hbox);
163 QSlider *slider_time = new QSlider(Qt::Orientation::Horizontal);
164 slider_time->setDisabled(true);
165 dock_hbox->addWidget(slider_time);
167 dock = new QDockWidget("Animation", this);
168 dock->setAllowedAreas(Qt::BottomDockWidgetArea);
169 dock->setWidget(dock_cont);
170 addDockWidget(Qt::BottomDockWidgetArea, dock);
172 return true;
173 }
175 bool GoatView::make_center()
176 {
177 glview = new GoatViewport(this);
178 setCentralWidget(glview);
179 return true;
180 }
182 void GoatView::open_scene()
183 {
184 std::string fname = QFileDialog::getOpenFileName(this, "Open scene file", "",
185 "Goat3D Scene (*.goatsce);;All Files (*)").toStdString();
186 if(fname.empty()) {
187 statusBar()->showMessage("Abort: No file selected!");
188 return;
189 }
191 statusBar()->showMessage("opening scene file");
192 if(!load_scene(fname.c_str())) {
193 statusBar()->showMessage("failed to load scene file");
194 }
195 }
197 void GoatView::open_anim()
198 {
199 statusBar()->showMessage("opening animation...");
200 }
203 // ---- OpenGL viewport ----
204 GoatViewport::GoatViewport(QWidget *main_win)
205 : QGLWidget(QGLFormat(QGL::DepthBuffer))
206 {
207 this->main_win = main_win;
208 initialized = false;
209 }
211 GoatViewport::~GoatViewport()
212 {
213 }
215 QSize GoatViewport::sizeHint() const
216 {
217 return QSize(800, 600);
218 }
220 #define CRITICAL(error, detail) \
221 do { \
222 fprintf(stderr, "%s: %s\n", error, detail); \
223 QMessageBox::critical(main_win, error, detail); \
224 abort(); \
225 } while(0)
227 void GoatViewport::initializeGL()
228 {
229 if(initialized) return;
230 initialized = true;
232 init_opengl();
234 if(!GLEW_ARB_transpose_matrix) {
235 CRITICAL("OpenGL initialization failed", "ARB_transpose_matrix extension not found!");
236 }
238 glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
240 glEnable(GL_DEPTH_TEST);
241 glEnable(GL_CULL_FACE);
242 if(use_lighting) {
243 glEnable(GL_LIGHTING);
244 }
245 glEnable(GL_LIGHT0);
247 float ldir[] = {-1, 1, 2, 0};
248 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
249 }
251 void GoatViewport::resizeGL(int xsz, int ysz)
252 {
253 glViewport(0, 0, xsz, ysz);
254 glMatrixMode(GL_PROJECTION);
255 glLoadIdentity();
256 gluPerspective(60.0, (float)xsz / (float)ysz, 0.5, 5000.0);
257 }
259 void GoatViewport::paintGL()
260 {
261 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
263 glMatrixMode(GL_MODELVIEW);
264 glLoadIdentity();
265 glTranslatef(0, 0, -cam_dist);
266 glRotatef(cam_phi, 1, 0, 0);
267 glRotatef(cam_theta, 0, 1, 0);
269 if(!scene) return;
271 if(use_nodes) {
272 int node_count = goat3d_get_node_count(scene);
273 for(int i=0; i<node_count; i++) {
274 goat3d_node *node = goat3d_get_node(scene, i);
275 if(!goat3d_get_node_parent(node)) {
276 draw_node(node); // only draw root nodes, the rest will be drawn recursively
277 }
278 }
279 } else {
280 int mesh_count = goat3d_get_mesh_count(scene);
281 for(int i=0; i<mesh_count; i++) {
282 goat3d_mesh *mesh = goat3d_get_mesh(scene, i);
283 draw_mesh(mesh);
284 }
285 }
286 }
288 void GoatViewport::toggle_lighting()
289 {
290 use_lighting = !use_lighting;
291 if(use_lighting) {
292 glEnable(GL_LIGHTING);
293 } else {
294 glDisable(GL_LIGHTING);
295 }
296 updateGL();
297 }
299 #ifndef GLEW_ARB_transpose_matrix
300 #error "GLEW_ARB_transpose_matrix undefined?"
301 #endif
303 static void draw_node(goat3d_node *node)
304 {
305 SceneNodeData *data = sdata ? sdata->get_node_data(node) : 0;
306 if(!data) return;
308 float xform[16];
309 goat3d_get_node_matrix(node, xform, anim_time);
311 glPushMatrix();
312 glMultTransposeMatrixf(xform);
314 if(data->visible) {
315 if(goat3d_get_node_type(node) == GOAT3D_NODE_MESH) {
316 goat3d_mesh *mesh = (goat3d_mesh*)goat3d_get_node_object(node);
318 draw_mesh(mesh);
319 }
320 }
322 int num_child = goat3d_get_node_child_count(node);
323 for(int i=0; i<num_child; i++) {
324 draw_node(goat3d_get_node_child(node, i));
325 }
327 glPopMatrix();
328 }
330 static void draw_mesh(goat3d_mesh *mesh)
331 {
332 static const float white[] = {1, 1, 1, 1};
333 static const float black[] = {0, 0, 0, 1};
335 const float *color;
336 goat3d_material *mtl = goat3d_get_mesh_mtl(mesh);
338 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_DIFFUSE))) {
339 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
340 } else {
341 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, white);
342 }
343 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_SPECULAR))) {
344 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
345 } else {
346 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
347 }
348 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_SHININESS))) {
349 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, color[0]);
350 } else {
351 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
352 }
353 // TODO texture
356 int num_faces = goat3d_get_mesh_face_count(mesh);
357 int num_verts = goat3d_get_mesh_attrib_count(mesh, GOAT3D_MESH_ATTR_VERTEX);
359 glEnableClientState(GL_VERTEX_ARRAY);
360 glVertexPointer(3, GL_FLOAT, 0, goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX));
362 float *data;
363 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL))) {
364 glEnableClientState(GL_NORMAL_ARRAY);
365 glNormalPointer(GL_FLOAT, 0, data);
366 }
367 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
368 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
369 glTexCoordPointer(2, GL_FLOAT, 0, data);
370 }
372 int *indices;
373 if((indices = goat3d_get_mesh_faces(mesh))) {
374 glDrawElements(GL_TRIANGLES, num_faces * 3, GL_UNSIGNED_INT, indices);
375 } else {
376 glDrawArrays(GL_TRIANGLES, 0, num_verts * 3);
377 }
379 glDisableClientState(GL_VERTEX_ARRAY);
380 glDisableClientState(GL_NORMAL_ARRAY);
381 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
382 }
385 static float prev_x, prev_y;
386 void GoatViewport::mousePressEvent(QMouseEvent *ev)
387 {
388 prev_x = ev->x();
389 prev_y = ev->y();
390 }
392 void GoatViewport::mouseMoveEvent(QMouseEvent *ev)
393 {
394 int dx = ev->x() - prev_x;
395 int dy = ev->y() - prev_y;
396 prev_x = ev->x();
397 prev_y = ev->y();
399 if(!dx && !dy) return;
401 if(ev->buttons() & Qt::LeftButton) {
402 cam_theta += dx * 0.5;
403 cam_phi += dy * 0.5;
405 if(cam_phi < -90) cam_phi = -90;
406 if(cam_phi > 90) cam_phi = 90;
407 }
408 if(ev->buttons() & Qt::RightButton) {
409 cam_dist += dy * 0.1;
411 if(cam_dist < 0.0) cam_dist = 0.0;
412 }
413 updateGL();
414 }
416 static const char *about_str =
417 "GoatView - Goat3D scene file viewer<br>"
418 "Copyright (C) 2014 John Tsiombikas &lt;<a href=\"mailto:nuclear@mutantstargoat.com\">nuclear@mutantstargoat.com</a>&gt;<br>"
419 "<br>"
420 "This program is free software: you can redistribute it and/or modify<br>"
421 "it under the terms of the GNU General Public License as published by<br>"
422 "the Free Software Foundation, either version 3 of the License, or<br>"
423 "(at your option) any later version.<br>"
424 "<br>"
425 "This program is distributed in the hope that it will be useful,<br>"
426 "but WITHOUT ANY WARRANTY; without even the implied warranty of<br>"
427 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>"
428 "GNU General Public License for more details.<br>"
429 "<br>"
430 "You should have received a copy of the GNU General Public License<br>"
431 "along with this program. If not, see <a href=\"http://www.gnu.org/licenses/gpl\">http://www.gnu.org/licenses/gpl</a>.";
433 void GoatView::show_about()
434 {
435 QMessageBox::information(this, "About GoatView", about_str);
436 }