Subversion Repositories gelsvn

Rev

Rev 554 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 554 Rev 595
-
 
1
/* ----------------------------------------------------------------------- *
-
 
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
-
 
3
 * Copyright (C) the authors and DTU Informatics
-
 
4
 * For license and list of authors, see ../../doc/intro.pdf
-
 
5
 * ----------------------------------------------------------------------- */
-
 
6
 
-
 
7
/**
-
 
8
 * @file Ray.h
-
 
9
 * @brief A ray class for ray tracing.
-
 
10
 */
-
 
11
 
1
#ifndef __GEOMETRY_RAY_H__
12
#ifndef __GEOMETRY_RAY_H__
2
#define __GEOMETRY_RAY_H__
13
#define __GEOMETRY_RAY_H__
3
 
14
 
4
#include "../CGLA/Vec3i.h"
15
#include "../CGLA/Vec3i.h"
5
#include "../CGLA/Vec3f.h"
16
#include "../CGLA/Vec3f.h"
6
#include "TriMesh.h"
17
#include "TriMesh.h"
7
#include "Material.h"
18
#include "Material.h"
8
 
19
 
9
namespace Geometry 
20
namespace Geometry 
10
{
21
{
11
    const double d_eps = 1.0e-12;
22
    const double d_eps = 1.0e-12;
12
    const float f_eps = 1.0e-6f;
23
    const float f_eps = 1.0e-6f;
13
 
24
 
-
 
25
    /// Represents a ray as used for ray tracing.
14
    struct Ray 
26
    struct Ray 
15
    {
27
    {
16
        // Constructor
28
        // Constructor
17
        Ray() 
29
        Ray() 
18
          : origin(0.0f), direction(0.0f), hit_pos(0.0f), hit_normal(0.0f), 
30
          : origin(0.0f), direction(0.0f), hit_pos(0.0f), hit_normal(0.0f), 
19
            has_hit(false), dist(CGLA::BIG), ior(1.0f), u(0.0f), v(0.0f), 
31
            has_hit(false), dist(CGLA::BIG), ior(1.0f), u(0.0f), v(0.0f), 
20
            hit_object(0), trace_depth(0), inside(false), did_hit_diffuse(false)
32
            hit_object(0), trace_depth(0), inside(false), did_hit_diffuse(false)
21
        { }
33
        { }
22
 
34
 
23
        Ray(const CGLA::Vec3f& _origin, const CGLA::Vec3f& _direction) 
35
        Ray(const CGLA::Vec3f& _origin, const CGLA::Vec3f& _direction) 
24
          : origin(_origin), direction(_direction), hit_pos(0.0f), hit_normal(0.0f), 
36
          : origin(_origin), direction(_direction), hit_pos(0.0f), hit_normal(0.0f), 
25
            has_hit(false), dist(CGLA::BIG), ior(1.0f), u(0.0f), v(0.0f), 
37
            has_hit(false), dist(CGLA::BIG), ior(1.0f), u(0.0f), v(0.0f), 
26
            hit_object(0), trace_depth(0), inside(false), did_hit_diffuse(false)
38
            hit_object(0), trace_depth(0), inside(false), did_hit_diffuse(false)
27
        { }
39
        { }
28
 
40
 
29
        CGLA::Vec3f origin;
41
        CGLA::Vec3f origin;
30
        CGLA::Vec3f direction;
42
        CGLA::Vec3f direction;
31
        CGLA::Vec3f hit_pos;
43
        CGLA::Vec3f hit_pos;
32
        CGLA::Vec3f hit_normal;
44
        CGLA::Vec3f hit_normal;
33
 
45
 
34
        bool has_hit; // Did the ray hit an object
46
        bool has_hit; // Did the ray hit an object
35
        bool inside;  // Is the ray inside an object
47
        bool inside;  // Is the ray inside an object
36
        bool did_hit_diffuse;
48
        bool did_hit_diffuse;
37
 
49
 
38
        double dist;  // Distance from origin to current intersection
50
        double dist;  // Distance from origin to current intersection
39
        float ior;    // Current index of refraction for media
51
        float ior;    // Current index of refraction for media
40
        float u, v;   // uv-coordinates on current surface
52
        float u, v;   // uv-coordinates on current surface
41
 
53
 
42
        int trace_depth;  // Current recursion number
54
        int trace_depth;  // Current recursion number
43
        size_t hit_face_id;
55
        size_t hit_face_id;
44
        int id;
56
        int id;
45
 
57
 
46
        const TriMesh* hit_object;
58
        const TriMesh* hit_object;
47
 
59
 
48
        const Material* get_hit_material() const
60
        const Material* get_hit_material() const
49
        {
61
        {
50
          if(!hit_object)
62
          if(!hit_object)
51
            return 0;
63
            return 0;
52
          return &hit_object->materials[hit_object->mat_idx[hit_face_id]];
64
          return &hit_object->materials[hit_object->mat_idx[hit_face_id]];
53
        }
65
        }
54
 
66
 
55
        void reset()
67
        void reset()
56
        {
68
        {
57
            has_hit=false;
69
            has_hit=false;
58
            inside = false;
70
            inside = false;
59
            did_hit_diffuse = false;
71
            did_hit_diffuse = false;
60
            dist = CGLA::BIG;
72
            dist = CGLA::BIG;
61
            ior = 1.0f;
73
            ior = 1.0f;
62
            u=0.0f;
74
            u=0.0f;
63
            v=0.0f;
75
            v=0.0f;
64
            trace_depth = 0;
76
            trace_depth = 0;
65
            hit_object = 0;
77
            hit_object = 0;
66
        }
78
        }
67
 
79
 
68
        void compute_position()
80
        void compute_position()
69
        {
81
        {
70
            hit_pos = origin + dist*direction;      
82
            hit_pos = origin + dist*direction;      
71
        }
83
        }
72
 
84
 
73
        void compute_normal()
85
        void compute_normal()
74
        {
86
        {
75
            const CGLA::Vec3i& face = hit_object->normals.face(hit_face_id);
87
            const CGLA::Vec3i& face = hit_object->normals.face(hit_face_id);
76
            const CGLA::Vec3f& normal0 = hit_object->normals.vertex(face[0]);
88
            const CGLA::Vec3f& normal0 = hit_object->normals.vertex(face[0]);
77
            const CGLA::Vec3f& normal1 = hit_object->normals.vertex(face[1]);
89
            const CGLA::Vec3f& normal1 = hit_object->normals.vertex(face[1]);
78
            const CGLA::Vec3f& normal2 = hit_object->normals.vertex(face[2]);
90
            const CGLA::Vec3f& normal2 = hit_object->normals.vertex(face[2]);
79
            hit_normal = normalize(normal0*(1 - u - v) + normal1*u + normal2*v);      
91
            hit_normal = normalize(normal0*(1 - u - v) + normal1*u + normal2*v);      
80
        }
92
        }
81
 
93
 
82
        void reflect(const CGLA::Vec3f &normal)
94
        void reflect(const CGLA::Vec3f &normal)
83
        {
95
        {
84
            assert(dot(direction, normal) < 0.0f);
96
            assert(dot(direction, normal) < 0.0f);
85
            direction = normal*2.0f*dot(-direction,normal) + direction;      
97
            direction = normal*2.0f*dot(-direction,normal) + direction;      
86
        }
98
        }
87
 
99
 
88
        void refract(const CGLA::Vec3f& normal, float new_ior)
100
        void refract(const CGLA::Vec3f& normal, float new_ior)
89
        {
101
        {
90
            float ref_ratio = ior/new_ior;
102
            float ref_ratio = ior/new_ior;
91
            float cos_N_I = dot(normal, direction);
103
            float cos_N_I = dot(normal, direction);
92
            CGLA::Vec3f norm(normal);
104
            CGLA::Vec3f norm(normal);
93
 
105
 
94
            if(cos_N_I > 0.0f)
106
            if(cos_N_I > 0.0f)
95
            {
107
            {
96
                norm = -norm;
108
                norm = -norm;
97
                cos_N_I = dot(norm, direction);
109
                cos_N_I = dot(norm, direction);
98
            }
110
            }
99
 
111
 
100
            float selector = 1+(ref_ratio*ref_ratio)*(cos_N_I*cos_N_I - 1);
112
            float selector = 1+(ref_ratio*ref_ratio)*(cos_N_I*cos_N_I - 1);
101
 
113
 
102
            if(selector > 0.0f) 
114
            if(selector > 0.0f) 
103
            {
115
            {
104
                direction = norm*(ref_ratio*(-cos_N_I) - sqrt(selector)) 
116
                direction = norm*(ref_ratio*(-cos_N_I) - sqrt(selector)) 
105
                            + direction*ref_ratio;
117
                            + direction*ref_ratio;
106
                ior = new_ior;
118
                ior = new_ior;
107
            } 
119
            } 
108
            else 
120
            else 
109
                // Total internal reflection.
121
                // Total internal reflection.
110
                reflect(normal);
122
                reflect(normal);
111
        }
123
        }
112
 
124
 
113
        bool cond_set_parameter(float t, float _u, float _v, const Geometry::TriMesh* mesh, size_t idx)
125
        bool cond_set_parameter(float t, float _u, float _v, const Geometry::TriMesh* mesh, size_t idx)
114
        {
126
        {
115
            if(t < dist)
127
            if(t < dist)
116
            {
128
            {
117
                dist = t;
129
                dist = t;
118
                u = _u;
130
                u = _u;
119
                v = _v;
131
                v = _v;
120
                hit_object = mesh;
132
                hit_object = mesh;
121
                hit_face_id = idx;
133
                hit_face_id = idx;
122
                has_hit = true;
134
                has_hit = true;
123
                return true;
135
                return true;
124
            }
136
            }
125
            return false;
137
            return false;
126
        }
138
        }
127
    };
139
    };
128
}
140
}
129
#endif
141
#endif
130
 
142
 
131
 
143