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