Subversion Repositories gelsvn

Rev

Rev 309 | Rev 312 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 309 Rev 310
1
#include <iostream>
1
#include <iostream>
2
#include <vector>
2
#include <vector>
3
#include <algorithm>
3
#include <algorithm>
4
#include <cmath>
4
#include <cmath>
5
 
5
 
6
#include "GL/glut.h"
6
#include "GL/glut.h"
7
 
7
 
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
 
13
 
14
#include "HMesh/build_manifold.h"
14
#include "HMesh/build_manifold.h"
15
#include "Geometry/TriMesh.h"
15
#include "Geometry/TriMesh.h"
16
#include "Geometry/obj_load.h"
16
#include "Geometry/obj_load.h"
17
#include "Geometry/Ray.h"
17
#include "Geometry/Ray.h"
18
#include "Geometry/BSPTree.h"
18
#include "Geometry/BSPTree.h"
19
#include "GLGraphics/GLViewController.h"
19
#include "GLGraphics/GLViewController.h"
20
 
20
 
21
#include "Geometry/build_bbtree.h"
21
#include "Geometry/build_bbtree.h"
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;
54
  bool done = false;
54
  bool done = false;
55
  bool shadow = false;
55
  bool shadow = false;
56
 
56
 
57
  unsigned int winx = TEX_SIZE;     // Screen width
57
  unsigned int winx = TEX_SIZE;     // Screen width
58
  unsigned int winy = TEX_SIZE;     // Screen height
58
  unsigned int winy = TEX_SIZE;     // Screen height
59
 
59
 
60
  unsigned int PIXEL_SUBDIVS = 1;
60
  unsigned int PIXEL_SUBDIVS = 1;
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
 
72
  double light_pow = 1.0;
72
  double light_pow = 1.0;
73
  Vec3f light_dir = normalize(Vec3f(1.0, 1.0, 1.0));
73
  Vec3f light_dir = normalize(Vec3f(1.0, 1.0, 1.0));
74
  Vec3d background(0.8, 0.9, 1.0);
74
  Vec3d background(0.8, 0.9, 1.0);
75
 
75
 
76
  BSPTree tree;
76
  BSPTree tree;
77
  Camera* cam;
77
  Camera* cam;
78
 
78
 
79
  Vec3f image[TEX_SIZE][TEX_SIZE];
79
  Vec3f image[TEX_SIZE][TEX_SIZE];
80
  unsigned int image_tex;
80
  unsigned int image_tex;
81
 
81
 
82
  // Function to generate a random number between 0 and 1.
82
  // Function to generate a random number between 0 and 1.
83
  // rand() returns a random number ranging from 0 to RAND_MAX.
83
  // rand() returns a random number ranging from 0 to RAND_MAX.
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
//////////////////////////////////////////////////////////////
97
//      I N I T I A L I Z A T I O N               
95
//      I N I T I A L I Z A T I O N               
98
//////////////////////////////////////////////////////////////
96
//////////////////////////////////////////////////////////////
99
 
97
 
100
void init_texture(unsigned int& tex)
98
void init_texture(unsigned int& tex)
101
{
99
{
102
  glGenTextures(1, &tex);
100
  glGenTextures(1, &tex);
103
 
101
 
104
  glBindTexture(GL_TEXTURE_2D, tex);
102
  glBindTexture(GL_TEXTURE_2D, tex);
105
 
103
 
106
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
104
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
107
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
105
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
108
 
106
 
109
  // load the texture image
107
  // load the texture image
110
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 
108
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 
111
	       TEX_SIZE, TEX_SIZE,
109
	       TEX_SIZE, TEX_SIZE,
112
	       0, GL_RGB, GL_FLOAT, image[0][0].get());
110
	       0, GL_RGB, GL_FLOAT, image[0][0].get());
113
}
111
}
114
 
112
 
115
void initRaytracer()
113
void initRaytracer()
116
{
114
{
117
  Vec3f c;
115
  Vec3f c;
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),
129
		   1.0f);
127
		   1.0f);
130
 
128
 
131
#ifdef USE_BDL
129
#ifdef USE_BDL
132
   cout << "Constructing BSP tree..." << endl;
130
   cout << "Constructing BSP tree..." << endl;
133
   tree.init(mesh_vector, transforms, MAX_OBJECTS, MAX_LEVEL);
131
   tree.init(mesh_vector, transforms, MAX_OBJECTS, MAX_LEVEL);
134
   tree.build();
132
   tree.build();
135
#else
133
#else
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
 
