Subversion Repositories gelsvn

Rev

Rev 346 | Rev 350 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 346 Rev 349
Line 6... Line 6...
6
using namespace Geometry;
6
using namespace Geometry;
7
using namespace CGLA;
7
using namespace CGLA;
8
 
8
 
9
scene::scene(void) : cam_(0)
9
scene::scene(void) : cam_(0)
10
{
10
{
11
	accel_ = new BSPTree;
11
    accel_ = new BSPTree;
12
}
12
}
13
 
13
 
14
scene::~scene(void)
14
scene::~scene(void)
15
{
15
{
16
	delete accel_;
16
    delete accel_;
17
}
17
}
18
 
18
 
19
void scene::insert(const luminaire* obj)
19
void scene::insert(const luminaire* obj)
20
{
20
{
21
	objs_.push_back(obj);
21
    objs_.push_back(obj);
22
}
22
}
23
 
23
 
24
size_t scene::luminaires(void) const
24
size_t scene::luminaires(void) const
25
{
25
{
26
	return objs_.size();
26
    return objs_.size();
27
}
27
}
28
 
28
 
29
const luminaire* scene::get_luminaire(size_t i) const
29
const luminaire* scene::get_luminaire(size_t i) const
30
{
30
{
31
	return objs_.at(i);
31
    return objs_.at(i);
32
}
32
}
33
 
33
 
34
bool scene::intersect(const ray& r)
34
bool scene::intersect(const ray& r)
35
{
35
{
36
	Ray s;
36
    Ray s;
37
	s.direction = r.direction;
37
    s.direction = r.direction;
38
	s.origin = r.origin;
38
    s.origin = r.origin;
39
	s.dist = r.distance;
39
    s.dist = r.distance;
40
	s.trace_depth = r.depth;
40
    s.trace_depth = r.depth;
41
 
41
 
42
	return accel_->intersect(s);
42
    return accel_->intersect(s);
43
}
43
}
44
 
44
 
45
bool scene::intersect(const ray& r, hit_info& hi)
45
bool scene::intersect(const ray& r, hit_info& hi)
46
{
46
{
-
 
47
    printf("%.2f, %.2f, %.2f\n", r.direction[0],
-
 
48
           r.direction[1], r.direction[2]);
-
 
49
    printf("%.2f, %.2f, %.2f\n", r.origin[0], r.origin[1], r.origin[2]);
-
 
50
 
-
 
51
    printf("%.2f, %i\n", r.distance, r.depth);
-
 
52
 
-
 
53
 
47
	Ray s;
54
    Ray s;
48
	s.direction = r.direction;
55
    s.direction = r.direction;
49
	s.origin = r.origin;
56
    s.origin = r.origin;
50
	s.trace_depth = r.depth;
57
    s.trace_depth = r.depth;
51
	s.dist = r.distance;
58
    s.dist = r.distance;
52
 
59
 
53
	bool hit = accel_->intersect(s);
60
    bool hit = accel_->intersect(s);
54
 
61
 
55
	if (hit)
62
    if (hit)
56
	{
63
    {
57
		//find the mesh which was hit
64
        //find the mesh which was hit
58
		const mesh& msh = dynamic_cast<const mesh&>(*objs_[s.id]);
65
        const mesh& msh = dynamic_cast<const mesh&>(*objs_[s.id]);
59
 
66
 
60
		//set hit position and normals
67
        //set hit position and normals
61
		hi.distance = s.dist;
68
        hi.distance = s.dist;
62
		hi.position = s.hit_pos;
69
        hi.position = s.hit_pos;
63
		hi.shading_normal = s.hit_normal;
70
        hi.shading_normal = s.hit_normal;
64
 
71
 
65
		const IndexedFaceSet& geometry = msh.get_trimesh().geometry;
72
        const IndexedFaceSet& geometry = msh.get_trimesh().geometry;
66
		const Vec3i& f  = geometry.face(s.hit_face_id);
73
        const Vec3i& f  = geometry.face(s.hit_face_id);
67
		const Vec3f p0 = geometry.vertex(f[0]);
74
        const Vec3f p0 = geometry.vertex(f[0]);
68
		const Vec3f a  = geometry.vertex(f[1]) - p0;
75
        const Vec3f a  = geometry.vertex(f[1]) - p0;
69
		const Vec3f b  = geometry.vertex(f[2]) - p0;
76
        const Vec3f b  = geometry.vertex(f[2]) - p0;
70
		Vec3f face_normal = cross(a,b);
77
        Vec3f face_normal = cross(a,b);
71
		float l = sqr_length(face_normal);
78
        float l = sqr_length(face_normal);
72
		if(l > 0.0f)
79
        if(l > 0.0f)
73
			face_normal /= sqrt(l);
80
            face_normal /= sqrt(l);
74
 
81
 
75
		hi.geometric_normal = msh.transform().mul_3D_vector(face_normal);
82
        hi.geometric_normal = msh.transform().mul_3D_vector(face_normal);
76
		//hi.geometric_normal = msh.triangle_normal(s.hit_face_id);
83
        //hi.geometric_normal = msh.triangle_normal(s.hit_face_id);
77
 
84
 
78
		hi.inside = dot(hi.geometric_normal, -s.direction) < 0.f;
85
        hi.inside = dot(hi.geometric_normal, -s.direction) < 0.f;
79
		if (hi.inside)
86
        if (hi.inside)
80
		{
87
        {
81
			hi.geometric_normal = -hi.geometric_normal;
88
            hi.geometric_normal = -hi.geometric_normal;
82
			hi.shading_normal = -hi.shading_normal;
89
            hi.shading_normal = -hi.shading_normal;
83
		}
90
        }
84
 
91
 
85
		bool has_texcoords = msh.get_trimesh().texcoords.no_faces() > 0;
92
        bool has_texcoords = msh.get_trimesh().texcoords.no_faces() > 0;
86
 
93
 
87
		if (has_texcoords)
94
        if (has_texcoords)
88
		{
95
        {
89
			//compute texture coords..
96
            //compute texture coords..
90
			const IndexedFaceSet& texcoords = msh.get_trimesh().texcoords;
97
            const IndexedFaceSet& texcoords = msh.get_trimesh().texcoords;
91
			const Vec3i& t = texcoords.face(s.hit_face_id);
98
            const Vec3i& t = texcoords.face(s.hit_face_id);
92
			Vec3f uv0 = texcoords.vertex(t[0]);
99
            Vec3f uv0 = texcoords.vertex(t[0]);
93
			Vec3f uv1 = texcoords.vertex(t[1]);
100
            Vec3f uv1 = texcoords.vertex(t[1]);
94
			Vec3f uv2 = texcoords.vertex(t[2]);
101
            Vec3f uv2 = texcoords.vertex(t[2]);
95
			hi.texcoords(0) = (1.f-s.u-s.v)*uv0(0) + s.u*uv1(0) + s.v*uv2(0);
102
            hi.texcoords(0) = (1.f-s.u-s.v)*uv0(0) + s.u*uv1(0) + s.v*uv2(0);
96
			hi.texcoords(1) = (1.f-s.u-s.v)*uv0(1) + s.u*uv1(1) + s.v*uv2(1);
103
            hi.texcoords(1) = (1.f-s.u-s.v)*uv0(1) + s.u*uv1(1) + s.v*uv2(1);
97
 
104
 
98
			//compute tangent vectors..
105
            //compute tangent vectors..
99
 
106
 
100
 
107
 
101
		}
108
        }
102
		else
109
        else
103
		{
110
        {
104
			//hmm.. no uvs. not good!
111
            //hmm.. no uvs. not good!
105
			hi.texcoords = Vec2f(0.f);
112
            hi.texcoords = Vec2f(0.f);
106
			//orthogonal(hi.shading_normal, hi.tangent, hi.bitangent);
113
            //orthogonal(hi.shading_normal, hi.tangent, hi.bitangent);
107
		}
114
        }
108
 
115
 
109
		//sample material to get bsdf
116
        //sample material to get bsdf
110
		const material* mat = msh.get_material();
117
        const material* mat = msh.get_material();
111
		if (mat)
118
        if (mat)
112
			mat->sample(r, hi);
119
            mat->sample(r, hi);
113
	
120
 
114
		//convert from radiant exitance to radiance
121
        //convert from radiant exitance to radiance
115
		if (!hi.inside)
122
        if (!hi.inside)
116
			hi.emitted = msh.exitance() / float(M_PI);
123
            hi.emitted = msh.exitance() / float(M_PI);
117
	}
124
    }
118
 
125
 
119
	return hit;
126
    return hit;
120
}
127
}
121
 
