clray

annotate src/clray.cc @ 20:63a6b46f58a0

fixed
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Aug 2010 12:55:40 +0100
parents 4b1604f9798a
children bd6c2b25f6e7
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@13 12 #include "mesh.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@13 28 static bool dbg_glrender = true;
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@2 131
nuclear@12 132 glBegin(GL_QUADS);
nuclear@12 133 glColor3f(1, 1, 1);
nuclear@12 134 glTexCoord2f(0, 1); glVertex2f(-1, -1);
nuclear@12 135 glTexCoord2f(1, 1); glVertex2f(1, -1);
nuclear@12 136 glTexCoord2f(1, 0); glVertex2f(1, 1);
nuclear@12 137 glTexCoord2f(0, 0); glVertex2f(-1, 1);
nuclear@12 138 glEnd();
nuclear@2 139
nuclear@12 140 glDisable(GL_TEXTURE_2D);
nuclear@12 141 }
nuclear@3 142
nuclear@3 143 glutSwapBuffers();
nuclear@3 144 }
nuclear@3 145
nuclear@3 146 void reshape(int x, int y)
nuclear@3 147 {
nuclear@3 148 glViewport(0, 0, x, y);
nuclear@3 149
nuclear@3 150 /* reallocate the framebuffer */
nuclear@3 151 /*delete [] fb;
nuclear@3 152 fb = new float[x * y * 4];
nuclear@3 153 set_framebuffer(fb, x, y);*/
nuclear@3 154 }
nuclear@3 155
nuclear@3 156 void keyb(unsigned char key, int x, int y)
nuclear@3 157 {
nuclear@3 158 switch(key) {
nuclear@3 159 case 27:
nuclear@3 160 exit(0);
nuclear@3 161
nuclear@12 162 case 'r':
nuclear@12 163 need_update = true;
nuclear@12 164 glutPostRedisplay();
nuclear@3 165 break;
nuclear@3 166
nuclear@12 167 case 'd':
nuclear@12 168 dbg_glrender = !dbg_glrender;
nuclear@12 169 if(dbg_glrender) {
nuclear@12 170 printf("DEBUG GL RENDER\n");
nuclear@12 171 }
nuclear@3 172 glutPostRedisplay();
nuclear@3 173 break;
nuclear@3 174
nuclear@3 175 default:
nuclear@3 176 break;
nuclear@3 177 }
nuclear@3 178 }
nuclear@3 179
nuclear@8 180 static bool bnstate[32];
nuclear@8 181 static int prev_x, prev_y;
nuclear@8 182
nuclear@3 183 void mouse(int bn, int state, int x, int y)
nuclear@3 184 {
nuclear@8 185 if(state == GLUT_DOWN) {
nuclear@8 186 prev_x = x;
nuclear@8 187 prev_y = y;
nuclear@8 188 bnstate[bn] = true;
nuclear@8 189 } else {
nuclear@8 190 bnstate[bn] = false;
nuclear@8 191 }
nuclear@3 192 }
nuclear@3 193
nuclear@8 194 #define ROT_SCALE 0.5
nuclear@8 195 #define PAN_SCALE 0.1
nuclear@8 196
nuclear@3 197 void motion(int x, int y)
nuclear@3 198 {
nuclear@8 199 int dx = x - prev_x;
nuclear@8 200 int dy = y - prev_y;
nuclear@8 201 prev_x = x;
nuclear@8 202 prev_y = y;
nuclear@8 203
nuclear@8 204 if(bnstate[0]) {
nuclear@8 205 cam_theta += dx * ROT_SCALE;
nuclear@8 206 cam_phi += dy * ROT_SCALE;
nuclear@8 207
nuclear@12 208 if(cam_phi < -89) cam_phi = -89;
nuclear@8 209 if(cam_phi > 89) cam_phi = 89;
nuclear@8 210
nuclear@8 211 need_update = true;
nuclear@8 212 glutPostRedisplay();
nuclear@8 213 }
nuclear@8 214 if(bnstate[2]) {
nuclear@8 215 cam_dist += dy * PAN_SCALE;
nuclear@8 216 if(cam_dist < 0) cam_dist = 0;
nuclear@8 217
nuclear@8 218 need_update = true;
nuclear@8 219 glutPostRedisplay();
nuclear@8 220 }
nuclear@2 221 }
nuclear@2 222
nuclear@2 223 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
nuclear@2 224 {
nuclear@2 225 FILE *fp;
nuclear@2 226
nuclear@2 227 if(!(fp = fopen(fname, "wb"))) {
nuclear@2 228 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
nuclear@2 229 return false;
nuclear@2 230 }
nuclear@2 231 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
nuclear@2 232
nuclear@2 233 for(int i=0; i<xsz * ysz * 4; i++) {
nuclear@2 234 if(i % 4 == 3) continue;
nuclear@2 235
nuclear@2 236 unsigned char c = (unsigned char)(fb[i] * 255.0);
nuclear@2 237 fputc(c, fp);
nuclear@2 238 }
nuclear@2 239 fclose(fp);
nuclear@2 240 return true;
nuclear@2 241 }