Subversion Repositories gelsvn

Rev

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

Rev 310 Rev 312
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_BSP
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 find out why BSP tree is suboptimal for the bunny mesh.
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 = 4;   // 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
}
90
}
91
 
91
 
92
void spin(int x);
92
void spin(int x);
93
 
93
 
94
//////////////////////////////////////////////////////////////
94
//////////////////////////////////////////////////////////////
95
//      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               
96
//////////////////////////////////////////////////////////////
96
//////////////////////////////////////////////////////////////
97
 
97
 
98
void init_texture(unsigned int& tex)
98
void init_texture(unsigned int& tex)
99
{
99
{
100
  glGenTextures(1, &tex);
100
  glGenTextures(1, &tex);
101
 
101
 
102
  glBindTexture(GL_TEXTURE_2D, tex);
102
  glBindTexture(GL_TEXTURE_2D, tex);
103
 
103
 
104
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
104
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
105
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
105
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
106
 
106
 
107
  // load the texture image
107
  // load the texture image
108
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 
108
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 
109
	       TEX_SIZE, TEX_SIZE,
109
	       TEX_SIZE, TEX_SIZE,
110
	       0, GL_RGB, GL_FLOAT, image[0][0].get());
110
	       0, GL_RGB, GL_FLOAT, image[0][0].get());
111
}
111
}
112
 
112
 
113
void initRaytracer()
113
void initRaytracer()
114
{
114
{
115
  Vec3f c;
115
  Vec3f c;
116
  float r;
116
  float r;
117
  mesh.get_bsphere(c, r);
117
  mesh.get_bsphere(c, r);
118
  r *= 1.5;
118
  r *= 1.5;
119
 
119
 
120
  // Initialize track ball
120
  // Initialize track ball
121
  vctrl = new GLViewController(winx, winy, c, r);
121
  vctrl = new GLViewController(winx, winy, c, r);
122
 
122
 
123
  // Initialize corresponding camera
123
  // Initialize corresponding camera
124
  cam = new Camera(c - Vec3f(r), 
124
  cam = new Camera(c - Vec3f(r), 
125
		   c, 
125
		   c, 
126
		   Vec3f(0.0f, 1.0f, 0.0f),
126
		   Vec3f(0.0f, 1.0f, 0.0f),
127
		   1.0f);
127
		   1.0f);
128
 
128
 
129
#ifdef USE_BDL
129
#ifdef USE_BSP
130
   cout << "Constructing BSP tree..." << endl;
130
   cout << "Constructing BSP tree..." << endl;
131
   tree.init(mesh_vector, transforms, MAX_OBJECTS, MAX_LEVEL);
131
   tree.init(mesh_vector, transforms, MAX_OBJECTS, MAX_LEVEL);
132
   tree.build();
132
   tree.build();
133
#else
133
#else
134
	// AABB TREE
134
	// AABB TREE
135
	Manifold m;
135
	Manifold m;
136
	vector<int> faces(mesh.geometry.no_faces(), 3);
136
	vector<int> faces(mesh.geometry.no_faces(), 3);
137
	cout << "Creating manifold" << endl;
137
	cout << "Creating manifold" << endl;
138
	build_manifold(m, 
138
	build_manifold(m, 
139
				   mesh.geometry.no_vertices(), 
139
				   mesh.geometry.no_vertices(), 
140
				   &mesh.geometry.vertex(0), 
140
				   &mesh.geometry.vertex(0), 
141
				   faces.size(), &faces[0], 
141
				   faces.size(), &faces[0], 
142
				   reinterpret_cast<const int*>(&mesh.geometry.face(0)));
142
				   reinterpret_cast<const int*>(&mesh.geometry.face(0)));
143
	cout << "Building tree" << endl;
143
	cout << "Building tree" << endl;
144
	build_AABBTree(m, bb_tree);
144
	build_AABBTree(m, bb_tree);
145
#endif
145
#endif
146
}
146
}
147
 
147
 
148
void initGL()
148
void initGL()
149
{
149
{
150
  glShadeModel(GL_SMOOTH); 
150
  glShadeModel(GL_SMOOTH); 
151
  glDisable(GL_CULL_FACE);
151
  glDisable(GL_CULL_FACE);
152
  glFrontFace(GL_CCW);
152
  glFrontFace(GL_CCW);
153
 
153
 
154
  glClearColor(1.0, 1.0, 1.0, 1.0);
154
  glClearColor(1.0, 1.0, 1.0, 1.0);
155
  glColor3f(0.0, 0.0, 0.0);
155
  glColor3f(0.0, 0.0, 0.0);
156
}
156
}
157
 