150
void initGL()
148
void initGL()
151
{
149
{
152
  glShadeModel(GL_SMOOTH); 
150
  glShadeModel(GL_SMOOTH); 
153
  glDisable(GL_CULL_FACE);
151
  glDisable(GL_CULL_FACE);
154
  glFrontFace(GL_CCW);
152
  glFrontFace(GL_CCW);
155
 
153
 
156
  glClearColor(1.0, 1.0, 1.0, 1.0);
154
  glClearColor(1.0, 1.0, 1.0, 1.0);
157
  glColor3f(0.0, 0.0, 0.0);
155
  glColor3f(0.0, 0.0, 0.0);
158
}
156
}
159
 
157
 
160
 
158
 
161
//////////////////////////////////////////////////////////////
159
//////////////////////////////////////////////////////////////
162
//      S H A D E   F U N C T I O N S
160
//      S H A D E   F U N C T I O N S
163
//////////////////////////////////////////////////////////////
161
//////////////////////////////////////////////////////////////
164
 
162
 
165
double shadow_shade(Ray& r)
163
double shadow_shade(Ray& r)
166
{
164
{
167
  r.compute_position();
165
  r.compute_position();
168
  Ray shadow(r.hit_pos, light_dir);
166
  Ray shadow(r.hit_pos, light_dir);
169
  double s = tree.intersect(shadow) ? 0.0 : 1.0;
167
  double s = tree.intersect(shadow) ? 0.0 : 1.0;
170
 
168
 
171
  r.compute_normal();
169
  r.compute_normal();
172
  return s*light_pow*dot(r.hit_normal, light_dir);
170
  return s*light_pow*dot(r.hit_normal, light_dir);
173
}
171
}
174
 
172
 
175
double lambertian_shade(Ray& r)
173
double lambertian_shade(Ray& r)
176
{
174
{
177
#ifdef USE_BDL
175
#ifdef USE_BDL
178
		r.compute_normal();
176
		r.compute_normal();
179
#endif
177
#endif
180
  return light_pow*dot(r.hit_normal, light_dir);
178
  return light_pow*dot(r.hit_normal, light_dir);
181
}
179
}
182
 
180
 
183
double (*shade_ray[2])(Ray&) = { lambertian_shade,
181
double (*shade_ray[2])(Ray&) = { lambertian_shade,
184
				 shadow_shade      };
182
				 shadow_shade      };
185
 
183
 
186
//////////////////////////////////////////////////////////////
184
//////////////////////////////////////////////////////////////
187
//      D R A W   F U N C T I O N S
185
//      D R A W   F U N C T I O N S
188
//////////////////////////////////////////////////////////////
186
//////////////////////////////////////////////////////////////
189
 
187
 
190
/*
188
/*
191
void enable_textures(TriMesh& tm)
189
void enable_textures(TriMesh& tm)
192
{
190
{
193
  for(unsigned int i=0;i<tm.materials.size(); ++i)
191
  for(unsigned int i=0;i<tm.materials.size(); ++i)
194
  {
192
  {
195
    Material& mat = tm.materials[i];
193
    Material& mat = tm.materials[i];
196
    if(mat.tex_name != "")
194
    if(mat.tex_name != "")
197
    {
195
    {
198
      string name = mat.tex_path + mat.tex_name;
196
      string name = mat.tex_path + mat.tex_name;
199
      
197
      
200
      GLuint tex_id;
198
      GLuint tex_id;
201
      if(load_image_into_texture(name, tex_id))
199
      if(load_image_into_texture(name, tex_id))
202
	mat.tex_id = tex_id;
200
	mat.tex_id = tex_id;
203
    }
201
    }
204
  }
202
  }
205
}
203
}
206
*/
204
*/
207
 
205
 
208
void set_perspective_proj()
206
void set_perspective_proj()
209
{
207
{
210
  glMatrixMode(GL_PROJECTION);	 
208
  glMatrixMode(GL_PROJECTION);	 
211
  glLoadIdentity();            
209
  glLoadIdentity();            
212
 
210
 
213
  gluPerspective(64.0, 1.0, 0.1, 1000.0);
211
  gluPerspective(64.0, 1.0, 0.1, 1000.0);
214
 
212
 
215
  glMatrixMode(GL_MODELVIEW);
213
  glMatrixMode(GL_MODELVIEW);
216
}
214
}
217
 
215
 
218
void set_ortho_proj()
216
void set_ortho_proj()
219
{
217
{
220
  glMatrixMode(GL_PROJECTION);	 
218
  glMatrixMode(GL_PROJECTION);	 
221
  glLoadIdentity();             
219
  glLoadIdentity();             
222
    
220
    
223
  glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
221
  glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
224
 
222
 
225
  glMatrixMode(GL_MODELVIEW);  
223
  glMatrixMode(GL_MODELVIEW);  
226
}
224
}
227
 
