Subversion Repositories gelsvn

Rev

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

Rev 309 Rev 310
Line 22... Line 22...
22
#include "Geometry/AABox.h"
22
#include "Geometry/AABox.h"
23
#include "Geometry/OBox.h"
23
#include "Geometry/OBox.h"
24
#include "Util/Timer.h"
24
#include "Util/Timer.h"
25
#include "Camera.h"
25
#include "Camera.h"
26
 
26
 
27
//#define USE_BDL
27
// #define USE_BSP
28
 
28
 
29
using namespace std;
29
using namespace std;
30
using namespace CGLA;
30
using namespace CGLA;
31
using namespace Geometry;
31
using namespace Geometry;
32
using namespace HMesh;
32
using namespace HMesh;
33
using namespace GLGraphics;
33
using namespace GLGraphics;
34
 
34
 
35
/*
35
/*
36
 * TODO: 
36
 * TODO: 
37
 * - BSP fix bug preventing bunny model from loading.
37
 * - BSP find out why BSP tree is suboptimal for the bunny mesh.
38
 * - BBOX remove HMesh dependency - that is crazy.
38
 * - BBOX remove HMesh dependency - that is crazy.
39
 * - BBox visit child nodes in order of how far away the intersection point on
39
 * - BBox visit child nodes in order of how far away the intersection point on
40
 *        the bbox is. Closest nodes visited first. Cull nodes farther than
40
 *        the bbox is. Closest nodes visited first. Cull nodes farther than
41
 *        an actual intersection point.
41
 *        an actual intersection point.
42
 * - BBox Smooth interpolation of triangle normals. Straightforward.
42
 * - BBox Smooth interpolation of triangle normals. Straightforward.
43
 */
43
 */
44
					
44
					
45
 
45
 
46
namespace
46
namespace
47
{
47
{
48
  const int MAX_OBJECTS = 2;   // Maximum number of triangles in a BSP tree node
48
  const int MAX_OBJECTS = 4;   // Maximum number of triangles in a BSP tree node
49
  const int MAX_LEVEL = 20;    // Maximum number of BSP tree subdivisions
49
  const int MAX_LEVEL = 20;    // Maximum number of BSP tree subdivisions
50
 
50
 
51
  const unsigned int TEX_SIZE = 512;
51
  const unsigned int TEX_SIZE = 512;
52
 
52
 
53
  bool raytrace = false;
53
  bool raytrace = false;
Line 61... Line 61...
61
 
61
 
62
  int mouse_state = GLUT_UP;
62
  int mouse_state = GLUT_UP;
63
  int mouse_button = 0;
63
  int mouse_button = 0;
64
  int spin_timer = 20;
64
  int spin_timer = 20;
65
 
65
 
66
		GLViewController *vctrl;
66
  GLViewController *vctrl;
67
 
67
 
68
  TriMesh mesh;
68
  TriMesh mesh;
69
  vector<TriMesh*> mesh_vector(1, &mesh);
69
  vector<TriMesh*> mesh_vector(1, &mesh);
70
  vector<Mat4x4f> transforms(1, identity_Mat4x4f());
70
  vector<Mat4x4f> transforms(1, identity_Mat4x4f());
71
 
71
 
Line 84... Line 84...
84
  inline double my_random()
84
  inline double my_random()
85
  {
85
  {
86
    return rand()/static_cast<double>(RAND_MAX);
86
    return rand()/static_cast<double>(RAND_MAX);
87
  }
87
  }
88
		
88
		
89
		AABBTree bb_tree;
89
  AABBTree bb_tree;		
90
		
-
 
91
 
-
 
92
}
90
}
93
 
91
 
94
void spin(int x);
92
void spin(int x);
95
 
93
 
96
//////////////////////////////////////////////////////////////
94
//////////////////////////////////////////////////////////////
Line 118... Line 116...
118
  float r;
116
  float r;
119
  mesh.get_bsphere(c, r);
117
  mesh.get_bsphere(c, r);
