Subversion Repositories gelsvn

Rev

Rev 417 | Rev 596 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 417 Rev 418
Line 103... Line 103...
103
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
103
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
104
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
104
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
105
 
105
 
106
  // load the texture image
106
  // load the texture image
107
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 
107
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 
108
	       TEX_SIZE, TEX_SIZE,
108
	             TEX_SIZE, TEX_SIZE,
109
	       0, GL_RGB, GL_FLOAT, image[0][0].get());
109
	             0, GL_RGB, GL_FLOAT, image[0][0].get());
110
}
110
}
111
 
111
 
112
void initRaytracer()
112
void initRaytracer()
113
{
113
{
114
  Vec3f c;
114
  Vec3f c;
Line 118... Line 118...
118
 
118
 
119
  // Initialize track ball
119
  // Initialize track ball
120
  vctrl = new GLViewController(winx, winy, c, r);
120
  vctrl = new GLViewController(winx, winy, c, r);
121
 
121
 
122
  // Initialize corresponding camera
122
  // Initialize corresponding camera
123
  cam = new Camera(c - Vec3f(r), 
123
  cam = new Camera(c - Vec3f(r), c, Vec3f(0.0f, 1.0f, 0.0f), 1.0f);
124
		   c, 
-
 
125
		   Vec3f(0.0f, 1.0f, 0.0f),
-
 
126
		   1.0f);
-
 
127
 
124
 
128
#ifdef USE_BSP
125
#ifdef USE_BSP
129
   cout << "Constructing BSP tree..." << endl;
126
   cout << "Constructing BSP tree..." << endl;
130
   tree.init(mesh_vector, transforms, MAX_OBJECTS, MAX_LEVEL);
127
   tree.init(mesh_vector, transforms, MAX_OBJECTS, MAX_LEVEL);
131
   tree.build();
128
   tree.build();
Line 133... Line 130...
133
	// AABB TREE
130
	// AABB TREE
134
	Manifold m;
131
	Manifold m;
135
	vector<int> faces(mesh.geometry.no_faces(), 3);
132
	vector<int> faces(mesh.geometry.no_faces(), 3);
136
	cout << "Creating manifold" << endl;
133
	cout << "Creating manifold" << endl;
137
	build_manifold(m, 
134
	build_manifold(m, 
138
				   mesh.geometry.no_vertices(), 
135
				         mesh.geometry.no_vertices(), 
139
				   &mesh.geometry.vertex(0), 
136
				         &mesh.geometry.vertex(0), 
140
				   faces.size(), &faces[0], 
137
				         faces.size(), &faces[0], 
141
				   reinterpret_cast<const int*>(&mesh.geometry.face(0)));
138
				         reinterpret_cast<const int*>(&mesh.geometry.face(0)));
142
	cout << "Building tree" << endl;
139
	cout << "Building tree" << endl;
143
	build_AABBTree(m, bb_tree);
140
	build_AABBTree(m, bb_tree);
144
#endif
141
#endif
145
}
142
}
146
 
143
 
Line 170... Line 167...
170
}
167
}
171
 
168
 
172
double lambertian_shade(Ray& r)
169
double lambertian_shade(Ray& r)
173
{
170
{
174
#ifdef USE_BSP
171
#ifdef USE_BSP
175
		r.compute_normal();
172
	r.compute_normal();
176
#endif
173
#endif
177
  return light_pow*dot(r.hit_normal, light_dir);
174
  return light_pow*dot(r.hit_normal, light_dir);
178
}
175
}
179
 
176
 
180
double (*shade_ray[2])(Ray&) = { lambertian_shade,
177
double (*shade_ray[2])(Ray&) = { lambertian_shade,
181
				 shadow_shade      };
178
				                         shadow_shade      };
182
 
179
 
183
//////////////////////////////////////////////////////////////
180
//////////////////////////////////////////////////////////////
184
//      D R A W   F U N C T I O N S
181
//      D R A W   F U N C T I O N S
185
//////////////////////////////////////////////////////////////
182
//////////////////////////////////////////////////////////////
186
 
183
 
