-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3dav_glut.c
396 lines (334 loc) · 8.49 KB
/
3dav_glut.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#if defined(__APPLE__) && defined(__MACH__)
#include <GLUT/glut.h>
#else
#include <GL/freeglut.h>
#endif
void setPerspectiveGLUT(float fov, float aspect, float nearx, float farx)
{
float f = 1.0f / tan(fov * 0.5f);
float range = nearx - farx;
float matrix[16] = {
f / aspect, 0.0f, 0.0f, 0.0f,
0.0f, f, 0.0f, 0.0f,
0.0f, 0.0f, (farx + nearx) / range, -1.0f,
0.0f, 0.0f, (2.0f * farx * nearx) / range, 0.0f
};
glMultMatrixf(matrix);
}
void setPerspective(float fov, float aspect, float nearx, float farx)
{
setPerspectiveGLUT(fov, aspect, nearx, farx);
// glFrustum(-aspect * nearx, aspect * nearx, -nearx, nearx, nearx, farx);
}
static const char *helpprompt[] = {"Press F1 for help", 0};
static const char *helptext[] = {
"Rotate: left mouse drag",
" Scale: right mouse drag up/down",
" Pan: middle mouse drag",
"",
"Toggle fullscreen: f",
"Toggle animation: space",
"Quit: escape",
0
};
void loadObj(char * x);
void display(void);
void drawObj(void);
void print_help(void);
void reshape(int x, int y);
void keypress(unsigned char key, int x, int y);
void skeypress(int key, int x, int y);
void mouse(int bn, int st, int x, int y);
void motion(int x, int y);
int win_width, win_height;
float cam_theta, cam_phi = 25, cam_dist = 8;
float cam_pan[3];
int mouse_x, mouse_y;
int bnstate[8];
int anim, help;
long anim_start;
long nframes;
#ifndef GL_FRAMEBUFFER_SRGB
#define GL_FRAMEBUFFER_SRGB 0x8db9
#endif
#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE 0x809d
#endif
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(800, 600);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("3dav");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keypress);
glutSpecialFunc(skeypress);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glClear(GL_DEPTH_BUFFER_BIT);
loadObj("porsche.obj");//replace porsche.obj with radar.obj or any other .obj to display it
glutMainLoop();
return 0;
}
void idle(void)
{
glutPostRedisplay();
}
GLuint object;
float objectrot;
char ch='1';
//other functions and main
//.obj loader code begins
void loadObj(char *fname) {
FILE *fp;
int read;
GLfloat x, y, z;
char ch;
object = glGenLists(1);
fp = fopen(fname, "r");
if (!fp) {
printf("can't open file %s\n", fname);
exit(1);
}
// Temporary storage for vertices and faces
GLfloat vertices[10000][3]; // Adjust size as needed
int faces[10000][3]; // Adjust size as needed
int vertexCount = 0;
int faceCount = 0;
// Read the file and store vertices and faces
while (!feof(fp)) {
read = fscanf(fp, " %c", &ch); // Read the first character of the line
if (ch == 'v') {
// Read vertex data
fscanf(fp, "%f %f %f", &vertices[vertexCount][0], &vertices[vertexCount][1], &vertices[vertexCount][2]);
vertexCount++;
} else if (ch == 'f') {
// Read face data (handling all formats)
char faceLine[256];
fgets(faceLine, sizeof(faceLine), fp); // Read the rest of the line
int v[3]; // Vertex indices for the face
int count = 0;
// Parse the face line
char *token = strtok(faceLine, " \t\n");
while (token != NULL && count < 3) {
// Extract the vertex index (first number in the group)
sscanf(token, "%d", &v[count]);
// Move to the next token
token = strtok(NULL, " \t\n");
count++;
}
// Store the face indices
for (int i=0; i < count; i++) faces[faceCount][i] = v[i];
faceCount++;
}
}
fclose(fp);
// Compile the display list
glNewList(object, GL_COMPILE);
{
glPushMatrix();
/*
// Draw points
glPointSize(3.0);
glBegin(GL_POINTS);
for (int i = 0; i < vertexCount; i++) {
glVertex3f(vertices[i][0], vertices[i][1], vertices[i][2]);
}
glEnd();
*/
// Draw triangles
glBegin(GL_TRIANGLES);
for (int i = 0; i < faceCount; i++) {
int v1 = faces[i][0] - 1; // OBJ indices start at 1
int v2 = faces[i][1] - 1;
int v3 = faces[i][2] - 1;
glVertex3f(vertices[v1][0], vertices[v1][1], vertices[v1][2]);
glVertex3f(vertices[v2][0], vertices[v2][1], vertices[v2][2]);
glVertex3f(vertices[v3][0], vertices[v3][1], vertices[v3][2]);
}
glEnd();
glPopMatrix();
}
glEndList();
}
void display(void)
{
long tm;
float lpos[] = {-1, 2, 3, 0};
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -cam_dist);
glRotatef(cam_phi, 1, 0, 0);
glRotatef(cam_theta, 0, 1, 0);
glTranslatef(cam_pan[0], cam_pan[1], cam_pan[2]);
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glPushMatrix();
if(anim) {
tm = glutGet(GLUT_ELAPSED_TIME) - anim_start;
glRotatef(tm / 10.0f, 0, 1, 0);
}
drawObj();
print_help();
glutSwapBuffers();
nframes++;
}
void drawObj()
{
glPushMatrix();
glColor3f(1.0,0.23,0.27);
glScalef(0.1,0.1,0.1);
glCallList(object);
glPopMatrix();
glBegin(GL_QUADS);
glNormal3f( 0, 1, 0);
glVertex3f(-5, -1.7, 5);
glVertex3f( 5, -1.7, 5);
glVertex3f( 5, -1.7, -5);
glVertex3f(-5, -1.7, -5);
glEnd();
}
void print_help(void)
{
int i;
const char *s, **text;
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, win_width, 0, win_height, -1, 1);
text = help ? helptext : helpprompt;
for(i=0; text[i]; i++) {
glColor3f(0, 0.1, 0);
glRasterPos2f(7, win_height - (i + 1) * 20 - 2);
s = text[i];
while(*s) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *s++);
}
glColor3f(0, 0.9, 0);
glRasterPos2f(5, win_height - (i + 1) * 20);
s = text[i];
while(*s) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *s++);
}
}
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopAttrib();
}
void reshape(int x, int y)
{
float vsz, aspect = (float)x / (float)y;
win_width = x;
win_height = y;
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
setPerspective(45, (GLfloat)x / (GLfloat)y, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void keypress(unsigned char key, int x, int y)
{
static int fullscr;
static int prev_xsz, prev_ysz;
switch(key) {
case 27:
case 'q':
exit(0);
break;
case ' ':
anim ^= 1;
glutIdleFunc(anim ? idle : 0);
glutPostRedisplay();
if(anim) {
anim_start = glutGet(GLUT_ELAPSED_TIME);
nframes = 0;
} else {
long tm = glutGet(GLUT_ELAPSED_TIME) - anim_start;
long fps = (nframes * 100000) / tm;
printf("framerate: %ld.%ld fps\n", fps / 100, fps % 100);
}
break;
case '\n':
case '\r':
if(!(glutGetModifiers() & GLUT_ACTIVE_ALT)) {
break;
}
case 'f':
fullscr ^= 1;
if(fullscr) {
prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
glutFullScreen();
} else {
glutReshapeWindow(prev_xsz, prev_ysz);
}
break;
}
}
void skeypress(int key, int x, int y)
{
switch(key) {
case GLUT_KEY_F1:
help ^= 1;
glutPostRedisplay();
default:
break;
}
}
void mouse(int bn, int st, int x, int y)
{
int bidx = bn - GLUT_LEFT_BUTTON;
bnstate[bidx] = st == GLUT_DOWN;
mouse_x = x;
mouse_y = y;
}
void motion(int x, int y)
{
int dx = x - mouse_x;
int dy = y - mouse_y;
mouse_x = x;
mouse_y = y;
if(!(dx | dy)) return;
if(bnstate[0]) {
cam_theta += dx * 0.5;
cam_phi += dy * 0.5;
if(cam_phi < -90) cam_phi = -90;
if(cam_phi > 90) cam_phi = 90;
glutPostRedisplay();
}
if(bnstate[1]) {
float up[3], right[3];
float theta = cam_theta * M_PI / 180.0f;
float phi = cam_phi * M_PI / 180.0f;
up[0] = -sin(theta) * sin(phi);
up[1] = -cos(phi);
up[2] = cos(theta) * sin(phi);
right[0] = cos(theta);
right[1] = 0;
right[2] = sin(theta);
cam_pan[0] += (right[0] * dx + up[0] * dy) * 0.01;
cam_pan[1] += up[1] * dy * 0.01;
cam_pan[2] += (right[2] * dx + up[2] * dy) * 0.01;
glutPostRedisplay();
}
if(bnstate[2]) {
cam_dist += dy * 0.1;
if(cam_dist < 0) cam_dist = 0;
glutPostRedisplay();
}
}