25 VisRTX::Context* rtx = VisRTX_GetContext();
30 if (this->type ==
"obj" || this->type ==
"luminous")
32 this->material = rtx->CreateBasicMaterial();
41 if (this->type ==
"alloy")
43 else if (this->type ==
"carPaint")
44 this->type =
"CarPaint";
45 else if (this->type ==
"glass")
47 else if (this->type ==
"metal")
49 else if (this->type ==
"metallicPaint")
50 this->type =
"MetallicPaint";
51 else if (this->type ==
"obj")
52 this->type =
"OBJMaterial";
53 else if (this->type ==
"principled")
54 this->type =
"Principled";
55 else if (this->type ==
"thinGlass")
56 this->type =
"ThinGlass";
58 const std::string materialname =
"::ospray::" + this->type;
61 this->material = rtx->CreateMDLMaterial(materialname.c_str(), (
char*)
OSPRay_mdl, (uint32_t)
sizeof(
OSPRay_mdl), 0,
nullptr, VisRTX::CompilationType::INSTANCE);
63 catch(
const std::exception&)
65 vtkLogF(ERROR,
"VisRTX Error: CreateMDLMaterial failed! Falling back to BasicMaterial.");
66 this->material =
nullptr;
70 this->material = rtx->CreateBasicMaterial();
73 assert(this->material !=
nullptr);
78 this->material->Release();
83 assert(this->material !=
nullptr);
88 if (this->type ==
"obj" && this->material->GetType() == VisRTX::MaterialType::BASIC)
90 VisRTX::BasicMaterial* basicMaterial =
dynamic_cast<VisRTX::BasicMaterial*
>(this->material);
91 assert(basicMaterial);
99 basicMaterial->SetDiffuse(this->
GetVec3f({
"kd",
"Kd" }, VisRTX::Vec3f(0.8f, 0.8f, 0.8f)));
100 basicMaterial->SetSpecular(this->
GetVec3f({
"ks",
"Ks" }, VisRTX::Vec3f(0.0f, 0.0f, 0.0f)));
101 basicMaterial->SetShininess(this->
GetFloat({
"ns",
"Ns" }, 10.0f));
102 basicMaterial->SetOpacity(this->
GetFloat({
"d",
"alpha" }, 1.0f));
103 basicMaterial->SetTransparencyFilter(this->
GetVec3f({
"tf",
"Tf" }, VisRTX::Vec3f(0.0f, 0.0f, 0.0f)));
105 Texture* diffuseTex = this->GetObject<Texture>({
"map_Kd",
"map_kd" });
107 basicMaterial->SetDiffuseTexture(diffuseTex->texture);
109 Texture* specularTex = this->GetObject<Texture>({
"map_Ks",
"map_ks" });
111 basicMaterial->SetSpecularTexture(specularTex->texture);
113 Texture* shininessTex = this->GetObject<Texture>({
"map_Ns",
"map_ns" });
115 basicMaterial->SetShininessTexture(shininessTex->texture);
117 Texture* opacityTex = this->GetObject<Texture>({
"map_d",
"map_alpha" });
119 basicMaterial->SetOpacityTexture(opacityTex->texture);
121 Texture* bumpTex = this->GetObject<Texture>({
"map_Bump",
"map_bump" });
123 basicMaterial->SetBumpMapTexture(bumpTex->texture);
129 else if (this->type ==
"luminous" && this->material->GetType() == VisRTX::MaterialType::BASIC)
131 VisRTX::BasicMaterial* basicMaterial =
dynamic_cast<VisRTX::BasicMaterial*
>(this->material);
132 assert(basicMaterial);
137 basicMaterial->SetEmissive(this->
GetVec3f({
"color" }, VisRTX::Vec3f(0.0f, 0.0f, 0.0f)));
138 basicMaterial->SetLuminosity(this->
GetFloat({
"intensity" }, 0.0f));
144 else if (this->material->GetType() == VisRTX::MaterialType::MDL)
146 VisRTX::MDLMaterial* mdlMaterial =
dynamic_cast<VisRTX::MDLMaterial*
>(this->material);
155#define PRINT_MATERIAL_PARAMETERS 0
156#if PRINT_MATERIAL_PARAMETERS
157 static std::set<std::string> mdltypes_printed;
158 if (mdltypes_printed.find(this->type) == mdltypes_printed.end())
160 std::vector<std::string> availableParams;
161 for (uint32_t i = 0; i < mdlMaterial->GetParameterCount(); ++i)
163 availableParams.push_back(mdlMaterial->GetParameterName(i));
166 for (
const auto ¶meter : availableParams)
168 std::string parameterType;
169 switch (mdlMaterial->GetParameterType(parameter.c_str()))
171 case VisRTX::ParameterType::NONE:
172 parameterType =
"none";
break;
173 case VisRTX::ParameterType::COLOR:
174 parameterType =
"color";
break;
175 case VisRTX::ParameterType::DOUBLE:
176 parameterType =
"double";
break;
177 case VisRTX::ParameterType::FLOAT:
178 parameterType =
"float";
break;
179 case VisRTX::ParameterType::INT:
180 parameterType =
"int";
break;
181 case VisRTX::ParameterType::BOOL:
182 parameterType =
"bool";
break;
183 case VisRTX::ParameterType::TEXTURE:
184 parameterType =
"texture";
break;
186 std::stringstream logStrBuf;
187 logStrBuf <<
"(mdl) " << this->type <<
": " << parameterType <<
" " << parameter;
188 vtkLogF(INFO,
"%s", logStrBuf.str().c_str());
190 mdltypes_printed.insert(this->type);
194 static std::set<std::string> ospparams_printed;
196 for (
auto param : ospparams_current)
198 std::string complete = this->type +
": " + param;
199 if (ospparams_printed.find(complete) == ospparams_printed.end())
201 vtkLogF(INFO,
"(osp) %s", complete.c_str());
202 ospparams_printed.insert(complete);
207#define WARN_NOT_IMPLEMENTED() vtkLogF(WARNING, "Warning: type \"%s\" not implemented (Material: %s, %s)", paramType.c_str(), this->type.c_str(), paramName.c_str());
209 for (
const std::string ¶m : ospparams_current)
211 std::string paramType, paramName;
213 std::istringstream iss(param);
219 std::vector<std::string> names;
220 names.push_back(paramName);
224 static const std::map<std::pair<std::string, std::string>, std::string> renameMap
226 { {
"obj",
"map_kd" },
"map_Kd"},
227 { {
"obj",
"map_bump" },
"map_Bump"},
228 { {
"Glass",
"etaInside" },
"eta"},
229 { {
"obj",
"alpha" },
"d"},
230 { {
"ThinGlass",
"transmission" },
"attenuationColor"}
234 auto rename_it = renameMap.find(std::make_pair(this->type, paramName));
235 if (rename_it != renameMap.end())
237 paramName = rename_it->second;
242 const std::string ospSuffix =
"Map";
243 const std::string mdlPrefix =
"map_";
244 if (paramName.length() >= ospSuffix.length()
245 && paramName.compare(paramName.length() - ospSuffix.length(),
246 ospSuffix.length(), ospSuffix) == 0)
249 paramName.substr(0, paramName.length() - ospSuffix.length());
250 paramName = mdlPrefix + name;
255 if (paramName == std::string(
"ior") && paramType == std::string(
"object"))
257 Data* iorData = this->GetObject<Data>(names);
262 vtkLogF(ERROR,
"Error: unexpected data type in ior object");
268 const VisRTX::Vec3f *input = (
const VisRTX::Vec3f*)iorData->
GetData();
270 static const unsigned spectrum_size = 8;
271 static const float wavelength_begin = 430.f;
272 static const float wavelength_spacing = 35.f;
274 float eta[spectrum_size], k[spectrum_size];
277 unsigned iinput = 0u, iprev = 0u;
278 for (
unsigned iwl = 0u; iwl < spectrum_size; iwl++)
280 const float currentwl = wavelength_begin + (float)iwl * wavelength_spacing;
281 for (; iinput < n_input - 1 && input[iinput].x < currentwl; ++iinput)
285 if (input[iprev].x == input[iinput].x)
287 eta[iwl] = input[iprev].y;
288 k[iwl] = input[iprev].z;
292 const float t = (currentwl - input[iprev].x) / (input[iinput].x - input[iprev].x);
293 eta[iwl] = (1.f - t) * input[iprev].y + t * input[iinput].y;
294 k[iwl] = (1.f - t) * input[iprev].z + t * input[iinput].z;
299 static const float response_sRGB_r[spectrum_size] =
311 static const float response_sRGB_g[spectrum_size] =
323 static const float response_sRGB_b[spectrum_size] =
336 VisRTX::Vec3f eta3(0.f, 0.f, 0.f), k3(0.f, 0.f, 0.f);
337 for (
unsigned iwl = 0u; iwl < spectrum_size; ++iwl)
339 VisRTX::Vec3f response(response_sRGB_r[iwl], response_sRGB_g[iwl], response_sRGB_b[iwl]);
341 eta3.x += response.x * eta[iwl];
342 eta3.y += response.y * eta[iwl];
343 eta3.z += response.z * eta[iwl];
344 k3.x += response.x * k[iwl];
345 k3.y += response.y * k[iwl];
346 k3.z += response.z * k[iwl];
349 mdlMaterial->SetParameterColor(
"eta", eta3);
350 mdlMaterial->SetParameterColor(
"k", k3);
352 else if (paramType == std::string(
"string"))
354 std::string ospParam = this->
GetString(names);
357 else if (paramType == std::string(
"object"))
359 Texture* ospParam = this->GetObject<Texture>(names);
362 mdlMaterial->SetParameterTexture(paramName.c_str(), ospParam->texture);
366 vtkLogF(WARNING,
"Object \"%s\" of material type \"%s\" is not a texture.", paramName.c_str(), this->type.c_str());
369 else if (paramType == std::string(
"int1"))
371 int ospParam = this->
GetInt(names);
373 if (mdlMaterial->GetParameterType(paramName.c_str()) == VisRTX::ParameterType::BOOL)
375 mdlMaterial->SetParameterBool(paramName.c_str(), ospParam > 0);
379 mdlMaterial->SetParameterInt(paramName.c_str(), ospParam);
382 else if (paramType == std::string(
"float1"))
384 float ospParam = this->
GetFloat(names);
386 if (mdlMaterial->GetParameterType(paramName.c_str()) == VisRTX::ParameterType::BOOL)
388 mdlMaterial->SetParameterBool(paramName.c_str(), ospParam > 0.0f);
392 mdlMaterial->SetParameterFloat(paramName.c_str(), ospParam);
395 else if (paramType == std::string(
"float2"))
397 VisRTX::Vec2f ospParam = this->
GetVec2f(names);
400 else if (paramType == std::string(
"int3"))
402 VisRTX::Vec3i ospParam = this->
GetVec3i(names);
405 else if (paramType == std::string(
"float3"))
407 VisRTX::Vec3f ospParam = this->
GetVec3f(names);
408 mdlMaterial->SetParameterColor(paramName.c_str(), ospParam);
410 else if (paramType == std::string(
"float4"))
412 VisRTX::Vec4f ospParam = this->
GetVec4f(names);
421 mdlMaterial->Compile();
427 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.