157
 
158
 
158
 
159
//////////////////////////////////////////////////////////////
159
//////////////////////////////////////////////////////////////
160
//      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
161
//////////////////////////////////////////////////////////////
161
//////////////////////////////////////////////////////////////
162
 
162
 
163
double shadow_shade(Ray& r)
163
double shadow_shade(Ray& r)
164
{
164
{
165
  r.compute_position();
165
  r.compute_position();
166
  Ray shadow(r.hit_pos, light_dir);
166
  Ray shadow(r.hit_pos, light_dir);
167
  double s = tree.intersect(shadow) ? 0.0 : 1.0;
167
  double s = tree.intersect(shadow) ? 0.0 : 1.0;
168
 
168
 
169
  r.compute_normal();
169
  r.compute_normal();
170
  return s*light_pow*dot(r.hit_normal, light_dir);
170
  return s*light_pow*dot(r.hit_normal, light_dir);
171
}
171
}
172
 
172
 
173
double lambertian_shade(Ray& r)
173
double lambertian_shade(Ray& r)
174
{
174
{
175
#ifdef USE_BDL
175
#ifdef USE_BSP
176
		r.compute_normal();
176
		r.compute_normal();
177
#endif
177
#endif
178
  return light_pow*dot(r.hit_normal, light_dir);
178
  return light_pow*dot(r.hit_normal, light_dir);
179
}
179
}
180
 
180
 
181
double (*shade_ray[2])(Ray&) = { lambertian_shade,
181
double (*shade_ray[2])(Ray&) = { lambertian_shade,
182
				 shadow_shade      };
182
				 shadow_shade      };
183
 
183
 
184
//////////////////////////////////////////////////////////////
184
//////////////////////////////////////////////////////////////
185
//      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
186
//////////////////////////////////////////////////////////////
186
//////////////////////////////////////////////////////////////
187
 
187
 
188
/*
188
/*
189
void enable_textures(TriMesh& tm)
189
void enable_textures(TriMesh& tm)
190
{
190
{
191
  for(unsigned int i=0;i<tm.materials.size(); ++i)
191
  for(unsigned int i=0;i<tm.materials.size(); ++i)
192
  {
192
  {
193
    Material& mat = tm.materials[i];
193
    Material& mat = tm.materials[i];
194
    if(mat.tex_name != "")
194
    if(mat.tex_name != "")
195
    {
195
    {
196
      string name = mat.tex_path + mat.tex_name;
196
      string name = mat.tex_path + mat.tex_name;
197
      
197
      
198
      GLuint tex_id;
198
      GLuint tex_id;
199
      if(load_image_into_texture(name, tex_id))
199
      if(load_image_into_texture(name, tex_id))
200
	mat.tex_id = tex_id;
200
	mat.tex_id = tex_id;
201
    }
201
    }
202
  }
202
  }
203
}
203
}
204
*/
204
*/
205
 
205
 
206
void set_perspective_proj()
206
void set_perspective_proj()
207
{
207
{
208
  glMatrixMode(GL_PROJECTION);	 
208
  glMatrixMode(GL_PROJECTION);	 
209
  glLoadIdentity();            
209
  glLoadIdentity();            
210
 
210
 
211
  gluPerspective(64.0, 1.0, 0.1, 1000.0);
211
  gluPerspective(64.0, 1.0, 0.1, 1000.0);
212
 
212
 
213
  glMatrixMode(GL_MODELVIEW);
213
  glMatrixMode(GL_MODELVIEW);
214
}
214
}
215
 
215
 
216
void set_ortho_proj()
216
void set_ortho_proj()
217
{
217
{
218
  glMatrixMode(GL_PROJECTION);	 
218
  glMatrixMode(GL_PROJECTION);	 
219
  glLoadIdentity();             
219
  glLoadIdentity();             
220
    
220
    
221
  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);
222
 
222
 
223
  glMatrixMode(GL_MODELVIEW);  
223
  glMatrixMode(GL_MODELVIEW);  
224
}
224
}
225
 
225
 
