Subversion Repositories gelsvn

Rev

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

Rev 305 Rev 309
Line 8... Line 8...
8
#include "CGLA/Vec2f.h"
8
#include "CGLA/Vec2f.h"
9
#include "CGLA/Vec3i.h"
9
#include "CGLA/Vec3i.h"
10
#include "CGLA/Vec3f.h"
10
#include "CGLA/Vec3f.h"
11
#include "CGLA/Vec3d.h"
11
#include "CGLA/Vec3d.h"
12
#include "CGLA/Mat4x4f.h"
12
#include "CGLA/Mat4x4f.h"
-
 
13
 
-
 
14
#include "HMesh/build_manifold.h"
13
#include "Geometry/TriMesh.h"
15
#include "Geometry/TriMesh.h"
14
#include "Geometry/obj_load.h"
16
#include "Geometry/obj_load.h"
15
#include "Geometry/Ray.h"
17
#include "Geometry/Ray.h"
16
#include "Geometry/BSPTree.h"
18
#include "Geometry/BSPTree.h"
17
#include "GLGraphics/QuatTrackBall.h"
19
#include "GLGraphics/GLViewController.h"
18
 
20
 
-
 
21
#include "Geometry/build_bbtree.h"
-
 
22
#include "Geometry/AABox.h"
-
 
23
#include "Geometry/OBox.h"
-
 
24
#include "Util/Timer.h"
19
#include "Camera.h"
25
#include "Camera.h"
20
 
26
 
-
 
27
//#define USE_BDL
-
 
28
 
21
using namespace std;
29
using namespace std;
22
using namespace CGLA;
30
using namespace CGLA;
23
using namespace Geometry;
31
using namespace Geometry;
-
 
32
using namespace HMesh;
24
using namespace GLGraphics;
33
using namespace GLGraphics;
25
 
34
 
-
 
35
/*
-
 
36
 * TODO: 
-
 
37
 * - BSP fix bug preventing bunny model from loading.
-
 
38
 * - BBOX remove HMesh dependency - that is crazy.
-
 
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
-
 
41
 *        an actual intersection point.
-
 
42
 * - BBox Smooth interpolation of triangle normals. Straightforward.
-
 
43
 */
-
 
44
					
-
 
45
 
26
namespace
46
namespace
27
{
47
{
28
  const int MAX_OBJECTS = 2;   // Maximum number of triangles in a BSP tree node
48
  const int MAX_OBJECTS = 2;   // Maximum number of triangles in a BSP tree node
29
  const int MAX_LEVEL = 20;    // Maximum number of BSP tree subdivisions
49
  const int MAX_LEVEL = 20;    // Maximum number of BSP tree subdivisions
30
 
50
 
Line 41... Line 61...
41
 
61
 
42
  int mouse_state = GLUT_UP;
62
  int mouse_state = GLUT_UP;
43
  int mouse_button = 0;
63
  int mouse_button = 0;
44
  int spin_timer = 20;
64
  int spin_timer = 20;
45
 
65
 
46
  QuatTrackBall* ball = 0;
66
		GLViewController *vctrl;
47
 
67
 
48
  TriMesh mesh;
68
  TriMesh mesh;
49
  vector<TriMesh*> mesh_vector(1, &mesh);
69
  vector<TriMesh*> mesh_vector(1, &mesh);
50
  vector<Mat4x4f> transforms(1, identity_Mat4x4f());
70
  vector<Mat4x4f> transforms(1, identity_Mat4x4f());
51
 
71
 
Line 63... Line 83...
63
  // rand() returns a random number ranging from 0 to RAND_MAX.
83
  // rand() returns a random number ranging from 0 to RAND_MAX.
64
  inline double my_random()
84
  inline double my_random()
65
  {
85
  {
66
    return rand()/static_cast<double>(RAND_MAX);
86
    return rand()/static_cast<double>(RAND_MAX);
67
  }
87
  }
-
 
88
		
-
 
89
		AABBTree bb_tree;
-
 
90
		
68
 
91
 
69
}
92
}
70
 
93
 
71
void spin(int x);
94
void spin(int x);
72
 
95
 
Line 95... Line 118...
95
  float r;
118
  float r;
96
  mesh.get_bsphere(c, r);
