Subversion Repositories gelsvn

Rev

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

Rev 417 Rev 418
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 "GLGraphics/gel_glut.h"
6
#include "GLGraphics/gel_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
 * - BBOX remove HMesh dependency - that is crazy.
37
 * - BBOX remove HMesh dependency - that is crazy.
38
 * - BBox visit child nodes in order of how far away the intersection point on
38
 * - BBox visit child nodes in order of how far away the intersection point on
39
 *        the bbox is. Closest nodes visited first. Cull nodes farther than
39
 *        the bbox is. Closest nodes visited first. Cull nodes farther than
40
 *        an actual intersection point.
40
 *        an actual intersection point.
41
 * - BBox Smooth interpolation of triangle normals. Straightforward.
41
 * - BBox Smooth interpolation of triangle normals. Straightforward.
42
 */
42
 */
43
					
43
					
44
 
44
 
45
namespace
45
namespace
46
{
46
{
47
  const int MAX_OBJECTS = 4;   // Maximum number of triangles in a BSP tree node
47
  const int MAX_OBJECTS = 4;   // Maximum number of triangles in a BSP tree node
48
  const int MAX_LEVEL = 20;    // Maximum number of BSP tree subdivisions
48
  const int MAX_LEVEL = 20;    // Maximum number of BSP tree subdivisions
49
 
49
 
50
  const unsigned int TEX_SIZE = 512;
50
  const unsigned int TEX_SIZE = 512;
51
 
51
 
52
  bool raytrace = false;
52
  bool raytrace = false;
53
  bool done = false;
53
  bool done = false;
54
  bool shadow = false;
54
  bool shadow = false;
55
 
55
 
56
  unsigned int winx = TEX_SIZE;     // Screen width
56
  unsigned int winx = TEX_SIZE;     // Screen width
57
  unsigned int winy = TEX_SIZE;     // Screen height
57
  unsigned int winy = TEX_SIZE;     // Screen height
58
 
58
 
59
  unsigned int PIXEL_SUBDIVS = 1;
59
  unsigned int PIXEL_SUBDIVS = 1;
60
 
60
 
61
  int mouse_state = GLUT_UP;
61
  int mouse_state = GLUT_UP;
62
  int mouse_button = 0;
62
  int mouse_button = 0;
63
  int spin_timer = 20;
63
  int spin_timer = 20;
64
 
64
 
65
  GLViewController *vctrl;
65
  GLViewController *vctrl;
66
 
66
 
67
  TriMesh mesh;
67
  TriMesh mesh;
68
  vector<const TriMesh*> mesh_vector(1, &mesh);
68
  vector<const TriMesh*> mesh_vector(1, &mesh);
69
  vector<Mat4x4f> transforms(1, identity_Mat4x4f());
69
  vector<Mat4x4f> transforms(1, identity_Mat4x4f());
70
 
70
 
71
  double light_pow = 1.0;
71
  double light_pow = 1.0;
72
  Vec3f light_dir = normalize(Vec3f(1.0, 1.0, 1.0));
72
  Vec3f light_dir = normalize(Vec3f(1.0, 1.0, 1.0));
73
  Vec3d background(0.8, 0.9, 1.0);
73
  Vec3d background(0.8, 0.9, 1.0);
74
 
74
 
75
  BSPTree tree;
75
  BSPTree tree;
76
  Camera* cam;
76
  Camera* cam;
77
 
77
 
78
  Vec3f image[TEX_SIZE][TEX_SIZE];
78
  Vec3f image[TEX_SIZE][TEX_SIZE];
79
  unsigned int image_tex;
79
  unsigned int image_tex;
80
 
80
 
81
  // Function to generate a random number between 0 and 1.
81
  // Function to generate a random number between 0 and 1.
82
  // gel_rand() returns a random number ranging from 0 to GEL_RAND_MAX.
82
  // gel_rand() returns a random number ranging from 0 to GEL_RAND_MAX.
83
  inline double my_random()
83
  inline double my_random()
84
  {
84
  {
85
    return gel_rand()/static_cast<double>(GEL_RAND_MAX);
85
    return gel_rand()/static_cast<double>(GEL_RAND_MAX);
86
  }
86
  }
87
		
87
		
88
  AABBTree bb_tree;		
88
  AABBTree bb_tree;		
89
}
89
}
90
 
