clray

view src/clray.cc @ 13:407935b73af3

bollocks
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 04 Aug 2010 04:51:06 +0100
parents 85fd61f374d9
children 754faf15ba36
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;
30 static Scene scn;
32 int main(int argc, char **argv)
33 {
34 glutInitWindowSize(800, 600);
35 glutInit(&argc, argv);
37 int loaded = 0;
38 for(int i=1; i<argc; i++) {
39 if(!scn.load(argv[i])) {
40 fprintf(stderr, "failed to load scene: %s\n", argv[i]);
41 return false;
42 }
43 loaded++;
44 }
46 if(!loaded) {
47 fprintf(stderr, "you must specify a scene file to load\n");
48 return false;
49 }
50 if(!scn.get_num_faces()) {
51 fprintf(stderr, "didn't load any polygons\n");
52 return false;
53 }
55 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
56 glutCreateWindow("OpenCL Raytracer");
58 xsz = glutGet(GLUT_WINDOW_WIDTH);
59 ysz = glutGet(GLUT_WINDOW_HEIGHT);
61 glutDisplayFunc(disp);
62 glutReshapeFunc(reshape);
63 glutKeyboardFunc(keyb);
64 glutMouseFunc(mouse);
65 glutMotionFunc(motion);
67 if(!init_renderer(xsz, ysz, &scn)) {
68 return 1;
69 }
70 atexit(cleanup);
72 /*glGenTextures(1, &tex);
73 glBindTexture(GL_TEXTURE_2D, tex);*/
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
77 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
78 glTexImage2D(GL_TEXTURE_2D, 0, 4, xsz, ysz, 0, GL_RGBA, GL_FLOAT, 0);
80 glutMainLoop();
81 return 0;
82 }
84 void cleanup()
85 {
86 destroy_renderer();
87 }
89 static Matrix4x4 mat, inv_mat, inv_trans;
91 void disp()
92 {
93 glMatrixMode(GL_MODELVIEW);
94 glLoadIdentity();
96 if(need_update) {
97 glPushMatrix();
98 glRotatef(-cam_theta, 0, 1, 0);
99 glRotatef(-cam_phi, 1, 0, 0);
100 glTranslatef(0, 0, cam_dist);
102 glGetFloatv(GL_MODELVIEW_MATRIX, mat.m);
104 inv_mat = mat;
105 inv_mat.invert();
107 /*inv_trans = inv_mat;
108 inv_trans.transpose();*/
109 inv_trans = mat;
110 inv_trans.m[3] = inv_trans.m[7] = inv_trans.m[11] = 0.0;
111 inv_trans.m[12] = inv_trans.m[13] = inv_trans.m[14] = 0.0;
112 inv_trans.m[15] = 1.0;
114 set_xform(mat.m, inv_trans.m);
115 glPopMatrix();
117 if(!dbg_glrender) {
118 if(!render()) {
119 exit(1);
120 }
121 need_update = false;
122 }
123 }
125 if(dbg_glrender) {
126 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
127 glLoadMatrixf(inv_mat.m);
128 dbg_render_gl(&scn);
129 } else {
130 glEnable(GL_TEXTURE_2D);
132 glBegin(GL_QUADS);
133 glColor3f(1, 1, 1);
134 glTexCoord2f(0, 1); glVertex2f(-1, -1);
135 glTexCoord2f(1, 1); glVertex2f(1, -1);
136 glTexCoord2f(1, 0); glVertex2f(1, 1);
137 glTexCoord2f(0, 0); glVertex2f(-1, 1);
138 glEnd();
140 glDisable(GL_TEXTURE_2D);
141 }
143 glutSwapBuffers();
144 }
146 void reshape(int x, int y)
147 {
148 glViewport(0, 0, x, y);
150 /* reallocate the framebuffer */
151 /*delete [] fb;
152 fb = new float[x * y * 4];
153 set_framebuffer(fb, x, y);*/
154 }
156 void keyb(unsigned char key, int x, int y)
157 {
158 switch(key) {
159 case 27:
160 exit(0);
162 case 'r':
163 need_update = true;
164 glutPostRedisplay();
165 break;
167 case 'd':
168 dbg_glrender = !dbg_glrender;
169 if(dbg_glrender) {
170 printf("DEBUG GL RENDER\n");
171 }
172 glutPostRedisplay();
173 break;
175 default:
176 break;
177 }
178 }
180 static bool bnstate[32];
181 static int prev_x, prev_y;
183 void mouse(int bn, int state, int x, int y)
184 {
185 if(state == GLUT_DOWN) {
186 prev_x = x;
187 prev_y = y;
188 bnstate[bn] = true;
189 } else {
190 bnstate[bn] = false;
191 }
192 }
194 #define ROT_SCALE 0.5
195 #define PAN_SCALE 0.1
197 void motion(int x, int y)
198 {
199 int dx = x - prev_x;
200 int dy = y - prev_y;
201 prev_x = x;
202 prev_y = y;
204 if(bnstate[0]) {
205 cam_theta += dx * ROT_SCALE;
206 cam_phi += dy * ROT_SCALE;
208 if(cam_phi < -89) cam_phi = -89;
209 if(cam_phi > 89) cam_phi = 89;
211 need_update = true;
212 glutPostRedisplay();
213 }
214 if(bnstate[2]) {
215 cam_dist += dy * PAN_SCALE;
216 if(cam_dist < 0) cam_dist = 0;
218 need_update = true;
219 glutPostRedisplay();
220 }
221 }
223 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
224 {
225 FILE *fp;
227 if(!(fp = fopen(fname, "wb"))) {
228 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
229 return false;
230 }
231 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
233 for(int i=0; i<xsz * ysz * 4; i++) {
234 if(i % 4 == 3) continue;
236 unsigned char c = (unsigned char)(fb[i] * 255.0);
237 fputc(c, fp);
238 }
239 fclose(fp);
240 return true;
241 }