41
41
#include " external/linalg.h"
42
42
#include " io-util.hh"
43
43
#include " linear-algebra.hh"
44
+ #include " pprinter.hh" // import to_string(tinyusdz::value::***)
44
45
#include " tinyusdz.hh"
45
46
#include " tydra/render-data.hh"
46
47
#include " tydra/scene-access.hh"
47
48
#include " value-pprint.hh" // import to_string(tinyusdz::value::***)
48
- #include " pprinter.hh" // import to_string(tinyusdz::value::***)
49
49
50
50
// local
51
51
#include " shader.hh"
@@ -105,13 +105,13 @@ struct GLTexState {
105
105
GLTexParams texParams;
106
106
std::string sampler_name;
107
107
uint32_t slot_id{0 };
108
- GLuint tex_id; // glBindTexture id
108
+ GLuint tex_id; // glBindTexture id
109
109
GLint u_tex{-1 }; // sampler glUniform location
110
110
111
- GLint u_transform; // texcoord transform
111
+ GLint u_transform; // texcoord transform
112
112
};
113
113
114
- template <typename T>
114
+ template <typename T>
115
115
struct GLTexOrFactor {
116
116
GLTexOrFactor (const T &v) : factor(v) {}
117
117
@@ -120,7 +120,7 @@ struct GLTexOrFactor {
120
120
GLint u_factor{-1 };
121
121
};
122
122
123
- template <typename T>
123
+ template <typename T>
124
124
struct GLUniformFactor {
125
125
GLUniformFactor (const T &v) : factor(v) {}
126
126
@@ -129,7 +129,6 @@ struct GLUniformFactor {
129
129
};
130
130
131
131
struct GLUsdPreviewSurfaceState {
132
-
133
132
static constexpr auto kDiffuseColor = " diffuseColor" ;
134
133
static constexpr auto kEmissiveColor = " emissiveColor" ;
135
134
static constexpr auto kSpecularColor = " specularColor" ;
@@ -147,10 +146,11 @@ struct GLUsdPreviewSurfaceState {
147
146
GLTexOrFactor<tinyusdz::tydra::vec3> diffuseColor{{0 .18f , 0 .18f , 0 .18f }};
148
147
GLTexOrFactor<tinyusdz::tydra::vec3> emissiveColor{{0 .0f , 0 .0f , 0 .0f }};
149
148
150
- GLUniformFactor<int > useSpecularWorkflow{0 }; // non-texturable
149
+ GLUniformFactor<int > useSpecularWorkflow{0 }; // non-texturable
151
150
152
- GLTexOrFactor<tinyusdz::tydra::vec3> specularColor{{0 .0f , 0 .0f , 0 .0f }}; // useSpecularWorkflow = 1
153
- GLTexOrFactor<float > metallic{0 .0f }; // useSpecularWorkflow = 0
151
+ GLTexOrFactor<tinyusdz::tydra::vec3> specularColor{
152
+ {0 .0f , 0 .0f , 0 .0f }}; // useSpecularWorkflow = 1
153
+ GLTexOrFactor<float > metallic{0 .0f }; // useSpecularWorkflow = 0
154
154
155
155
GLTexOrFactor<float > roughness{0 .5f };
156
156
GLTexOrFactor<float > clearcoat{0 .0f };
@@ -160,24 +160,22 @@ struct GLUsdPreviewSurfaceState {
160
160
161
161
GLTexOrFactor<float > ior{1 .5f };
162
162
163
- GLTexOrFactor<tinyusdz::tydra::vec3> normal {{0 .0f , 0 .0f , 1 .0f }}; // normal map
163
+ GLTexOrFactor<tinyusdz::tydra::vec3> normal {
164
+ {0 .0f , 0 .0f , 1 .0f }}; // normal map
164
165
165
166
// No displacement mapping on OpenGL
166
- // GLTexOrFactor<float> displacement{0.0f};
167
-
168
- GLTexOrFactor<float > occlusion{1 .0f };
167
+ // GLTexOrFactor<float> displacement{0.0f};
169
168
169
+ GLTexOrFactor<float > occlusion{1 .0f };
170
170
};
171
171
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,
172
+ template <typename T>
173
+ bool SetupGLUsdPreviewSurfaceParam (const GLuint prog_id,
174
+ const tinyusdz::tydra::RenderScene &scene,
175
+ const std::string &base_shadername,
176
+ const tinyusdz::tydra::ShaderParam<T> &s,
178
177
179
- GLTexOrFactor<T> &dst)
180
- {
178
+ GLTexOrFactor<T> &dst) {
181
179
if (s.is_texture ()) {
182
180
{
183
181
std::string u_name = base_shadername + " Tex" ;
@@ -192,7 +190,8 @@ bool SetupGLUsdPreviewSurfaceParam(
192
190
if (s.textureId < 0 || s.textureId >= scene.textures .size ()) {
193
191
std::cerr << " Invalid txtureId for " << base_shadername + " \n " ;
194
192
} else {
195
- const tinyusdz::tydra::UVTexture &uvtex = scene.textures [size_t (s.textureId )];
193
+ const tinyusdz::tydra::UVTexture &uvtex =
194
+ scene.textures [size_t (s.textureId )];
196
195
dst.tex .texParams .uv_transform = uvtex.transform ;
197
196
}
198
197
}
@@ -209,102 +208,130 @@ bool SetupGLUsdPreviewSurfaceParam(
209
208
return true ;
210
209
}
211
210
212
- bool ReloadShader (
213
- GLuint prog_id, const std::string &vert_filepath, const std::string &frag_filepath) {
211
+ bool ReloadShader (GLuint prog_id, const std::string &vert_filepath,
212
+ const std::string &frag_filepath) {
214
213
std::string vert_str;
215
214
std::string frag_str;
216
215
217
216
if (vert_filepath.size () && tinyusdz::io::FileExists (vert_filepath)) {
218
-
219
217
std::vector<uint8_t > bytes;
220
218
std::string err;
221
219
if (!tinyusdz::io::ReadWholeFile (&bytes, &err, vert_filepath)) {
222
220
std::cerr << " Read vertg shader failed: " << err << " \n " ;
223
221
return false ;
224
222
}
225
223
226
- vert_str = std::string (reinterpret_cast <char *>(bytes.data ()), bytes.size ());
224
+ vert_str =
225
+ std::string (reinterpret_cast <char *>(bytes.data ()), bytes.size ());
226
+
227
+ std::cout << " VERT:\n " << vert_str << " \n " ;
227
228
}
228
229
229
230
if (frag_filepath.size () && tinyusdz::io::FileExists (frag_filepath)) {
230
-
231
231
std::vector<uint8_t > bytes;
232
232
std::string err;
233
233
if (!tinyusdz::io::ReadWholeFile (&bytes, &err, frag_filepath)) {
234
234
std::cerr << " Read frag shader failed: " << err << " \n " ;
235
235
return false ;
236
236
}
237
237
238
- frag_str = std::string (reinterpret_cast <char *>(bytes.data ()), bytes.size ());
238
+ frag_str =
239
+ std::string (reinterpret_cast <char *>(bytes.data ()), bytes.size ());
240
+
241
+ std::cout << " FRAG:\n " << frag_str << " \n " ;
239
242
}
240
243
241
244
// TODO
242
245
return true ;
243
246
}
244
247
245
-
246
- bool SetupGLUsdPreviewSurface (
247
- GLuint prog_id,
248
- tinyusdz::tydra::RenderScene &scene,
249
- tinyusdz::tydra::RenderMaterial &m,
250
- GLUsdPreviewSurfaceState &dst)
251
- {
248
+ bool SetupGLUsdPreviewSurface (GLuint prog_id,
249
+ tinyusdz::tydra::RenderScene &scene,
250
+ tinyusdz::tydra::RenderMaterial &m,
251
+ GLUsdPreviewSurfaceState &dst) {
252
252
const auto surfaceShader = m.surfaceShader ;
253
253
254
- if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kDiffuseColor , surfaceShader.diffuseColor , dst.diffuseColor )) {
254
+ if (!SetupGLUsdPreviewSurfaceParam (
255
+ prog_id, scene, GLUsdPreviewSurfaceState::kDiffuseColor ,
256
+ surfaceShader.diffuseColor , dst.diffuseColor )) {
255
257
return false ;
256
258
}
257
259
258
- if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kEmissiveColor , surfaceShader.emissiveColor , dst.emissiveColor )) {
260
+ if (!SetupGLUsdPreviewSurfaceParam (
261
+ prog_id, scene, GLUsdPreviewSurfaceState::kEmissiveColor ,
262
+ surfaceShader.emissiveColor , dst.emissiveColor )) {
259
263
return false ;
260
264
}
261
265
262
- if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kSpecularColor , surfaceShader.specularColor , dst.specularColor )) {
266
+ if (!SetupGLUsdPreviewSurfaceParam (
267
+ prog_id, scene, GLUsdPreviewSurfaceState::kSpecularColor ,
268
+ surfaceShader.specularColor , dst.specularColor )) {
263
269
return false ;
264
270
}
265
271
266
- if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kMetallic , surfaceShader.metallic , dst.metallic )) {
272
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene,
273
+ GLUsdPreviewSurfaceState::kMetallic ,
274
+ surfaceShader.metallic , dst.metallic )) {
267
275
return false ;
268
276
}
269
277
270
- if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kRoughness , surfaceShader.roughness , dst.roughness )) {
278
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene,
279
+ GLUsdPreviewSurfaceState::kRoughness ,
280
+ surfaceShader.roughness , dst.roughness )) {
271
281
return false ;
272
282
}
273
283
274
- if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kClearcoat , surfaceShader.clearcoat , dst.clearcoat )) {
284
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene,
285
+ GLUsdPreviewSurfaceState::kClearcoat ,
286
+ surfaceShader.clearcoat , dst.clearcoat )) {
275
287
return false ;
276
288
}
277
289
278
- if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kClearcoatRoughness , surfaceShader.clearcoatRoughness , dst.clearcoatRoughness )) {
290
+ if (!SetupGLUsdPreviewSurfaceParam (
291
+ prog_id, scene, GLUsdPreviewSurfaceState::kClearcoatRoughness ,
292
+ surfaceShader.clearcoatRoughness , dst.clearcoatRoughness )) {
279
293
return false ;
280
294
}
281
295
282
- if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kOpacity , surfaceShader.opacity , dst.opacity )) {
296
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene,
297
+ GLUsdPreviewSurfaceState::kOpacity ,
298
+ surfaceShader.opacity , dst.opacity )) {
283
299
return false ;
284
300
}
285
301
286
- if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kOpacityThreshold , surfaceShader.opacityThreshold , dst.opacityThreshold )) {
302
+ if (!SetupGLUsdPreviewSurfaceParam (
303
+ prog_id, scene, GLUsdPreviewSurfaceState::kOpacityThreshold ,
304
+ surfaceShader.opacityThreshold , dst.opacityThreshold )) {
287
305
return false ;
288
306
}
289
307
290
- if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kIor , surfaceShader.ior , dst.ior )) {
308
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene,
309
+ GLUsdPreviewSurfaceState::kIor ,
310
+ surfaceShader.ior , dst.ior )) {
291
311
return false ;
292
312
}
293
313
294
- if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kOcclusion , surfaceShader.occlusion , dst.occlusion )) {
314
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene,
315
+ GLUsdPreviewSurfaceState::kOcclusion ,
316
+ surfaceShader.occlusion , dst.occlusion )) {
295
317
return false ;
296
318
}
297
319
298
- if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene, GLUsdPreviewSurfaceState::kNormal , surfaceShader.normal , dst.normal )) {
320
+ if (!SetupGLUsdPreviewSurfaceParam (prog_id, scene,
321
+ GLUsdPreviewSurfaceState::kNormal ,
322
+ surfaceShader.normal , dst.normal )) {
299
323
return false ;
300
324
}
301
325
302
326
{
303
- GLint loc = glGetUniformLocation (prog_id, GLUsdPreviewSurfaceState::kUseSpecularWorkflow );
327
+ GLint loc = glGetUniformLocation (
328
+ prog_id, GLUsdPreviewSurfaceState::kUseSpecularWorkflow );
304
329
if (loc < 0 ) {
305
- std::cerr << GLUsdPreviewSurfaceState::kUseSpecularWorkflow << " uniform not found in the shader.\n " ;
330
+ std::cerr << GLUsdPreviewSurfaceState::kUseSpecularWorkflow
331
+ << " uniform not found in the shader.\n " ;
306
332
}
307
- dst.useSpecularWorkflow .factor = surfaceShader.useSpecularWorkFlow ? 1 .0f : 0 .0f ;
333
+ dst.useSpecularWorkflow .factor =
334
+ surfaceShader.useSpecularWorkFlow ? 1 .0f : 0 .0f ;
308
335
dst.useSpecularWorkflow .u_factor = loc;
309
336
}
310
337
@@ -395,6 +422,9 @@ struct GUIContext {
395
422
396
423
std::string converter_info;
397
424
std::string converter_warn;
425
+
426
+ std::string vert_filename{" ../shaders/no_skinning.vert" };
427
+ std::string frag_filename{" ../shaders/usdpreviewsurface.frag" };
398
428
};
399
429
400
430
GUIContext gCtx ;
@@ -1071,13 +1101,12 @@ static void ComputeBoundingBox(const tinyusdz::tydra::RenderMesh &mesh,
1071
1101
static bool ProcScene (const example::shader &gl_shader,
1072
1102
const tinyusdz::Stage &stage,
1073
1103
std::string &asset_search_path, GLScene *scene) {
1074
-
1075
1104
tinyusdz::Axis upAxis{tinyusdz::Axis::Y};
1076
1105
if (stage.metas ().upAxis .authored ()) {
1077
1106
upAxis = stage.metas ().upAxis .get_value ();
1078
1107
}
1079
1108
std::cout << " upAxis " << tinyusdz::to_string (upAxis) << " \n " ;
1080
-
1109
+
1081
1110
//
1082
1111
// Stage to Renderable Scene
1083
1112
//
@@ -1153,7 +1182,8 @@ static bool ProcScene(const example::shader &gl_shader,
1153
1182
scene_bmax[2 ] = (std::max)(bmax[2 ], scene_bmax[2 ]);
1154
1183
1155
1184
GLMeshState gl_mesh;
1156
- if (!SetupMesh (upAxis, renderScene.meshes [i], gl_shader.get_program (), gl_mesh)) {
1185
+ if (!SetupMesh (upAxis, renderScene.meshes [i], gl_shader.get_program (),
1186
+ gl_mesh)) {
1157
1187
std::cerr << " SetupMesh for mesh[" << i << " ] failed.\n " ;
1158
1188
exit (-1 );
1159
1189
}
@@ -1396,7 +1426,7 @@ int main(int argc, char **argv) {
1396
1426
1397
1427
std::string filename = " models/suzanne.usdc" ;
1398
1428
// std::string filename = "models/texture-cat-plane.usdc";
1399
- // std::string filename = "models/texturedcube.usdc";
1429
+ // std::string filename = "models/texturedcube.usdc";
1400
1430
// std::string filename = "models/simple-plane.usdz";
1401
1431
#if defined(_MSC_VER)
1402
1432
std::cout << " cwd: " << _getcwd (nullptr , 0 ) << " \n " ;
@@ -1560,6 +1590,24 @@ int main(int argc, char **argv) {
1560
1590
ImGuiInputTextFlags_ReadOnly);
1561
1591
ImGui::End ();
1562
1592
1593
+ // For developers only
1594
+ ImGui::Begin (" dev" );
1595
+ {
1596
+ static bool compile_ok{true };
1597
+
1598
+ if (ImGui::Button (" Reload shader" )) {
1599
+ compile_ok = ReloadShader (curr_shader.get_program (), gCtx .vert_filename ,
1600
+ gCtx .frag_filename );
1601
+ }
1602
+
1603
+ if (compile_ok) {
1604
+ ImGui::TextColored (ImVec4 (0.3 , 1.0 , 0.4 , 1.0 ), " Shader Compile OK" );
1605
+ } else {
1606
+ ImGui::TextColored (ImVec4 (1.0 , 0.2 , 0.1 , 1.0 ), " Shader Compile Failed" );
1607
+ }
1608
+ ImGui::End ();
1609
+ }
1610
+
1563
1611
ImGui::Begin (" Camera" );
1564
1612
ImGui::SliderFloat (" fov" , &gCtx .fov , 0 .0f , 178 .0f );
1565
1613
ImGui::InputFloat (" znear" , &gCtx .znear );
@@ -1587,8 +1635,6 @@ int main(int argc, char **argv) {
1587
1635
gCtx .camera .setRotation ({gCtx .xrotate , gCtx .yrotate , 0 .0f });
1588
1636
gCtx .camera .setPerspective (gCtx .fov , aspect, gCtx .znear , gCtx .zfar );
1589
1637
1590
- gCtx .camera .matrices .perspective ;
1591
-
1592
1638
ImGui::Begin (" View matrix" );
1593
1639
{
1594
1640
tinyusdz::value::matrix4f view;
0 commit comments