90
 
91
void spin(int x);
91
void spin(int x);
92
 
92
 
93
//////////////////////////////////////////////////////////////
93
//////////////////////////////////////////////////////////////
94
//      I N I T I A L I Z A T I O N               
94
//      I N I T I A L I Z A T I O N               
95
//////////////////////////////////////////////////////////////
95
//////////////////////////////////////////////////////////////
96
 
96
 
97
void init_texture(unsigned int& tex)
97
void init_texture(unsigned int& tex)
98
{
98
{
99
  glGenTextures(1, reinterpret_cast<GLuint*>(&tex));
99
  glGenTextures(1, reinterpret_cast<GLuint*>(&tex));
100
 
100
 
101
  glBindTexture(GL_TEXTURE_2D, tex);
101
  glBindTexture(GL_TEXTURE_2D, tex);
102
 
102
 
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;
115
  float r;
115
  float r;
116
  mesh.get_bsphere(c, r);
116
  mesh.get_bsphere(c, r);
117
  r *= 1.5;
117
  r *= 1.5;
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();
132
#else
129
#else
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
 
147
void initGL()
144
void initGL()
148
{
145
{
149
  glShadeModel(GL_SMOOTH); 
146
  glShadeModel(GL_SMOOTH); 
150
  glDisable(GL_CULL_FACE);
147
  glDisable(GL_CULL_FACE);
151
  glFrontFace(GL_CCW);
148
  glFrontFace(GL_CCW);
152
 
149
 
153
  glClearColor(1.0, 1.0, 1.0, 1.0);
150
  glClearColor(1.0, 1.0, 1.0, 1.0);
154
  glColor3f(0.0, 0.0, 0.0);
151
  glColor3f(0.0, 0.0, 0.0);
155
}
152
}
156
 
153
 
157
 
154
 
158
//////////////////////////////////////////////////////////////
155
//////////////////////////////////////////////////////////////
159
//      S H A D E   F U N C T I O N S
156
//      S H A D E   F U N C T I O N S
160
//////////////////////////////////////////////////////////////
157
//////////////////////////////////////////////////////////////
161
 
158
 
162
double shadow_shade(Ray& r)
159
double shadow_shade(Ray& r)
163
{
160
{
164
  r.compute_position();
161
  r.compute_position();
165
  Ray shadow(r.hit_pos, light_dir);
162
  Ray shadow(r.hit_pos, light_dir);
166
  double s = tree.intersect(shadow) ? 0.0 : 1.0;
163
  double s = tree.intersect(shadow) ? 0.0 : 1.0;
167
 
164
 
168
  r.compute_normal();
165
  r.compute_normal();
169
  return s*light_pow*dot(r.hit_normal, light_dir);
166
  return s*light_pow*dot(r.hit_normal, light_dir);
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
 
187
/*
184
/*
188
void enable_textures(TriMesh& tm)
185
void enable_textures(TriMesh& tm)
189
{
186
{
190
  for(unsigned int i=0;i<tm.materials.size(); ++i)
187
  for(unsigned int i=0;i<tm.materials.size(); ++i)
191
  {
188
  {
192
    Material& mat = tm.materials[i];
189
    Material& mat = tm.materials[i];
193
    if(mat.tex_name != "")
190
    if(mat.tex_name != "")
194
    {
191
    {
195
      string name = mat.tex_path + mat.tex_name;
192
      string name = mat.tex_path + mat.tex_name;
196
      
193
      
197
      GLuint tex_id;
194
      GLuint tex_id;
198
      if(load_image_into_texture(name, tex_id))
195
      if(load_image_into_texture(name, tex_id))
199
	mat.tex_id = tex_id;
196
	mat.tex_id = tex_id;
200
    }
197
    }
201
  }
198
  }
202
}
199
}
203
*/
200
*/
204
 
201
 
205
void set_perspective_proj()
202
void set_perspective_proj()
206
{
203
{
207
  glMatrixMode(GL_PROJECTION);	 
204
  glMatrixMode(GL_PROJECTION);	 
208
  glLoadIdentity();            
205
  glLoadIdentity();            
209
 
206
 
210
  gluPerspective(64.0, 1.0, 0.1, 1000.0);
207
  gluPerspective(64.0, 1.0, 0.1, 1000.0);
211
 
208
 
212
  glMatrixMode(GL_MODELVIEW);
209
  glMatrixMode(GL_MODELVIEW);
213
}
210
}
214
 
211
 
215
void set_ortho_proj()
212
void set_ortho_proj()
216
{
213
{
217
  glMatrixMode(GL_PROJECTION);	 
214
  glMatrixMode(GL_PROJECTION);	 
218
  glLoadIdentity();             
215
  glLoadIdentity();             
219
    
216
    
220
  glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
217
  glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
221
 
218
 
222
  glMatrixMode(GL_MODELVIEW);  
219
  glMatrixMode(GL_MODELVIEW);  
223
}
220
}
224
 
221
 
225
void draw_texture(unsigned int tex)
222
void draw_texture(unsigned int tex)
226
{
223
{
227
  static GLfloat verts[] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
224
  static GLfloat verts[] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
228
 
225
 
229
  glColor4f(1.0, 1.0, 1.0, 1.0);
226
  glColor4f(1.0, 1.0, 1.0, 1.0);
230
 
227
 
231
  glBindTexture(GL_TEXTURE_2D, tex);
228
  glBindTexture(GL_TEXTURE_2D, tex);
232
  glEnable(GL_TEXTURE_2D);
229
  glEnable(GL_TEXTURE_2D);
233
 
230
 
234
  glEnableClientState(GL_VERTEX_ARRAY);
231
  glEnableClientState(GL_VERTEX_ARRAY);
235
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
232
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
236
  
233
  
237
  glVertexPointer(2, GL_FLOAT, 0, verts);
234
  glVertexPointer(2, GL_FLOAT, 0, verts);
238
  glTexCoordPointer(2, GL_FLOAT, 0, verts);
235
  glTexCoordPointer(2, GL_FLOAT, 0, verts);
239
 
236
 
240
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
237
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
241
 
238
 
242
  glDisableClientState(GL_VERTEX_ARRAY);
239
  glDisableClientState(GL_VERTEX_ARRAY);
243
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
240
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
244
 
241
 
245
  glDisable(GL_TEXTURE_2D);
242
  glDisable(GL_TEXTURE_2D);
246
}
243
}
247
 
244
 
248
void drawOBJ()
245
void drawOBJ()
249
{
246
{
250
  static bool washere = false;
247
  static bool washere = false;
251
  static unsigned int disp_list;
248
  static unsigned int disp_list;
252
 
249
 
253
  if(!washere)
250
  if(!washere)
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
  }
284
  glCallList(disp_list);
281
  glCallList(disp_list);
285
}
282
}
286
 
283
 
287
 
284
 
288
//////////////////////////////////////////////////////////////
285
//////////////////////////////////////////////////////////////
289
//      G L U T   C A L L B A C K   F U N C T I O N S                 
286
//      G L U T   C A L L B A C K   F U N C T I O N S                 
290
//////////////////////////////////////////////////////////////
287
//////////////////////////////////////////////////////////////
291
 
288
 
292
void display()
289
void display()
293
{
290
{
294
  static bool first = true;
291
  static bool first = true;
295
 
292
 
296
  if(first)
293
  if(first)
297
  {
294
  {
298
    first = false;
295
    first = false;
299
    glutTimerFunc(spin_timer, spin, 0);
296
    glutTimerFunc(spin_timer, spin, 0);
300
  }
297
  }
301
 
298
 
302
  Vec3f eye, focus, up;
299
  Vec3f eye, focus, up;
303
	vctrl->get_view_param(eye, focus, up);
300
	vctrl->get_view_param(eye, focus, up);
304
	cam->set(eye, focus, up, cam->get_focal_dist());
301
	cam->set(eye, focus, up, cam->get_focal_dist());
305
	
302
	
306
  if(raytrace)
303
  if(raytrace)
307
  {
304
  {
308
    raytrace = false;
305
    raytrace = false;
309
    
306
    
310
    cout << "Raytracing";
307
    cout << "Raytracing";
311
    float win_to_vp = 1.0f/static_cast<float>(TEX_SIZE);
308
    float win_to_vp = 1.0f/static_cast<float>(TEX_SIZE);
312
    float lowerleft = 0.5 + win_to_vp*0.5;
309
    float lowerleft = 0.5 + win_to_vp*0.5;
313
    float step = win_to_vp/static_cast<float>(PIXEL_SUBDIVS);
310
    float step = win_to_vp/static_cast<float>(PIXEL_SUBDIVS);
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;
362
  }
358
  }
363
 
359
 
364
  if(done)
360
  if(done)
365
  {
361
  {
366
    set_ortho_proj();
362
    set_ortho_proj();
367
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
363
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
368
    glLoadIdentity();
364
    glLoadIdentity();
369
 
365
 
370
    draw_texture(image_tex);
366
    draw_texture(image_tex);
371
  }
367
  }
372
  else
368
  else
373
  {
369
  {
374
    glEnable(GL_DEPTH_TEST);
370
    glEnable(GL_DEPTH_TEST);
375
 
371
 
376
    cam->glSetPerspective(winx, winy);
372
    cam->glSetPerspective(winx, winy);
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);
388
  }
384
  }
389
 
385
 
390
  glutSwapBuffers();  
386
  glutSwapBuffers();  
391
}
387
}
392
 
388
 
393
void reshape(int w, int h)
389
void reshape(int w, int h)
394
{
390
{
395
  winx = w; winy = h;
391
  winx = w; winy = h;
396
 
392
 
397
  vctrl->reshape(winx, winy);
393
  vctrl->reshape(winx, winy);
398
 
394
 
399
  glViewport(0, 0, winx, winy);
395
  glViewport(0, 0, winx, winy);
400
}
396
}
401
 
397
 
402
void keyboard(unsigned char key, int x, int y)
398
void keyboard(unsigned char key, int x, int y)
403
{
399
{
404
  switch(key)
400
  switch(key)
405
  {
401
  {
406
  case '+':
402
  case '+':
407
    ++PIXEL_SUBDIVS;
403
    ++PIXEL_SUBDIVS;
408
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
404
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
409
    break;
405
    break;
410
  case '-':
406
  case '-':
411
    if(PIXEL_SUBDIVS > 1)
407
    if(PIXEL_SUBDIVS > 1)
412
      --PIXEL_SUBDIVS;
408
      --PIXEL_SUBDIVS;
413
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
409
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
414
    break;
410
    break;
415
  case 'r':
411
  case 'r':
416
    if(done) done = false;
412
    if(done) done = false;
417
    else raytrace = true;
413
    else raytrace = true;
418
    break;
414
    break;
419
  case 's':
415
  case 's':
420
    shadow = !shadow;
416
    shadow = !shadow;
421
    cout << "Shadow " << (shadow ? "on." : "off.") << endl;
417
    cout << "Shadow " << (shadow ? "on." : "off.") << endl;
422
    break;
418
    break;
423
  case 27:
419
  case 27:
424
    delete vctrl;
420
    delete vctrl;
425
    delete cam;
421
    delete cam;
426
    exit(0);
422
    exit(0);
427
  }
423
  }
428
}
424
}
429
 
425
 
430
void mouse(int btn, int state, int x, int y)
426
void mouse(int btn, int state, int x, int y)
431
{
427
{
432
  if(state == GLUT_DOWN) 
428
  if(state == GLUT_DOWN) 
433
  {
429
  {
434
    if(btn == GLUT_LEFT_BUTTON) 
430
    if(btn == GLUT_LEFT_BUTTON) 
435
      vctrl->grab_ball(ROTATE_ACTION, Vec2i(x, y));
431
      vctrl->grab_ball(ROTATE_ACTION, Vec2i(x, y));
436
    else if(btn == GLUT_MIDDLE_BUTTON) 
432
    else if(btn == GLUT_MIDDLE_BUTTON) 
437
      vctrl->grab_ball(ZOOM_ACTION, Vec2i(x, y));
433
      vctrl->grab_ball(ZOOM_ACTION, Vec2i(x, y));
438
    else if(btn == GLUT_RIGHT_BUTTON) 
434
    else if(btn == GLUT_RIGHT_BUTTON) 
439
      vctrl->grab_ball(PAN_ACTION, Vec2i(x, y));
435
      vctrl->grab_ball(PAN_ACTION, Vec2i(x, y));
440
  }
436
  }
441
  else if(state == GLUT_UP)
437
  else if(state == GLUT_UP)
442
			vctrl->release_ball();
438
			vctrl->release_ball();
443
 
439
 
444
  mouse_state = state;
440
  mouse_state = state;
445
  mouse_button = btn;
441
  mouse_button = btn;
446
 
442
 
447
  glutPostRedisplay();
443
  glutPostRedisplay();
448
}
444
}
449
 
445
 
450
void move(int x, int y)
446
void move(int x, int y)
451
{
447
{
452
    vctrl->roll_ball(Vec2i(x, y));
448
    vctrl->roll_ball(Vec2i(x, y));
453
 
449
 
454
  glutPostRedisplay();
450
  glutPostRedisplay();
455
}
451
}
456
 
452
 
457
void spin(int x)
453
void spin(int x)
458
{
454
{
459
  vctrl->try_spin();
455
  vctrl->try_spin();
460
  glutTimerFunc(spin_timer, spin, 0);  
456
  glutTimerFunc(spin_timer, spin, 0);  
461
  glutPostRedisplay();
457
  glutPostRedisplay();
462
}
458
}
463
 
459
 
464
 
460
 
465
//////////////////////////////////////////////////////////////
461
//////////////////////////////////////////////////////////////
466
//                        M A I N
462
//                        M A I N
467
//////////////////////////////////////////////////////////////
463
//////////////////////////////////////////////////////////////
468
 
464
 
469
int main(int argc, char** argv)
465
int main(int argc, char** argv)
470
{
466
{
471
#ifdef __BORLANDC__
467
#ifdef __BORLANDC__
472
  _control87(MCW_EM, MCW_EM);  // Borland C++ will crash OpenGL if this
468
  _control87(MCW_EM, MCW_EM);  // Borland C++ will crash OpenGL if this
473
                               // magic line is not inserted
469
                               // magic line is not inserted
474
#endif
470
#endif
475
 
471
 
476
  glutInit(&argc, argv);
472
  glutInit(&argc, argv);
477
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
473
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
478
  glutInitWindowSize(winx, winy);
474
  glutInitWindowSize(winx, winy);
479
  glutCreateWindow("Press 'r' to raytrace");
475
  glutCreateWindow("Press 'r' to raytrace");
480
  glutDisplayFunc(display);
476
  glutDisplayFunc(display);
481
  glutReshapeFunc(reshape);
477
  glutReshapeFunc(reshape);
482
  glutKeyboardFunc(keyboard);
478
  glutKeyboardFunc(keyboard);
483
  glutMouseFunc(mouse);
479
  glutMouseFunc(mouse);
484
  glutMotionFunc(move);
480
  glutMotionFunc(move);
485
 
481
 
486
  // LOAD OBJ
482
  // LOAD OBJ
487
  string filename;
483
  string filename;
488
  if(argc > 1)
484
  if(argc > 1)
489
  {
485
  {
490
    filename = argv[1];
486
    filename = argv[1];
491
    cout << "Loading " << filename << endl;
487
    cout << "Loading " << filename << endl;
492
 
488
 
493
    obj_load(filename, mesh);
489
    obj_load(filename, mesh);
494
    //if(!mesh.has_normals())
490
    //if(!mesh.has_normals())
495
    //{
491
    //{
496
      cout << "Computing normals" << endl;
492
      cout << "Computing normals" << endl;
497
      mesh.compute_normals();
493
      mesh.compute_normals();
498
    //}
494
    //}
499
 
495
 
500
    cout << "No. of triangles: " << mesh.geometry.no_faces() << endl;
496
    cout << "No. of triangles: " << mesh.geometry.no_faces() << endl;
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();    
515
 
511
 
516
  glutMainLoop();
512
  glutMainLoop();
517
 
513
 
518
  return 0;
514
  return 0;
519
}
515
}
520
 
516
 
521
 
517