ccdemos/realtime_dragon.cc
branchpyrit
changeset 38 5d043eeb09d9
parent 36 b490093b0ac3
child 39 7079dcc3bd74
equal deleted inserted replaced
37:5f954c0d34fc 38:5d043eeb09d9
     1 #include <SDL.h>
     1 #include <SDL.h>
     2 
     2 
     3 #include "raytracer.h"
     3 #include "raytracer.h"
     4 #include "kdtree.h"
     4 #include "octree.h"
     5 #include <iostream>
     5 #include <iostream>
     6 #include <fstream>
     6 #include <fstream>
     7 
     7 
     8 int w = 320;
     8 int w = 320;
     9 int h = 200;
     9 int h = 200;
    39 		}
    39 		}
    40 	}
    40 	}
    41 
    41 
    42 	// read vertices
    42 	// read vertices
    43 	Vector3 P;
    43 	Vector3 P;
    44 	int num = vertex_num;
    44 	for (int i = 0; i < vertex_num; i++)
    45 	while (num--)
       
    46 	{
    45 	{
    47 		f >> P.x >> P.y >> P.z;
    46 		f >> P.x >> P.y >> P.z;
    48 		P.x = -scale*P.x;
    47 		P.x = -scale*P.x;
    49 		P.y = scale*P.y - 3.6;
    48 		P.y = scale*P.y - 3.6;
    50 		P.z = -scale*P.z;
    49 		P.z = -scale*P.z;
    54 	}
    53 	}
    55 
    54 
    56 	// read faces
    55 	// read faces
    57 	Triangle *face;
    56 	Triangle *face;
    58 	int v1, v2, v3;
    57 	int v1, v2, v3;
    59 	while (face_num--)
    58 	for (int i = 0; i < face_num; i++)
    60 	{
    59 	{
    61 		f >> num;
    60 		f >> num;
    62 		if (num != 3)
    61 		if (num != 3)
    63 		{
    62 		{
    64 			printf("ply error: faces of %d vertices not supported", num);
    63 			printf("ply error: faces of %d vertices not supported", num);
    75 		vertex_face_num.at(v2)++;
    74 		vertex_face_num.at(v2)++;
    76 		normals.at(v3) += face->getNormal();
    75 		normals.at(v3) += face->getNormal();
    77 		vertex_face_num.at(v3)++;
    76 		vertex_face_num.at(v3)++;
    78 	}
    77 	}
    79 
    78 
    80 	for (int i; i < vertex_num; i++)
    79 	for (int i = 0; i < vertex_num; i++)
    81 	{
    80 	{
    82 		normals.at(i) /= vertex_face_num.at(i);
    81 		normals.at(i) /= vertex_face_num.at(i);
    83 		normals.at(i).normalize();
    82 		normals.at(i).normalize();
    84 		vertices.at(i)->N = normals.at(i);
    83 		vertices.at(i)->N = normals.at(i);
    85 	}
    84 	}
   138 	}
   137 	}
   139 
   138 
   140 	/* initialize raytracer and prepare scene */
   139 	/* initialize raytracer and prepare scene */
   141 	render_buffer = (Float *) malloc(w*h*3*sizeof(Float));
   140 	render_buffer = (Float *) malloc(w*h*3*sizeof(Float));
   142 
   141 
   143 	rt.setThreads(1);
   142 	rt.setThreads(2);
   144 	rt.setMaxDepth(3);
   143 	rt.setMaxDepth(0);
   145 
   144 
   146 	KdTree top;
   145 	Octree top;
   147 	rt.setTop(&top);
   146 	rt.setTop(&top);
   148 
   147 
   149 	Light light1(Vector3(-5.0, 2.0, 8.0), Colour(0.9, 0.3, 0.6));
   148 	Light light1(Vector3(-5.0, 2.0, 8.0), Colour(0.9, 0.3, 0.6));
   150 	light1.castShadows(false);
   149 	light1.castShadows(false);
   151 	rt.addlight(&light1);
   150 	rt.addlight(&light1);
   153 	//Light light2(Vector3(-2.0, 10.0, 2.0), Colour(0.4, 0.6, 0.3));
   152 	//Light light2(Vector3(-2.0, 10.0, 2.0), Colour(0.4, 0.6, 0.3));
   154 	//light2.castShadows(false);
   153 	//light2.castShadows(false);
   155 	//rt.addlight(&light2);
   154 	//rt.addlight(&light2);
   156 
   155 
   157 	Material mat(Colour(0.9, 0.9, 0.9));
   156 	Material mat(Colour(0.9, 0.9, 0.9));
   158 	load_ply("../models/dragon/dragon_vrip_res4.ply", &mat, 29);
   157 	load_ply("../models/dragon/dragon_vrip.ply", &mat, 29);
   159 
   158 
   160 	rt.setCamera(&cam);
   159 	rt.setCamera(&cam);
   161 	cam.setEye(Vector3(0,0,10));
   160 	cam.setEye(Vector3(0,0,10));
   162 
   161 
   163 	/* build kd-tree */
       
   164 	top.setMaxDepth(30);
       
   165 	top.optimize();
   162 	top.optimize();
   166 
   163 
   167 	/* loop... */
   164 	/* loop... */
   168 	SDL_Event event;
   165 	SDL_Event event;
   169 	bool quit = false;
   166 	bool quit = false;