@@ -96,6 +96,9 @@ struct GLTexParams {
96
96
GLenum wrapS{GL_REPEAT};
97
97
GLenum wrapT{GL_REPEAT};
98
98
std::array<float , 4 > borderCol{0 .0f , 0 .0f , 0 .0f , 0 .0f }; // transparent black
99
+
100
+ // Use 3x3 mat to support pivot transform.
101
+ tinyusdz::tydra::mat3 uv_transform{tinyusdz::tydra::mat3::identity ()};
99
102
};
100
103
101
104
struct GLTexState {
@@ -104,6 +107,8 @@ struct GLTexState {
104
107
uint32_t slot_id{0 };
105
108
GLuint tex_id; // glBindTexture id
106
109
GLint u_tex{-1 }; // sampler glUniform location
110
+
111
+ GLint u_transform; // texcoord transform
107
112
};
108
113
109
114
template <typename T>
@@ -126,33 +131,21 @@ struct GLUniformFactor {
126
131
struct GLUsdPreviewSurfaceState {
127
132
128
133
static constexpr auto kDiffuseColor = " diffuseColor" ;
129
- static constexpr auto kDiffuseColorTex = " diffuseColorTex" ;
130
- static constexpr auto kEmissionColor = " emissionColor" ;
131
- static constexpr auto kEmissionColorTex = " emissionColorTex" ;
134
+ static constexpr auto kEmissiveColor = " emissiveColor" ;
132
135
static constexpr auto kSpecularColor = " specularColor" ;
133
- static constexpr auto kSpecularColorTex = " specularColorTex" ;
134
136
static constexpr auto kUseSpecularWorkflow = " useSpecularWorkflow" ;
135
137
static constexpr auto kMetallic = " metallic" ;
136
- static constexpr auto kMetallicTex = " metallicTex" ;
137
138
static constexpr auto kRoughness = " roughness" ;
138
- static constexpr auto kRoughnessTex = " roughnessTex" ;
139
139
static constexpr auto kClearcoat = " clearcoat" ;
140
- static constexpr auto kClearcoatTex = " clearcoatTex" ;
141
140
static constexpr auto kClearcoatRoughness = " clearcoatRoughness" ;
142
- static constexpr auto kClearcoatRoughnessTex = " clearcoatRoughnessTex" ;
143
141
static constexpr auto kOpacity = " opacity" ;
144
- static constexpr auto kOpacityTex = " opacityTex" ;
145
142
static constexpr auto kOpacityThreshold = " opacityThreshold" ;
146
- static constexpr auto kOpacityThresholdTex = " opacityThresholdTex" ;
147
143
static constexpr auto kIor = " ior" ;
148
- static constexpr auto kIorTex = " iorTex" ;
149
144
static constexpr auto kNormal = " normal" ;
150
- static constexpr auto kNormalTex = " normalTex" ;
151
145
static constexpr auto kOcclusion = " occlusion" ;
152
- static constexpr auto kOcclusionTex = " occlusionTex" ;
153
146
154
147
GLTexOrFactor<tinyusdz::tydra::vec3> diffuseColor{{0 .18f , 0 .18f , 0 .18f }};
155
- GLTexOrFactor<tinyusdz::tydra::vec3> emissionColor {{0 .0f , 0 .0f , 0 .0f }};
148
+ GLTexOrFactor<tinyusdz::tydra::vec3> emissiveColor {{0 .0f , 0 .0f , 0 .0f }};
156
149
157
150
GLUniformFactor<int > useSpecularWorkflow{0 }; // non-texturable
158
151
@@ -176,24 +169,148 @@ struct GLUsdPreviewSurfaceState {
176
169
177
170
};
178
171
179
- void SetupGLUsdPreviewSurface (
172
+ template <typename T>
173
+ bool SetupGLUsdPreviewSurfaceParam (
174
+ const GLuint prog_id,
175
+ const tinyusdz::tydra::RenderScene &scene,
176
+ const std::string &base_shadername,
177
+ const tinyusdz::tydra::ShaderParam<T> &s,
178
+
179
+ GLTexOrFactor<T> &dst)
180
+ {
181
+ if (s.is_texture ()) {
182
+ {
183
+ std::string u_name = base_shadername + " Tex" ;
184
+ GLint loc = glGetUniformLocation (prog_id, u_name.c_str ());
185
+ dst.tex .u_tex = loc;
186
+ }
187
+
188
+ {
189
+ std::string u_name = base_shadername + " TexTransform" ;
190
+ GLint loc = glGetUniformLocation (prog_id, u_name.c_str ());
191
+ dst.tex .u_transform = loc;
192
+ if (s.textureId < 0 || s.textureId >= scene.textures .size ()) {
193
+ std::cerr << " Invalid txtureId for " << base_shadername + " \n " ;
194
+ } else {
195
+ const tinyusdz::tydra::UVTexture &uvtex = scene.textures [size_t (s.textureId )];
196
+ dst.tex .texParams .uv_transform = uvtex.transform ;
197
+ }
198
+ }
199
+
200
+ } else {
201
+ GLint loc = glGetUniformLocation (prog_id, base_shadername.c_str ());
202
+ if (loc < 0 ) {
203
+ std::cerr << base_shadername << " uniform not found in the shader.\n " ;
204
+ }
205
+ dst.u_factor = loc;
206
+ dst.factor = s.value ;
207
+ }
208
+
209
+ return true ;
210
+ }
211
+
212
+ bool ReloadShader (
213
+ GLuint prog_id, const std::string &vert_filepath, const std::string &frag_filepath) {
214
+ std::string vert_str;
215
+ std::string frag_str;
216
+
217
+ if (vert_filepath.size () && tinyusdz::io::FileExists (vert_filepath)) {
218
+
219
+ std::vector<uint8_t > bytes;
220
+ std::string err;
221
+ if (!tinyusdz::io::ReadWholeFile (&bytes, &err, vert_filepath)) {
222
+ std::cerr << " Read vertg shader failed: " << err << " \n " ;
223
+ return false ;
224
+ }
225
+
226
+ vert_str = std::string (reinterpret_cast <char *>(bytes.data ()), bytes.size ());
227
+ }
228
+
229
+ if (frag_filepath.size () && tinyusdz::io::FileExists (frag_filepath)) {
230
+
231
+ std::vector<uint8_t > bytes;
232
+ std::string err;
233
+ if (!tinyusdz::io::ReadWholeFile (&bytes, &err, frag_filepath)) {
234
+ std::cerr << " Read frag shader failed: " << err << " \n " ;
235
+ return false ;
236
+ }
237
+
238
+ frag_str = std::string (reinterpret_cast <char *>(bytes.data ()), bytes.size ());
239
+ }
240
+
241
+ // TODO
242
+ return true ;
243
+ }
244
+
245
+
246
+ bool SetupGLUsdPreviewSurface (
180
247
GLuint prog_id,
248
+ tinyusdz::tydra::RenderScene &scene,
181
249
tinyusdz::tydra::RenderMaterial &m,
182
250
GLUsdPreviewSurfaceState &dst)
183
251
{
184
- if (m.surfaceShader .diffuseColor .is_texture ()) {
185
- GLint loc = glGetUniformLocation (prog_id, GLUsdPreviewSurfaceState::kDiffuseColorTex );
186
- dst.diffuseColor .tex .u_tex = loc;
187
- } else {
188
- GLint loc = glGetUniformLocation (prog_id, GLUsdPreviewSurfaceState::kDiffuseColor );
252
+ const auto surfaceShader = m.surfaceShader ;
253
+
254
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kDiffuseColor , surfaceShader.diffuseColor , dst.diffuseColor )) {
255
+ return false ;
256
+ }
257
+
258
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kEmissiveColor , surfaceShader.emissiveColor , dst.emissiveColor )) {
259
+ return false ;
260
+ }
261
+
262
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kSpecularColor , surfaceShader.specularColor , dst.specularColor )) {
263
+ return false ;
264
+ }
265
+
266
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kMetallic , surfaceShader.metallic , dst.metallic )) {
267
+ return false ;
268
+ }
269
+
270
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kRoughness , surfaceShader.roughness , dst.roughness )) {
271
+ return false ;
272
+ }
273
+
274
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kClearcoat , surfaceShader.clearcoat , dst.clearcoat )) {
275
+ return false ;
276
+ }
277
+
278
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kClearcoatRoughness , surfaceShader.clearcoatRoughness , dst.clearcoatRoughness )) {
279
+ return false ;
280
+ }
281
+
282
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kOpacity , surfaceShader.opacity , dst.opacity )) {
283
+ return false ;
284
+ }
285
+
286
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kOpacityThreshold , surfaceShader.opacityThreshold , dst.opacityThreshold )) {
287
+ return false ;
288
+ }
289
+
290
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kIor , surfaceShader.ior , dst.ior )) {
291
+ return false ;
292
+ }
293
+
294
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kOcclusion , surfaceShader.occlusion , dst.occlusion )) {
295
+ return false ;
296
+ }
297
+
298
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kNormal , surfaceShader.normal , dst.normal )) {
299
+ return false ;
300
+ }
301
+
302
+ {
303
+ GLint loc = glGetUniformLocation (prog_id, GLUsdPreviewSurfaceState::kUseSpecularWorkflow );
189
304
if (loc < 0 ) {
190
- std::cerr << GLUsdPreviewSurfaceState::kDiffuseColor << " uniform not found in the shader.\n " ;
305
+ std::cerr << GLUsdPreviewSurfaceState::kUseSpecularWorkflow << " uniform not found in the shader.\n " ;
191
306
}
192
- dst.diffuseColor . u_factor = loc ;
193
- dst.diffuseColor . factor = m. surfaceShader . diffuseColor . value ;
307
+ dst.useSpecularWorkflow . factor = surfaceShader. useSpecularWorkFlow ? 1 . 0f : 0 . 0f ;
308
+ dst.useSpecularWorkflow . u_factor = loc ;
194
309
}
195
310
196
- // TODO: Support shader params
311
+ // TODO: `displacement` param
312
+
313
+ return true ;
197
314
}
198
315
199
316
struct GLVertexUniformState {
0 commit comments