ccdemos/realtime_bunny.cc
branchpyrit
changeset 37 5f954c0d34fc
parent 36 b490093b0ac3
child 38 5d043eeb09d9
equal deleted inserted replaced
36:b490093b0ac3 37:5f954c0d34fc
    10 int h = 200;
    10 int h = 200;
    11 Float *render_buffer;
    11 Float *render_buffer;
    12 
    12 
    13 Raytracer rt;
    13 Raytracer rt;
    14 Camera cam;
    14 Camera cam;
       
    15 
       
    16 Uint32 fp10s_acc = 0;
       
    17 Uint32 fp10s_acc_samples = 0;
    15 
    18 
    16 void load_ply(const char *filename, Material *mat, Float scale)
    19 void load_ply(const char *filename, Material *mat, Float scale)
    17 {
    20 {
    18 	vector<NormalVertex*> vertices;
    21 	vector<NormalVertex*> vertices;
    19 	vector<Vector3> normals;
    22 	vector<Vector3> normals;
    79 		normals.at(v3) += face->getNormal();
    82 		normals.at(v3) += face->getNormal();
    80 		vertex_face_num.at(v3)++;
    83 		vertex_face_num.at(v3)++;
    81 		f.ignore(1000,'\n');
    84 		f.ignore(1000,'\n');
    82 	}
    85 	}
    83 
    86 
    84 	for (int i; i < vertex_num; i++)
    87 	for (int i = 0; i < vertex_num; i++)
    85 	{
    88 	{
    86 		normals.at(i) /= vertex_face_num.at(i);
    89 		normals.at(i) /= vertex_face_num.at(i);
    87 		normals.at(i).normalize();
    90 		normals.at(i).normalize();
    88 		vertices.at(i)->N = normals.at(i);
    91 		vertices.at(i)->N = normals.at(i);
    89 	}
    92 	}
    91 	f.close();
    94 	f.close();
    92 }
    95 }
    93 
    96 
    94 void update(SDL_Surface *screen)
    97 void update(SDL_Surface *screen)
    95 {
    98 {
       
    99 	static Uint32 t = 0;
       
   100 	Uint32 tnow = SDL_GetTicks();
       
   101 	int fp10s = 10000/(int)(tnow - t);
       
   102 	if (t != 0)
       
   103 	{
       
   104 		fp10s_acc += fp10s;
       
   105 		++fp10s_acc_samples;
       
   106 	}
       
   107 	t = tnow;
       
   108 	printf("\b\b\b\b\b\b\b\b\b%3d.%1d fps", fp10s/10, fp10s%10);
       
   109 	fflush(stdout);
       
   110 
    96 	rt.render(w, h, render_buffer);
   111 	rt.render(w, h, render_buffer);
    97 
   112 
    98 	if (SDL_MUSTLOCK(screen))
   113 	if (SDL_MUSTLOCK(screen))
    99 		if (SDL_LockSurface(screen) < 0)
   114 		if (SDL_LockSurface(screen) < 0)
   100 			return;
   115 			return;
   121 		SDL_Flip(screen);
   136 		SDL_Flip(screen);
   122 	else
   137 	else
   123 		SDL_UpdateRect(screen, 0, 0, w, h);
   138 		SDL_UpdateRect(screen, 0, 0, w, h);
   124 }
   139 }
   125 
   140 
       
   141 void quit()
       
   142 {
       
   143 	Uint32 fp100s_aver = fp10s_acc*10/fp10s_acc_samples;
       
   144 	printf("\naverlage fps: %3d.%2d\n", fp100s_aver/100, fp100s_aver%100);
       
   145 	SDL_Quit();
       
   146 }
       
   147 
   126 int main()
   148 int main()
   127 {
   149 {
   128 	/* initialize SDL */
   150 	/* initialize SDL */
   129 	SDL_Surface *screen;
   151 	SDL_Surface *screen;
   130 
   152 
   131 	if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   153 	if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   132 		fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   154 		fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   133 		exit(1);
   155 		exit(1);
   134 	}
   156 	}
   135 
   157 
   136 	atexit(SDL_Quit);
   158 	atexit(quit);
   137 
   159 
   138 	screen = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
   160 	screen = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
   139 	if ( screen == NULL ) {
   161 	if ( screen == NULL ) {
   140 		fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
   162 		fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
   141 		exit(1);
   163 		exit(1);
   142 	}
   164 	}
   143 
   165 
   144 	/* initialize raytracer and prepare scene */
   166 	/* initialize raytracer and prepare scene */
       
   167 	pyrit_verbosity = 0;
   145 	render_buffer = (Float *) malloc(w*h*3*sizeof(Float));
   168 	render_buffer = (Float *) malloc(w*h*3*sizeof(Float));
   146 
   169 
   147 	rt.setThreads(2);
   170 	rt.setThreads(2);
   148 	rt.setMaxDepth(0);
   171 	rt.setMaxDepth(0);
   149 
   172