clray

annotate src/clray.cc @ 24:13091c00d7ca

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