Subversion Repositories gelsvn

Rev

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

Rev 307 Rev 308
Line 8... Line 8...
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
Line 64... Line 72...
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
//////////////////////////////////////////////////////////////
Line 106... Line 118...
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); 
Line 138... Line 164...
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      };
Line 285... Line 313...
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;