226
void draw_texture(unsigned int tex)
226
void draw_texture(unsigned int tex)
227
{
227
{
228
  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 };
229
 
229
 
230
  glColor4f(1.0, 1.0, 1.0, 1.0);
230
  glColor4f(1.0, 1.0, 1.0, 1.0);
231
 
231
 
232
  glBindTexture(GL_TEXTURE_2D, tex);
232
  glBindTexture(GL_TEXTURE_2D, tex);
233
  glEnable(GL_TEXTURE_2D);
233
  glEnable(GL_TEXTURE_2D);
234
 
234
 
235
  glEnableClientState(GL_VERTEX_ARRAY);
235
  glEnableClientState(GL_VERTEX_ARRAY);
236
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
236
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
237
  
237
  
238
  glVertexPointer(2, GL_FLOAT, 0, verts);
238
  glVertexPointer(2, GL_FLOAT, 0, verts);
239
  glTexCoordPointer(2, GL_FLOAT, 0, verts);
239
  glTexCoordPointer(2, GL_FLOAT, 0, verts);
240
 
240
 
241
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
241
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
242
 
242
 
243
  glDisableClientState(GL_VERTEX_ARRAY);
243
  glDisableClientState(GL_VERTEX_ARRAY);
244
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
244
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
245
 
245
 
246
  glDisable(GL_TEXTURE_2D);
246
  glDisable(GL_TEXTURE_2D);
247
}
247
}
248
 
248
 
249
void drawOBJ()
249
void drawOBJ()
250
{
250
{
251
  static bool washere = false;
251
  static bool washere = false;
252
  static unsigned int disp_list;
252
  static unsigned int disp_list;
253
 
253
 
254
  if(!washere)
254
  if(!washere)
255
  {
255
  {
256
    disp_list = glGenLists(1);
256
    disp_list = glGenLists(1);
257
    glNewList(disp_list, GL_COMPILE);
257
    glNewList(disp_list, GL_COMPILE);
258
 
258
 
259
    glBegin(GL_TRIANGLES);
259
    glBegin(GL_TRIANGLES);
260
      for(int i = 0; i < mesh.geometry.no_faces(); ++i)
260
      for(int i = 0; i < mesh.geometry.no_faces(); ++i)
261
      {
261
      {
262
	Vec3i n_face = mesh.normals.face(i);
262
	Vec3i n_face = mesh.normals.face(i);
263
	Vec3i g_face = mesh.geometry.face(i);
263
	Vec3i g_face = mesh.geometry.face(i);
264
	for(int j=0;j<3;j++)
264
	for(int j=0;j<3;j++)
265
	{
265
	{
266
	  double shade = 0.5;
266
	  double shade = 0.5;
267
 
267
 
268
	  if(n_face != Geometry::NULL_FACE)
268
	  if(n_face != Geometry::NULL_FACE)
269
	  {
269
	  {
270
	    Vec3f norm = normalize(mesh.normals.vertex(n_face[j]));
270
	    Vec3f norm = normalize(mesh.normals.vertex(n_face[j]));
271
	    glNormal3fv(norm.get());
271
	    glNormal3fv(norm.get());
272
	    shade = light_pow*dot(norm, light_dir);
272
	    shade = light_pow*dot(norm, light_dir);
273
	  }
273
	  }
274
 
274
 
275
	  glColor3d(shade, shade, shade);
275
	  glColor3d(shade, shade, shade);
276
	  Vec3f vert = mesh.geometry.vertex(g_face[j]);	  
276
	  Vec3f vert = mesh.geometry.vertex(g_face[j]);	  
277
	  glVertex3fv(vert.get());
277
	  glVertex3fv(vert.get());
278
	}
278
	}
279
      }
279
      }
280
    glEnd();
280
    glEnd();
281
 
281
 
282
    glEndList();
282
    glEndList();
283
    washere = true;
283
    washere = true;
284
  }
284
  }
285
  glCallList(disp_list);
285
  glCallList(disp_list);
286
}
286
}
287
 
287
 
288
 
288
 
289
//////////////////////////////////////////////////////////////
289
//////////////////////////////////////////////////////////////
290
//      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                 
291
//////////////////////////////////////////////////////////////
291
//////////////////////////////////////////////////////////////
292
 
292
 
