clray

view src/clray.cc @ 18:4b1604f9798a

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