Line 254... Line 251...
254
  {
251
  {
255
    disp_list = glGenLists(1);
252
    disp_list = glGenLists(1);
256
    glNewList(disp_list, GL_COMPILE);
253
    glNewList(disp_list, GL_COMPILE);
257
 
254
 
258
    glBegin(GL_TRIANGLES);
255
    glBegin(GL_TRIANGLES);
259
      for(int i = 0; i < mesh.geometry.no_faces(); ++i)
256
    for(int i = 0; i < mesh.geometry.no_faces(); ++i)
-
 
257
    {
-
 
258
      Vec3i n_face = mesh.normals.face(i);
-
 
259
      Vec3i g_face = mesh.geometry.face(i);
-
 
260
      for(int j=0;j<3;j++)
260
      {
261
      {
261
	Vec3i n_face = mesh.normals.face(i);
-
 
262
	Vec3i g_face = mesh.geometry.face(i);
-
 
263
	for(int j=0;j<3;j++)
-
 
264
	{
-
 
265
	  double shade = 0.5;
262
        double shade = 0.5;
266
 
263
 
267
	  if(n_face != Geometry::NULL_FACE)
264
        if(n_face != Geometry::NULL_FACE)
268
	  {
265
        {
269
	    Vec3f norm = normalize(mesh.normals.vertex(n_face[j]));
266
          Vec3f norm = normalize(mesh.normals.vertex(n_face[j]));
270
	    glNormal3fv(norm.get());
267
          glNormal3fv(norm.get());
271
	    shade = light_pow*dot(norm, light_dir);
268
          shade = light_pow*dot(norm, light_dir);
272
	  }
269
        }
273
 
270
 
274
	  glColor3d(shade, shade, shade);
271
        glColor3d(shade, shade, shade);
275
	  Vec3f vert = mesh.geometry.vertex(g_face[j]);	  
272
        Vec3f vert = mesh.geometry.vertex(g_face[j]);	  
276
	  glVertex3fv(vert.get());
273
        glVertex3fv(vert.get());
277
	}
-
 
278
      }
274
      }
-
 
275
    }
279
    glEnd();
276
    glEnd();
280
 
277
 
281
    glEndList();
278
    glEndList();
282
    washere = true;
279
    washere = true;
283
  }
280
  }
Line 314... Line 311...
314
    
311
    
315
    vector<Vec2f> jitter(PIXEL_SUBDIVS*PIXEL_SUBDIVS); 
312
    vector<Vec2f> jitter(PIXEL_SUBDIVS*PIXEL_SUBDIVS); 
316
    for(unsigned int i = 0; i < PIXEL_SUBDIVS; ++i)
313
    for(unsigned int i = 0; i < PIXEL_SUBDIVS; ++i)
317
      for(unsigned int j = 0; j < PIXEL_SUBDIVS; ++j)
314
      for(unsigned int j = 0; j < PIXEL_SUBDIVS; ++j)
318
      {
315
      {
319
	    jitter[i*PIXEL_SUBDIVS + j][0] = (my_random() + (j%PIXEL_SUBDIVS))*step; 
316
	      jitter[i*PIXEL_SUBDIVS + j][0] = (my_random() + (j%PIXEL_SUBDIVS))*step; 
320
	    jitter[i*PIXEL_SUBDIVS + j][1] = (my_random() + (i%PIXEL_SUBDIVS))*step; 
317
	      jitter[i*PIXEL_SUBDIVS + j][1] = (my_random() + (i%PIXEL_SUBDIVS))*step; 
321
      }
318
      }
322
 
319
 
323
	Util::Timer tim;
320
    Util::Timer tim;
324
	tim.start();
321
    tim.start();
325
    for(unsigned int i = 0; i < TEX_SIZE; ++i)
322
    for(unsigned int i = 0; i < TEX_SIZE; ++i)
