Subversion Repositories gelsvn

Rev

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

Rev 643 Rev 653
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>
-
 
7
#include <GLGraphics/GLViewController.h>
6
#include <GLGraphics/GLViewController.h>
-
 
7
#include <GEL/GL/glew.h>
-
 
8
#ifdef __APPLE__
-
 
9
#include <GLUT/GLUT.h>
-
 
10
#else
-
 
11
#include <GL/glut.h>
-
 
12
#endif
8
 
13
 
9
#include <CGLA/Vec2f.h>
14
#include <CGLA/Vec2f.h>
10
#include <CGLA/Vec3i.h>
15
#include <CGLA/Vec3i.h>
11
#include <CGLA/Vec3f.h>
16
#include <CGLA/Vec3f.h>
12
#include <CGLA/Vec3d.h>
17
#include <CGLA/Vec3d.h>
13
#include <CGLA/Mat4x4f.h>
18
#include <CGLA/Mat4x4f.h>
14
 
19
 
15
#include <HMesh/Manifold.h>
20
#include <HMesh/Manifold.h>
16
 
21
 
17
#include <Geometry/TriMesh.h>
22
#include <Geometry/TriMesh.h>
18
#include <Geometry/obj_load.h>
23
#include <Geometry/obj_load.h>
19
#include <Geometry/Ray.h>
24
#include <Geometry/Ray.h>
20
#include <Geometry/BSPTree.h>
25
#include <Geometry/BSPTree.h>
21
#include <Geometry/build_bbtree.h>
26
#include <Geometry/build_bbtree.h>
22
#include <Geometry/AABox.h>
27
#include <Geometry/AABox.h>
23
#include <Geometry/OBox.h>
28
#include <Geometry/OBox.h>
24
 
29
 
25
#include <Util/Timer.h>
30
#include <Util/Timer.h>
26
 
31
 
27
#include "Camera.h"
32
#include "Camera.h"
28
 
33
 
29
//#define USE_BSP
34
//#define USE_BSP
30
 
35
 
31
using namespace std;
36
using namespace std;
32
using namespace CGLA;
37
using namespace CGLA;
33
using namespace Geometry;
38
using namespace Geometry;
34
using namespace HMesh;
39
using namespace HMesh;
35
using namespace GLGraphics;
40
using namespace GLGraphics;
36
 
41
 
37
/*
42
/*
38
 * TODO: 
43
 * TODO: 
39
 * - BBOX remove HMesh dependency - that is crazy.
44
 * - BBOX remove HMesh dependency - that is crazy.
40
 * - BBox visit child nodes in order of how far away the intersection point on
45
 * - BBox visit child nodes in order of how far away the intersection point on
41
 *        the bbox is. Closest nodes visited first. Cull nodes farther than
46
 *        the bbox is. Closest nodes visited first. Cull nodes farther than
42
 *        an actual intersection point.
47
 *        an actual intersection point.
43
 * - BBox Smooth interpolation of triangle normals. Straightforward.
48
 * - BBox Smooth interpolation of triangle normals. Straightforward.
44
 */
49
 */
45
					
50
					
46
 
51
 
