-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcbcview.c
313 lines (256 loc) · 6.28 KB
/
rcbcview.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define DO_OPENGL
#ifndef _SKIPGL
#include <GL/gl.h>
#include <GL/glut.h>
#endif
#include "rcbc.h"
#include "console.h"
#define PI 3.1415
#define Y_LOCK 0.1f /* Stops flipping the cube by rotating to high */
/* Zoom settings (mouse wheel) */
#define ZOOM_STEP 0.25f /* Amount to zoom for each wheel nodule */
#define ZOOM_MIN 0.5f
#define ZOOM_MAX 50.0f
#define COLOUR_WHITE_3F 1.0f, 1.0f, 1.0f
#define COLOUR_CUBE COLOUR_WHITE_3F
#define BOX_SIZE 1.0f
#define KEY_ESC 27
#define KEY_q 113
#define KEY_Q 81
#define KEY_w 119
#define KEY_W 87
struct globals {
unsigned int height;
unsigned int width;
Model* model;
int limitfps;
int mouse_x;
int mouse_y;
float cam_x;
float cam_y;
float cam_z;
float cam_zoom;
float cam_rot_x;
float cam_rot_y;
float cam_rot_x_temp;
float cam_rot_y_temp;
float cam_fov;
int wireframe;
};
struct globals g;
#ifndef _SKIPGL
/**
* Position camera around box using some voodoo math.
*/
void cameraPosition() {
/* http://en.wikipedia.org/wiki/Spherical_coordinates */
float theta = g.cam_rot_x+g.cam_rot_x_temp;
float phi = g.cam_rot_y+g.cam_rot_y_temp;
g.cam_x = g.cam_zoom * (cos(theta*(PI/180))) * (sin(phi*(PI/180)));
g.cam_y = g.cam_zoom * (cos(phi*(PI/180)));
g.cam_z = g.cam_zoom * (sin(theta*(PI/180))) * (sin(phi*(PI/180)));
gluLookAt( g.cam_x, g.cam_y, g.cam_z, 0.0f, 0.0f, 0.0f, 0.0f, 10.0f, 0.0f );
}
/**
* Set 3D perspective mode.
*/
void setPerspective() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(g.cam_fov, (GLfloat)g.width / (GLfloat)g.height, 0.1f, 10000.0f);
glMatrixMode(GL_MODELVIEW);
}
void displayFunc() {
static int last_render_time = 0;
static int last_fps_time = 0;
static int frame = 0;
int fps;
static int mpf;
int current_time = glutGet(GLUT_ELAPSED_TIME);
/* Limit framerate */
if(g.limitfps && !( (current_time - last_render_time) >= 1000/60) ) {
return;
}
/* Calculate FPS */
frame++;
if(current_time - last_fps_time > 1000) {
fps = frame*1000.0f/(current_time-last_fps_time);
last_fps_time = current_time;
frame = 0;
LOG("FPS: %d,\tMPF: %d", fps, mpf);
}
last_render_time = current_time;
setPerspective();
glLoadIdentity();
cameraPosition();
glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
glClearDepth(10000.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glutWireCube(1.0f);
RCBC_Render(g.model);
glFlush();
glutSwapBuffers();
mpf = glutGet(GLUT_ELAPSED_TIME) - current_time;
int error;
if ((error = glGetError()))
ERROR("GLUT: %s", gluErrorString(error));
}
/**
* Sets OpenGL info to Solid/Wireframe mode.
*/
void setPolygonMode() {
if(g.wireframe) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
} else {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
}
/**
* When the window is resized.
*/
void reshapeFunc(int width, int height) {
if (height==0) {
height=1;
}
g.height = height;
g.width = width;
glViewport(0, 0, width, height);
setPerspective();
}
/**
* Handle mouse rotation
*/
void motionFunc(int x, int y) {
int x_diff, y_diff;
x_diff = x - g.mouse_x;
y_diff = y - g.mouse_y;
g.cam_rot_x_temp = ((float)x_diff / g.width) * 100;
g.cam_rot_y_temp = -((float)y_diff / g.height) * 100;
/* Stop flipping over the top of the cube */
if(g.cam_rot_y_temp + g.cam_rot_y < Y_LOCK) {
g.cam_rot_y_temp = Y_LOCK-g.cam_rot_y;
} else if(g.cam_rot_y_temp + g.cam_rot_y > 180.0f - Y_LOCK) {
g.cam_rot_y_temp = (180.0f-Y_LOCK)-g.cam_rot_y;
}
}
/**
* When mouse clicked.
*/
void mouseFunc(int button, int state, int x, int y) {
switch(button) {
case(0):
if(!state) { /* Save the current x,y cords when starting to drag */
g.mouse_x = x;
g.mouse_y = y;
} else { /* Store the temp rotation as permenate when finished */
g.cam_rot_x+=g.cam_rot_x_temp;
g.cam_rot_y+=g.cam_rot_y_temp;
g.cam_rot_x_temp = 0.0f;
g.cam_rot_y_temp = 0.0f;
}
break;
case(4): /* Zoom out with mouse wheel */
g.cam_zoom+=ZOOM_STEP;
if(g.cam_zoom>ZOOM_MAX)
g.cam_zoom=ZOOM_MAX;
LOG("Zoom at %f.", g.cam_zoom);
break;
case(3): /* Zoom in with mouse wheel */
g.cam_zoom-=ZOOM_STEP;
if(g.cam_zoom<ZOOM_MIN)
g.cam_zoom=ZOOM_MIN;
LOG("Zoom at %f.", g.cam_zoom);
break;
}
}
/**
* GLUT idle function.
*/
void idleFunc() {
glutPostRedisplay();
}
/**
* Handle key presses
*/
void keyboardFunc(unsigned char key, int x, int y) {
switch(key) {
case(KEY_ESC): case(KEY_q): case(KEY_Q): /* ESC / Q */
LOG("Cleaning up...");
DELETE(g.model);
exit(0);
break;
case(KEY_w): case(KEY_W): /* Toggle wireframe on/off */
g.wireframe = !g.wireframe;
setPolygonMode();
break;
default:
DEBUG(DEBUG_LOW, "Pressed unknown key: %d\n", key);
break;
}
}
#endif
/**
* The glorious main function.
*/
int main(int argc, char** argv) {
char* filename;
if(argc < 2) {
filename = "data/models/monkey-test.dae"; // Default model to view
WARNING("No model file specified, using trying default '%s'.", filename);
} else {
filename = argv[1];
}
List* images = NEW(List);
g.height = 600;
g.width = 800;
RCBC_Init();
g.model = RCBC_LoadFile(filename, images);
Model* model2 = RCBC_LoadFile("data/models/3 sided wall.dae", images);
// Create window
LOG("Initilizing RCBC GLUT Viewer...");
#ifndef _SKIPGL
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(g.width, g.height);
glutCreateWindow("RCBC Viewer");
glutDisplayFunc(&displayFunc);
glutIdleFunc(&idleFunc);
glutReshapeFunc(&reshapeFunc);
glutMouseFunc(&mouseFunc);
glutMotionFunc(&motionFunc);
glutKeyboardFunc(&keyboardFunc);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(10000.0f);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_TEXTURE_2D);
#endif
g.mouse_x = 0;
g.mouse_y = 0;
g.wireframe = 0;
//setPolygonMode();
g.limitfps = 1;
g.cam_zoom = 2.5f;
g.cam_rot_x = -90.0f;
g.cam_rot_y = 45.0f;
g.cam_rot_x_temp = 0.0f;
g.cam_rot_y_temp = 0.0f;
g.cam_fov = 45.0f;
LOG("Starting GLUT main loop...");
#ifndef _SKIPGL
glutMainLoop();
#endif
DELETE(g.model);
DELETE(model2);
//List_DeleteData(images);
DEBUG_A("-------------------------");
DEBUG_A("-------------------------");
DEBUG_A("-------------------------");
List_ScrubImages(images);
DELETE(images);
exit(EXIT_SUCCESS);
}