nuclear@0: #include nuclear@3: #include nuclear@2: #include nuclear@2: #include nuclear@3: #ifndef __APPLE__ nuclear@3: #include nuclear@3: #else nuclear@3: #include nuclear@3: #endif nuclear@3: #include "rt.h" nuclear@0: nuclear@3: void cleanup(); nuclear@3: void disp(); nuclear@3: void reshape(int x, int y); nuclear@3: void keyb(unsigned char key, int x, int y); nuclear@3: void mouse(int bn, int status, int x, int y); nuclear@3: void motion(int x, int y); nuclear@2: bool write_ppm(const char *fname, float *fb, int xsz, int ysz); nuclear@2: nuclear@3: static float *fb; nuclear@3: static int xsz, ysz; nuclear@3: static bool need_update = true; nuclear@3: nuclear@3: int main(int argc, char **argv) nuclear@0: { nuclear@3: glutInitWindowSize(800, 600); nuclear@3: glutInit(&argc, argv); nuclear@3: glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); nuclear@3: glutCreateWindow("OpenCL Raytracer"); nuclear@0: nuclear@3: xsz = glutGet(GLUT_WINDOW_WIDTH); nuclear@3: ysz = glutGet(GLUT_WINDOW_HEIGHT); nuclear@2: nuclear@3: glutDisplayFunc(disp); nuclear@3: glutReshapeFunc(reshape); nuclear@3: glutKeyboardFunc(keyb); nuclear@3: glutMouseFunc(mouse); nuclear@3: glutMotionFunc(motion); nuclear@0: nuclear@3: fb = new float[xsz * ysz * 4]; nuclear@3: if(!init_renderer(xsz, ysz, fb)) { nuclear@0: return 1; nuclear@0: } nuclear@3: atexit(cleanup); nuclear@0: nuclear@3: /*glGenTextures(1, &tex); nuclear@3: glBindTexture(GL_TEXTURE_2D, tex);*/ nuclear@3: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); nuclear@3: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); nuclear@3: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); nuclear@3: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); nuclear@3: glTexImage2D(GL_TEXTURE_2D, 0, 4, xsz, ysz, 0, GL_RGBA, GL_FLOAT, 0); nuclear@2: nuclear@3: glutMainLoop(); nuclear@0: return 0; nuclear@0: } nuclear@2: nuclear@3: void cleanup() nuclear@2: { nuclear@3: delete [] fb; nuclear@3: destroy_renderer(); nuclear@3: } nuclear@2: nuclear@3: void disp() nuclear@3: { nuclear@3: if(need_update) { nuclear@3: render(); nuclear@3: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGBA, GL_FLOAT, fb); nuclear@3: need_update = false; nuclear@3: } nuclear@2: nuclear@3: glEnable(GL_TEXTURE_2D); nuclear@2: nuclear@3: glBegin(GL_QUADS); nuclear@3: glTexCoord2f(0, 1); glVertex2f(-1, -1); nuclear@3: glTexCoord2f(1, 1); glVertex2f(1, -1); nuclear@3: glTexCoord2f(1, 0); glVertex2f(1, 1); nuclear@3: glTexCoord2f(0, 0); glVertex2f(-1, 1); nuclear@3: glEnd(); nuclear@2: nuclear@3: glDisable(GL_TEXTURE_2D); nuclear@3: nuclear@3: glutSwapBuffers(); nuclear@3: } nuclear@3: nuclear@3: void reshape(int x, int y) nuclear@3: { nuclear@3: glViewport(0, 0, x, y); nuclear@3: nuclear@3: /* reallocate the framebuffer */ nuclear@3: /*delete [] fb; nuclear@3: fb = new float[x * y * 4]; nuclear@3: set_framebuffer(fb, x, y);*/ nuclear@3: } nuclear@3: nuclear@3: void keyb(unsigned char key, int x, int y) nuclear@3: { nuclear@3: switch(key) { nuclear@3: case 27: nuclear@3: exit(0); nuclear@3: nuclear@3: case 's': nuclear@3: if(write_ppm("shot.ppm", fb, xsz, ysz)) { nuclear@3: printf("captured screenshot shot.ppm\n"); nuclear@3: } nuclear@3: break; nuclear@3: nuclear@3: case 'r': nuclear@3: need_update = true; nuclear@3: glutPostRedisplay(); nuclear@3: break; nuclear@3: nuclear@3: default: nuclear@3: break; nuclear@3: } nuclear@3: } nuclear@3: nuclear@3: void mouse(int bn, int state, int x, int y) nuclear@3: { nuclear@3: } nuclear@3: nuclear@3: void motion(int x, int y) nuclear@3: { nuclear@2: } nuclear@2: nuclear@2: bool write_ppm(const char *fname, float *fb, int xsz, int ysz) nuclear@2: { nuclear@2: FILE *fp; nuclear@2: nuclear@2: if(!(fp = fopen(fname, "wb"))) { nuclear@2: fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno)); nuclear@2: return false; nuclear@2: } nuclear@2: fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz); nuclear@2: nuclear@2: for(int i=0; i