47
namespace
52
namespace
48
{
53
{
49
  const int MAX_OBJECTS = 4;   // Maximum number of triangles in a BSP tree node
54
  const int MAX_OBJECTS = 4;   // Maximum number of triangles in a BSP tree node
50
  const int MAX_LEVEL = 20;    // Maximum number of BSP tree subdivisions
55
  const int MAX_LEVEL = 20;    // Maximum number of BSP tree subdivisions
51
 
56
 
52
  const unsigned int TEX_SIZE = 512;
57
  const unsigned int TEX_SIZE = 512;
53
 
58
 
54
  bool raytrace = false;
59
  bool raytrace = false;
55
  bool done = false;
60
  bool done = false;
56
  bool shadow = false;
61
  bool shadow = false;
57
 
62
 
58
  unsigned int winx = TEX_SIZE;     // Screen width
63
  unsigned int winx = TEX_SIZE;     // Screen width
59
  unsigned int winy = TEX_SIZE;     // Screen height
64
  unsigned int winy = TEX_SIZE;     // Screen height
60
 
65
 
61
  unsigned int PIXEL_SUBDIVS = 1;
66
  unsigned int PIXEL_SUBDIVS = 1;
62
 
67
 
63
  int mouse_state = GLUT_UP;
68
  int mouse_state = GLUT_UP;
64
  int mouse_button = 0;
69
  int mouse_button = 0;
65
  int spin_timer = 20;
70
  int spin_timer = 20;
66
 
71
 
67
  GLViewController *vctrl;
72
  GLViewController *vctrl;
68
 
73
 
69
  TriMesh mesh;
74
  TriMesh mesh;
70
  vector<const TriMesh*> mesh_vector(1, &mesh);
75
  vector<const TriMesh*> mesh_vector(1, &mesh);
71
  vector<Mat4x4f> transforms(1, identity_Mat4x4f());
76
  vector<Mat4x4f> transforms(1, identity_Mat4x4f());
72
 
77
 
73
  double light_pow = 1.0;
78
  double light_pow = 1.0;
74
  Vec3f light_dir = normalize(Vec3f(1.0, 1.0, 1.0));
79
  Vec3f light_dir = normalize(Vec3f(1.0, 1.0, 1.0));
75
  Vec3d background(0.8, 0.9, 1.0);
80
  Vec3d background(0.8, 0.9, 1.0);
76
 
81
 
77
  BSPTree tree;
82
  BSPTree tree;
78
  Camera* cam;
83
  Camera* cam;
79
 
84
 
80
  Vec3f image[TEX_SIZE][TEX_SIZE];
85
  Vec3f image[TEX_SIZE][TEX_SIZE];
81
  unsigned int image_tex;
86
  unsigned int image_tex;
82
 
87
 
83
  // Function to generate a random number between 0 and 1.
88
  // Function to generate a random number between 0 and 1.
84
  // gel_rand() returns a random number ranging from 0 to GEL_RAND_MAX.
89
  // gel_rand() returns a random number ranging from 0 to GEL_RAND_MAX.
85
  inline double my_random()
90
  inline double my_random()
86
  {
91
  {
87
    return gel_rand()/static_cast<double>(GEL_RAND_MAX);
92
    return gel_rand()/static_cast<double>(GEL_RAND_MAX);
88
  }
93
  }
89
		
94
		
90
  AABBTree bb_tree;		
95
  AABBTree bb_tree;		
91
}
96
}
92
 
97
 
93
void spin(int x);
98
void spin(int x);
94
 
99
 
95
//////////////////////////////////////////////////////////////
100
//////////////////////////////////////////////////////////////
96
//      I N I T I A L I Z A T I O N               
101
//      I N I T I A L I Z A T I O N               
97
//////////////////////////////////////////////////////////////
102
//////////////////////////////////////////////////////////////
98
 
103
 
99
void init_texture(unsigned int& tex)
104
void init_texture(unsigned int& tex)
100
{
105
{
101
  glGenTextures(1, reinterpret_cast<GLuint*>(&tex));
106
  glGenTextures(1, reinterpret_cast<GLuint*>(&tex));
102
 
107
 
103
  glBindTexture(GL_TEXTURE_2D, tex);
108
  glBindTexture(GL_TEXTURE_2D, tex);
104
 
109
 
105
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
110
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
106
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
111
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
107
 
112
 
108
  // load the texture image
113
  // load the texture image
109
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 
114
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 
110
	             TEX_SIZE, TEX_SIZE,
115
	             TEX_SIZE, TEX_SIZE,
111
	             0, GL_RGB, GL_FLOAT, image[0][0].get());
116
	             0, GL_RGB, GL_FLOAT, image[0][0].get());
112
}
117
}
113
 
118
 
