MCPcopy Create free account
hub / github.com/StereoKit/StereoKit / sh_calculate

Function sh_calculate

StereoKitC/spherical_harmonics.cpp:90–143  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

88///////////////////////////////////////////
89
90spherical_harmonics_t sh_calculate(void **env_map_data, tex_format_ format, int32_t face_size) {
91 spherical_harmonics_t result = {};
92 size_t col_size = tex_format_size(format);
93 vec3 (*convert)(uint8_t *) = nullptr;
94 switch (format) {
95 case tex_format_rgba128: convert = to_color_128; break;
96 case tex_format_rgba32: convert = to_color_32_linear; break;
97 case tex_format_rgba32_linear: convert = to_color_32; break;
98 default: return {};
99 }
100
101 float half_px = 0.5f / face_size;
102 for (int32_t i = 0; i < 6; i++) {
103 uint8_t *data = (uint8_t*)env_map_data[i];
104 vec3 p1 = math_cubemap_corner(i * 4);
105 vec3 p2 = math_cubemap_corner(i * 4+1);
106 vec3 p3 = math_cubemap_corner(i * 4+2);
107 vec3 p4 = math_cubemap_corner(i * 4+3);
108
109 for (int32_t y = 0; y < face_size; y++) {
110 float py = 1 - (y / (float)face_size + half_px);
111
112 // Top face is flipped on both axes
113 if (i == 2) {
114 py = 1 - py;
115 }
116 for (int32_t x = 0; x < face_size; x++) {
117 float px = x / (float)face_size + half_px;
118
119 // Top face is flipped on both axes
120 if (i == 2) {
121 px = 1 - px;
122 }
123
124 vec3 pl = vec3_lerp(p1, p4, py);
125 vec3 pr = vec3_lerp(p2, p3, py);
126 vec3 pt = vec3_lerp(pl, pr, px);
127 pt = vec3_normalize(pt);
128
129 vec3 color = convert(&data[(x + y * face_size) * col_size]);
130 sh_add(result, pt, color);
131 }
132 }
133 }
134
135 float count = face_size * face_size * 6.f;
136 for (int32_t i = 0; i < 9; i++)
137 result.coefficients[i] /= count;
138
139 // Apply windowing to prevent overshooting
140 sh_windowing(result, .01f);
141
142 return result;
143}
144
145///////////////////////////////////////////
146

Callers 1

tex_get_cubemap_lightingFunction · 0.85

Calls 6

tex_format_sizeFunction · 0.85
math_cubemap_cornerFunction · 0.85
vec3_lerpFunction · 0.85
vec3_normalizeFunction · 0.85
sh_addFunction · 0.85
sh_windowingFunction · 0.85

Tested by

no test coverage detected