19VTK_ABI_NAMESPACE_BEGIN
27 VisRTX::Context* rtx = VisRTX_GetContext();
32 if (this->type ==
"obj" || this->type ==
"luminous")
34 this->material = rtx->CreateBasicMaterial();
43 if (this->type ==
"alloy")
45 else if (this->type ==
"carPaint")
46 this->type =
"CarPaint";
47 else if (this->type ==
"glass")
49 else if (this->type ==
"metal")
51 else if (this->type ==
"metallicPaint")
52 this->type =
"MetallicPaint";
53 else if (this->type ==
"obj")
54 this->type =
"OBJMaterial";
55 else if (this->type ==
"principled")
56 this->type =
"Principled";
57 else if (this->type ==
"thinGlass")
58 this->type =
"ThinGlass";
60 const std::string materialname =
"::ospray::" + this->type;
63 this->material = rtx->CreateMDLMaterial(materialname.c_str(), (
char*)
OSPRay_mdl, (uint32_t)
sizeof(
OSPRay_mdl), 0,
nullptr, VisRTX::CompilationType::INSTANCE);
65 catch(
const std::exception&)
67 vtkLogF(ERROR,
"VisRTX Error: CreateMDLMaterial failed! Falling back to BasicMaterial.");
68 this->material =
nullptr;
72 this->material = rtx->CreateBasicMaterial();
75 assert(this->material !=
nullptr);
80 this->material->Release();
85 assert(this->material !=
nullptr);
90 if (this->type ==
"obj" && this->material->GetType() == VisRTX::MaterialType::BASIC)
92 VisRTX::BasicMaterial* basicMaterial =
dynamic_cast<VisRTX::BasicMaterial*
>(this->material);
93 assert(basicMaterial);
101 basicMaterial->SetDiffuse(this->
GetVec3f({
"kd",
"Kd" }, VisRTX::Vec3f(0.8f, 0.8f, 0.8f)));
102 basicMaterial->SetSpecular(this->
GetVec3f({
"ks",
"Ks" }, VisRTX::Vec3f(0.0f, 0.0f, 0.0f)));
103 basicMaterial->SetShininess(this->
GetFloat({
"ns",
"Ns" }, 10.0f));
104 basicMaterial->SetOpacity(this->
GetFloat({
"d",
"alpha" }, 1.0f));
105 basicMaterial->SetTransparencyFilter(this->
GetVec3f({
"tf",
"Tf" }, VisRTX::Vec3f(0.0f, 0.0f, 0.0f)));
107 Texture* diffuseTex = this->GetObject<Texture>({
"map_Kd",
"map_kd" });
109 basicMaterial->SetDiffuseTexture(diffuseTex->texture);
111 Texture* specularTex = this->GetObject<Texture>({
"map_Ks",
"map_ks" });
113 basicMaterial->SetSpecularTexture(specularTex->texture);
115 Texture* shininessTex = this->GetObject<Texture>({
"map_Ns",
"map_ns" });
117 basicMaterial->SetShininessTexture(shininessTex->texture);
119 Texture* opacityTex = this->GetObject<Texture>({
"map_d",
"map_alpha" });
121 basicMaterial->SetOpacityTexture(opacityTex->texture);
123 Texture* bumpTex = this->GetObject<Texture>({
"map_Bump",
"map_bump" });
125 basicMaterial->SetBumpMapTexture(bumpTex->texture);
131 else if (this->type ==
"luminous" && this->material->GetType() == VisRTX::MaterialType::BASIC)
133 VisRTX::BasicMaterial* basicMaterial =
dynamic_cast<VisRTX::BasicMaterial*
>(this->material);
134 assert(basicMaterial);
139 basicMaterial->SetEmissive(this->
GetVec3f({
"color" }, VisRTX::Vec3f(0.0f, 0.0f, 0.0f)));
140 basicMaterial->SetLuminosity(this->
GetFloat({
"intensity" }, 0.0f));
146 else if (this->material->GetType() == VisRTX::MaterialType::MDL)
148 VisRTX::MDLMaterial* mdlMaterial =
dynamic_cast<VisRTX::MDLMaterial*
>(this->material);
157#define PRINT_MATERIAL_PARAMETERS 0
158#if PRINT_MATERIAL_PARAMETERS
159 static std::set<std::string> mdltypes_printed;
160 if (mdltypes_printed.find(this->type) == mdltypes_printed.end())
162 std::vector<std::string> availableParams;
163 for (uint32_t i = 0; i < mdlMaterial->GetParameterCount(); ++i)
165 availableParams.push_back(mdlMaterial->GetParameterName(i));
168 for (
const auto ¶meter : availableParams)
170 std::string parameterType;
171 switch (mdlMaterial->GetParameterType(parameter.c_str()))
173 case VisRTX::ParameterType::NONE:
174 parameterType =
"none";
break;
175 case VisRTX::ParameterType::COLOR:
176 parameterType =
"color";
break;
177 case VisRTX::ParameterType::DOUBLE:
178 parameterType =
"double";
break;
179 case VisRTX::ParameterType::FLOAT:
180 parameterType =
"float";
break;
181 case VisRTX::ParameterType::INT:
182 parameterType =
"int";
break;
183 case VisRTX::ParameterType::BOOL:
184 parameterType =
"bool";
break;
185 case VisRTX::ParameterType::TEXTURE:
186 parameterType =
"texture";
break;
188 std::stringstream logStrBuf;
189 logStrBuf <<
"(mdl) " << this->type <<
": " << parameterType <<
" " << parameter;
190 vtkLogF(INFO,
"%s", logStrBuf.str().c_str());
192 mdltypes_printed.insert(this->type);
196 static std::set<std::string> ospparams_printed;
198 for (
auto param : ospparams_current)
200 std::string complete = this->type +
": " + param;
201 if (ospparams_printed.find(complete) == ospparams_printed.end())
203 vtkLogF(INFO,
"(osp) %s", complete.c_str());
204 ospparams_printed.insert(complete);
209#define WARN_NOT_IMPLEMENTED() vtkLogF(WARNING, "Warning: type \"%s\" not implemented (Material: %s, %s)", paramType.c_str(), this->type.c_str(), paramName.c_str());
211 for (
const std::string ¶m : ospparams_current)
213 std::string paramType, paramName;
215 std::istringstream iss(param);
221 std::vector<std::string> names;
222 names.push_back(paramName);
226 static const std::map<std::pair<std::string, std::string>, std::string> renameMap
228 { {
"obj",
"map_kd" },
"map_Kd"},
229 { {
"obj",
"map_bump" },
"map_Bump"},
230 { {
"Glass",
"etaInside" },
"eta"},
231 { {
"obj",
"alpha" },
"d"},
232 { {
"ThinGlass",
"transmission" },
"attenuationColor"}
236 auto rename_it = renameMap.find(std::make_pair(this->type, paramName));
237 if (rename_it != renameMap.end())
239 paramName = rename_it->second;
244 const std::string ospSuffix =
"Map";
245 const std::string mdlPrefix =
"map_";
246 if (paramName.length() >= ospSuffix.length()
247 && paramName.compare(paramName.length() - ospSuffix.length(),
248 ospSuffix.length(), ospSuffix) == 0)
251 paramName.substr(0, paramName.length() - ospSuffix.length());
252 paramName = mdlPrefix + name;
257 if (paramName == std::string(
"ior") && paramType == std::string(
"object"))
259 Data* iorData = this->GetObject<Data>(names);
264 vtkLogF(ERROR,
"Error: unexpected data type in ior object");
270 const VisRTX::Vec3f *input = (
const VisRTX::Vec3f*)iorData->
GetData();
272 static const unsigned spectrum_size = 8;
273 static const float wavelength_begin = 430.f;
274 static const float wavelength_spacing = 35.f;
276 float eta[spectrum_size], k[spectrum_size];
279 unsigned iinput = 0u, iprev = 0u;
280 for (
unsigned iwl = 0u; iwl < spectrum_size; iwl++)
282 const float currentwl = wavelength_begin + (float)iwl * wavelength_spacing;
283 for (; iinput < n_input - 1 && input[iinput].x < currentwl; ++iinput)
287 if (input[iprev].x == input[iinput].x)
289 eta[iwl] = input[iprev].y;
290 k[iwl] = input[iprev].z;
294 const float t = (currentwl - input[iprev].x) / (input[iinput].x - input[iprev].x);
295 eta[iwl] = (1.f - t) * input[iprev].y + t * input[iinput].y;
296 k[iwl] = (1.f - t) * input[iprev].z + t * input[iinput].z;
301 static const float response_sRGB_r[spectrum_size] =
313 static const float response_sRGB_g[spectrum_size] =
325 static const float response_sRGB_b[spectrum_size] =
338 VisRTX::Vec3f eta3(0.f, 0.f, 0.f), k3(0.f, 0.f, 0.f);
339 for (
unsigned iwl = 0u; iwl < spectrum_size; ++iwl)
341 VisRTX::Vec3f response(response_sRGB_r[iwl], response_sRGB_g[iwl], response_sRGB_b[iwl]);
343 eta3.x += response.x * eta[iwl];
344 eta3.y += response.y * eta[iwl];
345 eta3.z += response.z * eta[iwl];
346 k3.x += response.x * k[iwl];
347 k3.y += response.y * k[iwl];
348 k3.z += response.z * k[iwl];
351 mdlMaterial->SetParameterColor(
"eta", eta3);
352 mdlMaterial->SetParameterColor(
"k", k3);
354 else if (paramType == std::string(
"string"))
356 std::string ospParam = this->
GetString(names);
359 else if (paramType == std::string(
"object"))
361 Texture* ospParam = this->GetObject<Texture>(names);
364 mdlMaterial->SetParameterTexture(paramName.c_str(), ospParam->texture);
368 vtkLogF(WARNING,
"Object \"%s\" of material type \"%s\" is not a texture.", paramName.c_str(), this->type.c_str());
371 else if (paramType == std::string(
"int1"))
373 int ospParam = this->
GetInt(names);
375 if (mdlMaterial->GetParameterType(paramName.c_str()) == VisRTX::ParameterType::BOOL)
377 mdlMaterial->SetParameterBool(paramName.c_str(), ospParam > 0);
381 mdlMaterial->SetParameterInt(paramName.c_str(), ospParam);
384 else if (paramType == std::string(
"float1"))
386 float ospParam = this->
GetFloat(names);
388 if (mdlMaterial->GetParameterType(paramName.c_str()) == VisRTX::ParameterType::BOOL)
390 mdlMaterial->SetParameterBool(paramName.c_str(), ospParam > 0.0f);
394 mdlMaterial->SetParameterFloat(paramName.c_str(), ospParam);
397 else if (paramType == std::string(
"float2"))
399 VisRTX::Vec2f ospParam = this->
GetVec2f(names);
402 else if (paramType == std::string(
"int3"))
404 VisRTX::Vec3i ospParam = this->
GetVec3i(names);
407 else if (paramType == std::string(
"float3"))
409 VisRTX::Vec3f ospParam = this->
GetVec3f(names);
410 mdlMaterial->SetParameterColor(paramName.c_str(), ospParam);
412 else if (paramType == std::string(
"float4"))
414 VisRTX::Vec4f ospParam = this->
GetVec4f(names);
423 mdlMaterial->Compile();
429 VisRTX::Material* material =
nullptr;
#define WARN_NOT_IMPLEMENTED()
unsigned char OSPRay_mdl[]
size_t GetNumElements() const
RTWDataType GetElementDataType() const
Material(const std::string &type)
int32_t GetInt(const std::vector< std::string > &ids, int32_t defaultValue=0, bool *found=nullptr) const
const std::string GetString(const std::vector< std::string > &ids, const std::string &defaultValue="", bool *found=nullptr) const
VisRTX::Vec2f GetVec2f(const std::vector< std::string > &ids, const VisRTX::Vec2f &defaultValue=VisRTX::Vec2f(), bool *found=nullptr) const
std::set< std::string > GetAllParameters() const
VisRTX::Vec3i GetVec3i(const std::vector< std::string > &ids, const VisRTX::Vec3i &defaultValue=VisRTX::Vec3i(), bool *found=nullptr) const
float GetFloat(const std::vector< std::string > &ids, float defaultValue=0.0f, bool *found=nullptr) const
VisRTX::Vec4f GetVec4f(const std::vector< std::string > &ids, const VisRTX::Vec4f &defaultValue=VisRTX::Vec4f(), bool *found=nullptr) const
VisRTX::Vec3f GetVec3f(const std::vector< std::string > &ids, const VisRTX::Vec3f &defaultValue=VisRTX::Vec3f(), bool *found=nullptr) const
#define vtkLogF(verbosity_name,...)
Add to log given the verbosity level.