225
 
228
void draw_texture(unsigned int tex)
226
void draw_texture(unsigned int tex)
229
{
227
{
230
  static GLfloat verts[] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
228
  static GLfloat verts[] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
231
 
229
 
232
  glColor4f(1.0, 1.0, 1.0, 1.0);
230
  glColor4f(1.0, 1.0, 1.0, 1.0);
233
 
231
 
234
  glBindTexture(GL_TEXTURE_2D, tex);
232
  glBindTexture(GL_TEXTURE_2D, tex);
235
  glEnable(GL_TEXTURE_2D);
233
  glEnable(GL_TEXTURE_2D);
236
 
234
 
237
  glEnableClientState(GL_VERTEX_ARRAY);
235
  glEnableClientState(GL_VERTEX_ARRAY);
238
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
236
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
239
  
237
  
240
  glVertexPointer(2, GL_FLOAT, 0, verts);
238
  glVertexPointer(2, GL_FLOAT, 0, verts);
241
  glTexCoordPointer(2, GL_FLOAT, 0, verts);
239
  glTexCoordPointer(2, GL_FLOAT, 0, verts);
242
 
240
 
243
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
241
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
244
 
242
 
245
  glDisableClientState(GL_VERTEX_ARRAY);
243
  glDisableClientState(GL_VERTEX_ARRAY);
246
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
244
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
247
 
245
 
248
  glDisable(GL_TEXTURE_2D);
246
  glDisable(GL_TEXTURE_2D);
249
}
247
}
250
 
248
 
251
void drawOBJ()
249
void drawOBJ()
252
{
250
{
253
  static bool washere = false;
251
  static bool washere = false;
254
  static unsigned int disp_list;
252
  static unsigned int disp_list;
255
 
253
 
256
  if(!washere)
254
  if(!washere)
257
  {
255
  {
258
    disp_list = glGenLists(1);
256
    disp_list = glGenLists(1);
259
    glNewList(disp_list, GL_COMPILE);
257
    glNewList(disp_list, GL_COMPILE);
260
 
258
 
261
    glBegin(GL_TRIANGLES);
259
    glBegin(GL_TRIANGLES);
262
      for(int i = 0; i < mesh.geometry.no_faces(); ++i)
260
      for(int i = 0; i < mesh.geometry.no_faces(); ++i)
263
      {
261
      {
264
	Vec3i n_face = mesh.normals.face(i);
262
	Vec3i n_face = mesh.normals.face(i);
265
	Vec3i g_face = mesh.geometry.face(i);
263
	Vec3i g_face = mesh.geometry.face(i);
266
	for(int j=0;j<3;j++)
264
	for(int j=0;j<3;j++)
267
	{
265
	{
268
	  double shade = 0.5;
266
	  double shade = 0.5;
269
 
267
 
270
	  if(n_face != Geometry::NULL_FACE)
268
	  if(n_face != Geometry::NULL_FACE)
271
	  {
269
	  {
272
	    Vec3f norm = normalize(mesh.normals.vertex(n_face[j]));
270
	    Vec3f norm = normalize(mesh.normals.vertex(n_face[j]));
273
	    glNormal3fv(norm.get());
271
	    glNormal3fv(norm.get());
274
	    shade = light_pow*dot(norm, light_dir);
272
	    shade = light_pow*dot(norm, light_dir);
275
	  }
273
	  }
276
 
274
 
277
	  glColor3d(shade, shade, shade);
275
	  glColor3d(shade, shade, shade);
278
	  Vec3f vert = mesh.geometry.vertex(g_face[j]);	  
276
	  Vec3f vert = mesh.geometry.vertex(g_face[j]);	  
279
	  glVertex3fv(vert.get());
277
	  glVertex3fv(vert.get());
280
	}
278
	}
281
      }
279
      }
282
    glEnd();
280
    glEnd();
283
 
281
 
284
    glEndList();
282
    glEndList();
285
    washere = true;
283
    washere = true;
286
  }
284
  }
287
  glCallList(disp_list);
285
  glCallList(disp_list);
288
}
286
}
289
 
287
 
290
 
288
 
291
//////////////////////////////////////////////////////////////
289
//////////////////////////////////////////////////////////////
292
//      G L U T   C A L L B A C K   F U N C T I O N S                 
290
//      G L U T   C A L L B A C K   F U N C T I O N S                 
293
//////////////////////////////////////////////////////////////
291
//////////////////////////////////////////////////////////////
294
 
