goat3d
view goatview/src/goatview.cc @ 75:76dea247f75c
in progress
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 08 May 2014 00:50:16 +0300 |
parents | ab66cdabf6f2 |
children | 9785847d52d4 |
line source
1 #include <GL/glu.h>
2 #include "goatview.h"
3 #include "goat3d.h"
5 goat3d *scene;
6 QSettings *settings;
8 static long anim_time;
9 static float cam_theta, cam_phi, cam_dist = 8;
12 GoatView::GoatView()
13 {
14 make_menu();
15 make_dock();
16 make_center();
18 statusBar();
20 setWindowTitle("GoatView");
21 resize(settings->value("main/size", QSize(1024, 768)).toSize());
22 move(settings->value("main/pos", QPoint(100, 100)).toPoint());
23 }
25 GoatView::~GoatView()
26 {
27 }
29 void GoatView::closeEvent(QCloseEvent *ev)
30 {
31 settings->setValue("main/size", size());
32 settings->setValue("main/pos", pos());
33 }
35 bool GoatView::make_menu()
36 {
37 QMenu *menu_file = menuBar()->addMenu("&File");
39 QAction *act_open_sce = new QAction("&Open Scene", this);
40 act_open_sce->setShortcuts(QKeySequence::Open);
41 connect(act_open_sce, &QAction::triggered, this, &GoatView::open_scene);
42 menu_file->addAction(act_open_sce);
44 QAction *act_open_anm = new QAction("Open &Animation", this);
45 connect(act_open_anm, &QAction::triggered, this, &GoatView::open_anim);
46 menu_file->addAction(act_open_anm);
48 QAction *act_quit = new QAction("&Quit", this);
49 act_quit->setShortcuts(QKeySequence::Quit);
50 connect(act_quit, &QAction::triggered, [&](){qApp->quit();});
51 menu_file->addAction(act_quit);
52 return true;
53 }
55 bool GoatView::make_dock()
56 {
57 // ---- side-dock ----
58 QWidget *dock_cont = new QWidget;
59 QVBoxLayout *dock_vbox = new QVBoxLayout;
60 dock_cont->setLayout(dock_vbox);
62 QPushButton *bn_quit = new QPushButton("quit");
63 dock_vbox->addWidget(bn_quit);
64 connect(bn_quit, &QPushButton::clicked, [&](){qApp->quit();});
66 QDockWidget *dock = new QDockWidget("Scene graph", this);
67 dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
68 dock->setWidget(dock_cont);
69 addDockWidget(Qt::LeftDockWidgetArea, dock);
71 // ---- bottom dock ----
72 dock_cont = new QWidget;
73 QHBoxLayout *dock_hbox = new QHBoxLayout;
74 dock_cont->setLayout(dock_hbox);
76 QSlider *slider_time = new QSlider(Qt::Orientation::Horizontal);
77 slider_time->setDisabled(true);
78 dock_hbox->addWidget(slider_time);
80 dock = new QDockWidget("Animation", this);
81 dock->setAllowedAreas(Qt::BottomDockWidgetArea);
82 dock->setWidget(dock_cont);
83 addDockWidget(Qt::BottomDockWidgetArea, dock);
85 return true;
86 }
88 bool GoatView::make_center()
89 {
90 GoatViewport *vport = new GoatViewport;
91 setCentralWidget(vport);
92 return true;
93 }
95 void GoatView::open_scene()
96 {
97 std::string fname = QFileDialog::getOpenFileName(this, "Open scene file", "",
98 "Goat3D Scene (*.goatsce);;All Files (*)").toStdString();
99 if(fname.empty()) {
100 statusBar()->showMessage("Abort: No file selected!");
101 return;
102 }
104 if(scene) {
105 goat3d_free(scene);
106 }
108 statusBar()->showMessage("opening scene file");
109 if(!(scene = goat3d_create()) || goat3d_load(scene, fname.c_str()) == -1) {
110 statusBar()->showMessage("failed to load scene file");
111 }
112 }
114 void GoatView::open_anim()
115 {
116 statusBar()->showMessage("opening animation...");
117 }
120 // ---- OpenGL viewport ----
121 GoatViewport::GoatViewport()
122 : QGLWidget(QGLFormat(QGL::DepthBuffer))
123 {
124 }
126 GoatViewport::~GoatViewport()
127 {
128 }
130 QSize GoatViewport::sizeHint() const
131 {
132 return QSize(800, 600);
133 }
135 void GoatViewport::initializeGL()
136 {
137 glClearColor(0.1, 0.1, 0.1, 1);
138 }
140 void GoatViewport::resizeGL(int xsz, int ysz)
141 {
142 glViewport(0, 0, xsz, ysz);
143 glMatrixMode(GL_PROJECTION);
144 glLoadIdentity();
145 gluPerspective(60.0, (float)xsz / (float)ysz, 0.5, 5000.0);
146 }
148 static void draw_node(goat3d_node *node);
150 void GoatViewport::paintGL()
151 {
152 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
154 glMatrixMode(GL_MODELVIEW);
155 glLoadIdentity();
156 glTranslatef(0, 0, -cam_dist);
157 glRotatef(cam_phi, 1, 0, 0);
158 glRotatef(cam_theta, 0, 1, 0);
160 if(scene) {
161 int node_count = goat3d_get_node_count(scene);
162 for(int i=0; i<node_count; i++) {
163 goat3d_node *node = goat3d_get_node(scene, i);
164 draw_node(node);
165 }
166 }
167 }
169 static void draw_node(goat3d_node *node)
170 {
171 float xform[16];
172 goat3d_get_node_matrix(node, xform, anim_time);
173 glMultMatrixf(xform);
175 if(goat3d_get_node_type(node) == GOAT3D_NODE_MESH) {
176 goat3d_mesh *mesh = (goat3d_mesh*)goat3d_get_node_object(node);
178 int num_faces = goat3d_get_mesh_face_count(mesh);
179 int num_verts = goat3d_get_mesh_attrib_count(mesh, GOAT3D_MESH_ATTR_VERTEX);
181 glEnableClientState(GL_VERTEX_ARRAY);
182 glVertexPointer(3, GL_FLOAT, 0, goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX));
184 float *data;
185 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL))) {
186 glEnableClientState(GL_NORMAL_ARRAY);
187 glNormalPointer(GL_FLOAT, 0, data);
188 }
189 if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
190 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
191 glTexCoordPointer(2, GL_FLOAT, 0, data);
192 }
194 int *indices;
195 if((indices = goat3d_get_mesh_faces(mesh))) {
196 glDrawElements(GL_TRIANGLES, num_faces * 3, GL_UNSIGNED_INT, indices);
197 } else {
198 glDrawArrays(GL_TRIANGLES, 0, num_verts * 3);
199 }
201 glDisableClientState(GL_VERTEX_ARRAY);
202 glDisableClientState(GL_NORMAL_ARRAY);
203 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
204 }
206 int num_child = goat3d_get_node_child_count(node);
207 for(int i=0; i<num_child; i++) {
208 draw_node(goat3d_get_node_child(node, i));
209 }
210 }