Subversion Repositories gelsvn

Rev

Rev 307 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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