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