ccdemos/realtime.cc
branchpyrit
changeset 20 f22952603f29
child 21 79b516a3803d
equal deleted inserted replaced
19:4e0955fca797 20:f22952603f29
       
     1 #include <SDL.h>
       
     2 
       
     3 #include "raytracer.h"
       
     4 
       
     5 int w = 512;
       
     6 int h = 200;
       
     7 float *render_buffer;
       
     8 
       
     9 Raytracer rt;
       
    10 Camera cam;
       
    11 
       
    12 void update(SDL_Surface *screen)
       
    13 {
       
    14 	rt.render(w, h, render_buffer);
       
    15 
       
    16 	if ( SDL_MUSTLOCK(screen) ) {
       
    17 		if ( SDL_LockSurface(screen) < 0 ) {
       
    18 			return;
       
    19 		}
       
    20 	}
       
    21 
       
    22 	Uint32 *bufp = (Uint32 *)screen->pixels;
       
    23 	unsigned char c[3];
       
    24 	for (float *fd = render_buffer; fd != render_buffer + w*h*3; fd += 3)
       
    25 	{
       
    26 		for (int i = 0; i < 3; i++)
       
    27 		{
       
    28 			if (fd[i] > 1.0)
       
    29 				c[i] = 255;
       
    30 			else
       
    31 				c[i] = (unsigned char)(fd[i] * 255.0);
       
    32 		}
       
    33 		*bufp = SDL_MapRGB(screen->format, c[0], c[1], c[2]);
       
    34 		bufp++;
       
    35 	}
       
    36 
       
    37 	if ( SDL_MUSTLOCK(screen) ) {
       
    38 		SDL_UnlockSurface(screen);
       
    39 	}
       
    40 
       
    41 	SDL_UpdateRect(screen, 0, 0, w, h);
       
    42 	SDL_Flip(screen);
       
    43 }
       
    44 
       
    45 int main()
       
    46 {
       
    47 	/* initialize SDL */
       
    48 	SDL_Surface *screen;
       
    49 
       
    50 	if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
       
    51 		fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
       
    52 		exit(1);
       
    53 	}
       
    54 
       
    55 	atexit(SDL_Quit);
       
    56 
       
    57 	screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
       
    58 	if ( screen == NULL ) {
       
    59 		fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
       
    60 		exit(1);
       
    61 	}
       
    62 
       
    63 	/* initialize raytracer and prepare scene */
       
    64 	render_buffer = (float *) malloc(w*h*3*sizeof(float));
       
    65 
       
    66 	rt.setThreads(2);
       
    67 
       
    68 	KdTree top;
       
    69 	rt.setTop(&top);
       
    70 
       
    71 	Light light1(Vector3(2.0, -5.0, -5.0), Colour(0.7, 0.3, 0.6));
       
    72 	rt.addlight(&light1);
       
    73 
       
    74 	Light light2(Vector3(-2.0, 10.0, 2.0), Colour(0.4, 0.6, 0.3));
       
    75 	rt.addlight(&light2);
       
    76 
       
    77 	Material mat_sph(Colour(1.0, 1.0, 1.0));
       
    78 	for (int y=0; y<20; y++)
       
    79 		for (int x=0; x<20; x++)
       
    80 			rt.addshape(new Sphere(Vector3(x-10, (float)random()/RAND_MAX*5.0, y-10), 0.45, &mat_sph));
       
    81 
       
    82 	rt.setCamera(&cam);
       
    83 	cam.setEye(Vector3(0,0,10));
       
    84 
       
    85 	/* build kd-tree */
       
    86 	top.optimize();
       
    87 
       
    88 	/* loop... */
       
    89 	SDL_Event event;
       
    90 	bool quit = false;
       
    91 	float roty = 0.0, rotx = 0.0, move = 0.0;
       
    92 	while (!quit)
       
    93 	{
       
    94 		while (SDL_PollEvent(&event))
       
    95 		{
       
    96 			switch (event.type) {
       
    97 				case SDL_VIDEORESIZE:
       
    98 					w = event.resize.w;
       
    99 					h = event.resize.h;
       
   100 					render_buffer = (float *) realloc(render_buffer, w*h*3*sizeof(float));
       
   101 					screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
       
   102 					break;
       
   103 				case SDL_KEYDOWN:
       
   104 					if (event.key.keysym.sym == SDLK_ESCAPE) {
       
   105 						quit = true;
       
   106 						break;
       
   107 					}
       
   108 					if (event.key.keysym.sym == SDLK_LEFT) {
       
   109 						roty = -0.01;
       
   110 						break;
       
   111 					}
       
   112 					if (event.key.keysym.sym == SDLK_RIGHT) {
       
   113 						roty = +0.01;
       
   114 						break;
       
   115 					}
       
   116 					if (event.key.keysym.sym == SDLK_DOWN) {
       
   117 						rotx = +0.01;
       
   118 						break;
       
   119 					}
       
   120 					if (event.key.keysym.sym == SDLK_UP) {
       
   121 						rotx = -0.01;
       
   122 						break;
       
   123 					}
       
   124 					if (event.key.keysym.sym == SDLK_w) {
       
   125 						move = +0.5;
       
   126 						break;
       
   127 					}
       
   128 					if (event.key.keysym.sym == SDLK_s) {
       
   129 						move = -0.5;
       
   130 						break;
       
   131 					}
       
   132 					break;
       
   133 				case SDL_KEYUP:
       
   134 					if (event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_RIGHT) {
       
   135 						roty = 0.0;
       
   136 						break;
       
   137 					}
       
   138 					if (event.key.keysym.sym == SDLK_UP || event.key.keysym.sym == SDLK_DOWN) {
       
   139 						rotx = 0.0;
       
   140 						break;
       
   141 					}
       
   142 					if (event.key.keysym.sym == SDLK_w || event.key.keysym.sym == SDLK_s) {
       
   143 							move = 0.0;
       
   144 							break;
       
   145 						}
       
   146 					break;
       
   147 				case SDL_QUIT:
       
   148 					quit = true;
       
   149 			}
       
   150 		}
       
   151 		cam.rotate(Quaternion(cos(roty),0,sin(roty),0).normalize());
       
   152 		cam.rotate(Quaternion(cos(rotx),cam.u[0]*sin(rotx),0,cam.u[2]*sin(rotx)).normalize());
       
   153 		cam.u.y = 0;
       
   154 		cam.u.normalize();
       
   155 		cam.move(move,0,0);
       
   156 		update(screen);
       
   157 	}
       
   158 
       
   159 	free(render_buffer);
       
   160 }