114
void initRaytracer()
119
void initRaytracer()
115
{
120
{
116
  Vec3f c;
121
  Vec3f c;
117
  float r;
122
  float r;
118
  mesh.get_bsphere(c, r);
123
  mesh.get_bsphere(c, r);
119
  r *= 1.5;
124
  r *= 1.5;
120
 
125
 
121
  // Initialize track ball
126
  // Initialize track ball
122
  vctrl = new GLViewController(winx, winy, c, r);
127
  vctrl = new GLViewController(winx, winy, c, r);
123
 
128
 
124
  // Initialize corresponding camera
129
  // Initialize corresponding camera
125
  cam = new Camera(c - Vec3f(r), c, Vec3f(0.0f, 1.0f, 0.0f), 1.0f);
130
  cam = new Camera(c - Vec3f(r), c, Vec3f(0.0f, 1.0f, 0.0f), 1.0f);
126
 
131
 
127
#ifdef USE_BSP
132
#ifdef USE_BSP
128
   cout << "Constructing BSP tree..." << endl;
133
   cout << "Constructing BSP tree..." << endl;
129
   tree.init(mesh_vector, transforms, MAX_OBJECTS, MAX_LEVEL);
134
   tree.init(mesh_vector, transforms, MAX_OBJECTS, MAX_LEVEL);
130
   tree.build();
135
   tree.build();
131
#else
136
#else
132
	// AABB TREE
137
	// AABB TREE
133
	Manifold m;
138
	Manifold m;
134
	vector<int> faces(mesh.geometry.no_faces(), 3);
139
	vector<int> faces(mesh.geometry.no_faces(), 3);
135
	cout << "Creating manifold" << endl;
140
	cout << "Creating manifold" << endl;
136
    m.build(mesh.geometry.no_vertices(),
141
    m.build(mesh.geometry.no_vertices(),
137
            reinterpret_cast<const float*>(&mesh.geometry.vertex(0)),
142
            reinterpret_cast<const float*>(&mesh.geometry.vertex(0)),
138
            faces.size(),
143
            faces.size(),
139
            &faces[0],
144
            &faces[0],
140
            reinterpret_cast<const int*>(&mesh.geometry.face(0)));
145
            reinterpret_cast<const int*>(&mesh.geometry.face(0)));
141
	cout << "Building tree" << endl;
146
	cout << "Building tree" << endl;
142
	build_AABBTree(m, bb_tree);
147
	build_AABBTree(m, bb_tree);
143
#endif
148
#endif
144
}
149
}
145
 
150
 
146
void initGL()
151
void initGL()
147
{
152
{
148
  glShadeModel(GL_SMOOTH); 
153
  glShadeModel(GL_SMOOTH); 
149
  glDisable(GL_CULL_FACE);
154
  glDisable(GL_CULL_FACE);
150
  glFrontFace(GL_CCW);
155
  glFrontFace(GL_CCW);
151
 
156
 
152
  glClearColor(1.0, 1.0, 1.0, 1.0);
157
  glClearColor(1.0, 1.0, 1.0, 1.0);
153
  glColor3f(0.0, 0.0, 0.0);
158
  glColor3f(0.0, 0.0, 0.0);
154
}
159
}
155
 
160
 
156
 
161
 
157
//////////////////////////////////////////////////////////////
162
//////////////////////////////////////////////////////////////
158
//      S H A D E   F U N C T I O N S
163
//      S H A D E   F U N C T I O N S
159
//////////////////////////////////////////////////////////////
164
//////////////////////////////////////////////////////////////
160
 
165
 
161
double shadow_shade(Ray& r)
166
double shadow_shade(Ray& r)
162
{
167
{
163
  r.compute_position();
168
  r.compute_position();
164
  Ray shadow(r.hit_pos, light_dir);
169
  Ray shadow(r.hit_pos, light_dir);
165
  double s = tree.intersect(shadow) ? 0.0 : 1.0;
170
  double s = tree.intersect(shadow) ? 0.0 : 1.0;
166
 
171
 
167
  r.compute_normal();
172
  r.compute_normal();
168
  return s*light_pow*dot(r.hit_normal, light_dir);
173
  return s*light_pow*dot(r.hit_normal, light_dir);
169
}
174
}
170
 
175
 
171
double lambertian_shade(Ray& r)
176
double lambertian_shade(Ray& r)
172
{
177
{
173
#ifdef USE_BSP
178
#ifdef USE_BSP
174
	r.compute_normal();
179
	r.compute_normal();
175
#endif
180
#endif
176
  return light_pow*dot(r.hit_normal, light_dir);
181
  return light_pow*dot(r.hit_normal, light_dir);
177
}
182
}
178
 
183
 
179
double (*shade_ray[2])(Ray&) = { lambertian_shade,
184
double (*shade_ray[2])(Ray&) = { lambertian_shade,
180
				                         shadow_shade      };
185
				                         shadow_shade      };
181
 
186
 
182
//////////////////////////////////////////////////////////////
187
//////////////////////////////////////////////////////////////
183
//      D R A W   F U N C T I O N S
188
//      D R A W   F U N C T I O N S
184
//////////////////////////////////////////////////////////////
189
//////////////////////////////////////////////////////////////
185
 
190
 
