clray

annotate src/clray.cc @ 10:2da57bedc550

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