Subversion Repositories gelsvn

Rev

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

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