|
1 #include <SDL.h> |
|
2 |
|
3 #include "raytracer.h" |
|
4 #include <iostream> |
|
5 #include <fstream> |
|
6 #include <iomanip> |
|
7 |
|
8 int w = 320; |
|
9 int h = 200; |
|
10 Float *render_buffer; |
|
11 |
|
12 Raytracer rt; |
|
13 Camera cam; |
|
14 |
|
15 void load_ply(const char *filename, Material *mat, Float scale) |
|
16 { |
|
17 vector<NormalVertex*> vertices; |
|
18 vector<Vector3> normals; |
|
19 vector<int> vertex_face_num; |
|
20 ifstream f(filename); |
|
21 string token = "a"; |
|
22 if (!f.is_open()) |
|
23 { |
|
24 cout << "File not found: " << filename <<endl; |
|
25 exit(1); |
|
26 } |
|
27 // read header |
|
28 int vertex_num, face_num; |
|
29 while (token != "end_header") |
|
30 { |
|
31 f >> token; |
|
32 cout << token << endl; |
|
33 if (token == "element") |
|
34 { |
|
35 f >> token; |
|
36 if (token == "vertex") |
|
37 f >> vertex_num; |
|
38 if (token == "face") |
|
39 f >> face_num; |
|
40 } |
|
41 f.ignore(1000,'\n'); |
|
42 } |
|
43 |
|
44 // read vertices |
|
45 Vector3 P; |
|
46 int num = vertex_num; |
|
47 while (num--) |
|
48 { |
|
49 f >> P.x >> P.y >> P.z; |
|
50 P.x = -scale*P.x - 1.0; |
|
51 P.y = scale*P.y - 3.0; |
|
52 P.z = scale*P.z; |
|
53 vertices.push_back(new NormalVertex(P)); |
|
54 normals.push_back(Vector3()); |
|
55 vertex_face_num.push_back(0); |
|
56 f.ignore(1000,'\n'); |
|
57 } |
|
58 |
|
59 // read faces |
|
60 Triangle *face; |
|
61 int v1, v2, v3; |
|
62 while (face_num--) |
|
63 { |
|
64 f >> num; |
|
65 if (num != 3) |
|
66 { |
|
67 printf("ply error: faces of %d vertices not supported", num); |
|
68 continue; |
|
69 } |
|
70 f >> v1 >> v2 >> v3; |
|
71 face = new Triangle(vertices.at(v1), vertices.at(v3), vertices.at(v2), mat); |
|
72 rt.addshape(face); |
|
73 face->setSmooth(); |
|
74 |
|
75 normals.at(v1) += face->getNormal(); |
|
76 vertex_face_num.at(v1)++; |
|
77 normals.at(v2) += face->getNormal(); |
|
78 vertex_face_num.at(v2)++; |
|
79 normals.at(v3) += face->getNormal(); |
|
80 vertex_face_num.at(v3)++; |
|
81 f.ignore(1000,'\n'); |
|
82 } |
|
83 |
|
84 for (int i; i < vertex_num; i++) |
|
85 { |
|
86 normals.at(i) /= vertex_face_num.at(i); |
|
87 normals.at(i).normalize(); |
|
88 vertices.at(i)->N = normals.at(i); |
|
89 } |
|
90 |
|
91 f.close(); |
|
92 } |
|
93 |
|
94 void update(SDL_Surface *screen) |
|
95 { |
|
96 rt.render(w, h, render_buffer); |
|
97 |
|
98 if (SDL_MUSTLOCK(screen)) |
|
99 if (SDL_LockSurface(screen) < 0) |
|
100 return; |
|
101 |
|
102 Uint32 *bufp = (Uint32 *)screen->pixels; |
|
103 unsigned char c[3]; |
|
104 for (Float *fd = render_buffer; fd != render_buffer + w*h*3; fd += 3) |
|
105 { |
|
106 for (int i = 0; i < 3; i++) |
|
107 { |
|
108 if (fd[i] > 1.0) |
|
109 c[i] = 255; |
|
110 else |
|
111 c[i] = (unsigned char)(fd[i] * 255.0); |
|
112 } |
|
113 *bufp = SDL_MapRGB(screen->format, c[0], c[1], c[2]); |
|
114 bufp++; |
|
115 } |
|
116 |
|
117 if (SDL_MUSTLOCK(screen)) |
|
118 SDL_UnlockSurface(screen); |
|
119 |
|
120 if (screen->flags & SDL_DOUBLEBUF) |
|
121 SDL_Flip(screen); |
|
122 else |
|
123 SDL_UpdateRect(screen, 0, 0, w, h); |
|
124 } |
|
125 |
|
126 int main() |
|
127 { |
|
128 /* initialize SDL */ |
|
129 SDL_Surface *screen; |
|
130 |
|
131 if( SDL_Init(SDL_INIT_VIDEO) < 0 ) { |
|
132 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
|
133 exit(1); |
|
134 } |
|
135 |
|
136 atexit(SDL_Quit); |
|
137 |
|
138 screen = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); |
|
139 if ( screen == NULL ) { |
|
140 fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError()); |
|
141 exit(1); |
|
142 } |
|
143 |
|
144 /* initialize raytracer and prepare scene */ |
|
145 render_buffer = (Float *) malloc(w*h*3*sizeof(Float)); |
|
146 |
|
147 rt.setThreads(1); |
|
148 rt.setMaxDepth(3); |
|
149 |
|
150 KdTree top; |
|
151 rt.setTop(&top); |
|
152 |
|
153 Light light1(Vector3(-5.0, 2.0, 8.0), Colour(0.9, 0.3, 0.6)); |
|
154 light1.castShadows(false); |
|
155 rt.addlight(&light1); |
|
156 |
|
157 //Light light2(Vector3(-2.0, 10.0, 2.0), Colour(0.4, 0.6, 0.3)); |
|
158 //light2.castShadows(false); |
|
159 //rt.addlight(&light2); |
|
160 |
|
161 Material mat(Colour(0.9, 0.9, 0.9)); |
|
162 load_ply("../models/bunny/bun_zipper_res4.ply", &mat, 29); |
|
163 |
|
164 rt.setCamera(&cam); |
|
165 cam.setEye(Vector3(0,0,10)); |
|
166 |
|
167 /* build kd-tree */ |
|
168 top.setMaxDepth(100); |
|
169 top.optimize(); |
|
170 |
|
171 /* loop... */ |
|
172 SDL_Event event; |
|
173 bool quit = false; |
|
174 Float roty = 0.0, rotx = 0.0, move = 0.0; |
|
175 while (!quit) |
|
176 { |
|
177 while (SDL_PollEvent(&event)) |
|
178 { |
|
179 switch (event.type) { |
|
180 case SDL_VIDEORESIZE: |
|
181 w = event.resize.w; |
|
182 h = event.resize.h; |
|
183 render_buffer = (Float *) realloc(render_buffer, w*h*3*sizeof(Float)); |
|
184 screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); |
|
185 break; |
|
186 case SDL_KEYDOWN: |
|
187 if (event.key.keysym.sym == SDLK_ESCAPE) { |
|
188 quit = true; |
|
189 break; |
|
190 } |
|
191 if (event.key.keysym.sym == SDLK_LEFT) { |
|
192 roty = -0.01; |
|
193 break; |
|
194 } |
|
195 if (event.key.keysym.sym == SDLK_RIGHT) { |
|
196 roty = +0.01; |
|
197 break; |
|
198 } |
|
199 if (event.key.keysym.sym == SDLK_DOWN) { |
|
200 rotx = +0.01; |
|
201 break; |
|
202 } |
|
203 if (event.key.keysym.sym == SDLK_UP) { |
|
204 rotx = -0.01; |
|
205 break; |
|
206 } |
|
207 if (event.key.keysym.sym == SDLK_w) { |
|
208 move = +0.5; |
|
209 break; |
|
210 } |
|
211 if (event.key.keysym.sym == SDLK_s) { |
|
212 move = -0.5; |
|
213 break; |
|
214 } |
|
215 break; |
|
216 case SDL_KEYUP: |
|
217 if (event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_RIGHT) { |
|
218 roty = 0.0; |
|
219 break; |
|
220 } |
|
221 if (event.key.keysym.sym == SDLK_UP || event.key.keysym.sym == SDLK_DOWN) { |
|
222 rotx = 0.0; |
|
223 break; |
|
224 } |
|
225 if (event.key.keysym.sym == SDLK_w || event.key.keysym.sym == SDLK_s) { |
|
226 move = 0.0; |
|
227 break; |
|
228 } |
|
229 break; |
|
230 case SDL_QUIT: |
|
231 quit = true; |
|
232 } |
|
233 } |
|
234 cam.rotate(Quaternion(cos(roty),0,sin(roty),0).normalize()); |
|
235 cam.rotate(Quaternion(cos(rotx),cam.u[0]*sin(rotx),0,cam.u[2]*sin(rotx)).normalize()); |
|
236 cam.u.y = 0; |
|
237 cam.u.normalize(); |
|
238 cam.move(move,0,0); |
|
239 update(screen); |
|
240 } |
|
241 |
|
242 free(render_buffer); |
|
243 } |