128
 
122
const camera* scene::get_camera(void) const
129
const camera* scene::get_camera(void) const
123
{
130
{
124
	return cam_;
131
    return cam_;
125
}
132
}
126
 
133
 
127
void scene::set_camera(const camera* c)
134
void scene::set_camera(const camera* c)
128
{
135
{
129
	cam_ = c;
136
    cam_ = c;
130
}
137
}
131
 
138
 
132
void scene::initialize(int mo, int md)
139
void scene::initialize(int mo, int md)
133
{
140
{
134
	assert(cam_);
141
    assert(cam_);
135
	assert(!objs_.empty());
142
    assert(!objs_.empty());
136
 
143
 
137
	std::vector<const TriMesh*> msh;
144
    std::vector<const TriMesh*> msh;
138
	std::vector<Mat4x4f> trs;
145
    std::vector<Mat4x4f> trs;
139
 
146
 
140
	std::vector<const luminaire*>::iterator it;
147
    std::vector<const luminaire*>::iterator it;
141
	for (it=objs_.begin(); it!=objs_.end(); ++it)
148
    for (it=objs_.begin(); it!=objs_.end(); ++it)
142
	{
149
    {
143
		const luminaire* obj = *it;
150
        const luminaire* obj = *it;
144
		
151
 
145
		const mesh* mxx = dynamic_cast<const mesh*>(obj);
152
        const mesh* mxx = dynamic_cast<const mesh*>(obj);
146
		if (!mxx)
153
        if (!mxx)
147
			continue;
154
            continue;
148
 
155
 
149
		msh.push_back(&mxx->get_trimesh());	
156
        msh.push_back(&mxx->get_trimesh());
150
		trs.push_back(mxx->transform());	
157
        trs.push_back(mxx->transform());
151
	}
158
    }
152
 
159
 
153
	accel_->init(msh, trs, mo, md);
160
    accel_->init(msh, trs, mo, md);
154
	accel_->build();
161
    accel_->build();
155
}
162
}
156
 
163
 
157
//02566 framework, Anders Wang Kristensen, awk@imm.dtu.dk, 2007
164
//02566 framework, Anders Wang Kristensen, awk@imm.dtu.dk, 2007