-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexture.cpp
176 lines (152 loc) · 4.07 KB
/
Texture.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
166
167
168
169
170
171
172
173
174
175
176
#include "StdAfx.h"
#include "Texture.h"
#include "ConfigManager.h"
void Texture::loadFile(const string& fileName)
{
if(fileName.substr(fileName.size()-3, 3) == "pfm")
{
char c;
FILE* file = fopen(fileName.c_str(), "rb");
fscanf(file, "P");
fscanf(file, "%c", &c);
if(c == 'f')
hasColor = false;
else if(c == 'F')
hasColor = true;
fscanf(file, "%d %d", &width, &height);
if(hasColor)
colors.resize(width*height);
else
values.resize(width*height);
float form;
fscanf(file, "%f", &form);
fread(&c, sizeof(char), 1, file);
if(c == 0x0D)
fread(&c, sizeof(char), 1, file);
if(hasColor)
{
fread(colors.data(), sizeof(vec3f), colors.size(), file);
}
else
{
fread(values.data(), sizeof(float), values.size(), file);
}
fclose(file);
}
}
vec3f Texture::getColor(const vec3f& coord) const
{
vec3f transCoord = coordTransform * vec4f(coord, 1);
float u = transCoord.x*width - 0.5;
float v = transCoord.y*height - 0.5;
int left = floor(u);
int right = left + 1;
int bottom = floor(v);
int top = bottom + 1;
u -= left;
v -= bottom;
left %= width;
right %= width;
top %= height;
bottom %= height;
if(left < 0) left += width;
if(right < 0) right += width;
if(top < 0) top += height;
if(bottom < 0) bottom += height;
if(hasColor)
{
vec3f lbColor = colors[bottom*width+left];
vec3f rbColor = colors[bottom*width+right];
vec3f ltColor = colors[top*width+left];
vec3f rtColor = colors[top*width+right];
vec3f color = vec3f(0, 0, 0);
color += lbColor*(1-u)*(1-v);
color += rbColor*u*(1-v);
color += ltColor*(1-u)*v;
color += rtColor*u*v;
return vec3f(colorTransform * vec4f(color, 1));
}
else
{
float lbColor = values[bottom*width+left];
float rbColor = values[bottom*width+right];
float ltColor = values[top*width+left];
float rtColor = values[top*width+left];
float color = 0;
color += lbColor*(1-u)*(1-v);
color += rbColor*u*(1-v);
color += ltColor*(1-u)*v;
color += rtColor*u*v;
return vec3f(colorTransform * vec4f(color, color, color, 1));
}
}
vec3f Texture::getGrad(const vec3f& coord) const
{
vec3f transCoord = coordTransform * vec4f(coord, 1);
float u = transCoord.x*width - 0.5;
float v = transCoord.y*height - 0.5;
int left = floor(u);
int right = left + 1;
int bottom = floor(v);
int top = bottom + 1;
u -= left;
v -= bottom;
left %= width;
right %= width;
top %= height;
bottom %= height;
if(left < 0) left += width;
if(right < 0) right += width;
if(top < 0) top += height;
if(bottom < 0) bottom += height;
vec3f image_grad(0, 0, 0);
if(hasColor)
{
float lbColor = colors[bottom*width+left].x;
float rbColor = colors[bottom*width+right].x;
float ltColor = colors[top*width+left].x;
image_grad.x = rbColor - lbColor;
image_grad.y = ltColor - lbColor;
}
else
{
float lbColor = values[bottom*width+left];
float rbColor = values[bottom*width+right];
float ltColor = values[top*width+left];
image_grad.x = rbColor - lbColor;
image_grad.y = ltColor - lbColor;
}
vec3f uv_grad;
float var_n = image_grad.length();
var_n = (colorTransform * vec4f(var_n, var_n, var_n, 0)).x;
image_grad.normalize();
uv_grad = vec3f(inverse(coordTransform)*vec4f(image_grad, 0));
uv_grad.z = var_n;
return uv_grad;
}
void Texture::loadTextureFromXML(const ConfigManager* cm, xml_node<>* nodeTex)
{
loadFile(cm->getFullPath(nodeTex->first_node("path")->value()));
if(nodeTex->first_node("coordTransform"))
setCoordTransform(readMatrix(nodeTex->first_node("coordTransform")->value()));
if(nodeTex->first_node("colorTransform"))
setCoordTransform(readMatrix(nodeTex->first_node("colorTransform")->value()));
if(nodeTex->first_node("colorScale"))
{
float scale = atof(nodeTex->first_node("colorScale")->value());
setColorTransform(colorTransform*matrix4<float>(scale,0,0,0,0,scale,0,0,0,0,scale,0,0,0,0,1));
}
}
void Texture::toGrayScale()
{
hasColor = false;
if(colors.size() == 0 || values.size() > 0)
{
colors.clear();
return;
}
values.resize(colors.size());
for(unsigned i=0; i<colors.size(); i++)
values[i] = (colors[i].x + colors[i].y + colors[i].z) / 3;
colors.clear();
}