119
  mesh.get_bsphere(c, r);
97
  r *= 1.5;
120
  r *= 1.5;
98
 
121
 
99
  // Initialize track ball
122
  // Initialize track ball
100
  ball = new QuatTrackBall(c,        // Center
123
	vctrl = new GLViewController(winx, winy, c, r);
101
		       r,        // Eye distance
-
 
102
		       winx,     // Window width
-
 
103
		       winy);    // Window height
-
 
104
 
124
 
105
  // Initialize corresponding camera
125
  // Initialize corresponding camera
106
  cam = new Camera(c - Vec3f(r), 
126
  cam = new Camera(c - Vec3f(r), 
107
		   c, 
127
		   c, 
108
		   Vec3f(0.0f, 1.0f, 0.0f),
128
		   Vec3f(0.0f, 1.0f, 0.0f),
109
		   1.0f);
129
		   1.0f);
110
 
130
 
-
 
131
#ifdef USE_BDL
111
  cout << "Constructing BSP tree..." << endl;
132
   cout << "Constructing BSP tree..." << endl;
112
  tree.init(mesh_vector, transforms, MAX_OBJECTS, MAX_LEVEL);
133
   tree.init(mesh_vector, transforms, MAX_OBJECTS, MAX_LEVEL);
113
  tree.build();
134
   tree.build();
-
 
135
#else
-
 
136
	// AABB TREE
-
 
137
	Manifold m;
-
 
138
	vector<int> faces(mesh.geometry.no_faces(), 3);
-
 
139
	cout << "Creating manifold" << endl;
-
 
140
	build_manifold(m, 
-
 
141
								 mesh.geometry.no_vertices(), 
-
 
142
								 &mesh.geometry.vertex(0), 
-
 
143
								 faces.size(), &faces[0], 
-
 
144
								 reinterpret_cast<const int*>(&mesh.geometry.face(0)));
-
 
145
	cout << "Building tree" << endl;
-
 
146
	build_AABBTree(m, bb_tree);
-
 
147
#endif
114
}
148
}
115
 
149
 
116
void initGL()
150
void initGL()
117
{
151
{
118
  glShadeModel(GL_SMOOTH); 
152
  glShadeModel(GL_SMOOTH); 
119
  glEnable(GL_CULL_FACE);
153
  glDisable(GL_CULL_FACE);
120
  glFrontFace(GL_CCW);
154
  glFrontFace(GL_CCW);
121
 
155
 
122
  glClearColor(1.0, 1.0, 1.0, 1.0);
156
  glClearColor(1.0, 1.0, 1.0, 1.0);
123
  glColor3f(0.0, 0.0, 0.0);
157
  glColor3f(0.0, 0.0, 0.0);
124
}
158
}
Line 138... Line 172...
138
  return s*light_pow*dot(r.hit_normal, light_dir);
172
  return s*light_pow*dot(r.hit_normal, light_dir);
139
}
173
}
140
 
174
 
141
double lambertian_shade(Ray& r)
175
double lambertian_shade(Ray& r)
142
{
176
{
-
 
177
#ifdef USE_BDL
143
  r.compute_normal();
178
		r.compute_normal();
-
 
179
#endif
144
  return light_pow*dot(r.hit_normal, light_dir);
180
  return light_pow*dot(r.hit_normal, light_dir);
145
}
181
}
146
 
182
 
147
double (*shade_ray[2])(Ray&) = { lambertian_shade,
183
double (*shade_ray[2])(Ray&) = { lambertian_shade,
148
				 shadow_shade      };
184
				 shadow_shade      };
Line 265... Line 301...
265
    first = false;
301
    first = false;
266
    glutTimerFunc(spin_timer, spin, 0);
302
    glutTimerFunc(spin_timer, spin, 0);
267
  }
303
  }
268
 
304
 
269
  Vec3f eye, focus, up;
305
  Vec3f eye, focus, up;
270
  ball->get_view_param(eye, focus, up);
306
	vctrl->get_view_param(eye, focus, up);
271
  cam->set(eye, focus, up, cam->get_focal_dist());
307
	cam->set(eye, focus, up, cam->get_focal_dist());
272
 
308
	
273
  if(raytrace)
