1 #include <SDL.h> |
1 #include "raytracer.h" |
|
2 #include "octree.h" |
2 |
3 |
3 #include "raytracer.h" |
4 #include "common_sdl.h" |
4 #include "kdtree.h" |
|
5 |
|
6 int w = 480; |
|
7 int h = 288; |
|
8 Float *render_buffer; |
|
9 |
|
10 Raytracer rt; |
|
11 Camera cam; |
|
12 |
|
13 void update(SDL_Surface *screen) |
|
14 { |
|
15 rt.render(w, h, render_buffer); |
|
16 |
|
17 if (SDL_MUSTLOCK(screen)) |
|
18 if (SDL_LockSurface(screen) < 0) |
|
19 return; |
|
20 |
|
21 Uint32 *bufp = (Uint32 *)screen->pixels; |
|
22 unsigned char c[3]; |
|
23 for (Float *fd = render_buffer; fd != render_buffer + w*h*3; fd += 3) |
|
24 { |
|
25 for (int i = 0; i < 3; i++) |
|
26 { |
|
27 if (fd[i] > 1.0) |
|
28 c[i] = 255; |
|
29 else |
|
30 c[i] = (unsigned char)(fd[i] * 255.0); |
|
31 } |
|
32 *bufp = SDL_MapRGB(screen->format, c[0], c[1], c[2]); |
|
33 bufp++; |
|
34 } |
|
35 |
|
36 if (SDL_MUSTLOCK(screen)) |
|
37 SDL_UnlockSurface(screen); |
|
38 |
|
39 if (screen->flags & SDL_DOUBLEBUF) |
|
40 SDL_Flip(screen); |
|
41 else |
|
42 SDL_UpdateRect(screen, 0, 0, w, h); |
|
43 } |
|
44 |
5 |
45 int main() |
6 int main() |
46 { |
7 { |
47 /* initialize SDL */ |
8 Raytracer rt; |
48 SDL_Surface *screen; |
9 Octree top; |
|
10 Camera cam; |
49 |
11 |
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_SWSURFACE|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 rt.setMaxDepth(3); |
12 rt.setMaxDepth(3); |
68 |
|
69 KdTree top; |
|
70 rt.setTop(&top); |
13 rt.setTop(&top); |
71 |
14 |
72 Light light1(Vector3(2.0, -5.0, -5.0), Colour(0.7, 0.3, 0.6)); |
15 Light light1(Vector3(2.0, -5.0, -5.0), Colour(0.7, 0.3, 0.6)); |
73 light1.castShadows(false); |
16 light1.castShadows(false); |
74 rt.addlight(&light1); |
17 rt.addlight(&light1); |
83 rt.addshape(new Sphere(Vector3(x*2-10, (Float)random()/RAND_MAX*5.0, y*2-10), 0.45, &mat_sph)); |
26 rt.addshape(new Sphere(Vector3(x*2-10, (Float)random()/RAND_MAX*5.0, y*2-10), 0.45, &mat_sph)); |
84 |
27 |
85 rt.setCamera(&cam); |
28 rt.setCamera(&cam); |
86 cam.setEye(Vector3(0,0,10)); |
29 cam.setEye(Vector3(0,0,10)); |
87 |
30 |
88 /* build kd-tree */ |
|
89 top.setMaxDepth(100); |
|
90 top.optimize(); |
31 top.optimize(); |
91 |
32 |
92 /* loop... */ |
33 loop_sdl(rt, cam); |
93 SDL_Event event; |
|
94 bool quit = false; |
|
95 Float roty = 0.0, rotx = 0.0, move = 0.0; |
|
96 while (!quit) |
|
97 { |
|
98 while (SDL_PollEvent(&event)) |
|
99 { |
|
100 switch (event.type) { |
|
101 case SDL_VIDEORESIZE: |
|
102 w = event.resize.w; |
|
103 h = event.resize.h; |
|
104 render_buffer = (Float *) realloc(render_buffer, w*h*3*sizeof(Float)); |
|
105 screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); |
|
106 break; |
|
107 case SDL_KEYDOWN: |
|
108 if (event.key.keysym.sym == SDLK_ESCAPE) { |
|
109 quit = true; |
|
110 break; |
|
111 } |
|
112 if (event.key.keysym.sym == SDLK_LEFT) { |
|
113 roty = -0.01; |
|
114 break; |
|
115 } |
|
116 if (event.key.keysym.sym == SDLK_RIGHT) { |
|
117 roty = +0.01; |
|
118 break; |
|
119 } |
|
120 if (event.key.keysym.sym == SDLK_DOWN) { |
|
121 rotx = +0.01; |
|
122 break; |
|
123 } |
|
124 if (event.key.keysym.sym == SDLK_UP) { |
|
125 rotx = -0.01; |
|
126 break; |
|
127 } |
|
128 if (event.key.keysym.sym == SDLK_w) { |
|
129 move = +0.5; |
|
130 break; |
|
131 } |
|
132 if (event.key.keysym.sym == SDLK_s) { |
|
133 move = -0.5; |
|
134 break; |
|
135 } |
|
136 break; |
|
137 case SDL_KEYUP: |
|
138 if (event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_RIGHT) { |
|
139 roty = 0.0; |
|
140 break; |
|
141 } |
|
142 if (event.key.keysym.sym == SDLK_UP || event.key.keysym.sym == SDLK_DOWN) { |
|
143 rotx = 0.0; |
|
144 break; |
|
145 } |
|
146 if (event.key.keysym.sym == SDLK_w || event.key.keysym.sym == SDLK_s) { |
|
147 move = 0.0; |
|
148 break; |
|
149 } |
|
150 break; |
|
151 case SDL_QUIT: |
|
152 quit = true; |
|
153 } |
|
154 } |
|
155 cam.rotate(Quaternion(cos(roty),0,sin(roty),0).normalize()); |
|
156 cam.rotate(Quaternion(cos(rotx),cam.u[0]*sin(rotx),0,cam.u[2]*sin(rotx)).normalize()); |
|
157 cam.u.y = 0; |
|
158 cam.u.normalize(); |
|
159 cam.move(move,0,0); |
|
160 update(screen); |
|
161 } |
|
162 |
|
163 free(render_buffer); |
|
164 } |
34 } |