186
/*
191
/*
187
void enable_textures(TriMesh& tm)
192
void enable_textures(TriMesh& tm)
188
{
193
{
189
  for(unsigned int i=0;i<tm.materials.size(); ++i)
194
  for(unsigned int i=0;i<tm.materials.size(); ++i)
190
  {
195
  {
191
    Material& mat = tm.materials[i];
196
    Material& mat = tm.materials[i];
192
    if(mat.tex_name != "")
197
    if(mat.tex_name != "")
193
    {
198
    {
194
      string name = mat.tex_path + mat.tex_name;
199
      string name = mat.tex_path + mat.tex_name;
195
      
200
      
196
      GLuint tex_id;
201
      GLuint tex_id;
197
      if(load_image_into_texture(name, tex_id))
202
      if(load_image_into_texture(name, tex_id))
198
	mat.tex_id = tex_id;
203
	mat.tex_id = tex_id;
199
    }
204
    }
200
  }
205
  }
201
}
206
}
202
*/
207
*/
203
 
208
 
204
void set_perspective_proj()
209
void set_perspective_proj()
205
{
210
{
206
  glMatrixMode(GL_PROJECTION);	 
211
  glMatrixMode(GL_PROJECTION);	 
207
  glLoadIdentity();            
212
  glLoadIdentity();            
208
 
213
 
209
  gluPerspective(64.0, 1.0, 0.1, 1000.0);
214
  gluPerspective(64.0, 1.0, 0.1, 1000.0);
210
 
215
 
211
  glMatrixMode(GL_MODELVIEW);
216
  glMatrixMode(GL_MODELVIEW);
212
}
217
}
213
 
218
 
214
void set_ortho_proj()
219
void set_ortho_proj()
215
{
220
{
216
  glMatrixMode(GL_PROJECTION);	 
221
  glMatrixMode(GL_PROJECTION);	 
217
  glLoadIdentity();             
222
  glLoadIdentity();             
218
    
223
    
219
  glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
224
  glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
220
 
225
 
221
  glMatrixMode(GL_MODELVIEW);  
226
  glMatrixMode(GL_MODELVIEW);  
222
}
227
}
223
 
228
 
224
void draw_texture(unsigned int tex)
229
void draw_texture(unsigned int tex)
225
{
230
{
226
  static GLfloat verts[] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
231
  static GLfloat verts[] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
227
 
232
 
228
  glColor4f(1.0, 1.0, 1.0, 1.0);
233
  glColor4f(1.0, 1.0, 1.0, 1.0);
229
 
234
 
230
  glBindTexture(GL_TEXTURE_2D, tex);
235
  glBindTexture(GL_TEXTURE_2D, tex);
231
  glEnable(GL_TEXTURE_2D);
236
  glEnable(GL_TEXTURE_2D);
232
 
237
 
233
  glEnableClientState(GL_VERTEX_ARRAY);
238
  glEnableClientState(GL_VERTEX_ARRAY);
234
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
239
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
235
  
240
  
236
  glVertexPointer(2, GL_FLOAT, 0, verts);
241
  glVertexPointer(2, GL_FLOAT, 0, verts);
237
  glTexCoordPointer(2, GL_FLOAT, 0, verts);
242
  glTexCoordPointer(2, GL_FLOAT, 0, verts);
238
 
243
 
239
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
244
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
240
 
245
 
241
  glDisableClientState(GL_VERTEX_ARRAY);
246
  glDisableClientState(GL_VERTEX_ARRAY);
242
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
247
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
243
 
248
 
244
  glDisable(GL_TEXTURE_2D);
249
  glDisable(GL_TEXTURE_2D);
245
}
250
}
246
 
251
 
