goat3d
view goatview/src/goatview.cc @ 84:022b13ed975b
minor gui fix
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 12 May 2014 07:37:30 +0300 |
parents | 57e745dd13c2 |
children | 91e3aa1a60c3 |
line source
1 #include <stdio.h>
2 #include "opengl.h"
3 #include <QtOpenGL/QtOpenGL>
4 #include <vmath/vmath.h>
5 #include "goatview.h"
6 #include "goat3d.h"
8 static void update_tree(QTreeWidget *tree);
9 static void add_tree(QTreeWidget *tree, goat3d_node *node, QTreeWidgetItem *parent);
11 static void draw_node(goat3d_node *node);
12 static void draw_mesh(goat3d_mesh *mesh);
14 goat3d *scene;
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;
23 GoatView::GoatView()
24 {
25 glview = 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 }
46 void GoatView::closeEvent(QCloseEvent *ev)
47 {
48 QSettings settings;
49 settings.setValue("main/size", size());
50 settings.setValue("main/pos", pos());
51 settings.setValue("use_nodes", use_nodes);
52 settings.setValue("use_lighting", use_lighting);
53 }
56 bool GoatView::load_scene(const char *fname)
57 {
58 if(scene) {
59 goat3d_free(scene);
60 }
61 if(!(scene = goat3d_create()) || goat3d_load(scene, fname) == -1) {
62 return false;
63 }
65 float bmin[3], bmax[3];
66 if(goat3d_get_bounds(scene, bmin, bmax) != -1) {
67 float bsize = (Vector3(bmax[0], bmax[1], bmax[2]) - Vector3(bmin[0], bmin[1], bmin[2])).length();
68 cam_dist = bsize / tan(DEG_TO_RAD(fov) / 2.0);
69 printf("bounds size: %f, cam_dist: %f\n", bsize, cam_dist);
70 }
72 update_tree(scntree);
73 return true;
74 }
76 bool GoatView::make_menu()
77 {
78 // file menu
79 QMenu *menu_file = menuBar()->addMenu("&File");
81 QAction *act_open_sce = new QAction("&Open Scene", this);
82 act_open_sce->setShortcuts(QKeySequence::Open);
83 connect(act_open_sce, &QAction::triggered, this, &GoatView::open_scene);
84 menu_file->addAction(act_open_sce);
86 QAction *act_open_anm = new QAction("Open &Animation", this);
87 connect(act_open_anm, &QAction::triggered, this, &GoatView::open_anim);
88 menu_file->addAction(act_open_anm);
90 QAction *act_quit = new QAction("&Quit", this);
91 act_quit->setShortcuts(QKeySequence::Quit);
92 connect(act_quit, &QAction::triggered, [&](){qApp->quit();});
93 menu_file->addAction(act_quit);
95 // view menu
96 QMenu *menu_view = menuBar()->addMenu("&View");
98 QAction *act_use_nodes = new QAction("use nodes", this);
99 act_use_nodes->setCheckable(true);
100 act_use_nodes->setChecked(use_nodes);
101 connect(act_use_nodes, &QAction::triggered, this,
102 [&](){ use_nodes = !use_nodes; glview->updateGL(); });
103 menu_view->addAction(act_use_nodes);
105 QAction *act_use_lighting = new QAction("lighting", this);
106 act_use_lighting->setCheckable(true);
107 act_use_lighting->setChecked(use_lighting);
108 connect(act_use_lighting, &QAction::triggered, glview, &GoatViewport::toggle_lighting);
109 menu_view->addAction(act_use_lighting);
111 // help menu
112 QMenu *menu_help = menuBar()->addMenu("&Help");
114 QAction *act_about = new QAction("&About", this);
115 connect(act_about, &QAction::triggered, this, &GoatView::show_about);
116 menu_help->addAction(act_about);
117 return true;
118 }
120 bool GoatView::make_dock()
121 {
122 // ---- side-dock ----
123 QWidget *dock_cont = new QWidget;
124 QVBoxLayout *dock_vbox = new QVBoxLayout;
125 dock_cont->setLayout(dock_vbox);
127 QDockWidget *dock = new QDockWidget("Scene graph", this);
128 dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
129 dock->setWidget(dock_cont);
130 addDockWidget(Qt::LeftDockWidgetArea, dock);
132 // make the tree view widget
133 scntree = new QTreeWidget;
134 scntree->setColumnCount(1);
135 QStringList hdrstr;
136 hdrstr << "Node";// << "Type";
137 scntree->setHeaderItem(new QTreeWidgetItem((QTreeWidget*)0, hdrstr));
138 scntree->setAlternatingRowColors(true);
139 dock_vbox->addWidget(scntree);
141 update_tree(scntree);
143 // misc
144 QPushButton *bn_quit = new QPushButton("quit");
145 dock_vbox->addWidget(bn_quit);
146 connect(bn_quit, &QPushButton::clicked, [&](){qApp->quit();});
148 // ---- bottom dock ----
149 dock_cont = new QWidget;
150 QHBoxLayout *dock_hbox = new QHBoxLayout;
151 dock_cont->setLayout(dock_hbox);
153 QSlider *slider_time = new QSlider(Qt::Orientation::Horizontal);
154 slider_time->setDisabled(true);
155 dock_hbox->addWidget(slider_time);
157 dock = new QDockWidget("Animation", this);
158 dock->setAllowedAreas(Qt::BottomDockWidgetArea);
159 dock->setWidget(dock_cont);
160 addDockWidget(Qt::BottomDockWidgetArea, dock);
162 return true;
163 }
165 bool GoatView::make_center()
166 {
167 glview = new GoatViewport(this);
168 setCentralWidget(glview);
169 return true;
170 }
172 void GoatView::open_scene()
173 {
174 std::string fname = QFileDialog::getOpenFileName(this, "Open scene file", "",
175 "Goat3D Scene (*.goatsce);;All Files (*)").toStdString();
176 if(fname.empty()) {
177 statusBar()->showMessage("Abort: No file selected!");
178 return;
179 }
181 statusBar()->showMessage("opening scene file");
182 if(!load_scene(fname.c_str())) {
183 statusBar()->showMessage("failed to load scene file");
184 }
185 }
187 void GoatView::open_anim()
188 {
189 statusBar()->showMessage("opening animation...");
190 }
192 static void update_tree(QTreeWidget *tree)
193 {
194 tree->clear();
196 if(!scene) return;
198 int num_nodes = goat3d_get_node_count(scene);
199 for(int i=0; i<num_nodes; i++) {
200 goat3d_node *node = goat3d_get_node(scene, i);
201 if(goat3d_get_node_parent(node)) {
202 continue;
203 }
205 // only add the root nodes, the rest will be added recursively by them
206 add_tree(tree, node, 0);
207 }
208 tree->expandAll();
209 }
211 static void add_tree(QTreeWidget *tree, goat3d_node *node, QTreeWidgetItem *parent)
212 {
213 //char icon_name[64];
214 //sprintf(icon_name, ":/icons/icons/icon_%s.png", node->get_type());
216 QStringList row;
217 row << goat3d_get_node_name(node) << "M";
218 QTreeWidgetItem *item = new QTreeWidgetItem(parent, row);
219 //item->setIcon(0, QIcon(icon_name));
220 tree->addTopLevelItem(item);
222 int num_children = goat3d_get_node_child_count(node);
223 for(int i=0; i<num_children; i++) {
224 add_tree(tree, goat3d_get_node_child(node, i), item);
225 }
226 }
230 // ---- OpenGL viewport ----
231 GoatViewport::GoatViewport(QWidget *main_win)
232 : QGLWidget(QGLFormat(QGL::DepthBuffer))
233 {
234 this->main_win = main_win;
235 initialized = false;
236 }
238 GoatViewport::~GoatViewport()
239 {
240 }
242 QSize GoatViewport::sizeHint() const
243 {
244 return QSize(800, 600);
245 }
247 #define CRITICAL(error, detail) \
248 do { \
249 fprintf(stderr, "%s: %s\n", error, detail); \
250 QMessageBox::critical(main_win, error, detail); \
251 abort(); \
252 } while(0)
254 void GoatViewport::initializeGL()
255 {
256 if(initialized) return;
257 initialized = true;
259 init_opengl();
261 if(!GLEW_ARB_transpose_matrix) {
262 CRITICAL("OpenGL initialization failed", "ARB_transpose_matrix extension not found!");
263 }
265 glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
267 glEnable(GL_DEPTH_TEST);
268 glEnable(GL_CULL_FACE);
269 if(use_lighting) {
270 glEnable(GL_LIGHTING);
271 }
272 glEnable(GL_LIGHT0);
274 float ldir[] = {-1, 1, 2, 0};
275 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
276 }
278 void GoatViewport::resizeGL(int xsz, int ysz)
279 {
280 glViewport(0, 0, xsz, ysz);
281 glMatrixMode(GL_PROJECTION);
282 glLoadIdentity();
283 gluPerspective(60.0, (float)xsz / (float)ysz, 0.5, 5000.0);
284 }
286 void GoatViewport::paintGL()
287 {
288 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
290 glMatrixMode(GL_MODELVIEW);
291 glLoadIdentity();
292 glTranslatef(0, 0, -cam_dist);
293 glRotatef(cam_phi, 1, 0, 0);
294 glRotatef(cam_theta, 0, 1, 0);
296 if(!scene) return;
298 if(use_nodes) {
299 int node_count = goat3d_get_node_count(scene);
300 for(int i=0; i<node_count; i++) {
301 goat3d_node *node = goat3d_get_node(scene, i);
302 if(!goat3d_get_node_parent(node)) {
303 draw_node(node); // only draw root nodes, the rest will be drawn recursively
304 }
305 }
306 } else {
307 int mesh_count = goat3d_get_mesh_count(scene);
308 for(int i=0; i<mesh_count; i++) {
309 goat3d_mesh *mesh = goat3d_get_mesh(scene, i);
310 draw_mesh(mesh);
311 }
312 }
313 }
315 void GoatViewport::toggle_lighting()
316 {
317 use_lighting = !use_lighting;
318 if(use_lighting) {
319 glEnable(GL_LIGHTING);
320 } else {
321 glDisable(GL_LIGHTING);
322 }
323 updateGL();
324 }
326 #ifndef GLEW_ARB_transpose_matrix
327 #error "GLEW_ARB_transpose_matrix undefined?"
328 #endif
330 static void draw_node(goat3d_node *node)
331 {
332 float xform[16];
333 goat3d_get_node_matrix(node, xform, anim_time);
335 glPushMatrix();
336 glMultTransposeMatrixf(xform);
338 if(goat3d_get_node_type(node) == GOAT3D_NODE_MESH) {
339 goat3d_mesh *mesh = (goat3d_mesh*)goat3d_get_node_object(node);
341 draw_mesh(mesh);
342 }
344 int num_child = goat3d_get_node_child_count(node);
345 for(int i=0; i<num_child; i++) {
346 draw_node(goat3d_get_node_child(node, i));
347 }
349 glPopMatrix();
350 }
352 static void draw_mesh(goat3d_mesh *mesh)
353 {
354 static const float white[] = {1, 1, 1, 1};
355 static const float black[] = {0, 0, 0, 1};
357 const float *color;
358 goat3d_material *mtl = goat3d_get_mesh_mtl(mesh);
360 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_DIFFUSE))) {
361 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
362 } else {
363 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, white);
364 }
365 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_SPECULAR))) {
366 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
367 } else {
368 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
369 }
370 if(mtl && (color = goat3d_get_mtl_attrib(mtl, GOAT3D_MAT_ATTR_SHININESS))) {
371 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, color[0]);
372 } else {
373 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
374 }
375 // TODO texture
378 int num_faces = goat3d_get_mesh_face_count(mesh);
379 int num_verts = goat3d_get_mesh_attrib_count(mesh, GOAT3D_MESH_ATTR_VERTEX);
381 glEnableClientState(GL_VERTEX_ARRAY);
382 glVertexPointer(3, GL_FLOAT, 0, goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX));
384 float *data;
385 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL))) {
386 glEnableClientState(GL_NORMAL_ARRAY);
387 glNormalPointer(GL_FLOAT, 0, data);
388 }
389 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
390 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
391 glTexCoordPointer(2, GL_FLOAT, 0, data);
392 }
394 int *indices;
395 if((indices = goat3d_get_mesh_faces(mesh))) {
396 glDrawElements(GL_TRIANGLES, num_faces * 3, GL_UNSIGNED_INT, indices);
397 } else {
398 glDrawArrays(GL_TRIANGLES, 0, num_verts * 3);
399 }
401 glDisableClientState(GL_VERTEX_ARRAY);
402 glDisableClientState(GL_NORMAL_ARRAY);
403 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
404 }
407 static float prev_x, prev_y;
408 void GoatViewport::mousePressEvent(QMouseEvent *ev)
409 {
410 prev_x = ev->x();
411 prev_y = ev->y();
412 }
414 void GoatViewport::mouseMoveEvent(QMouseEvent *ev)
415 {
416 int dx = ev->x() - prev_x;
417 int dy = ev->y() - prev_y;
418 prev_x = ev->x();
419 prev_y = ev->y();
421 if(!dx && !dy) return;
423 if(ev->buttons() & Qt::LeftButton) {
424 cam_theta += dx * 0.5;
425 cam_phi += dy * 0.5;
427 if(cam_phi < -90) cam_phi = -90;
428 if(cam_phi > 90) cam_phi = 90;
429 }
430 if(ev->buttons() & Qt::RightButton) {
431 cam_dist += dy * 0.1;
433 if(cam_dist < 0.0) cam_dist = 0.0;
434 }
435 updateGL();
436 }
438 static const char *about_str =
439 "GoatView - Goat3D scene file viewer<br>"
440 "Copyright (C) 2014 John Tsiombikas <<a href=\"mailto:nuclear@mutantstargoat.com\">nuclear@mutantstargoat.com</a>><br>"
441 "<br>"
442 "This program is free software: you can redistribute it and/or modify<br>"
443 "it under the terms of the GNU General Public License as published by<br>"
444 "the Free Software Foundation, either version 3 of the License, or<br>"
445 "(at your option) any later version.<br>"
446 "<br>"
447 "This program is distributed in the hope that it will be useful,<br>"
448 "but WITHOUT ANY WARRANTY; without even the implied warranty of<br>"
449 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>"
450 "GNU General Public License for more details.<br>"
451 "<br>"
452 "You should have received a copy of the GNU General Public License<br>"
453 "along with this program. If not, see <a href=\"http://www.gnu.org/licenses/gpl\">http://www.gnu.org/licenses/gpl</a>.";
455 void GoatView::show_about()
456 {
457 QMessageBox::information(this, "About GoatView", about_str);
458 }