-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsb.c
365 lines (294 loc) · 7.97 KB
/
sb.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include <GL/glut.h>
#include "tb.h"
#include "trackball.h"
#include "glm.h"
GLuint model_list = 0; /* display list for object */
char* model_file = NULL; /* name of the obect file */
GLboolean facet_normal = GL_FALSE; /* draw with facet normal? */
GLMmodel* model;
GLfloat smoothing_angle = 90.0; /* smoothing angle */
GLfloat scale; /* scaling factor */
GLdouble pan_x = 0.0;
GLdouble pan_y = 0.0;
GLdouble pan_z = 0.0;
GLint mouse_state = -1;
GLint mouse_button = -1;
GLboolean bounding_box = GL_FALSE;
GLboolean performance = GL_FALSE;
GLboolean stats = GL_FALSE;
GLfloat weld_distance = 0.00001;
GLuint material_mode = 0;
GLfloat fps=0.0;
GLint nframe=0;
/* text: general purpose text routine. draws a string according to
* format in a stroke font at x, y after scaling it by the scale
* specified (scale is in window-space (lower-left origin) pixels).
*
* x - position in x (in window-space)
* y - position in y (in window-space)
* scale - scale in pixels
* format - as in printf()
*/
void
text(GLuint x, GLuint y, GLfloat scale, char* format, ...)
{
va_list args;
char buffer[255], *p;
GLfloat font_scale = 119.05 + 33.33;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, glutGet(GLUT_WINDOW_WIDTH), 0, glutGet(GLUT_WINDOW_HEIGHT));
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glTranslatef(x, y, 0.0);
glScalef(scale/font_scale, scale/font_scale, scale/font_scale);
for(p = buffer; *p; p++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
glPopAttrib();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void
lists(void)
{
GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
GLfloat specular[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat shininess = 65.0;
if (model_list)
glDeleteLists(model_list, 1);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
/* generate a list */
if (material_mode == 0) {
if (facet_normal)
model_list = glmList(model, GLM_FLAT);
else
model_list = glmList(model, GLM_SMOOTH);
} else if (material_mode == 1) {
if (facet_normal)
model_list = glmList(model, GLM_FLAT | GLM_COLOR);
else
model_list = glmList(model, GLM_SMOOTH | GLM_COLOR);
} else if (material_mode == 2) {
if (facet_normal)
model_list = glmList(model, GLM_FLAT | GLM_MATERIAL);
else
model_list = glmList(model, GLM_SMOOTH | GLM_MATERIAL);
}
}
void
init(void)
{
tbInit(GLUT_MIDDLE_BUTTON);
/* read in the model */
model = glmReadOBJ(model_file);
scale = glmUnitize(model);
glmFacetNormals(model);
glmVertexNormals(model, smoothing_angle);
if (model->nummaterials > 0)
material_mode = 2;
/* create new display lists */
lists();
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
}
void
reshape(int width, int height)
{
tbReshape(width, height);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10., 10., -10., 10., -10.,10);
//gluPerspective(30.0, (GLfloat)height / (GLfloat)width, 1.0, 128.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.0);
}
void
display(void)
{
nframe++;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (performance) {
glColor3f(1.0, 1.0, 1.0);
//text(5, 5, 20, "%.2f fps", 1.0 / ((end - last) / 1000.0));
text(5, 5, 20, "%.2f FPS", fps);
}
glPushMatrix();
glTranslatef(pan_x, pan_y, 0.0);
tbMatrix();
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glColor3f(0.5, 0.5, 0.5);
glCallList(model_list);
if (bounding_box) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glColor4f(1.0, 0.0, 0.0, 0.25);
glutSolidCube(2.0);
}
glPopMatrix();
if (stats) {
glColor3f(1.0, 1.0, 1.0);
text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*1), 20, "%s",
model->pathname);
text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*2), 20, "%d vertices",
model->numvertices);
text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*3), 20, "%d triangles",
model->numtriangles);
text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*4), 20, "%d normals",
model->numnormals);
text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*5), 20, "%d texcoords",
model->numtexcoords);
text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*6), 20, "%d groups",
model->numgroups);
text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*7), 20, "%d materials",
model->nummaterials);
}
glutSwapBuffers();
}
/* ARGSUSED1 */
void
keyboard(unsigned char key, int x, int y)
{
//GLint params[2];
switch (key) {
case 'q':
case 27:
exit(0);
break;
}
glutPostRedisplay();
}
void
menu(int item)
{
keyboard((unsigned char)item, 0, 0);
}
void
mouse(int button, int state, int x, int y)
{
GLdouble model[4*4];
GLdouble proj[4*4];
GLint view[4];
tbMouse(button, state, x, y);
mouse_state = state;
mouse_button = button;
if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON) {
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);
gluProject((GLdouble)x, (GLdouble)y, 0.0,
model, proj, view,
&pan_x, &pan_y, &pan_z);
gluUnProject((GLdouble)x, (GLdouble)y, pan_z,
model, proj, view,
&pan_x, &pan_y, &pan_z);
pan_y = -pan_y;
}
glutPostRedisplay();
}
void
motion(int x, int y)
{
GLdouble model[4*4];
GLdouble proj[4*4];
GLint view[4];
tbMotion(x, y);
if (mouse_state == GLUT_DOWN && mouse_button == GLUT_LEFT_BUTTON) {
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);
gluProject((GLdouble)x, (GLdouble)y, 0.0,
model, proj, view,
&pan_x, &pan_y, &pan_z);
gluUnProject((GLdouble)x, (GLdouble)y, pan_z,
model, proj, view,
&pan_x, &pan_y, &pan_z);
pan_y = -pan_y;
}
glutPostRedisplay();
}
void fpstimer(int value)
{
static int prev=0, now=0;
static unsigned int nframe_prev=0;
now = glutGet(GLUT_ELAPSED_TIME);
fps = (float)(nframe - nframe_prev)/(now - prev)*1000.0;
nframe_prev = nframe;
prev = now;
glutTimerFunc(1000, fpstimer, 0);
}
#define USAGE "Usage : \n\t$ ./sb MODEL_ID\n\tMODEL_ID = 1:SB128 2:SB256 3:neurons\n"
int
main(int argc, char** argv)
{
char sb_filename[100];
if(argc!=2){
printf("%s", USAGE);
exit(-1);
}
if(argv[1][0]=='1'){
strcpy(sb_filename, "obj/SB128.obj");
}else if(argv[1][0]=='2'){
strcpy(sb_filename, "obj/SB256.obj");
}else if(argv[1][0]=='3'){
strcpy(sb_filename, "obj/bench_neuron.obj");
}else if(argv[1][0]=='0'){
strcpy(sb_filename, "obj/cube.obj");
}else{
printf("%s", USAGE);
exit(-1);
}
glutInitWindowSize(900, 900);
glutInit(&argc, argv);
model_file = sb_filename;
performance = 1;
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("Standard Brain Benchmark");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
init();
if(argv[1][0]=='1'){
glmScale(model, 5.);
}else if(argv[1][0]=='2'){
glmScale(model, 5.);
}else if(argv[1][0]=='3'){
glmScale(model, 9.);
}else if(argv[1][0]=='0'){
glmScale(model, 7.0);
}else{
printf("%s", USAGE);
exit(-1);
}
lists();
glutTimerFunc(1000, fpstimer, 0);
glutMainLoop();
return 0;
}