247
void drawOBJ()
252
void drawOBJ()
248
{
253
{
249
  static bool washere = false;
254
  static bool washere = false;
250
  static unsigned int disp_list;
255
  static unsigned int disp_list;
251
 
256
 
252
  if(!washere)
257
  if(!washere)
253
  {
258
  {
254
    disp_list = glGenLists(1);
259
    disp_list = glGenLists(1);
255
    glNewList(disp_list, GL_COMPILE);
260
    glNewList(disp_list, GL_COMPILE);
256
 
261
 
257
    glBegin(GL_TRIANGLES);
262
    glBegin(GL_TRIANGLES);
258
    for(int i = 0; i < mesh.geometry.no_faces(); ++i)
263
    for(int i = 0; i < mesh.geometry.no_faces(); ++i)
259
    {
264
    {
260
      Vec3i n_face = mesh.normals.face(i);
265
      Vec3i n_face = mesh.normals.face(i);
261
      Vec3i g_face = mesh.geometry.face(i);
266
      Vec3i g_face = mesh.geometry.face(i);
262
      for(int j=0;j<3;j++)
267
      for(int j=0;j<3;j++)
263
      {
268
      {
264
        double shade = 0.5;
269
        double shade = 0.5;
265
 
270
 
266
        if(n_face != Geometry::NULL_FACE)
271
        if(n_face != Geometry::NULL_FACE)
267
        {
272
        {
268
          Vec3f norm = normalize(mesh.normals.vertex(n_face[j]));
273
          Vec3f norm = normalize(mesh.normals.vertex(n_face[j]));
269
          glNormal3fv(norm.get());
274
          glNormal3fv(norm.get());
270
          shade = light_pow*dot(norm, light_dir);
275
          shade = light_pow*dot(norm, light_dir);
271
        }
276
        }
272
 
277
 
273
        glColor3d(shade, shade, shade);
278
        glColor3d(shade, shade, shade);
274
        Vec3f vert = mesh.geometry.vertex(g_face[j]);	  
279
        Vec3f vert = mesh.geometry.vertex(g_face[j]);	  
275
        glVertex3fv(vert.get());
280
        glVertex3fv(vert.get());
276
      }
281
      }
277
    }
282
    }
278
    glEnd();
283
    glEnd();
279
 
284
 
280
    glEndList();
285
    glEndList();
281
    washere = true;
286
    washere = true;
282
  }
287
  }
283
  glCallList(disp_list);
288
  glCallList(disp_list);
284
}
289
}
285
 
290
 
286
 
291
 
287
//////////////////////////////////////////////////////////////
292
//////////////////////////////////////////////////////////////
288
//      G L U T   C A L L B A C K   F U N C T I O N S                 
293
//      G L U T   C A L L B A C K   F U N C T I O N S                 
289
//////////////////////////////////////////////////////////////
294
//////////////////////////////////////////////////////////////
290
 
295
 
