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