-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculate.cpp
More file actions
150 lines (120 loc) · 2.86 KB
/
Calculate.cpp
File metadata and controls
150 lines (120 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <windows.h>
#include <math.h>
#include <iostream>
#include <stdio.h>
#include "scene_io.h"
#include "Timer.h"
#include "CImg.h"
#include "Eigen\Eigen"
#include "Eigen\Dense"
#define PI 3.14159265
using namespace cimg_library;
using namespace Eigen;
using Eigen::MatrixXd;
float RaySphereIntersection (Vector3f SphereCenter, float Radius, Vector3f RayDir, Vector3f RayOri)
{
float d = 0;
float define = (pow((RayDir.dot(RayOri - SphereCenter)),2) - (RayOri - SphereCenter).dot(RayOri - SphereCenter) + Radius*Radius);
if(define > 0)
{
float d = -(RayDir.dot(RayOri - SphereCenter)) - sqrt(define);
if (d<0)
{
d = -(RayDir.dot(RayOri - SphereCenter)) + sqrt(define);
}
if (d>0) {return d;}
else{ return 0;}
}
else
{
return 0;
}
}
Vector3f RayTriangleIntersection (Vector3f V0, Vector3f V1, Vector3f V2, Vector3f RayDir, Vector3f RayOri)
{
Vector3f E1 = V1 - V0;
Vector3f E2 = V2 - V0;
Vector3f T = RayOri - V0;
Vector3f P = RayDir.cross(E2);
Vector3f Q = T.cross(E1);
float t = Q.dot(E2) / P.dot(E1);
float u = P.dot(T) / P.dot(E1);
float v = Q.dot(RayDir) / P.dot(E1);
Vector3f result;
result[0] = t;
result[1] = u;
result[2] = v;
return result;
}
bool EncounterTriOrNot (Vector3f LigPosition, Vector3f LigDir, Vector3f V0, Vector3f V1, Vector3f V2)
{
return TRUE;
}
Vector3f getRefractionDir (Vector3f Normal, Vector3f RayDir, float n1, float n2 )
{
RayDir = -RayDir;
float define = Normal.dot(RayDir);
float theta1 = std::acos(std::abs(Normal.dot(RayDir)));
if (define<0)
{
//Not sure???????????????????????????????????????????????????????????????????????????????????????????????????
Normal = - Normal;
float theta2 = std::asin (sin(theta1) * 1.5f / 1.f);
Vector3f Q = cos(theta1) * Normal;
Vector3f M = (sin(theta2)/sin(theta1)) * (Q - RayDir);
Vector3f P = -cos(theta2) * Normal;
Vector3f T = M + P;
T.normalize();
return T;
}
if (define >= 0)
{
float theta2 = std::asin (sin(theta1) * 1.f / 1.5f);
Vector3f Q = cos(theta1) * Normal;
Vector3f M = (sin(theta2)/sin(theta1)) * (Q - RayDir);
Vector3f P = -cos(theta2) * Normal;
Vector3f T = M + P;
T.normalize();
return T;
}
}
float clamp(float x)
{
if(x>0)
{return x;}
else
{return 0;}
}
Vector3f Normalize_color(float x, float y, float z)
{
float max = max(x,y,z);
Vector3f result;
result[0] = x/max;
result[1] = y/max;
result[2] = z/max;
return result;
}
float RandomFloat()
{
float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
return r;
}
Vector3f getVertical(Vector3f A)
{
Vector3f result;
Vector3f C;
if (A[1] != 0 || A[2] != 0)
{
C[0] = 1;
C[1] = 0;
C[2] = 0;
}
else
{
C[0] = 0;
C[1] = 1;
C[2] = 0;
}
Vector3f B = A.cross(C);
return B;
}