291
void display()
296
void display()
292
{
297
{
293
  static bool first = true;
298
  static bool first = true;
294
 
299
 
295
  if(first)
300
  if(first)
296
  {
301
  {
297
    first = false;
302
    first = false;
298
    glutTimerFunc(spin_timer, spin, 0);
303
    glutTimerFunc(spin_timer, spin, 0);
299
  }
304
  }
300
 
305
 
301
  Vec3f eye, focus, up;
306
  Vec3f eye, focus, up;
302
	vctrl->get_view_param(eye, focus, up);
307
	vctrl->get_view_param(eye, focus, up);
303
	cam->set(eye, focus, up, cam->get_focal_dist());
308
	cam->set(eye, focus, up, cam->get_focal_dist());
304
	
309
	
305
  if(raytrace)
310
  if(raytrace)
306
  {
311
  {
307
    raytrace = false;
312
    raytrace = false;
308
    
313
    
309
    cout << "Raytracing";
314
    cout << "Raytracing";
310
    float win_to_vp = 1.0f/static_cast<float>(TEX_SIZE);
315
    float win_to_vp = 1.0f/static_cast<float>(TEX_SIZE);
311
    float lowerleft = 0.5 + win_to_vp*0.5;
316
    float lowerleft = 0.5 + win_to_vp*0.5;
312
    float step = win_to_vp/static_cast<float>(PIXEL_SUBDIVS);
317
    float step = win_to_vp/static_cast<float>(PIXEL_SUBDIVS);
313
    
318
    
314
    vector<Vec2f> jitter(PIXEL_SUBDIVS*PIXEL_SUBDIVS); 
319
    vector<Vec2f> jitter(PIXEL_SUBDIVS*PIXEL_SUBDIVS); 
315
    for(unsigned int i = 0; i < PIXEL_SUBDIVS; ++i)
320
    for(unsigned int i = 0; i < PIXEL_SUBDIVS; ++i)
316
      for(unsigned int j = 0; j < PIXEL_SUBDIVS; ++j)
321
      for(unsigned int j = 0; j < PIXEL_SUBDIVS; ++j)
317
      {
322
      {
318
	      jitter[i*PIXEL_SUBDIVS + j][0] = (my_random() + (j%PIXEL_SUBDIVS))*step; 
323
	      jitter[i*PIXEL_SUBDIVS + j][0] = (my_random() + (j%PIXEL_SUBDIVS))*step; 
319
	      jitter[i*PIXEL_SUBDIVS + j][1] = (my_random() + (i%PIXEL_SUBDIVS))*step; 
324
	      jitter[i*PIXEL_SUBDIVS + j][1] = (my_random() + (i%PIXEL_SUBDIVS))*step; 
320
      }
325
      }
321
 
326
 
322
    Util::Timer tim;
327
    Util::Timer tim;
323
    tim.start();
328
    tim.start();
324
    for(unsigned int i = 0; i < TEX_SIZE; ++i)
329
    for(unsigned int i = 0; i < TEX_SIZE; ++i)
325
    {
330
    {
326
	    for(unsigned int j = 0; j < TEX_SIZE; ++j)
331
	    for(unsigned int j = 0; j < TEX_SIZE; ++j)
327
	    {
332
	    {
328
		    Vec3d sum(0.0f);
333
		    Vec3d sum(0.0f);
329
		    Vec2f vp_pos(j*win_to_vp - lowerleft, i*win_to_vp - lowerleft);
334
		    Vec2f vp_pos(j*win_to_vp - lowerleft, i*win_to_vp - lowerleft);
330
    	
335
    	
331
		    for(unsigned int ky = 0; ky < PIXEL_SUBDIVS; ++ky)
336
		    for(unsigned int ky = 0; ky < PIXEL_SUBDIVS; ++ky)
332
		      for(unsigned int kx = 0; kx < PIXEL_SUBDIVS; ++kx)
337
		      for(unsigned int kx = 0; kx < PIXEL_SUBDIVS; ++kx)
333
		      {
338
		      {
334
			      Ray r = cam->get_ray(vp_pos + jitter[ky*PIXEL_SUBDIVS + kx]);
339
			      Ray r = cam->get_ray(vp_pos + jitter[ky*PIXEL_SUBDIVS + kx]);
335
 
340
 
336
#ifdef USE_BSP
341
#ifdef USE_BSP
337
			      if(tree.intersect(r))
342
			      if(tree.intersect(r))
338
			        sum += Vec3d(shade_ray[shadow](r));
343
			        sum += Vec3d(shade_ray[shadow](r));
339
			      else
344
			      else
340
			        sum += background;
345
			        sum += background;
341
#else
346
#else
342
			      float t = FLT_MAX;
347
			      float t = FLT_MAX;
343
			      bb_tree.intersect(r);
348
			      bb_tree.intersect(r);
344
			      if(r.has_hit)
349
			      if(r.has_hit)
345
			        sum += Vec3d(shade_ray[0](r));
350
			        sum += Vec3d(shade_ray[0](r));
346
			      else 
351
			      else 
347
			        sum += background;
352
			        sum += background;
348
#endif		
353
#endif		
349
		      }
354
		      }
350
	      image[i][j] = Vec3f(sum/static_cast<double>(PIXEL_SUBDIVS*PIXEL_SUBDIVS));
355
	      image[i][j] = Vec3f(sum/static_cast<double>(PIXEL_SUBDIVS*PIXEL_SUBDIVS));
351
      }
356
      }
352
      if(((i + 1) % 50) == 0) cerr << ".";
357
      if(((i + 1) % 50) == 0) cerr << ".";
353
    }
358
    }
354
	  cout << " - " << tim.get_secs() << " secs " << endl;
359
	  cout << " - " << tim.get_secs() << " secs " << endl;
355
    cout << endl;
360
    cout << endl;
356
 
361
 
357
    init_texture(image_tex);
362
    init_texture(image_tex);
358
 
363
 
359
    done = true;
364
    done = true;
360
  }
365
  }
361
 
366
 
362
  if(done)
367
  if(done)
363
  {
368
  {
364
    set_ortho_proj();
369
    set_ortho_proj();
365
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
370
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
366
    glLoadIdentity();
371
    glLoadIdentity();
367
 
372
 
368
    draw_texture(image_tex);
373
    draw_texture(image_tex);
369
  }
374
  }
370
  else
375
  else
371
  {
376
  {
372
    glEnable(GL_DEPTH_TEST);
377
    glEnable(GL_DEPTH_TEST);
373
 
378
 
374
    cam->glSetPerspective(winx, winy);
379
    cam->glSetPerspective(winx, winy);
375
 
380
 
376
    glClearColor(background[0], background[1], background[2], 1.0);
381
    glClearColor(background[0], background[1], background[2], 1.0);
377
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
382
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
378
    glLoadIdentity();
383
    glLoadIdentity();
379
 
384
 
380
	  cam->glSetCamera();
385
	  cam->glSetCamera();
381
    
386
    
382
    glColor3f(0.5, 0.5, 0.5);
387
    glColor3f(0.5, 0.5, 0.5);
383
    drawOBJ();
388
    drawOBJ();
384
 
389
 
385
    glDisable(GL_DEPTH_TEST);
390
    glDisable(GL_DEPTH_TEST);
386
  }
391
  }
387
 
392
 
388
  glutSwapBuffers();  
393
  glutSwapBuffers();  
389
}
394
}
390
 
