-
Notifications
You must be signed in to change notification settings - Fork 2
/
texture.h
376 lines (330 loc) · 8.1 KB
/
texture.h
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#ifndef __TEXTURE_H__
#define __TEXTURE_H__
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
/* standard conversion from rgbe to float pixels */
/* note: Ward uses ldexp(rgb, e-(128+8)) */
void rgbe2float(float& red, float& green, float& blue, unsigned char rgbe[4])
{
if (rgbe[3]) { /*nonzero pixel*/
float f = ldexp(1.0f, rgbe[3] - (int)(128 + 8));
red = rgbe[0] * f;
green = rgbe[1] * f;
blue = rgbe[2] * f;
}
else {
red = green = blue = 0.0f;
}
}
vector4_t* load_tex_impl(const char* tex_path, int& width, int& height, bool is_srgb, bool vertical_flip = true)
{
int components = 0;
stbi_us* st_img = stbi_load_16(tex_path, &width, &height, &components, STBI_rgb_alpha);
if (st_img == nullptr) {
// failed to load the file.
printf("Failed to load image %s\nReason: %s\n", tex_path, stbi_failure_reason());
return nullptr;
}
vector4_t* texture = new vector4_t[width * height];
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
int src_idx = i * width + j;
int dst_idx = (vertical_flip ? height - 1 - i : i) * width + j;
vector4_t c;
c.r = st_img[src_idx * 4 + 0] * cRevt65535;
c.g = st_img[src_idx * 4 + 1] * cRevt65535;
c.b = st_img[src_idx * 4 + 2] * cRevt65535;
c.a = st_img[src_idx * 4 + 3] * cRevt65535;
c = is_srgb ? srgb_to_linear(c) : c;
texture[dst_idx] = c;
}
}
stbi_image_free(st_img);
return texture;
}
void save_tex_impl(const char* tex_path, const int& width, const int& height, unsigned int* data)
{
stbi_write_png(tex_path, width, height, 4, data, width * 4);
}
vector4_t sample_bilinear(vector4_t* texture, int width, int height, const texcoord_t& texcoord)
{
float u = clamp(texcoord.u, 0.0f, 1.0f) * (width - 1);
float v = clamp(texcoord.v, 0.0f, 1.0f) * (height - 1);
int x0 = (int)(u);
int y0 = (int)(v);
int x1 = clamp(x0 + 1, 0, width - 1);
int y1 = clamp(y0 + 1, 0, height - 1);
const vector4_t& t00 = texture[y0 * width + x0];
const vector4_t& t01 = texture[y1 * width + x0];
const vector4_t& t10 = texture[y0 * width + x1];
const vector4_t& t11 = texture[y1 * width + x1];
float u_weight = u - x0;
float v_weight = v - y0;
// bilinear interpolation
vector4_t tu0 = lerp(t00, t10, u_weight);
vector4_t tu1 = lerp(t01, t11, u_weight);
return lerp(tu0, tu1, v_weight);
}
/*
¡ª¡ª¡ª¡ª¡ª¡ª
| +z |
|face2 |
¡ª¡ª¡ª¡ª¡ª¡ª ¡ª¡ª¡ª¡ª¡ª¡ª ¡ª¡ª¡ª¡ª¡ª¡ª ¡ª¡ª¡ª¡ª¡ª¡ª
| -x | -y | +x | +y |
|face1 |face4 |face0 |face5 |
¡ª¡ª¡ª¡ª¡ª¡ª ¡ª¡ª¡ª¡ª¡ª¡ª ¡ª¡ª¡ª¡ª¡ª¡ª ¡ª¡ª¡ª¡ª¡ª¡ª
| -z |
|face3 |
¡ª¡ª¡ª¡ª¡ª¡ª
^z
|
|¡ª¡ª¡ª¡ª¡ª¡ª
| +x |
|face0 |
¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª>y
^z
|
|¡ª¡ª¡ª¡ª¡ª¡ª
| -x |
|face1 |
¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª>-y
^y
|
|¡ª¡ª¡ª¡ª¡ª¡ª
| +z |
|face2 |
¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª>+x
^-y
|
|¡ª¡ª¡ª¡ª¡ª¡ª
| -z |
|face3 |
¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª>+x
^z
|
|¡ª¡ª¡ª¡ª¡ª¡ª
| -y |
|face4 |
¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª>+x
^z
|
|¡ª¡ª¡ª¡ª¡ª¡ª
| +y |
|face5 |
¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª>-x
*/
int direction_to_cubeuv(const vector3_t& dir, texcoord_t& tc)
{
int face_index = 0;
float absX = abs(dir.x);
float absY = abs(dir.y);
float absZ = abs(dir.z);
float ma;
if (absZ >= absX && absZ >= absY)
{
face_index = dir.z > 0.0f ? 2 : 3;
tc.set(dir.x, dir.z > 0.0f ? dir.y : -dir.y);
ma = absZ;
}
else if (absY >= absX)
{
face_index = dir.y > 0.0f ? 5 : 4;
tc.set(dir.y > 0.0f ? -dir.x : dir.x, dir.z);
ma = absY;
}
else
{
face_index = dir.x > 0.0f ? 0 : 1;
tc.set(dir.x > 0.0f ? dir.y : -dir.y, dir.z);
ma = absX;
}
tc.u = (tc.u / ma + 1.0f) * 0.5f;
tc.v = (tc.v / ma + 1.0f) * 0.5f;
return face_index;
}
void cube_uv_to_direction(int index, const texcoord_t& tc, vector3_t& dir)
{
// convert range [0, 1] to [-1, 1]
float u = 2.0f * tc.u - 1.0f;
float v = 2.0f * tc.v - 1.0f;
switch (index)
{
case 0: dir.set(1.0f, u, v); break; // +X
case 1: dir.set(-1.0f, -u, v); break; // -X
case 2: dir.set(u, v, 1.0f); break; // +Z
case 3: dir.set(u, -v, -1.0f); break; // -Z
case 4: dir.set(u, -1.0f, v); break; // -Y
case 5: dir.set(-u, 1.0f, v); break; // +Y
}
dir.normalize();
}
class texture2d_t
{
vector4_t* texture;
int width, height;
public:
texture2d_t() :
texture(nullptr), width(0), height(0)
{ }
~texture2d_t()
{
delete[] texture;
texture = nullptr;
}
int tex_width() const
{
return width;
}
int tex_height() const
{
return width;
}
void init(int w, int h)
{
if (width != w || height != h) {
width = w;
height = h;
if (texture != nullptr) {
delete texture;
}
texture = new vector4_t[width * height];
}
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int idx = i * width + j;
texture[idx].set(0.0f, 0.0f, 0.0f, 1.0f);
}
}
}
void load_tex(const char* tex_path, bool is_srgb, bool vertical_flip = true)
{
if (texture) {
delete texture;
}
texture = load_tex_impl(tex_path, width, height, is_srgb, vertical_flip);
}
void save_tex(const char* tex_path, bool is_srgb, bool vertical_flip = true)
{
unsigned int* data = new unsigned int[width * height];
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int src_idx = i * width + j;
int dst_idx = (vertical_flip ? height - 1 - i : i) * width + j;
vector4_t color = is_srgb ? gamma_correction(texture[src_idx]) : texture[src_idx];
data[dst_idx] = makefour(color);
}
}
save_tex_impl(tex_path, width, height, data);
delete[] data;
}
void write_at(int x, int y, const vector4_t& color)
{
assert(x >= 0 && x < width);
assert(y >= 0 && y < height);
texture[y * width + x] = color;
}
vector4_t read_at(int x, int y) const
{
assert(x >= 0 && x < width);
assert(y >= 0 && y < height);
return texture[y * width + x];
}
vector4_t sample(const texcoord_t& texcoord) const
{
return sample_bilinear(texture, width, height, texcoord);
}
};
class cube_texture_t
{
const int base_xy[6][2] = {
{2, 1}, {0, 1}, {1, 2},
{1, 0}, {1, 1}, {3, 1}
};
texture2d_t faces[6];
public:
cube_texture_t()
{
}
cube_texture_t(int siz)
{
for (int i = 0; i < 6; ++i) {
faces[i].init(siz, siz);
}
}
int size() const
{
return faces[0].tex_width();
}
const texture2d_t& get_face(int face_id) const
{
return faces[face_id];
}
texture2d_t& get_face(int face_id)
{
return faces[face_id];
}
void load_tex(const std::string& tex_path, const std::string& ext_name, bool is_srgb, bool vertical_flip = true)
{
const std::string face_tags[6] = { "px", "nx", "pz", "nz", "ny", "py" };
for (int i = 0; i < 6; ++i) {
std::string path = tex_path + "_" + face_tags[i] + ext_name;
faces[i].load_tex(path.c_str(), is_srgb, vertical_flip);
}
}
void load_tex(const std::string& tex_path, bool is_srgb, bool vertical_flip = true)
{
int w = 0, h = 0;
vector4_t *texture = load_tex_impl(tex_path.c_str(), w, h, is_srgb, vertical_flip);
assert(w * 3 == h * 4);
int siz = w / 4;
assert(w == siz * 4);
for (int k = 0; k < 6; ++k) {
faces[k].init(siz, siz);
int bx = base_xy[k][0] * siz;
int by = base_xy[k][1] * siz;
for (int i = 0; i < siz; ++i) {
for (int j = 0; j < siz; ++j) {
int idx = (i + by) * w + (j + bx);
faces[k].write_at(j, i, texture[idx]);
}
}
}
delete[] texture;
}
void save_tex(const std::string& tex_path, bool is_srgb)
{
int siz = this->size();
int w = siz * 4;
int h = siz * 3;
unsigned int* data = new unsigned int[w * h];
for (int i = 0; i < w *h; ++i) {
data[i] = 0xFFFFFFFF;
}
for (int k = 0; k < 6; ++k) {
int bx = base_xy[k][0] * siz;
int by = base_xy[k][1] * siz;
for (int i = 0; i < siz; ++i) {
for (int j = 0; j < siz; ++j) {
int idx = (h - 1 - (i + by)) * w + (j + bx);
vector4_t color = faces[k].read_at(j, i);
color = is_srgb ? gamma_correction(color) : color;
data[idx] = makefour(color);
}
}
}
save_tex_impl(tex_path.c_str(), w, h, data);
delete[] data;
}
vector4_t sample(const vector3_t& dir) const
{
assert(appro_equal(dir.length(), 1.0f));
texcoord_t tc;
int face_id = direction_to_cubeuv(dir, tc);
return faces[face_id].sample(tc);
}
};
#endif //__TEXTURE_H__