clray
view src/clray.cc @ 21:bd6c2b25f6e7
fixed, now we need to start with optimizations
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 10 Aug 2010 07:24:18 +0100 |
parents | 63a6b46f58a0 |
children | 6c44e4b1726d |
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_DEPTH | 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 OpenGL rendering\n");
171 } else {
172 printf("Raytracing\n");
173 }
174 glutPostRedisplay();
175 break;
177 default:
178 break;
179 }
180 }
182 static bool bnstate[32];
183 static int prev_x, prev_y;
185 void mouse(int bn, int state, int x, int y)
186 {
187 if(state == GLUT_DOWN) {
188 prev_x = x;
189 prev_y = y;
190 bnstate[bn] = true;
191 } else {
192 bnstate[bn] = false;
193 }
194 }
196 #define ROT_SCALE 0.5
197 #define PAN_SCALE 0.1
199 void motion(int x, int y)
200 {
201 int dx = x - prev_x;
202 int dy = y - prev_y;
203 prev_x = x;
204 prev_y = y;
206 if(bnstate[0]) {
207 cam_theta += dx * ROT_SCALE;
208 cam_phi += dy * ROT_SCALE;
210 if(cam_phi < -89) cam_phi = -89;
211 if(cam_phi > 89) cam_phi = 89;
213 need_update = true;
214 glutPostRedisplay();
215 }
216 if(bnstate[2]) {
217 cam_dist += dy * PAN_SCALE;
218 if(cam_dist < 0) cam_dist = 0;
220 need_update = true;
221 glutPostRedisplay();
222 }
223 }
225 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
226 {
227 FILE *fp;
229 if(!(fp = fopen(fname, "wb"))) {
230 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
231 return false;
232 }
233 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
235 for(int i=0; i<xsz * ysz * 4; i++) {
236 if(i % 4 == 3) continue;
238 unsigned char c = (unsigned char)(fb[i] * 255.0);
239 fputc(c, fp);
240 }
241 fclose(fp);
242 return true;
243 }