309
  if(raytrace)
274
  {
310
  {
275
    raytrace = false;
311
    raytrace = false;
276
    
312
    
277
    cout << "Raytracing";
313
    cout << "Raytracing";
Line 285... Line 321...
285
      {
321
      {
286
	jitter[i*PIXEL_SUBDIVS + j][0] = (my_random() + (j%PIXEL_SUBDIVS))*step; 
322
	jitter[i*PIXEL_SUBDIVS + j][0] = (my_random() + (j%PIXEL_SUBDIVS))*step; 
287
	jitter[i*PIXEL_SUBDIVS + j][1] = (my_random() + (i%PIXEL_SUBDIVS))*step; 
323
	jitter[i*PIXEL_SUBDIVS + j][1] = (my_random() + (i%PIXEL_SUBDIVS))*step; 
288
      }
324
      }
289
 
325
 
-
 
326
		Util::Timer tim;
-
 
327
		tim.start();
290
    for(unsigned int i = 0; i < TEX_SIZE; ++i)
328
    for(unsigned int i = 0; i < TEX_SIZE; ++i)
291
    {
329
    {
292
      for(unsigned int j = 0; j < TEX_SIZE; ++j)
330
				for(unsigned int j = 0; j < TEX_SIZE; ++j)
293
      {
331
				{
294
	Vec3d sum(0.0f);
332
						Vec3d sum(0.0f);
295
	Vec2f vp_pos(j*win_to_vp - lowerleft, i*win_to_vp - lowerleft);
333
						Vec2f vp_pos(j*win_to_vp - lowerleft, i*win_to_vp - lowerleft);
296
	
334
	
297
	for(unsigned int ky = 0; ky < PIXEL_SUBDIVS; ++ky)
335
						for(unsigned int ky = 0; ky < PIXEL_SUBDIVS; ++ky)
298
	  for(unsigned int kx = 0; kx < PIXEL_SUBDIVS; ++kx)
336
								for(unsigned int kx = 0; kx < PIXEL_SUBDIVS; ++kx)
299
          {
337
								{
300
	    Ray r = cam->get_ray(vp_pos + jitter[ky*PIXEL_SUBDIVS + kx]);
338
										Ray r = cam->get_ray(vp_pos + jitter[ky*PIXEL_SUBDIVS + kx]);
301
 
339
 
-
 
340
#ifdef USE_BDL
302
	    if(tree.intersect(r))
341
										if(tree.intersect(r))
303
	      sum += Vec3d(shade_ray[shadow](r));
342
												sum += Vec3d(shade_ray[shadow](r));
-
 
343
										else
-
 
344
												sum += background;
304
	    else
345
#else
-
 
346
										float t = FLT_MAX;
-
 
347
										bb_tree.intersect(r);
-
 
348
										if(r.has_hit)
-
 
349
												sum += Vec3d(shade_ray[0](r));
305
	      sum += background;
350
										else sum += background;
-
 
351
#endif		
306
	  }
352
								}
307
 
353
 
308
	image[i][j] = Vec3f(sum/static_cast<double>(PIXEL_SUBDIVS*PIXEL_SUBDIVS));
354
						image[i][j] = Vec3f(sum/static_cast<double>(PIXEL_SUBDIVS*PIXEL_SUBDIVS));
309
      }
355
				}
310
      if(((i + 1) % 50) == 0) cerr << ".";
356
				if(((i + 1) % 50) == 0) cerr << ".";
311
    }
357
    }
-
 
358
		cout << " - " << tim.get_secs() << " secs " << endl;
312
    cout << endl;
359
    cout << endl;
313
 
360
 
314
    init_texture(image_tex);
361
    init_texture(image_tex);
315
 
362
 
316
    done = true;
363
    done = true;
Line 327... Line 374...
327
  else
374
  else
328
  {
375
  {
329
    glEnable(GL_DEPTH_TEST);
376
    glEnable(GL_DEPTH_TEST);
330
 
377
 
331
    cam->glSetPerspective(winx, winy);
378
    cam->glSetPerspective(winx, winy);
-
 
379
		vctrl->reset_projection();
332
 
380
 
333
    glClearColor(background[0], background[1], background[2], 1.0);
381
    glClearColor(background[0], background[1], background[2], 1.0);
334
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
382
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
335
    glLoadIdentity();
383
    glLoadIdentity();
336
 
384
 
337
    cam->glSetCamera();
385
		vctrl->set_gl_modelview();
338
    
386
    
339
    glColor3f(0.5, 0.5, 0.5);
387
    glColor3f(0.5, 0.5, 0.5);
340
    drawOBJ();
388
    drawOBJ();
341
 
389
 
342
    glDisable(GL_DEPTH_TEST);
390
    glDisable(GL_DEPTH_TEST);
Line 347... Line 395...
347
 
395
 
348
void reshape(int w, int h)
396
void reshape(int w, int h)
349
{
397
{
350
  winx = w; winy = h;
398
  winx = w; winy = h;
351
 
399
 
352
  ball->set_screen_window(winx, winy);
400
  vctrl->reshape(winx, winy);
353
  ball->set_screen_centre(Vec2i(winx/2, winy/2));
-
 
354
 
401
 
355
  glViewport(0, 0, winx, winy);
402
  glViewport(0, 0, winx, winy);
356
  set_ortho_proj();
403
  set_ortho_proj();
357
}
404
}
358
 
405
 
Line 376... Line 423...
376
  case 's':
423
  case 's':
377
    shadow = !shadow;
424
    shadow = !shadow;
378
    cout << "Shadow " << (shadow ? "on." : "off.") << endl;
425
    cout << "Shadow " << (shadow ? "on." : "off.") << endl;
379
    break;
426
    break;
380
  case 27:
427
  case 27:
381
    delete ball;
428
    delete vctrl;
382
    delete cam;
429
    delete cam;
383
    exit(0);
430
    exit(0);
384
  }
431
  }
385
}
432
}
386
 
