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