293
void display()
293
void display()
294
{
294
{
295
  static bool first = true;
295
  static bool first = true;
296
 
296
 
297
  if(first)
297
  if(first)
298
  {
298
  {
299
    first = false;
299
    first = false;
300
    glutTimerFunc(spin_timer, spin, 0);
300
    glutTimerFunc(spin_timer, spin, 0);
301
  }
301
  }
302
 
302
 
303
  Vec3f eye, focus, up;
303
  Vec3f eye, focus, up;
304
	vctrl->get_view_param(eye, focus, up);
304
	vctrl->get_view_param(eye, focus, up);
305
	cam->set(eye, focus, up, cam->get_focal_dist());
305
	cam->set(eye, focus, up, cam->get_focal_dist());
306
	
306
	
307
  if(raytrace)
307
  if(raytrace)
308
  {
308
  {
309
    raytrace = false;
309
    raytrace = false;
310
    
310
    
311
    cout << "Raytracing";
311
    cout << "Raytracing";
312
    float win_to_vp = 1.0f/static_cast<float>(TEX_SIZE);
312
    float win_to_vp = 1.0f/static_cast<float>(TEX_SIZE);
313
    float lowerleft = 0.5 + win_to_vp*0.5;
313
    float lowerleft = 0.5 + win_to_vp*0.5;
314
    float step = win_to_vp/static_cast<float>(PIXEL_SUBDIVS);
314
    float step = win_to_vp/static_cast<float>(PIXEL_SUBDIVS);
315
    
315
    
316
    vector<Vec2f> jitter(PIXEL_SUBDIVS*PIXEL_SUBDIVS); 
316
    vector<Vec2f> jitter(PIXEL_SUBDIVS*PIXEL_SUBDIVS); 
317
    for(unsigned int i = 0; i < PIXEL_SUBDIVS; ++i)
317
    for(unsigned int i = 0; i < PIXEL_SUBDIVS; ++i)
318
      for(unsigned int j = 0; j < PIXEL_SUBDIVS; ++j)
318
      for(unsigned int j = 0; j < PIXEL_SUBDIVS; ++j)
319
      {
319
      {
320
	    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; 
321
	    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; 
322
      }
322
      }
323
 
323
 
324
	Util::Timer tim;
324
	Util::Timer tim;
325
	tim.start();
325
	tim.start();
326
    for(unsigned int i = 0; i < TEX_SIZE; ++i)
326
    for(unsigned int i = 0; i < TEX_SIZE; ++i)
327
    {
327
    {
328
	  for(unsigned int j = 0; j < TEX_SIZE; ++j)
328
	  for(unsigned int j = 0; j < TEX_SIZE; ++j)
329
	  {
329
	  {
330
		Vec3d sum(0.0f);
330
		Vec3d sum(0.0f);
331
		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);
332
	
332
	
333
		for(unsigned int ky = 0; ky < PIXEL_SUBDIVS; ++ky)
333
		for(unsigned int ky = 0; ky < PIXEL_SUBDIVS; ++ky)
334
		  for(unsigned int kx = 0; kx < PIXEL_SUBDIVS; ++kx)
334
		  for(unsigned int kx = 0; kx < PIXEL_SUBDIVS; ++kx)
335
		  {
335
		  {
336
			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]);
337
 
337
 
338
#ifdef USE_BDL
338
#ifdef USE_BSP
339
			if(tree.intersect(r))
339
			if(tree.intersect(r))
340
			  sum += Vec3d(shade_ray[shadow](r));
340
			  sum += Vec3d(shade_ray[shadow](r));
341
			else
341
			else
342
			  sum += background;
342
			  sum += background;
343
#else
343
#else
344
			float t = FLT_MAX;
344
			float t = FLT_MAX;
345
			bb_tree.intersect(r);
345
			bb_tree.intersect(r);
346
			if(r.has_hit)
346
			if(r.has_hit)
347
			  sum += Vec3d(shade_ray[0](r));
347
			  sum += Vec3d(shade_ray[0](r));
348
			else 
348
			else 
349
			  sum += background;
349
			  sum += background;
350
#endif		
350
#endif		
351
		  }
351
		  }
352
 
352
 
353
		  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));
354
	  }
354
	  }
355
	  if(((i + 1) % 50) == 0) cerr << ".";
355
	  if(((i + 1) % 50) == 0) cerr << ".";
356
	}
356
	}
357
	cout << " - " << tim.get_secs() << " secs " << endl;
357
	cout << " - " << tim.get_secs() << " secs " << endl;
358
    cout << endl;
358
    cout << endl;
359
 
359
 
360
    init_texture(image_tex);
360
    init_texture(image_tex);
361
 
361
 
362
    done = true;
362
    done = true;
363
  }
363
  }
364
 
364
 
365
  if(done)
365
  if(done)
