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