120
  r *= 1.5;
118
  r *= 1.5;
121
 
119
 
122
  // Initialize track ball
120
  // Initialize track ball
123
	vctrl = new GLViewController(winx, winy, c, r);
121
  vctrl = new GLViewController(winx, winy, c, r);
124
 
122
 
125
  // Initialize corresponding camera
123
  // Initialize corresponding camera
126
  cam = new Camera(c - Vec3f(r), 
124
  cam = new Camera(c - Vec3f(r), 
127
		   c, 
125
		   c, 
128
		   Vec3f(0.0f, 1.0f, 0.0f),
126
		   Vec3f(0.0f, 1.0f, 0.0f),
Line 136... Line 134...
136
	// AABB TREE
134
	// AABB TREE
137
	Manifold m;
135
	Manifold m;
138
	vector<int> faces(mesh.geometry.no_faces(), 3);
136
	vector<int> faces(mesh.geometry.no_faces(), 3);
139
	cout << "Creating manifold" << endl;
137
	cout << "Creating manifold" << endl;
140
	build_manifold(m, 
138
	build_manifold(m, 
141
								 mesh.geometry.no_vertices(), 
139
				   mesh.geometry.no_vertices(), 
142
								 &mesh.geometry.vertex(0), 
140
				   &mesh.geometry.vertex(0), 
143
								 faces.size(), &faces[0], 
141
				   faces.size(), &faces[0], 
144
								 reinterpret_cast<const int*>(&mesh.geometry.face(0)));
142
				   reinterpret_cast<const int*>(&mesh.geometry.face(0)));
145
	cout << "Building tree" << endl;
143
	cout << "Building tree" << endl;
146
	build_AABBTree(m, bb_tree);
144
	build_AABBTree(m, bb_tree);
147
#endif
145
#endif
148
}
146
}
149
 
147
 
Line 317... Line 315...
317
    
315
    
318
    vector<Vec2f> jitter(PIXEL_SUBDIVS*PIXEL_SUBDIVS); 
316
    vector<Vec2f> jitter(PIXEL_SUBDIVS*PIXEL_SUBDIVS); 
319
    for(unsigned int i = 0; i < PIXEL_SUBDIVS; ++i)
317
    for(unsigned int i = 0; i < PIXEL_SUBDIVS; ++i)
320
      for(unsigned int j = 0; j < PIXEL_SUBDIVS; ++j)
318
      for(unsigned int j = 0; j < PIXEL_SUBDIVS; ++j)
321
      {
319
      {
322
	jitter[i*PIXEL_SUBDIVS + j][0] = (my_random() + (j%PIXEL_SUBDIVS))*step; 
320
	    jitter[i*PIXEL_SUBDIVS + j][0] = (my_random() + (j%PIXEL_SUBDIVS))*step; 
323
	jitter[i*PIXEL_SUBDIVS + j][1] = (my_random() + (i%PIXEL_SUBDIVS))*step; 
321
	    jitter[i*PIXEL_SUBDIVS + j][1] = (my_random() + (i%PIXEL_SUBDIVS))*step; 
324
      }
322
      }
325
 
323
 
326
		Util::Timer tim;
324
	Util::Timer tim;
327
		tim.start();
325
	tim.start();
328
    for(unsigned int i = 0; i < TEX_SIZE; ++i)
326
    for(unsigned int i = 0; i < TEX_SIZE; ++i)
