clray

view src/clray.cc @ 3:88ac4eb2d18a

added OpenGL display of the framebuffer
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 13 Jul 2010 03:38:29 +0300
parents 41d6253492ad
children deaf85acf6af
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 int main(int argc, char **argv)
25 {
26 glutInitWindowSize(800, 600);
27 glutInit(&argc, argv);
28 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
29 glutCreateWindow("OpenCL Raytracer");
31 xsz = glutGet(GLUT_WINDOW_WIDTH);
32 ysz = glutGet(GLUT_WINDOW_HEIGHT);
34 glutDisplayFunc(disp);
35 glutReshapeFunc(reshape);
36 glutKeyboardFunc(keyb);
37 glutMouseFunc(mouse);
38 glutMotionFunc(motion);
40 fb = new float[xsz * ysz * 4];
41 if(!init_renderer(xsz, ysz, fb)) {
42 return 1;
43 }
44 atexit(cleanup);
46 /*glGenTextures(1, &tex);
47 glBindTexture(GL_TEXTURE_2D, tex);*/
48 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
50 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
51 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
52 glTexImage2D(GL_TEXTURE_2D, 0, 4, xsz, ysz, 0, GL_RGBA, GL_FLOAT, 0);
54 glutMainLoop();
55 return 0;
56 }
58 void cleanup()
59 {
60 delete [] fb;
61 destroy_renderer();
62 }
64 void disp()
65 {
66 if(need_update) {
67 render();
68 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGBA, GL_FLOAT, fb);
69 need_update = false;
70 }
72 glEnable(GL_TEXTURE_2D);
74 glBegin(GL_QUADS);
75 glTexCoord2f(0, 1); glVertex2f(-1, -1);
76 glTexCoord2f(1, 1); glVertex2f(1, -1);
77 glTexCoord2f(1, 0); glVertex2f(1, 1);
78 glTexCoord2f(0, 0); glVertex2f(-1, 1);
79 glEnd();
81 glDisable(GL_TEXTURE_2D);
83 glutSwapBuffers();
84 }
86 void reshape(int x, int y)
87 {
88 glViewport(0, 0, x, y);
90 /* reallocate the framebuffer */
91 /*delete [] fb;
92 fb = new float[x * y * 4];
93 set_framebuffer(fb, x, y);*/
94 }
96 void keyb(unsigned char key, int x, int y)
97 {
98 switch(key) {
99 case 27:
100 exit(0);
102 case 's':
103 if(write_ppm("shot.ppm", fb, xsz, ysz)) {
104 printf("captured screenshot shot.ppm\n");
105 }
106 break;
108 case 'r':
109 need_update = true;
110 glutPostRedisplay();
111 break;
113 default:
114 break;
115 }
116 }
118 void mouse(int bn, int state, int x, int y)
119 {
120 }
122 void motion(int x, int y)
123 {
124 }
126 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
127 {
128 FILE *fp;
130 if(!(fp = fopen(fname, "wb"))) {
131 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
132 return false;
133 }
134 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
136 for(int i=0; i<xsz * ysz * 4; i++) {
137 if(i % 4 == 3) continue;
139 unsigned char c = (unsigned char)(fb[i] * 255.0);
140 fputc(c, fp);
141 }
142 fclose(fp);
143 return true;
144 }