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