-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextures.c
150 lines (97 loc) · 3.41 KB
/
textures.c
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 "objects.h"
#include "operations.h"
#include "textures.h"
#include <stdio.h>
#include <math.h>
POINT2D getRectangleTexture(OBJECT obj, POINT intersection){
POLYGON polygon = obj.polygon;
POINT p = polygon.rectangle[4];
double H = getDistance(polygon.points[0], polygon.points[1]);
double L = getDistance(polygon.points[0], polygon.points[3]);
VECTOR U;
U.x = (polygon.points[1].x - polygon.points[0].x)/H;
U.y = (polygon.points[1].y - polygon.points[0].y)/H;
U.z = (polygon.points[1].z - polygon.points[0].z)/H;
VECTOR V;
V.x = (polygon.points[3].x - polygon.points[0].x)/L;
V.y = (polygon.points[3].y - polygon.points[0].y)/L;
V.z = (polygon.points[3].z - polygon.points[0].z)/L;
VECTOR d;
d.x = intersection.x - polygon.points[0].x;
d.y = intersection.y - polygon.points[0].y;
d.z = intersection.z - polygon.points[0].z;
POINT2D coord;
coord.u = pointProduct(d, U)/H;
coord.v = pointProduct(d, V)/L;
return coord;
}
POINT2D getCylinderTexture(OBJECT obj, POINT intersection){
CYLINDER cylinder = obj.cylinder;
VECTOR d;
d.x = intersection.x - cylinder.anchor.x;
d.y = intersection.y - cylinder.anchor.y;
d.z = intersection.z - cylinder.anchor.z;
// d = normalizeVector(d);
double u = acos(pointProduct(getN(obj, intersection), cylinder.G))/(2*PI);
POINT2D coord;
coord.v = pointProduct(cylinder.axis, d)/(cylinder.d2 - cylinder.d1);
PEQUATION eq = cylinder.equation;
double darkSide = eq.a*intersection.x + eq.b*intersection.y + eq.c*intersection.z + eq.d;
if(darkSide < 0){
u = 1 - u;
}
coord.u = u;
return coord;
}
POINT2D getConeTexture(OBJECT obj, POINT intersection){
CONE cone = obj.cone;
VECTOR d;
VECTOR temp;
POINT2D coord;
double u;
d.x = (intersection.x - cone.anchor.x)*cone.axis.x;
d.y = (intersection.y - cone.anchor.y)*cone.axis.y;
d.z = (intersection.z - cone.anchor.z)*cone.axis.z;
coord.v = pointProduct(cone.axis,d)/(cone.d2 - cone.d1);
temp.x = intersection.x-(cone.anchor.x - d.x*cone.axis.x);
temp.y = intersection.y-(cone.anchor.y - d.y*cone.axis.y);
temp.z = intersection.z-(cone.anchor.z - d.z*cone.axis.z);
double magnitude = getMagnitude(temp);
u = acosf(temp.x/magnitude)/(2*PI);
PEQUATION eq = cone.equation;
double darkSide = eq.a*intersection.x + eq.b*intersection.y + eq.c*intersection.z + eq.d;
if(darkSide < 0) u = 1 - u;
coord.u = u;
return coord;
}
POINT2D getSphereTexture(OBJECT obj, POINT intersection){
SPHERE sphere = obj.sphere;
VECTOR norte;
POINT2D coord;
VECTOR N;
VECTOR temp;
VECTOR greenwich;
double u,d;
norte.x = 1;
norte.y = 0;
norte.z = 0;
greenwich.x = 0;
greenwich.y = 1;
greenwich.z = 0;
N = getN(obj,intersection);
coord.v = acosf(pointProduct(N,norte))/PI;
d = norte.x * intersection.x + norte.y * intersection.y + norte.z * intersection.z +
(-(norte.x*intersection.x)-(norte.y*intersection.y)-norte.z*intersection.z);
temp.x = (intersection.x - d*norte.x) - sphere.center.x;
temp.y = (intersection.y - d*norte.y) - sphere.center.y;
temp.z = (intersection.z - d*norte.z) - sphere.center.z;
double magnitude = getMagnitude(temp);
u = (temp.x/magnitude)*greenwich.x+(temp.y/magnitude)*greenwich.y+(temp.z/magnitude)*greenwich.z;
u = acosf(u)/(PI*2);
VECTOR aux;
aux = cruxProduct(greenwich,norte);
d = (aux.x*sphere.center.x + aux.y*sphere.center.y + aux.z*sphere.center.z);
if(aux.x*intersection.x+aux.y*intersection.y+aux.z*intersection.z + d > 0) u = 1-u;
coord.u = u;
return coord;
}