ccdemos/realtime.cc
branchpyrit
changeset 21 79b516a3803d
parent 20 f22952603f29
child 22 76b7bd51d64a
equal deleted inserted replaced
20:f22952603f29 21:79b516a3803d
     1 #include <SDL.h>
     1 #include <SDL.h>
     2 
     2 
     3 #include "raytracer.h"
     3 #include "raytracer.h"
     4 
     4 
     5 int w = 512;
     5 int w = 480;
     6 int h = 200;
     6 int h = 288;
     7 float *render_buffer;
     7 float *render_buffer;
     8 
     8 
     9 Raytracer rt;
     9 Raytracer rt;
    10 Camera cam;
    10 Camera cam;
    11 
    11 
    12 void update(SDL_Surface *screen)
    12 void update(SDL_Surface *screen)
    13 {
    13 {
    14 	rt.render(w, h, render_buffer);
    14 	rt.render(w, h, render_buffer);
    15 
    15 
    16 	if ( SDL_MUSTLOCK(screen) ) {
    16 	if (SDL_MUSTLOCK(screen))
    17 		if ( SDL_LockSurface(screen) < 0 ) {
    17 		if (SDL_LockSurface(screen) < 0)
    18 			return;
    18 			return;
    19 		}
       
    20 	}
       
    21 
    19 
    22 	Uint32 *bufp = (Uint32 *)screen->pixels;
    20 	Uint32 *bufp = (Uint32 *)screen->pixels;
    23 	unsigned char c[3];
    21 	unsigned char c[3];
    24 	for (float *fd = render_buffer; fd != render_buffer + w*h*3; fd += 3)
    22 	for (float *fd = render_buffer; fd != render_buffer + w*h*3; fd += 3)
    25 	{
    23 	{
    32 		}
    30 		}
    33 		*bufp = SDL_MapRGB(screen->format, c[0], c[1], c[2]);
    31 		*bufp = SDL_MapRGB(screen->format, c[0], c[1], c[2]);
    34 		bufp++;
    32 		bufp++;
    35 	}
    33 	}
    36 
    34 
    37 	if ( SDL_MUSTLOCK(screen) ) {
    35 	if (SDL_MUSTLOCK(screen))
    38 		SDL_UnlockSurface(screen);
    36 		SDL_UnlockSurface(screen);
    39 	}
       
    40 
    37 
    41 	SDL_UpdateRect(screen, 0, 0, w, h);
    38 	if (screen->flags & SDL_DOUBLEBUF)
    42 	SDL_Flip(screen);
    39 		SDL_Flip(screen);
       
    40 	else
       
    41 		SDL_UpdateRect(screen, 0, 0, w, h);
    43 }
    42 }
    44 
    43 
    45 int main()
    44 int main()
    46 {
    45 {
    47 	/* initialize SDL */
    46 	/* initialize SDL */
    62 
    61 
    63 	/* initialize raytracer and prepare scene */
    62 	/* initialize raytracer and prepare scene */
    64 	render_buffer = (float *) malloc(w*h*3*sizeof(float));
    63 	render_buffer = (float *) malloc(w*h*3*sizeof(float));
    65 
    64 
    66 	rt.setThreads(2);
    65 	rt.setThreads(2);
       
    66 	rt.setMaxDepth(1);
    67 
    67 
    68 	KdTree top;
    68 	KdTree top;
    69 	rt.setTop(&top);
    69 	rt.setTop(&top);
    70 
    70 
    71 	Light light1(Vector3(2.0, -5.0, -5.0), Colour(0.7, 0.3, 0.6));
    71 	Light light1(Vector3(2.0, -5.0, -5.0), Colour(0.7, 0.3, 0.6));
       
    72 	light1.castShadows(false);
    72 	rt.addlight(&light1);
    73 	rt.addlight(&light1);
    73 
    74 
    74 	Light light2(Vector3(-2.0, 10.0, 2.0), Colour(0.4, 0.6, 0.3));
    75 	Light light2(Vector3(-2.0, 10.0, 2.0), Colour(0.4, 0.6, 0.3));
       
    76 	light2.castShadows(false);
    75 	rt.addlight(&light2);
    77 	rt.addlight(&light2);
    76 
    78 
    77 	Material mat_sph(Colour(1.0, 1.0, 1.0));
    79 	Material mat_sph(Colour(1.0, 1.0, 1.0));
    78 	for (int y=0; y<20; y++)
    80 	for (int y=0; y<10; y++)
    79 		for (int x=0; x<20; x++)
    81 		for (int x=0; x<10; x++)
    80 			rt.addshape(new Sphere(Vector3(x-10, (float)random()/RAND_MAX*5.0, y-10), 0.45, &mat_sph));
    82 			rt.addshape(new Sphere(Vector3(x*2-10, (float)random()/RAND_MAX*5.0, y*2-10), 0.45, &mat_sph));
    81 
    83 
    82 	rt.setCamera(&cam);
    84 	rt.setCamera(&cam);
    83 	cam.setEye(Vector3(0,0,10));
    85 	cam.setEye(Vector3(0,0,10));
    84 
    86 
    85 	/* build kd-tree */
    87 	/* build kd-tree */
       
    88 	top.setMaxDepth(100);
    86 	top.optimize();
    89 	top.optimize();
    87 
    90 
    88 	/* loop... */
    91 	/* loop... */
    89 	SDL_Event event;
    92 	SDL_Event event;
    90 	bool quit = false;
    93 	bool quit = false;