395
 
391
void reshape(int w, int h)
396
void reshape(int w, int h)
392
{
397
{
393
  winx = w; winy = h;
398
  winx = w; winy = h;
394
 
399
 
395
  vctrl->reshape(winx, winy);
400
  vctrl->reshape(winx, winy);
396
 
401
 
397
  glViewport(0, 0, winx, winy);
402
  glViewport(0, 0, winx, winy);
398
}
403
}
399
 
404
 
400
void keyboard(unsigned char key, int x, int y)
405
void keyboard(unsigned char key, int x, int y)
401
{
406
{
402
  switch(key)
407
  switch(key)
403
  {
408
  {
404
  case '+':
409
  case '+':
405
    ++PIXEL_SUBDIVS;
410
    ++PIXEL_SUBDIVS;
406
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
411
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
407
    break;
412
    break;
408
  case '-':
413
  case '-':
409
    if(PIXEL_SUBDIVS > 1)
414
    if(PIXEL_SUBDIVS > 1)
410
      --PIXEL_SUBDIVS;
415
      --PIXEL_SUBDIVS;
411
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
416
    cout << "Rays per pixel: " << PIXEL_SUBDIVS*PIXEL_SUBDIVS << endl;
412
    break;
417
    break;
413
  case 'r':
418
  case 'r':
414
    if(done) done = false;
419
    if(done) done = false;
415
    else raytrace = true;
420
    else raytrace = true;
416
    break;
421
    break;
417
  case 's':
422
  case 's':
418
    shadow = !shadow;
423
    shadow = !shadow;
419
    cout << "Shadow " << (shadow ? "on." : "off.") << endl;
424
    cout << "Shadow " << (shadow ? "on." : "off.") << endl;
420
    break;
425
    break;
421
  case 27:
426
  case 27:
422
    delete vctrl;
427
    delete vctrl;
423
    delete cam;
428
    delete cam;
424
    exit(0);
429
    exit(0);
425
  }
430
  }
426
}
431
}
427
 
432
 
428
void mouse(int btn, int state, int x, int y)
433
void mouse(int btn, int state, int x, int y)
429
{
434
{
430
  if(state == GLUT_DOWN) 
435
  if(state == GLUT_DOWN) 
431
  {
436
  {
432
    if(btn == GLUT_LEFT_BUTTON) 
437
    if(btn == GLUT_LEFT_BUTTON) 
433
      vctrl->grab_ball(ROTATE_ACTION, Vec2i(x, y));
438
      vctrl->grab_ball(ROTATE_ACTION, Vec2i(x, y));
434
    else if(btn == GLUT_MIDDLE_BUTTON) 
439
    else if(btn == GLUT_MIDDLE_BUTTON) 
435
      vctrl->grab_ball(ZOOM_ACTION, Vec2i(x, y));
440
      vctrl->grab_ball(ZOOM_ACTION, Vec2i(x, y));
436
    else if(btn == GLUT_RIGHT_BUTTON) 
441
    else if(btn == GLUT_RIGHT_BUTTON) 
437
      vctrl->grab_ball(PAN_ACTION, Vec2i(x, y));
442
      vctrl->grab_ball(PAN_ACTION, Vec2i(x, y));
438
  }
443
  }
439
  else if(state == GLUT_UP)
444
  else if(state == GLUT_UP)
440
			vctrl->release_ball();
445
			vctrl->release_ball();
441
 
446
 
442
  mouse_state = state;
447
  mouse_state = state;
443
  mouse_button = btn;
448
  mouse_button = btn;
444
 
449
 
445
  glutPostRedisplay();
450
  glutPostRedisplay();
446
}
451
}
447
 