433
 
387
void mouse(int btn, int state, int x, int y)
434
void mouse(int btn, int state, int x, int y)
388
{
435
{
389
  if(state == GLUT_DOWN) 
436
  if(state == GLUT_DOWN) 
390
  {
437
  {
391
    if(btn == GLUT_LEFT_BUTTON) 
438
    if(btn == GLUT_LEFT_BUTTON) 
392
      ball->grab_ball(ROTATE_ACTION, Vec2i(x, y));
439
      vctrl->grab_ball(ROTATE_ACTION, Vec2i(x, y));
393
    else if(btn == GLUT_MIDDLE_BUTTON) 
440
    else if(btn == GLUT_MIDDLE_BUTTON) 
394
      ball->grab_ball(ZOOM_ACTION, Vec2i(x, y));
441
      vctrl->grab_ball(ZOOM_ACTION, Vec2i(x, y));
395
    else if(btn == GLUT_RIGHT_BUTTON) 
442
    else if(btn == GLUT_RIGHT_BUTTON) 
396
      ball->grab_ball(PAN_ACTION, Vec2i(x, y));
443
      vctrl->grab_ball(PAN_ACTION, Vec2i(x, y));
397
  }
444
  }
398
  else if(state == GLUT_UP)
445
  else if(state == GLUT_UP)
399
    ball->release_ball();
446
			vctrl->release_ball();
400
 
447
 
401
  mouse_state = state;
448
  mouse_state = state;
402
  mouse_button = btn;
449
  mouse_button = btn;
403
 
450
 
404
  glutPostRedisplay();
451
  glutPostRedisplay();
405
}
452
}
406
 
453
 
407
void move(int x, int y)
454
void move(int x, int y)
408
{
455
{
409
  if(mouse_state == GLUT_DOWN)
-
 
410
    ball->roll_ball(Vec2i(x, y));
456
    vctrl->roll_ball(Vec2i(x, y));
411
 
457
 
412
  glutPostRedisplay();
458
  glutPostRedisplay();
413
}
459
}
414
 
460
 
415
void spin(int x)
461
void spin(int x)
416
{
462
{
417
  ball->do_spin();
463
  vctrl->try_spin();
418
  glutTimerFunc(spin_timer, spin, 0);  
464
  glutTimerFunc(spin_timer, spin, 0);  
419
  glutPostRedisplay();
465
  glutPostRedisplay();
420
}
466
}
421
 
467
 
422
 
468