326
    {
323
    {
327
	  for(unsigned int j = 0; j < TEX_SIZE; ++j)
324
	    for(unsigned int j = 0; j < TEX_SIZE; ++j)
328
	  {
325
	    {
329
		Vec3d sum(0.0f);
326
		    Vec3d sum(0.0f);
330
		Vec2f vp_pos(j*win_to_vp - lowerleft, i*win_to_vp - lowerleft);
327
		    Vec2f vp_pos(j*win_to_vp - lowerleft, i*win_to_vp - lowerleft);
331
	
328
    	
332
		for(unsigned int ky = 0; ky < PIXEL_SUBDIVS; ++ky)
329
		    for(unsigned int ky = 0; ky < PIXEL_SUBDIVS; ++ky)
333
		  for(unsigned int kx = 0; kx < PIXEL_SUBDIVS; ++kx)
330
		      for(unsigned int kx = 0; kx < PIXEL_SUBDIVS; ++kx)
334
		  {
331
		      {
335
			Ray r = cam->get_ray(vp_pos + jitter[ky*PIXEL_SUBDIVS + kx]);
332
			      Ray r = cam->get_ray(vp_pos + jitter[ky*PIXEL_SUBDIVS + kx]);
336
 
333
 
337
#ifdef USE_BSP
334
#ifdef USE_BSP
338
			if(tree.intersect(r))
335
			      if(tree.intersect(r))
339
			  sum += Vec3d(shade_ray[shadow](r));
336
			        sum += Vec3d(shade_ray[shadow](r));
340
			else
337
			      else
341
			  sum += background;
338
			        sum += background;
342
#else
339
#else
343
			float t = FLT_MAX;
340
			      float t = FLT_MAX;
344
			bb_tree.intersect(r);
341
			      bb_tree.intersect(r);
345
			if(r.has_hit)
342
			      if(r.has_hit)
346
			  sum += Vec3d(shade_ray[0](r));
343
			        sum += Vec3d(shade_ray[0](r));
347
			else 
344
			      else 
348
			  sum += background;
345
			        sum += background;
349
#endif		
346
#endif		
350
		  }
347
		      }
351
 
-
 
352
		  image[i][j] = Vec3f(sum/static_cast<double>(PIXEL_SUBDIVS*PIXEL_SUBDIVS));
348
	      image[i][j] = Vec3f(sum/static_cast<double>(PIXEL_SUBDIVS*PIXEL_SUBDIVS));
353
	  }
349
      }
354
	  if(((i + 1) % 50) == 0) cerr << ".";
350
      if(((i + 1) % 50) == 0) cerr << ".";
355
	}
351
    }
356
	cout << " - " << tim.get_secs() << " secs " << endl;
352
	  cout << " - " << tim.get_secs() << " secs " << endl;
357
    cout << endl;
353
    cout << endl;
358
 
354
 
359
    init_texture(image_tex);
355
    init_texture(image_tex);
360
 
356
 
361
    done = true;
357
    done = true;
Line 377... Line 373...
377
 
373
 
378
    glClearColor(background[0], background[1], background[2], 1.0);
374
    glClearColor(background[0], background[1], background[2], 1.0);
379
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
375
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
380
    glLoadIdentity();
376
    glLoadIdentity();
381
 
377
 
382
	cam->glSetCamera();
378
	  cam->glSetCamera();
383
    
379
    
384
    glColor3f(0.5, 0.5, 0.5);
380
    glColor3f(0.5, 0.5, 0.5);
385
    drawOBJ();
381
    drawOBJ();
386
 
382
 
387
    glDisable(GL_DEPTH_TEST);
383
    glDisable(GL_DEPTH_TEST);
Line 501... Line 497...
501
  }
497
  }
502
  else
498
  else
503
  {
499
  {
504
    obj_load("../../data/dolphins.obj", mesh);
500
    obj_load("../../data/dolphins.obj", mesh);
505
 
501
 
506
	cout << "Computing normals" << endl;
502
  	cout << "Computing normals" << endl;
507
    mesh.compute_normals();
503
    mesh.compute_normals();
508
 
504
 
509
	//cout << "Usage: raytrace any_object.obj";
505
    //cout << "Usage: raytrace any_object.obj";
510
    //exit(0);
506
    //exit(0);
511
  }
507
  }
512
 
508
 
513
  initRaytracer();
509
  initRaytracer();
514
  initGL();    
510
  initGL();