Subversion Repositories gelsvn

Rev

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

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