clray

view src/clray.cc @ 11:d9a1bab1c3f5

ported to windows
author John Tsiombikas
date Sat, 31 Jul 2010 22:23:57 +0100
parents 88ac4eb2d18a
children 85fd61f374d9
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"
12 void cleanup();
13 void disp();
14 void reshape(int x, int y);
15 void keyb(unsigned char key, int x, int y);
16 void mouse(int bn, int status, int x, int y);
17 void motion(int x, int y);
18 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
20 static float *fb;
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 int main(int argc, char **argv)
28 {
29 glutInitWindowSize(800, 600);
30 glutInit(&argc, argv);
31 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
32 glutCreateWindow("OpenCL Raytracer");
34 xsz = glutGet(GLUT_WINDOW_WIDTH);
35 ysz = glutGet(GLUT_WINDOW_HEIGHT);
37 glutDisplayFunc(disp);
38 glutReshapeFunc(reshape);
39 glutKeyboardFunc(keyb);
40 glutMouseFunc(mouse);
41 glutMotionFunc(motion);
43 fb = new float[xsz * ysz * 4];
44 if(!init_renderer(xsz, ysz, fb)) {
45 return 1;
46 }
47 atexit(cleanup);
49 /*glGenTextures(1, &tex);
50 glBindTexture(GL_TEXTURE_2D, tex);*/
51 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
52 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
53 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
55 glTexImage2D(GL_TEXTURE_2D, 0, 4, xsz, ysz, 0, GL_RGBA, GL_FLOAT, 0);
57 glutMainLoop();
58 return 0;
59 }
61 void cleanup()
62 {
63 delete [] fb;
64 destroy_renderer();
65 }
67 void disp()
68 {
69 glMatrixMode(GL_MODELVIEW);
70 glLoadIdentity();
72 if(need_update) {
73 float mat[16];
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);
81 set_xform(mat);
82 glPopMatrix();
84 render();
85 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGBA, GL_FLOAT, fb);
86 need_update = false;
87 }
89 glEnable(GL_TEXTURE_2D);
91 glBegin(GL_QUADS);
92 glTexCoord2f(0, 1); glVertex2f(-1, -1);
93 glTexCoord2f(1, 1); glVertex2f(1, -1);
94 glTexCoord2f(1, 0); glVertex2f(1, 1);
95 glTexCoord2f(0, 0); glVertex2f(-1, 1);
96 glEnd();
98 glDisable(GL_TEXTURE_2D);
100 glutSwapBuffers();
101 }
103 void reshape(int x, int y)
104 {
105 glViewport(0, 0, x, y);
107 /* reallocate the framebuffer */
108 /*delete [] fb;
109 fb = new float[x * y * 4];
110 set_framebuffer(fb, x, y);*/
111 }
113 void keyb(unsigned char key, int x, int y)
114 {
115 switch(key) {
116 case 27:
117 exit(0);
119 case 's':
120 if(write_ppm("shot.ppm", fb, xsz, ysz)) {
121 printf("captured screenshot shot.ppm\n");
122 }
123 break;
125 case 'r':
126 need_update = true;
127 glutPostRedisplay();
128 break;
130 default:
131 break;
132 }
133 }
135 static bool bnstate[32];
136 static int prev_x, prev_y;
138 void mouse(int bn, int state, int x, int y)
139 {
140 if(state == GLUT_DOWN) {
141 prev_x = x;
142 prev_y = y;
143 bnstate[bn] = true;
144 } else {
145 bnstate[bn] = false;
146 }
147 }
149 #define ROT_SCALE 0.5
150 #define PAN_SCALE 0.1
152 void motion(int x, int y)
153 {
154 int dx = x - prev_x;
155 int dy = y - prev_y;
156 prev_x = x;
157 prev_y = y;
159 if(bnstate[0]) {
160 cam_theta += dx * ROT_SCALE;
161 cam_phi += dy * ROT_SCALE;
163 if(cam_phi < -89) cam_phi = 89;
164 if(cam_phi > 89) cam_phi = 89;
166 need_update = true;
167 glutPostRedisplay();
168 }
169 if(bnstate[2]) {
170 cam_dist += dy * PAN_SCALE;
171 if(cam_dist < 0) cam_dist = 0;
173 need_update = true;
174 glutPostRedisplay();
175 }
176 }
178 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
179 {
180 FILE *fp;
182 if(!(fp = fopen(fname, "wb"))) {
183 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
184 return false;
185 }
186 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
188 for(int i=0; i<xsz * ysz * 4; i++) {
189 if(i % 4 == 3) continue;
191 unsigned char c = (unsigned char)(fb[i] * 255.0);
192 fputc(c, fp);
193 }
194 fclose(fp);
195 return true;
196 }