-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawable.cpp
166 lines (123 loc) · 3.74 KB
/
drawable.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "drawable.h"
#include <iostream>
#include <glm/gtx/intersect.hpp>
static const double MINIMUM_INTERSECT_DISTANCE = 0.00001;
const glm::vec3 Drawable::blackColor = glm::vec3(0,0,0);
const glm::vec3 Drawable::whiteColor = glm::vec3(1,1,1);
/*** Drawable ***/
Drawable::Drawable(std::string newName) :
name(newName),
type(Type::OPAQUE),
specularColor(glm::vec3(1,1,1)),
refractionIndice(1)
{
}
/*** Sphere ***/
Sphere::Sphere(float radius,std::string newName) : Drawable(newName)
{
this->radius = radius;
}
Sphere::~Sphere()
{
}
bool Sphere::hasIntercepted(glm::vec3 ray, glm::vec3 origin, glm::vec3 & touchPoint) const
{
// glm::vec3 n;
// return glm::intersectRaySphere(origin,glm::normalize(ray),position,radius,touchPoint,n);
double rayDirX = ray.x;
double rayDirY = ray.y;
double rayDirZ = ray.z;
double rayOriginX = origin.x;
double rayOriginY = origin.y;
double rayOriginZ = origin.z;
double sphereCenterX = position.x;
double sphereCenterY = position.y;
double sphereCenterZ = position.z;
double A = rayDirX * rayDirX + rayDirY * rayDirY + rayDirZ * rayDirZ;
double B = 2 * (rayDirX * (rayOriginX - sphereCenterX) + rayDirY * (rayOriginY - sphereCenterY) + rayDirZ * (rayOriginZ - sphereCenterZ));
double C = (rayOriginX - sphereCenterX)*(rayOriginX - sphereCenterX) + (rayOriginY - sphereCenterY)*(rayOriginY - sphereCenterY) + (rayOriginZ - sphereCenterZ)*(rayOriginZ - sphereCenterZ) - radius*radius;
double delta = B * B - 4 * A * C;
if ( delta < 0 )
return false;
double root = sqrt( delta );
double t1 = (-B + root) / (2 * A);
double t2 = (-B - root) / (2 * A);
if ( t1 < 0 && t2 < 0 )
return false;
if ( t2 < t1 ) // garante que t2 È sempre maior ou igual a t1 aqui
std::swap( t1, t2 );
double minT = t1;
if ( t1 < 0 )
minT = t2;
else if ( t1 < MINIMUM_INTERSECT_DISTANCE )
minT = t2;
if ( minT < MINIMUM_INTERSECT_DISTANCE )
return false;
// intersectionPoint = CVetor3D(rayOrigin + rayDirection * minT ).getPointRepresentation();
touchPoint = origin + (ray*(float)minT);
return true;
}
glm::vec3 Sphere::getNormal(glm::vec3 point) const
{
glm::vec3 normal = point-position;
return glm::normalize(normal);
}
glm::vec3 Sphere::getDiffuseColor(glm::vec3 /*point*/) const
{
return diffuseColor;
}
/*** Plain ***/
Plane::Plane(std::string newName) : Drawable(newName)
{
}
Plane::~Plane()
{
}
bool Plane::hasIntercepted(glm::vec3 ray, glm::vec3 origin, glm::vec3 & touchPoint) const
{
glm::vec3 n = getNormal(position);
float denom = glm::dot(n, glm::normalize(ray));
if (denom > 1e-6) {
glm::vec3 p0l0 = position - origin;
float d = glm::dot(p0l0, n) / denom;
touchPoint = origin + (ray) * d;
return (d >= 0);
}
return false;
}
glm::vec3 Plane::getNormal(glm::vec3 /*point*/) const
{
return glm::normalize(glm::cross(pointA - position, pointB -position));
}
glm::vec3 Plane::getDiffuseColor(glm::vec3 /*point*/) const
{
return diffuseColor;
}
/*** Chess ***/
Chess::Chess(std::string name) : Plane(name)
{
}
glm::vec3 Chess::getDiffuseColor(glm::vec3 point) const
{
int xt =(int)roundf(point.x /tileSize);
int yt =(int)roundf(point.z /tileSize);
bool evenX = xt % 2 == 0;
bool evenY = yt % 2 ==0;
if(evenX && evenY)
{
return color1;
}
else if(!evenX && evenY)
{
return color2;
}
else if(evenX && !evenY)
{
return color2;
}
else if(!evenX && !evenY)
{
return color1;
}
return diffuseColor; // Should never arrive
}