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