366
  {
366
  {
367
    set_ortho_proj();
367
    set_ortho_proj();
368
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
368
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
369
    glLoadIdentity();
369
    glLoadIdentity();
370
 
370
 
371
    draw_texture(image_tex);
371
    draw_texture(image_tex);
372
  }
372
  }
373
  else
373
  else
374
  {
374
  {
375
    glEnable(GL_DEPTH_TEST);
375
    glEnable(GL_DEPTH_TEST);
376
 
376
 
377
    cam->glSetPerspective(winx, winy);
377
    cam->glSetPerspective(winx, winy);
378
 
378
 
379
    glClearColor(background[0], background[1], background[2], 1.0);
379
    glClearColor(background[0], background[1], background[2], 1.0);
380
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
380
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
381
    glLoadIdentity();
381
    glLoadIdentity();
382
 
382
 
383
	vctrl->set_gl_modelview();
383
	cam->glSetCamera();
384
    
384
    
385
    glColor3f(0.5, 0.5, 0.5);
385
    glColor3f(0.5, 0.5, 0.5);
386
    drawOBJ();
386
    drawOBJ();
387
 
387
 
388
    glDisable(GL_DEPTH_TEST);
388
    glDisable(GL_DEPTH_TEST);
389
  }
389
  }
390
 
390
 
391
  glutSwapBuffers();  
391
  glutSwapBuffers();  
392
}
392
}
393
 
393
 
394
void reshape(int w, int h)
394
void reshape(int w, int h)
395
{
395
{
396
  winx = w; winy = h;
396
  winx = w; winy = h;
397
 
397
 
398
  vctrl->reshape(winx, winy);
398
  vctrl->reshape(winx, winy);
399
 
399
 
400
  glViewport(0, 0, winx, winy);
400
  glViewport(0, 0, winx, winy);
401
}
401
}
402
 
402
 
403
void keyboard(unsigned char key, int x, int y)
403
void keyboard(unsigned char key, int x, int y)
404
{
404
{
405
  switch(key)
405
  switch(key)
406
  {
406
  {
407
  case '+':
407
  case '+':
408
    ++PIXEL_SUBDIVS;
408
    ++PIXEL_SUBDIVS;
409
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
409
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
410
    break;
410
    break;
411
  case '-':
411
  case '-':
412
    if(PIXEL_SUBDIVS > 1)
412
    if(PIXEL_SUBDIVS > 1)
413
      --PIXEL_SUBDIVS;
413
      --PIXEL_SUBDIVS;
414
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
414
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
415
    break;
415
    break;
416
  case 'r':
416
  case 'r':
417
    if(done) done = false;
417
    if(done) done = false;
418
    else raytrace = true;
418
    else raytrace = true;
419
    break;
419
    break;
420
  case 's':
420
  case 's':
421
    shadow = !shadow;
421
    shadow = !shadow;
422
    cout << "Shadow " << (shadow ? "on." : "off.") << endl;
422
    cout << "Shadow " << (shadow ? "on." : "off.") << endl;
423
    break;
423
    break;
424
  case 27:
424
  case 27:
425
    delete vctrl;
425
    delete vctrl;
426
    delete cam;
426
    delete cam;
427
    exit(0);
427
    exit(0);
428
  }
428
  }
429
}
429
}
430
 
430
 
431
void mouse(int btn, int state, int x, int y)
431
void mouse(int btn, int state, int x, int y)
432
{
432
{
433
  if(state == GLUT_DOWN) 
433
  if(state == GLUT_DOWN) 
434
  {
434
  {
435
    if(btn == GLUT_LEFT_BUTTON) 
435
    if(btn == GLUT_LEFT_BUTTON) 
436
      vctrl->grab_ball(ROTATE_ACTION, Vec2i(x, y));
436
      vctrl->grab_ball(ROTATE_ACTION, Vec2i(x, y));
437
    else if(btn == GLUT_MIDDLE_BUTTON) 
437
    else if(btn == GLUT_MIDDLE_BUTTON) 
438
      vctrl->grab_ball(ZOOM_ACTION, Vec2i(x, y));
438
      vctrl->grab_ball(ZOOM_ACTION, Vec2i(x, y));
439
    else if(btn == GLUT_RIGHT_BUTTON) 
439
    else if(btn == GLUT_RIGHT_BUTTON) 
440
      vctrl->grab_ball(PAN_ACTION, Vec2i(x, y));
440
      vctrl->grab_ball(PAN_ACTION, Vec2i(x, y));
441
  }
441
  }
442
  else if(state == GLUT_UP)
442
  else if(state == GLUT_UP)
443
			vctrl->release_ball();
443
			vctrl->release_ball();
444
 
444
 
445
  mouse_state = state;
445
  mouse_state = state;
446
  mouse_button = btn;
446
  mouse_button = btn;
447
 
447
 
448
  glutPostRedisplay();
448
  glutPostRedisplay();
449
}
449
}
450
 
