goat3d

annotate goatview/src/goatview.cc @ 85:91e3aa1a60c3

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