292
 
295
void display()
293
void display()
296
{
294
{
297
  static bool first = true;
295
  static bool first = true;
298
 
296
 
299
  if(first)
297
  if(first)
300
  {
298
  {
301
    first = false;
299
    first = false;
302
    glutTimerFunc(spin_timer, spin, 0);
300
    glutTimerFunc(spin_timer, spin, 0);
303
  }
301
  }
304
 
302
 
305
  Vec3f eye, focus, up;
303
  Vec3f eye, focus, up;
306
	vctrl->get_view_param(eye, focus, up);
304
	vctrl->get_view_param(eye, focus, up);
307
	cam->set(eye, focus, up, cam->get_focal_dist());
305
	cam->set(eye, focus, up, cam->get_focal_dist());
308
	
306
	
309
  if(raytrace)
307
  if(raytrace)
310
  {
308
  {
311
    raytrace = false;
309
    raytrace = false;
312
    
310
    
313
    cout << "Raytracing";
311
    cout << "Raytracing";
314
    float win_to_vp = 1.0f/static_cast<float>(TEX_SIZE);
312
    float win_to_vp = 1.0f/static_cast<float>(TEX_SIZE);
315
    float lowerleft = 0.5 + win_to_vp*0.5;
313
    float lowerleft = 0.5 + win_to_vp*0.5;
316
    float step = win_to_vp/static_cast<float>(PIXEL_SUBDIVS);
314
    float step = win_to_vp/static_cast<float>(PIXEL_SUBDIVS);
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;
364
  }
363
  }
365
 
364
 
366
  if(done)
365
  if(done)
367
  {
366
  {
368
    set_ortho_proj();
367
    set_ortho_proj();
369
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
368
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
370
    glLoadIdentity();
369
    glLoadIdentity();
371
 
370
 
372
    draw_texture(image_tex);
371
    draw_texture(image_tex);
373
  }
372
  }
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);
391
  }
389
  }
392
 
390
 
393
  glutSwapBuffers();  
391
  glutSwapBuffers();  
394
}
392
}
395
 
393
 
396
void reshape(int w, int h)
394
void reshape(int w, int h)
397
{
395
{
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)
409
  {
406
  {
410
  case '+':
407
  case '+':
411
    ++PIXEL_SUBDIVS;
408
    ++PIXEL_SUBDIVS;
412
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
409
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
413
    break;
410
    break;
414
  case '-':
411
  case '-':
415
    if(PIXEL_SUBDIVS > 1)
412
    if(PIXEL_SUBDIVS > 1)
416
      --PIXEL_SUBDIVS;
413
      --PIXEL_SUBDIVS;
417
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
414
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
418
    break;
415
    break;
419
  case 'r':
416
  case 'r':
420
    if(done) done = false;
417
    if(done) done = false;
421
    else raytrace = true;
418
    else raytrace = true;
422
    break;
419
    break;
423
  case 's':
420
  case 's':
424
    shadow = !shadow;
421
    shadow = !shadow;
425
    cout << "Shadow " << (shadow ? "on." : "off.") << endl;
422
    cout << "Shadow " << (shadow ? "on." : "off.") << endl;
426
    break;
423
    break;
427
  case 27:
424
  case 27:
428
    delete vctrl;
425
    delete vctrl;
429
    delete cam;
426
    delete cam;
430
    exit(0);
427
    exit(0);
431
  }
428
  }
432
}
429
}
433
 
430
 
434
void mouse(int btn, int state, int x, int y)
431
void mouse(int btn, int state, int x, int y)
435
{
432
{
436
  if(state == GLUT_DOWN) 
433
  if(state == GLUT_DOWN) 
437
  {
434
  {
438
    if(btn == GLUT_LEFT_BUTTON) 
435
    if(btn == GLUT_LEFT_BUTTON) 
439
      vctrl->grab_ball(ROTATE_ACTION, Vec2i(x, y));
436
      vctrl->grab_ball(ROTATE_ACTION, Vec2i(x, y));
440
    else if(btn == GLUT_MIDDLE_BUTTON) 
437
    else if(btn == GLUT_MIDDLE_BUTTON) 
441
      vctrl->grab_ball(ZOOM_ACTION, Vec2i(x, y));
438
      vctrl->grab_ball(ZOOM_ACTION, Vec2i(x, y));
442
    else if(btn == GLUT_RIGHT_BUTTON) 
439
    else if(btn == GLUT_RIGHT_BUTTON) 
443
      vctrl->grab_ball(PAN_ACTION, Vec2i(x, y));
440
      vctrl->grab_ball(PAN_ACTION, Vec2i(x, y));
444
  }
441
  }
445
  else if(state == GLUT_UP)
442
  else if(state == GLUT_UP)
446
			vctrl->release_ball();
443
			vctrl->release_ball();
447
 
444
 
448
  mouse_state = state;
445
  mouse_state = state;
449
  mouse_button = btn;
446
  mouse_button = btn;
450
 
447
 
451
  glutPostRedisplay();
448
  glutPostRedisplay();
452
}
449
}
453
 