329
    {
327
    {
330
				for(unsigned int j = 0; j < TEX_SIZE; ++j)
328
	  for(unsigned int j = 0; j < TEX_SIZE; ++j)
331
				{
329
	  {
332
						Vec3d sum(0.0f);
330
		Vec3d sum(0.0f);
333
						Vec2f vp_pos(j*win_to_vp - lowerleft, i*win_to_vp - lowerleft);
331
		Vec2f vp_pos(j*win_to_vp - lowerleft, i*win_to_vp - lowerleft);
334
	
332
	
335
						for(unsigned int ky = 0; ky < PIXEL_SUBDIVS; ++ky)
333
		for(unsigned int ky = 0; ky < PIXEL_SUBDIVS; ++ky)
336
								for(unsigned int kx = 0; kx < PIXEL_SUBDIVS; ++kx)
334
		  for(unsigned int kx = 0; kx < PIXEL_SUBDIVS; ++kx)
337
								{
335
		  {
338
										Ray r = cam->get_ray(vp_pos + jitter[ky*PIXEL_SUBDIVS + kx]);
336
			Ray r = cam->get_ray(vp_pos + jitter[ky*PIXEL_SUBDIVS + kx]);
339
 
337
 
340
#ifdef USE_BDL
338
#ifdef USE_BDL
341
										if(tree.intersect(r))
339
			if(tree.intersect(r))
342
												sum += Vec3d(shade_ray[shadow](r));
340
			  sum += Vec3d(shade_ray[shadow](r));
343
										else
341
			else
344
												sum += background;
342
			  sum += background;
345
#else
343
#else
346
										float t = FLT_MAX;
344
			float t = FLT_MAX;
347
										bb_tree.intersect(r);
345
			bb_tree.intersect(r);
348
										if(r.has_hit)
346
			if(r.has_hit)
349
												sum += Vec3d(shade_ray[0](r));
347
			  sum += Vec3d(shade_ray[0](r));
-
 
348
			else 
350
										else sum += background;
349
			  sum += background;
351
#endif		
350
#endif		
352
								}
351
		  }
353
 
352
 
354
						image[i][j] = Vec3f(sum/static_cast<double>(PIXEL_SUBDIVS*PIXEL_SUBDIVS));
353
		  image[i][j] = Vec3f(sum/static_cast<double>(PIXEL_SUBDIVS*PIXEL_SUBDIVS));
355
				}
354
	  }
356
				if(((i + 1) % 50) == 0) cerr << ".";
355
	  if(((i + 1) % 50) == 0) cerr << ".";
357
    }
356
	}
358
		cout << " - " << tim.get_secs() << " secs " << endl;
357
	cout << " - " << tim.get_secs() << " secs " << endl;
359
    cout << endl;
358
    cout << endl;
360
 
359
 
361
    init_texture(image_tex);
360
    init_texture(image_tex);
362
 
361
 
363
    done = true;
362
    done = true;
Line 374... Line 373...
374
  else
373
  else
375
  {
374
  {
376
    glEnable(GL_DEPTH_TEST);
375
    glEnable(GL_DEPTH_TEST);
377
 
376
 
378
    cam->glSetPerspective(winx, winy);
377
    cam->glSetPerspective(winx, winy);
379
		vctrl->reset_projection();
-
 
380
 
378
 
381
    glClearColor(background[0], background[1], background[2], 1.0);
379
    glClearColor(background[0], background[1], background[2], 1.0);
382
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
380
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
383
    glLoadIdentity();
381
    glLoadIdentity();
384
 
382
 
385
		vctrl->set_gl_modelview();
383
	vctrl->set_gl_modelview();
386
    
384
    
387
    glColor3f(0.5, 0.5, 0.5);
385
    glColor3f(0.5, 0.5, 0.5);
388
    drawOBJ();
386
    drawOBJ();
389
 
387
 
390
    glDisable(GL_DEPTH_TEST);
388
    glDisable(GL_DEPTH_TEST);
Line 398... Line 396...
398
  winx = w; winy = h;
396
  winx = w; winy = h;
399
 
397
 
400
  vctrl->reshape(winx, winy);
398
  vctrl->reshape(winx, winy);
401
 
399
 
402
  glViewport(0, 0, winx, winy);
400
  glViewport(0, 0, winx, winy);
403
  set_ortho_proj();
-
 
404
}
401
}
405
 
402
 
406
void keyboard(unsigned char key, int x, int y)
403
void keyboard(unsigned char key, int x, int y)
407
{
404
{
408
  switch(key)
405
  switch(key)