Subversion Repositories gelsvn

Rev

Rev 653 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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