450
 
454
void move(int x, int y)
451
void move(int x, int y)
455
{
452
{
456
    vctrl->roll_ball(Vec2i(x, y));
453
    vctrl->roll_ball(Vec2i(x, y));
457
 
454
 
458
  glutPostRedisplay();
455
  glutPostRedisplay();
459
}
456
}
460
 
457
 
461
void spin(int x)
458
void spin(int x)
462
{
459
{
463
  vctrl->try_spin();
460
  vctrl->try_spin();
464
  glutTimerFunc(spin_timer, spin, 0);  
461
  glutTimerFunc(spin_timer, spin, 0);  
465
  glutPostRedisplay();
462
  glutPostRedisplay();
466
}
463
}
467
 
464
 
468
 
465
 
469
//////////////////////////////////////////////////////////////
466
//////////////////////////////////////////////////////////////
470
//                        M A I N
467
//                        M A I N
471
//////////////////////////////////////////////////////////////
468
//////////////////////////////////////////////////////////////
472
 
469
 
473
int main(int argc, char** argv)
470
int main(int argc, char** argv)
474
{
471
{
475
#ifdef __BORLANDC__
472
#ifdef __BORLANDC__
476
  _control87(MCW_EM, MCW_EM);  // Borland C++ will crash OpenGL if this
473
  _control87(MCW_EM, MCW_EM);  // Borland C++ will crash OpenGL if this
477
                               // magic line is not inserted
474
                               // magic line is not inserted
478
#endif
475
#endif
479
 
476
 
480
  glutInit(&argc, argv);
477
  glutInit(&argc, argv);
481
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
478
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
482
  glutInitWindowSize(winx, winy);
479
  glutInitWindowSize(winx, winy);
483
  glutCreateWindow("Press 'r' to raytrace");
480
  glutCreateWindow("Press 'r' to raytrace");
484
  glutDisplayFunc(display);
481
  glutDisplayFunc(display);
485
  glutReshapeFunc(reshape);
482
  glutReshapeFunc(reshape);
486
  glutKeyboardFunc(keyboard);
483
  glutKeyboardFunc(keyboard);
487
  glutMouseFunc(mouse);
484
  glutMouseFunc(mouse);
488
  glutMotionFunc(move);
485
  glutMotionFunc(move);
489
 
486
 
490
  // LOAD OBJ
487
  // LOAD OBJ
491
  string filename;
488
  string filename;
492
  if(argc > 1)
489
  if(argc > 1)
493
  {
490
  {
494
    filename = argv[1];
491
    filename = argv[1];
495
    cout << "Loading " << filename << endl;
492
    cout << "Loading " << filename << endl;
496
 
493
 
497
    obj_load(filename, mesh);
494
    obj_load(filename, mesh);
498
    //if(!mesh.has_normals())
495
    //if(!mesh.has_normals())
499
    //{
496
    //{
500
      cout << "Computing normals" << endl;
497
      cout << "Computing normals" << endl;
501
      mesh.compute_normals();
498
      mesh.compute_normals();
502
    //}
499
    //}
503
 
500
 
504
    cout << "No. of triangles: " << mesh.geometry.no_faces() << endl;
501
    cout << "No. of triangles: " << mesh.geometry.no_faces() << endl;
505
  }
502
  }
506
  else
503
  else
507
  {
504
  {
508
    obj_load("../../data/dolphins.obj", mesh);
505
    obj_load("../../data/dolphins.obj", mesh);
509
 
506
 
510
	cout << "Computing normals" << endl;
507
	cout << "Computing normals" << endl;
511
    mesh.compute_normals();
508
    mesh.compute_normals();
512
 
509
 
513
	//cout << "Usage: raytrace any_object.obj";
510
	//cout << "Usage: raytrace any_object.obj";
514
    //exit(0);
511
    //exit(0);
515
  }
512
  }
516
 
513
 
517
  initRaytracer();
514
  initRaytracer();
518
  initGL();    
515
  initGL();    
519
 
516
 
520
  glutMainLoop();
517
  glutMainLoop();
521
 
518
 
522
  return 0;
519
  return 0;
523
}
520
}
524
 
521
 
525
 
522