goat3d

view goatview/src/goatview.cc @ 82:70b7c41a4f17

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 11 May 2014 22:04:54 +0300
parents c5e997e8fd62
children 57e745dd13c2
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;
22 GoatView::GoatView()
23 {
24 glview = 0;
26 make_menu();
27 make_dock();
28 make_center();
30 statusBar();
32 setWindowTitle("GoatView");
34 QSettings *settings = new QSettings;
35 resize(settings->value("main/size", QSize(1024, 768)).toSize());
36 move(settings->value("main/pos", QPoint(100, 100)).toPoint());
37 delete settings;
38 }
40 GoatView::~GoatView()
41 {
42 }
44 bool GoatView::load_scene(const char *fname)
45 {
46 if(scene) {
47 goat3d_free(scene);
48 }
49 if(!(scene = goat3d_create()) || goat3d_load(scene, fname) == -1) {
50 return false;
51 }
53 float bmin[3], bmax[3];
54 if(goat3d_get_bounds(scene, bmin, bmax) != -1) {
55 float bsize = (Vector3(bmax[0], bmax[1], bmax[2]) - Vector3(bmin[0], bmin[1], bmin[2])).length();
56 cam_dist = bsize / tan(DEG_TO_RAD(fov) / 2.0) + bsize;
57 printf("bounds size: %f, cam_dist: %f\n", bsize, cam_dist);
58 }
60 update_tree(scntree);
61 return true;
62 }
64 void GoatView::closeEvent(QCloseEvent *ev)
65 {
66 QSettings *settings = new QSettings;
67 settings->setValue("main/size", size());
68 settings->setValue("main/pos", pos());
69 delete settings;
70 }
72 bool GoatView::make_menu()
73 {
74 // file menu
75 QMenu *menu_file = menuBar()->addMenu("&File");
77 QAction *act_open_sce = new QAction("&Open Scene", this);
78 act_open_sce->setShortcuts(QKeySequence::Open);
79 connect(act_open_sce, &QAction::triggered, this, &GoatView::open_scene);
80 menu_file->addAction(act_open_sce);
82 QAction *act_open_anm = new QAction("Open &Animation", this);
83 connect(act_open_anm, &QAction::triggered, this, &GoatView::open_anim);
84 menu_file->addAction(act_open_anm);
86 QAction *act_quit = new QAction("&Quit", this);
87 act_quit->setShortcuts(QKeySequence::Quit);
88 connect(act_quit, &QAction::triggered, [&](){qApp->quit();});
89 menu_file->addAction(act_quit);
91 // view menu
92 QMenu *menu_view = menuBar()->addMenu("&View");
94 QAction *act_use_nodes = new QAction("use nodes", this);
95 act_use_nodes->setCheckable(true);
96 act_use_nodes->setChecked(use_nodes);
97 connect(act_use_nodes, &QAction::triggered, this, [&](){use_nodes = !use_nodes; glview->updateGL();});
98 menu_view->addAction(act_use_nodes);
99 return true;
100 }
102 bool GoatView::make_dock()
103 {
104 // ---- side-dock ----
105 QWidget *dock_cont = new QWidget;
106 QVBoxLayout *dock_vbox = new QVBoxLayout;
107 dock_cont->setLayout(dock_vbox);
109 QDockWidget *dock = new QDockWidget("Scene graph", this);
110 dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
111 dock->setWidget(dock_cont);
112 addDockWidget(Qt::LeftDockWidgetArea, dock);
114 // make the tree view widget
115 scntree = new QTreeWidget;
116 scntree->setColumnCount(1);
117 QStringList hdrstr;
118 hdrstr << "Node";// << "Type";
119 scntree->setHeaderItem(new QTreeWidgetItem((QTreeWidget*)0, hdrstr));
120 scntree->setAlternatingRowColors(true);
121 dock_vbox->addWidget(scntree);
123 update_tree(scntree);
125 // misc
126 QPushButton *bn_quit = new QPushButton("quit");
127 dock_vbox->addWidget(bn_quit);
128 connect(bn_quit, &QPushButton::clicked, [&](){qApp->quit();});
130 // ---- bottom dock ----
131 dock_cont = new QWidget;
132 QHBoxLayout *dock_hbox = new QHBoxLayout;
133 dock_cont->setLayout(dock_hbox);
135 QSlider *slider_time = new QSlider(Qt::Orientation::Horizontal);
136 slider_time->setDisabled(true);
137 dock_hbox->addWidget(slider_time);
139 dock = new QDockWidget("Animation", this);
140 dock->setAllowedAreas(Qt::BottomDockWidgetArea);
141 dock->setWidget(dock_cont);
142 addDockWidget(Qt::BottomDockWidgetArea, dock);
144 return true;
145 }
147 bool GoatView::make_center()
148 {
149 glview = new GoatViewport(this);
150 setCentralWidget(glview);
151 return true;
152 }
154 void GoatView::open_scene()
155 {
156 std::string fname = QFileDialog::getOpenFileName(this, "Open scene file", "",
157 "Goat3D Scene (*.goatsce);;All Files (*)").toStdString();
158 if(fname.empty()) {
159 statusBar()->showMessage("Abort: No file selected!");
160 return;
161 }
163 statusBar()->showMessage("opening scene file");
164 if(!load_scene(fname.c_str())) {
165 statusBar()->showMessage("failed to load scene file");
166 }
167 }
169 void GoatView::open_anim()
170 {
171 statusBar()->showMessage("opening animation...");
172 }
174 static void update_tree(QTreeWidget *tree)
175 {
176 if(!scene) return;
178 int num_nodes = goat3d_get_node_count(scene);
179 for(int i=0; i<num_nodes; i++) {
180 goat3d_node *node = goat3d_get_node(scene, i);
181 if(goat3d_get_node_parent(node)) {
182 continue;
183 }
185 // only add the root nodes, the rest will be added recursively by them
186 add_tree(tree, node, 0);
187 }
188 tree->expandAll();
189 }
191 static void add_tree(QTreeWidget *tree, goat3d_node *node, QTreeWidgetItem *parent)
192 {
193 //char icon_name[64];
194 //sprintf(icon_name, ":/icons/icons/icon_%s.png", node->get_type());
196 QStringList row;
197 row << goat3d_get_node_name(node) << "M";
198 QTreeWidgetItem *item = new QTreeWidgetItem(parent, row);
199 //item->setIcon(0, QIcon(icon_name));
200 tree->addTopLevelItem(item);
202 int num_children = goat3d_get_node_child_count(node);
203 for(int i=0; i<num_children; i++) {
204 add_tree(tree, goat3d_get_node_child(node, i), item);
205 }
206 }
210 // ---- OpenGL viewport ----
211 GoatViewport::GoatViewport(QWidget *main_win)
212 : QGLWidget(QGLFormat(QGL::DepthBuffer))
213 {
214 this->main_win = main_win;
215 initialized = false;
216 }
218 GoatViewport::~GoatViewport()
219 {
220 }
222 QSize GoatViewport::sizeHint() const
223 {
224 return QSize(800, 600);
225 }
227 #define CRITICAL(error, detail) \
228 do { \
229 fprintf(stderr, "%s: %s\n", error, detail); \
230 QMessageBox::critical(main_win, error, detail); \
231 abort(); \
232 } while(0)
234 void GoatViewport::initializeGL()
235 {
236 if(initialized) return;
237 initialized = true;
239 init_opengl();
241 if(!GLEW_ARB_transpose_matrix) {
242 CRITICAL("OpenGL initialization failed", "ARB_transpose_matrix extension not found!");
243 }
245 glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
247 glEnable(GL_DEPTH_TEST);
248 glEnable(GL_CULL_FACE);
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 draw_node(node); // only draw root nodes, the rest will be drawn recursively
276 }
277 } else {
278 int mesh_count = goat3d_get_mesh_count(scene);
279 for(int i=0; i<mesh_count; i++) {
280 goat3d_mesh *mesh = goat3d_get_mesh(scene, i);
281 draw_mesh(mesh);
282 }
283 }
284 }
286 #ifndef GLEW_ARB_transpose_matrix
287 #error "GLEW_ARB_transpose_matrix undefined?"
288 #endif
290 static void draw_node(goat3d_node *node)
291 {
292 float xform[16];
293 goat3d_get_node_matrix(node, xform, anim_time);
295 glPushMatrix();
296 glMultTransposeMatrixf(xform);
298 if(goat3d_get_node_type(node) == GOAT3D_NODE_MESH) {
299 goat3d_mesh *mesh = (goat3d_mesh*)goat3d_get_node_object(node);
301 draw_mesh(mesh);
302 }
304 /*int num_child = goat3d_get_node_child_count(node);
305 for(int i=0; i<num_child; i++) {
306 draw_node(goat3d_get_node_child(node, i));
307 }*/
309 glPopMatrix();
310 }
312 static void draw_mesh(goat3d_mesh *mesh)
313 {
314 int num_faces = goat3d_get_mesh_face_count(mesh);
315 int num_verts = goat3d_get_mesh_attrib_count(mesh, GOAT3D_MESH_ATTR_VERTEX);
317 glEnableClientState(GL_VERTEX_ARRAY);
318 glVertexPointer(3, GL_FLOAT, 0, goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX));
320 float *data;
321 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL))) {
322 glEnableClientState(GL_NORMAL_ARRAY);
323 glNormalPointer(GL_FLOAT, 0, data);
324 }
325 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
326 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
327 glTexCoordPointer(2, GL_FLOAT, 0, data);
328 }
330 int *indices;
331 if((indices = goat3d_get_mesh_faces(mesh))) {
332 glDrawElements(GL_TRIANGLES, num_faces * 3, GL_UNSIGNED_INT, indices);
333 } else {
334 glDrawArrays(GL_TRIANGLES, 0, num_verts * 3);
335 }
337 glDisableClientState(GL_VERTEX_ARRAY);
338 glDisableClientState(GL_NORMAL_ARRAY);
339 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
340 }
343 static float prev_x, prev_y;
344 void GoatViewport::mousePressEvent(QMouseEvent *ev)
345 {
346 prev_x = ev->x();
347 prev_y = ev->y();
348 }
350 void GoatViewport::mouseMoveEvent(QMouseEvent *ev)
351 {
352 int dx = ev->x() - prev_x;
353 int dy = ev->y() - prev_y;
354 prev_x = ev->x();
355 prev_y = ev->y();
357 if(!dx && !dy) return;
359 if(ev->buttons() & Qt::LeftButton) {
360 cam_theta += dx * 0.5;
361 cam_phi += dy * 0.5;
363 if(cam_phi < -90) cam_phi = -90;
364 if(cam_phi > 90) cam_phi = 90;
365 }
366 if(ev->buttons() & Qt::RightButton) {
367 cam_dist += dy * 0.1;
369 if(cam_dist < 0.0) cam_dist = 0.0;
370 }
371 updateGL();
372 }