Subversion Repositories gelsvn

Rev

Rev 346 | Rev 350 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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