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