-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNormalMap.cpp
More file actions
executable file
·149 lines (105 loc) · 3.51 KB
/
NormalMap.cpp
File metadata and controls
executable file
·149 lines (105 loc) · 3.51 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
#include "NormalMap.h"
Vector3D CorrugatedNormal::bump(const Point3D& p) {
return Vector3D(
this -> magnitude *
sin(this -> phaseOffset + this -> period * PI * p[0]),
0,
0
);
}
Vector3D RadialCorrugatedNormal::bump (const Point3D& p) {
Vector3D delta = p - this -> centre;
double res =
this -> magnitude *
sin(
this -> period * 2 * PI * delta.length() +
this -> phaseOffset
);
delta.normalize();
return res * delta;
}
Vector3D NoiseyNormal::bump(const Point3D& point) {
return this -> magnitude * Vector3D(
((rand() % 50) - 25.0) / 50,
((rand() % 50) - 25.0) / 50,
0.0
);
}
Vector3D BricksNormal::bump(const Point3D& point) {
double cosx = cos(2 * PI * this -> xPeriod * point[0]);
Vector3D offset(0, 0, 0);
double dx = 0;
double dy = 0;
bool dxSet = false, dySet = false;
if (cosx <= depthThreshold) {
double sinx = sin(2 * PI * this -> xPeriod * point[0]);
dx = sinx * magnitude;
dxSet = true;
}
double whichPhase = cosx > 0 ? -1 : 1;
double cosy1 = cos(2 * PI * this -> yPeriod * point[1]);
if (cosy1 <= depthThreshold) {
double siny = sin(2 * PI * this -> yPeriod * point[1]);
dy = siny * magnitude;
dySet = true;
}
if (dySet && dxSet) {
// Pick the minimum of the two slopes.
if (abs(dy) < abs(dx)) {
offset[1] = dy;
} else {
offset[0] = dx;
}
} else {
offset[0] = dx;
offset[1] = dy;
}
return offset;
}
Vector3D LumpyNormal::bump(const Point3D& point) {
Vector3D offset(0,0,0);
double cosx = cos(2 * PI * this -> period * point[0]);
double cosy = cos(2 * PI * this -> period * point[1]);
if (cosx * cosy < this -> depthThreshold)
return offset;
offset[0] = sin(2 * PI * this -> period * point[0]);
offset[1] = sin(2 * PI * this -> period * point[1]);
return this -> magnitude * (cosx < 0 ? -1 : 1) * offset;
}
Vector3D RibbedNormal::bump (const Point3D& point) {
double cosx = cos(2 * PI * this -> period * point[0]);
double cosx4 = pow(cosx, 4);
if (cosx4 > this -> heightCutoff) {
return Vector3D(0, 0, 0);
}
// Derivative.
return this -> magnitude * Vector3D(4 * pow(cosx, 3) * sin(point[0]), 0, 0);
}
Vector3D PolynomialNoiseNormal::bump (const Point3D& point) {
double xheight = 0.0;
double yheight = 0.0;
for (int i = 0; i < this -> order; i++) {
xheight += (i % 2 == 0 ? 1 : -1) *
sin(PI * point[0] * this -> xroots[i] + this -> xoffsets[i]);
yheight += (i % 2 == 0 ? 1: -1) *
sin(PI * point[1] * this -> yroots[i] + this -> yoffsets[i]);
}
return (1.0 / (this -> order)) * Vector3D(xheight, yheight, 0);
}
Vector3D MetallicGrainNormal::bump(const Point3D& point) {
bool hasBeenSet = false;
Vector3D offset(0, 0, 0);
for (int i = 0; i < this -> dentLocations.size(); i++) {
Vector3D delta = (Vector3D(point[0], point[1], 0) - this -> dentLocations[i]);
double deltaLenSquared = delta[0] * delta[0] + delta[1] * delta[1];
if (!hasBeenSet) {
offset = -delta;
hasBeenSet = true;
} else {
if (deltaLenSquared < pow(offset[0], 2) + pow(offset[1], 2)) {
offset = delta;
}
}
}
return offset;
}