Subversion Repositories gelsvn

Rev

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

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