ccdemos/realtime.cc
branchpyrit
changeset 22 76b7bd51d64a
parent 21 79b516a3803d
child 23 7e258561a690
equal deleted inserted replaced
21:79b516a3803d 22:76b7bd51d64a
     2 
     2 
     3 #include "raytracer.h"
     3 #include "raytracer.h"
     4 
     4 
     5 int w = 480;
     5 int w = 480;
     6 int h = 288;
     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)
    17 		if (SDL_LockSurface(screen) < 0)
    17 		if (SDL_LockSurface(screen) < 0)
    18 			return;
    18 			return;
    19 
    19 
    20 	Uint32 *bufp = (Uint32 *)screen->pixels;
    20 	Uint32 *bufp = (Uint32 *)screen->pixels;
    21 	unsigned char c[3];
    21 	unsigned char c[3];
    22 	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)
    23 	{
    23 	{
    24 		for (int i = 0; i < 3; i++)
    24 		for (int i = 0; i < 3; i++)
    25 		{
    25 		{
    26 			if (fd[i] > 1.0)
    26 			if (fd[i] > 1.0)
    27 				c[i] = 255;
    27 				c[i] = 255;
    58 		fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
    58 		fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
    59 		exit(1);
    59 		exit(1);
    60 	}
    60 	}
    61 
    61 
    62 	/* initialize raytracer and prepare scene */
    62 	/* initialize raytracer and prepare scene */
    63 	render_buffer = (float *) malloc(w*h*3*sizeof(float));
    63 	render_buffer = (Float *) malloc(w*h*3*sizeof(Float));
    64 
    64 
    65 	rt.setThreads(2);
    65 	rt.setThreads(2);
    66 	rt.setMaxDepth(1);
    66 	rt.setMaxDepth(3);
    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));
    77 	rt.addlight(&light2);
    77 	rt.addlight(&light2);
    78 
    78 
    79 	Material mat_sph(Colour(1.0, 1.0, 1.0));
    79 	Material mat_sph(Colour(1.0, 1.0, 1.0));
    80 	for (int y=0; y<10; y++)
    80 	for (int y=0; y<10; y++)
    81 		for (int x=0; x<10; x++)
    81 		for (int x=0; x<10; x++)
    82 			rt.addshape(new Sphere(Vector3(x*2-10, (float)random()/RAND_MAX*5.0, y*2-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));
    83 
    83 
    84 	rt.setCamera(&cam);
    84 	rt.setCamera(&cam);
    85 	cam.setEye(Vector3(0,0,10));
    85 	cam.setEye(Vector3(0,0,10));
    86 
    86 
    87 	/* build kd-tree */
    87 	/* build kd-tree */
    89 	top.optimize();
    89 	top.optimize();
    90 
    90 
    91 	/* loop... */
    91 	/* loop... */
    92 	SDL_Event event;
    92 	SDL_Event event;
    93 	bool quit = false;
    93 	bool quit = false;
    94 	float roty = 0.0, rotx = 0.0, move = 0.0;
    94 	Float roty = 0.0, rotx = 0.0, move = 0.0;
    95 	while (!quit)
    95 	while (!quit)
    96 	{
    96 	{
    97 		while (SDL_PollEvent(&event))
    97 		while (SDL_PollEvent(&event))
    98 		{
    98 		{
    99 			switch (event.type) {
    99 			switch (event.type) {
   100 				case SDL_VIDEORESIZE:
   100 				case SDL_VIDEORESIZE:
   101 					w = event.resize.w;
   101 					w = event.resize.w;
   102 					h = event.resize.h;
   102 					h = event.resize.h;
   103 					render_buffer = (float *) realloc(render_buffer, w*h*3*sizeof(float));
   103 					render_buffer = (Float *) realloc(render_buffer, w*h*3*sizeof(Float));
   104 					screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
   104 					screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
   105 					break;
   105 					break;
   106 				case SDL_KEYDOWN:
   106 				case SDL_KEYDOWN:
   107 					if (event.key.keysym.sym == SDLK_ESCAPE) {
   107 					if (event.key.keysym.sym == SDLK_ESCAPE) {
   108 						quit = true;
   108 						quit = true;