Subversion Repositories gelsvn

Rev

Rev 290 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
208 jrf 1
#ifndef __RAY_H__
2
#define __RAY_H__
3
 
4
#include "CGLA/Vec3f.h"
5
#include "Geometry/TriMesh.h"
6
 
7
namespace BRender 
8
{
9
  const double d_eps = 0.000001;
10
  const float f_eps = 0.01f;
11
 
12
  struct Ray 
13
  {
14
    // Constructor
15
    Ray() 
16
      : origin(0), direction(0), hit_pos(0), hit_normal(0), 
17
        has_hit(false), dist(CGLA::BIG), ior(1), u(0), v(0), 
18
	hit_object(0)
19
    { }
20
 
21
    CGLA::Vec3f origin;
22
    CGLA::Vec3f direction;
23
    CGLA::Vec3f hit_pos;
24
    CGLA::Vec3f hit_normal;
25
 
26
    bool has_hit; // Did the ray hit an object
27
 
28
    double dist;  // Distance from origin to current intersection
29
    float ior;    // Current index of refraction for media
30
    float u, v;   // uv-coordinates on current surface
31
    float ran;
32
 
33
    int trace_depth;  // Current recursion number
34
    int hit_face_id;
35
    int id;
36
 
37
    Geometry::TriMesh* hit_object;
38
 
39
    void reset()
40
    {
41
      dist = CGLA::BIG; 
42
      hit_object = 0;
43
      u=0;
44
      v=0;
45
      has_hit=false;      
46
    }
47
 
48
    void reflect(const CGLA::Vec3f &normal)
49
    {
50
      assert(dot(direction, normal) < 0);
51
      direction = normal*2.0f*dot(-direction,normal) + direction;      
52
    }
53
 
54
    void refract(const CGLA::Vec3f& normal, float new_ior)
55
    {
56
      float ref_ratio = ior/new_ior;
57
      float cos_N_I = dot(normal, direction);
58
      CGLA::Vec3f norm(normal);
59
 
60
      if(cos_N_I > 0)
61
      {
62
	norm = -norm;
63
	cos_N_I = dot(norm, direction);
64
      }
65
 
66
      float selector = 1 + (ref_ratio*ref_ratio)*(cos_N_I*cos_N_I - 1);
67
 
68
      if(selector > 0) 
69
      {
70
	direction = norm*(ref_ratio*(-cos_N_I) - sqrt(selector)) 
71
	            + direction*ref_ratio;
72
	ior = new_ior;
73
      } 
74
      else 
75
	// Total internal reflection.
76
	reflect(normal);
77
    }
78
  };
79
 
80
}
81
#endif