clray

view src/clray.cc @ 12:85fd61f374d9

fixed the bloody intersection bug
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 03 Aug 2010 13:06:59 +0100
parents deaf85acf6af
children 407935b73af3
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"
13 void cleanup();
14 void disp();
15 void reshape(int x, int y);
16 void keyb(unsigned char key, int x, int y);
17 void mouse(int bn, int status, int x, int y);
18 void motion(int x, int y);
19 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
21 static int xsz, ysz;
22 static bool need_update = true;
24 static float cam_theta, cam_phi = 25.0;
25 static float cam_dist = 10.0;
27 static bool dbg_glrender;
29 int main(int argc, char **argv)
30 {
31 glutInitWindowSize(800, 600);
32 glutInit(&argc, argv);
33 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
34 glutCreateWindow("OpenCL Raytracer");
36 xsz = glutGet(GLUT_WINDOW_WIDTH);
37 ysz = glutGet(GLUT_WINDOW_HEIGHT);
39 glutDisplayFunc(disp);
40 glutReshapeFunc(reshape);
41 glutKeyboardFunc(keyb);
42 glutMouseFunc(mouse);
43 glutMotionFunc(motion);
45 if(!init_renderer(xsz, ysz)) {
46 return 1;
47 }
48 atexit(cleanup);
50 /*glGenTextures(1, &tex);
51 glBindTexture(GL_TEXTURE_2D, tex);*/
52 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
53 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
56 glTexImage2D(GL_TEXTURE_2D, 0, 4, xsz, ysz, 0, GL_RGBA, GL_FLOAT, 0);
58 glutMainLoop();
59 return 0;
60 }
62 void cleanup()
63 {
64 destroy_renderer();
65 }
67 static Matrix4x4 mat, inv_mat, inv_trans;
69 void disp()
70 {
71 glMatrixMode(GL_MODELVIEW);
72 glLoadIdentity();
74 if(need_update) {
75 glPushMatrix();
76 glRotatef(-cam_theta, 0, 1, 0);
77 glRotatef(-cam_phi, 1, 0, 0);
78 glTranslatef(0, 0, cam_dist);
80 glGetFloatv(GL_MODELVIEW_MATRIX, mat.m);
82 inv_mat = mat;
83 inv_mat.invert();
85 /*inv_trans = inv_mat;
86 inv_trans.transpose();*/
87 inv_trans = mat;
88 inv_trans.m[3] = inv_trans.m[7] = inv_trans.m[11] = 0.0;
89 inv_trans.m[12] = inv_trans.m[13] = inv_trans.m[14] = 0.0;
90 inv_trans.m[15] = 1.0;
92 set_xform(mat.m, inv_trans.m);
93 glPopMatrix();
95 if(!render()) {
96 exit(1);
97 }
98 need_update = false;
99 }
101 if(dbg_glrender) {
102 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
103 glLoadMatrixf(inv_mat.m);
104 dbg_render_gl();
105 } else {
106 glEnable(GL_TEXTURE_2D);
108 glBegin(GL_QUADS);
109 glColor3f(1, 1, 1);
110 glTexCoord2f(0, 1); glVertex2f(-1, -1);
111 glTexCoord2f(1, 1); glVertex2f(1, -1);
112 glTexCoord2f(1, 0); glVertex2f(1, 1);
113 glTexCoord2f(0, 0); glVertex2f(-1, 1);
114 glEnd();
116 glDisable(GL_TEXTURE_2D);
117 }
119 glutSwapBuffers();
120 }
122 void reshape(int x, int y)
123 {
124 glViewport(0, 0, x, y);
126 /* reallocate the framebuffer */
127 /*delete [] fb;
128 fb = new float[x * y * 4];
129 set_framebuffer(fb, x, y);*/
130 }
132 void keyb(unsigned char key, int x, int y)
133 {
134 switch(key) {
135 case 27:
136 exit(0);
138 case 'r':
139 need_update = true;
140 glutPostRedisplay();
141 break;
143 case 'd':
144 dbg_glrender = !dbg_glrender;
145 if(dbg_glrender) {
146 printf("DEBUG GL RENDER\n");
147 }
148 glutPostRedisplay();
149 break;
151 default:
152 break;
153 }
154 }
156 static bool bnstate[32];
157 static int prev_x, prev_y;
159 void mouse(int bn, int state, int x, int y)
160 {
161 if(state == GLUT_DOWN) {
162 prev_x = x;
163 prev_y = y;
164 bnstate[bn] = true;
165 } else {
166 bnstate[bn] = false;
167 }
168 }
170 #define ROT_SCALE 0.5
171 #define PAN_SCALE 0.1
173 void motion(int x, int y)
174 {
175 int dx = x - prev_x;
176 int dy = y - prev_y;
177 prev_x = x;
178 prev_y = y;
180 if(bnstate[0]) {
181 cam_theta += dx * ROT_SCALE;
182 cam_phi += dy * ROT_SCALE;
184 if(cam_phi < -89) cam_phi = -89;
185 if(cam_phi > 89) cam_phi = 89;
187 need_update = true;
188 glutPostRedisplay();
189 }
190 if(bnstate[2]) {
191 cam_dist += dy * PAN_SCALE;
192 if(cam_dist < 0) cam_dist = 0;
194 need_update = true;
195 glutPostRedisplay();
196 }
197 }
199 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
200 {
201 FILE *fp;
203 if(!(fp = fopen(fname, "wb"))) {
204 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
205 return false;
206 }
207 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
209 for(int i=0; i<xsz * ysz * 4; i++) {
210 if(i % 4 == 3) continue;
212 unsigned char c = (unsigned char)(fb[i] * 255.0);
213 fputc(c, fp);
214 }
215 fclose(fp);
216 return true;
217 }