rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@3
|
2 #include <stdlib.h>
|
nuclear@2
|
3 #include <string.h>
|
nuclear@2
|
4 #include <errno.h>
|
nuclear@3
|
5 #ifndef __APPLE__
|
nuclear@3
|
6 #include <GL/glut.h>
|
nuclear@3
|
7 #else
|
nuclear@3
|
8 #include <GLUT/glut.h>
|
nuclear@3
|
9 #endif
|
nuclear@3
|
10 #include "rt.h"
|
nuclear@12
|
11 #include "matrix.h"
|
nuclear@13
|
12 #include "mesh.h"
|
nuclear@0
|
13
|
nuclear@3
|
14 void cleanup();
|
nuclear@3
|
15 void disp();
|
nuclear@3
|
16 void reshape(int x, int y);
|
nuclear@3
|
17 void keyb(unsigned char key, int x, int y);
|
nuclear@3
|
18 void mouse(int bn, int status, int x, int y);
|
nuclear@3
|
19 void motion(int x, int y);
|
nuclear@2
|
20 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
|
nuclear@2
|
21
|
nuclear@3
|
22 static int xsz, ysz;
|
nuclear@3
|
23 static bool need_update = true;
|
nuclear@3
|
24
|
nuclear@8
|
25 static float cam_theta, cam_phi = 25.0;
|
nuclear@8
|
26 static float cam_dist = 10.0;
|
nuclear@8
|
27
|
nuclear@13
|
28 static bool dbg_glrender = true;
|
John@15
|
29 static int dbg_dbg = 8;
|
nuclear@13
|
30
|
nuclear@13
|
31 static Scene scn;
|
nuclear@12
|
32
|
nuclear@3
|
33 int main(int argc, char **argv)
|
nuclear@0
|
34 {
|
nuclear@3
|
35 glutInitWindowSize(800, 600);
|
nuclear@3
|
36 glutInit(&argc, argv);
|
nuclear@13
|
37
|
nuclear@13
|
38 int loaded = 0;
|
nuclear@13
|
39 for(int i=1; i<argc; i++) {
|
nuclear@13
|
40 if(!scn.load(argv[i])) {
|
nuclear@13
|
41 fprintf(stderr, "failed to load scene: %s\n", argv[i]);
|
nuclear@13
|
42 return false;
|
nuclear@13
|
43 }
|
nuclear@13
|
44 loaded++;
|
nuclear@13
|
45 }
|
nuclear@13
|
46
|
nuclear@13
|
47 if(!loaded) {
|
nuclear@13
|
48 fprintf(stderr, "you must specify a scene file to load\n");
|
nuclear@13
|
49 return false;
|
nuclear@13
|
50 }
|
nuclear@13
|
51 if(!scn.get_num_faces()) {
|
nuclear@13
|
52 fprintf(stderr, "didn't load any polygons\n");
|
nuclear@13
|
53 return false;
|
nuclear@13
|
54 }
|
nuclear@13
|
55
|
nuclear@3
|
56 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
|
nuclear@3
|
57 glutCreateWindow("OpenCL Raytracer");
|
nuclear@0
|
58
|
nuclear@3
|
59 xsz = glutGet(GLUT_WINDOW_WIDTH);
|
nuclear@3
|
60 ysz = glutGet(GLUT_WINDOW_HEIGHT);
|
nuclear@2
|
61
|
nuclear@3
|
62 glutDisplayFunc(disp);
|
nuclear@3
|
63 glutReshapeFunc(reshape);
|
nuclear@3
|
64 glutKeyboardFunc(keyb);
|
nuclear@3
|
65 glutMouseFunc(mouse);
|
nuclear@3
|
66 glutMotionFunc(motion);
|
nuclear@0
|
67
|
nuclear@13
|
68 if(!init_renderer(xsz, ysz, &scn)) {
|
nuclear@0
|
69 return 1;
|
nuclear@0
|
70 }
|
nuclear@3
|
71 atexit(cleanup);
|
nuclear@0
|
72
|
nuclear@3
|
73 /*glGenTextures(1, &tex);
|
nuclear@3
|
74 glBindTexture(GL_TEXTURE_2D, tex);*/
|
nuclear@3
|
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
nuclear@3
|
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
nuclear@3
|
77 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@3
|
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@3
|
79 glTexImage2D(GL_TEXTURE_2D, 0, 4, xsz, ysz, 0, GL_RGBA, GL_FLOAT, 0);
|
nuclear@2
|
80
|
nuclear@3
|
81 glutMainLoop();
|
nuclear@0
|
82 return 0;
|
nuclear@0
|
83 }
|
nuclear@2
|
84
|
nuclear@3
|
85 void cleanup()
|
nuclear@2
|
86 {
|
nuclear@3
|
87 destroy_renderer();
|
nuclear@3
|
88 }
|
nuclear@2
|
89
|
nuclear@12
|
90 static Matrix4x4 mat, inv_mat, inv_trans;
|
nuclear@12
|
91
|
nuclear@3
|
92 void disp()
|
nuclear@3
|
93 {
|
nuclear@8
|
94 glMatrixMode(GL_MODELVIEW);
|
nuclear@8
|
95 glLoadIdentity();
|
nuclear@8
|
96
|
nuclear@3
|
97 if(need_update) {
|
nuclear@12
|
98 glPushMatrix();
|
nuclear@12
|
99 glRotatef(-cam_theta, 0, 1, 0);
|
nuclear@12
|
100 glRotatef(-cam_phi, 1, 0, 0);
|
nuclear@12
|
101 glTranslatef(0, 0, cam_dist);
|
nuclear@8
|
102
|
nuclear@12
|
103 glGetFloatv(GL_MODELVIEW_MATRIX, mat.m);
|
nuclear@8
|
104
|
nuclear@12
|
105 inv_mat = mat;
|
nuclear@12
|
106 inv_mat.invert();
|
nuclear@12
|
107
|
nuclear@12
|
108 /*inv_trans = inv_mat;
|
nuclear@12
|
109 inv_trans.transpose();*/
|
nuclear@12
|
110 inv_trans = mat;
|
nuclear@12
|
111 inv_trans.m[3] = inv_trans.m[7] = inv_trans.m[11] = 0.0;
|
nuclear@12
|
112 inv_trans.m[12] = inv_trans.m[13] = inv_trans.m[14] = 0.0;
|
nuclear@12
|
113 inv_trans.m[15] = 1.0;
|
nuclear@12
|
114
|
nuclear@12
|
115 set_xform(mat.m, inv_trans.m);
|
nuclear@8
|
116 glPopMatrix();
|
nuclear@8
|
117
|
nuclear@13
|
118 if(!dbg_glrender) {
|
nuclear@13
|
119 if(!render()) {
|
nuclear@13
|
120 exit(1);
|
nuclear@13
|
121 }
|
nuclear@13
|
122 need_update = false;
|
nuclear@12
|
123 }
|
nuclear@3
|
124 }
|
nuclear@2
|
125
|
nuclear@12
|
126 if(dbg_glrender) {
|
nuclear@12
|
127 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@12
|
128 glLoadMatrixf(inv_mat.m);
|
nuclear@13
|
129 dbg_render_gl(&scn);
|
nuclear@12
|
130 } else {
|
nuclear@12
|
131 glEnable(GL_TEXTURE_2D);
|
nuclear@2
|
132
|
nuclear@12
|
133 glBegin(GL_QUADS);
|
nuclear@12
|
134 glColor3f(1, 1, 1);
|
nuclear@12
|
135 glTexCoord2f(0, 1); glVertex2f(-1, -1);
|
nuclear@12
|
136 glTexCoord2f(1, 1); glVertex2f(1, -1);
|
nuclear@12
|
137 glTexCoord2f(1, 0); glVertex2f(1, 1);
|
nuclear@12
|
138 glTexCoord2f(0, 0); glVertex2f(-1, 1);
|
nuclear@12
|
139 glEnd();
|
nuclear@2
|
140
|
nuclear@12
|
141 glDisable(GL_TEXTURE_2D);
|
nuclear@12
|
142 }
|
nuclear@3
|
143
|
nuclear@3
|
144 glutSwapBuffers();
|
nuclear@3
|
145 }
|
nuclear@3
|
146
|
nuclear@3
|
147 void reshape(int x, int y)
|
nuclear@3
|
148 {
|
nuclear@3
|
149 glViewport(0, 0, x, y);
|
nuclear@3
|
150
|
nuclear@3
|
151 /* reallocate the framebuffer */
|
nuclear@3
|
152 /*delete [] fb;
|
nuclear@3
|
153 fb = new float[x * y * 4];
|
nuclear@3
|
154 set_framebuffer(fb, x, y);*/
|
nuclear@3
|
155 }
|
nuclear@3
|
156
|
nuclear@3
|
157 void keyb(unsigned char key, int x, int y)
|
nuclear@3
|
158 {
|
nuclear@3
|
159 switch(key) {
|
nuclear@3
|
160 case 27:
|
nuclear@3
|
161 exit(0);
|
nuclear@3
|
162
|
nuclear@12
|
163 case 'r':
|
nuclear@12
|
164 need_update = true;
|
nuclear@12
|
165 glutPostRedisplay();
|
nuclear@3
|
166 break;
|
nuclear@3
|
167
|
nuclear@12
|
168 case 'd':
|
nuclear@12
|
169 dbg_glrender = !dbg_glrender;
|
nuclear@12
|
170 if(dbg_glrender) {
|
nuclear@12
|
171 printf("DEBUG GL RENDER\n");
|
nuclear@12
|
172 }
|
nuclear@3
|
173 glutPostRedisplay();
|
nuclear@3
|
174 break;
|
nuclear@3
|
175
|
John@15
|
176 case '=':
|
John@15
|
177 dbg_set_dbg(++dbg_dbg);
|
John@15
|
178 need_update = true;
|
John@15
|
179 glutPostRedisplay();
|
John@15
|
180 break;
|
John@15
|
181
|
John@15
|
182 case '-':
|
John@15
|
183 dbg_set_dbg(--dbg_dbg);
|
John@15
|
184 need_update = true;
|
John@15
|
185 glutPostRedisplay();
|
John@15
|
186 break;
|
John@15
|
187
|
nuclear@3
|
188 default:
|
nuclear@3
|
189 break;
|
nuclear@3
|
190 }
|
nuclear@3
|
191 }
|
nuclear@3
|
192
|
nuclear@8
|
193 static bool bnstate[32];
|
nuclear@8
|
194 static int prev_x, prev_y;
|
nuclear@8
|
195
|
nuclear@3
|
196 void mouse(int bn, int state, int x, int y)
|
nuclear@3
|
197 {
|
nuclear@8
|
198 if(state == GLUT_DOWN) {
|
nuclear@8
|
199 prev_x = x;
|
nuclear@8
|
200 prev_y = y;
|
nuclear@8
|
201 bnstate[bn] = true;
|
nuclear@8
|
202 } else {
|
nuclear@8
|
203 bnstate[bn] = false;
|
nuclear@8
|
204 }
|
nuclear@3
|
205 }
|
nuclear@3
|
206
|
nuclear@8
|
207 #define ROT_SCALE 0.5
|
nuclear@8
|
208 #define PAN_SCALE 0.1
|
nuclear@8
|
209
|
nuclear@3
|
210 void motion(int x, int y)
|
nuclear@3
|
211 {
|
nuclear@8
|
212 int dx = x - prev_x;
|
nuclear@8
|
213 int dy = y - prev_y;
|
nuclear@8
|
214 prev_x = x;
|
nuclear@8
|
215 prev_y = y;
|
nuclear@8
|
216
|
nuclear@8
|
217 if(bnstate[0]) {
|
nuclear@8
|
218 cam_theta += dx * ROT_SCALE;
|
nuclear@8
|
219 cam_phi += dy * ROT_SCALE;
|
nuclear@8
|
220
|
nuclear@12
|
221 if(cam_phi < -89) cam_phi = -89;
|
nuclear@8
|
222 if(cam_phi > 89) cam_phi = 89;
|
nuclear@8
|
223
|
nuclear@8
|
224 need_update = true;
|
nuclear@8
|
225 glutPostRedisplay();
|
nuclear@8
|
226 }
|
nuclear@8
|
227 if(bnstate[2]) {
|
nuclear@8
|
228 cam_dist += dy * PAN_SCALE;
|
nuclear@8
|
229 if(cam_dist < 0) cam_dist = 0;
|
nuclear@8
|
230
|
nuclear@8
|
231 need_update = true;
|
nuclear@8
|
232 glutPostRedisplay();
|
nuclear@8
|
233 }
|
nuclear@2
|
234 }
|
nuclear@2
|
235
|
nuclear@2
|
236 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
|
nuclear@2
|
237 {
|
nuclear@2
|
238 FILE *fp;
|
nuclear@2
|
239
|
nuclear@2
|
240 if(!(fp = fopen(fname, "wb"))) {
|
nuclear@2
|
241 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
|
nuclear@2
|
242 return false;
|
nuclear@2
|
243 }
|
nuclear@2
|
244 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
|
nuclear@2
|
245
|
nuclear@2
|
246 for(int i=0; i<xsz * ysz * 4; i++) {
|
nuclear@2
|
247 if(i % 4 == 3) continue;
|
nuclear@2
|
248
|
nuclear@2
|
249 unsigned char c = (unsigned char)(fb[i] * 255.0);
|
nuclear@2
|
250 fputc(c, fp);
|
nuclear@2
|
251 }
|
nuclear@2
|
252 fclose(fp);
|
nuclear@2
|
253 return true;
|
nuclear@2
|
254 }
|