452
 
448
void move(int x, int y)
453
void move(int x, int y)
449
{
454
{
450
    vctrl->roll_ball(Vec2i(x, y));
455
    vctrl->roll_ball(Vec2i(x, y));
451
 
456
 
452
  glutPostRedisplay();
457
  glutPostRedisplay();
453
}
458
}
454
 
459
 
455
void spin(int x)
460
void spin(int x)
456
{
461
{
457
  vctrl->try_spin();
462
  vctrl->try_spin();
458
  glutTimerFunc(spin_timer, spin, 0);  
463
  glutTimerFunc(spin_timer, spin, 0);  
459
  glutPostRedisplay();
464
  glutPostRedisplay();
460
}
465
}
461
 
466
 
462
 
467
 
463
//////////////////////////////////////////////////////////////
468
//////////////////////////////////////////////////////////////
464
//                        M A I N
469
//                        M A I N
465
//////////////////////////////////////////////////////////////
470
//////////////////////////////////////////////////////////////
466
 
471
 
467
int main(int argc, char** argv)
472
int main(int argc, char** argv)
468
{
473
{
469
#ifdef __BORLANDC__
474
#ifdef __BORLANDC__
470
  _control87(MCW_EM, MCW_EM);  // Borland C++ will crash OpenGL if this
475
  _control87(MCW_EM, MCW_EM);  // Borland C++ will crash OpenGL if this
471
                               // magic line is not inserted
476
                               // magic line is not inserted
472
#endif
477
#endif
473
 
478
 
474
  glutInit(&argc, argv);
479
  glutInit(&argc, argv);
475
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
480
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
476
  glutInitWindowSize(winx, winy);
481
  glutInitWindowSize(winx, winy);
477
  glutCreateWindow("Press 'r' to raytrace");
482
  glutCreateWindow("Press 'r' to raytrace");
478
  glutDisplayFunc(display);
483
  glutDisplayFunc(display);
479
  glutReshapeFunc(reshape);
484
  glutReshapeFunc(reshape);
480
  glutKeyboardFunc(keyboard);
485
  glutKeyboardFunc(keyboard);
481
  glutMouseFunc(mouse);
486
  glutMouseFunc(mouse);
482
  glutMotionFunc(move);
487
  glutMotionFunc(move);
483
 
488
 
484
  // LOAD OBJ
489
  // LOAD OBJ
485
  string filename;
490
  string filename;
486
  if(argc > 1)
491
  if(argc > 1)
487
  {
492
  {
488
    filename = argv[1];
493
    filename = argv[1];
489
    cout << "Loading " << filename << endl;
494
    cout << "Loading " << filename << endl;
490
 
495
 
491
    obj_load(filename, mesh);
496
    obj_load(filename, mesh);
492
    //if(!mesh.has_normals())
497
    //if(!mesh.has_normals())
493
    //{
498
    //{
494
      cout << "Computing normals" << endl;
499
      cout << "Computing normals" << endl;
495
      mesh.compute_normals();
500
      mesh.compute_normals();
496
    //}
501
    //}
497
 
502
 
498
    cout << "No. of triangles: " << mesh.geometry.no_faces() << endl;
503
    cout << "No. of triangles: " << mesh.geometry.no_faces() << endl;
499
  }
504
  }
500
  else
505
  else
501
  {
506
  {
502
    obj_load("../../../data/dolphins.obj", mesh);
507
    obj_load("../../data/dolphins.obj", mesh);
503
 
508
 
504
  	cout << "Computing normals" << endl;
509
  	cout << "Computing normals" << endl;
505
    mesh.compute_normals();
510
    mesh.compute_normals();
506
 
511
 
507
    //cout << "Usage: raytrace any_object.obj";
512
    //cout << "Usage: raytrace any_object.obj";
508
    //exit(0);
513
    //exit(0);
509
  }
514
  }
510
 
515
 
511
  initRaytracer();
516
  initRaytracer();
512
  initGL();    
517
  initGL();    
513
 
518
 
514
  glutMainLoop();
519
  glutMainLoop();
515
 
520
 
516
  return 0;
521
  return 0;
517
}
522
}
518
 
523
 
519
 
524