450
 
451
void move(int x, int y)
451
void move(int x, int y)
452
{
452
{
453
    vctrl->roll_ball(Vec2i(x, y));
453
    vctrl->roll_ball(Vec2i(x, y));
454
 
454
 
455
  glutPostRedisplay();
455
  glutPostRedisplay();
456
}
456
}
457
 
457
 
458
void spin(int x)
458
void spin(int x)
459
{
459
{
460
  vctrl->try_spin();
460
  vctrl->try_spin();
461
  glutTimerFunc(spin_timer, spin, 0);  
461
  glutTimerFunc(spin_timer, spin, 0);  
462
  glutPostRedisplay();
462
  glutPostRedisplay();
463
}
463
}
464
 
464
 
465
 
465
 
466
//////////////////////////////////////////////////////////////
466
//////////////////////////////////////////////////////////////
467
//                        M A I N
467
//                        M A I N
468
//////////////////////////////////////////////////////////////
468
//////////////////////////////////////////////////////////////
469
 
469
 
470
int main(int argc, char** argv)
470
int main(int argc, char** argv)
471
{
471
{
472
#ifdef __BORLANDC__
472
#ifdef __BORLANDC__
473
  _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
474
                               // magic line is not inserted
474
                               // magic line is not inserted
475
#endif
475
#endif
476
 
476
 
477
  glutInit(&argc, argv);
477
  glutInit(&argc, argv);
478
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
478
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
479
  glutInitWindowSize(winx, winy);
479
  glutInitWindowSize(winx, winy);
480
  glutCreateWindow("Press 'r' to raytrace");
480
  glutCreateWindow("Press 'r' to raytrace");
481
  glutDisplayFunc(display);
481
  glutDisplayFunc(display);
482
  glutReshapeFunc(reshape);
482
  glutReshapeFunc(reshape);
483
  glutKeyboardFunc(keyboard);
483
  glutKeyboardFunc(keyboard);
484
  glutMouseFunc(mouse);
484
  glutMouseFunc(mouse);
485
  glutMotionFunc(move);
485
  glutMotionFunc(move);
486
 
486
 
487
  // LOAD OBJ
487
  // LOAD OBJ
488
  string filename;
488
  string filename;
489
  if(argc > 1)
489
  if(argc > 1)
490
  {
490
  {
491
    filename = argv[1];
491
    filename = argv[1];
492
    cout << "Loading " << filename << endl;
492
    cout << "Loading " << filename << endl;
493
 
493
 
494
    obj_load(filename, mesh);
494
    obj_load(filename, mesh);
495
    //if(!mesh.has_normals())
495
    //if(!mesh.has_normals())
496
    //{
496
    //{
497
      cout << "Computing normals" << endl;
497
      cout << "Computing normals" << endl;
498
      mesh.compute_normals();
498
      mesh.compute_normals();
499
    //}
499
    //}
500
 
500
 
501
    cout << "No. of triangles: " << mesh.geometry.no_faces() << endl;
501
    cout << "No. of triangles: " << mesh.geometry.no_faces() << endl;
502
  }
502
  }
503
  else
503
  else
504
  {
504
  {
505
    obj_load("../../data/dolphins.obj", mesh);
505
    obj_load("../../data/dolphins.obj", mesh);
506
 
506
 
507
	cout << "Computing normals" << endl;
507
	cout << "Computing normals" << endl;
508
    mesh.compute_normals();
508
    mesh.compute_normals();
509
 
509
 
510
	//cout << "Usage: raytrace any_object.obj";
510
	//cout << "Usage: raytrace any_object.obj";
511
    //exit(0);
511
    //exit(0);
512
  }
512
  }
513
 
513
 
514
  initRaytracer();
514
  initRaytracer();
515
  initGL();    
515
  initGL();    
516
 
516
 
517
  glutMainLoop();
517
  glutMainLoop();
518
 
518
 
519
  return 0;
519
  return 0;
520
}
520
}
521
 
521
 
522
 
522