22 VisRTX::Context* rtx = VisRTX_GetContext();
27 if (this->type ==
"OBJMaterial" || this->type ==
"Luminous")
29 this->material = rtx->CreateBasicMaterial();
40 this->material = rtx->CreateMDLMaterial(materialname.c_str(), (
char*)
OSPRay_mdl, (uint32_t)
sizeof(
OSPRay_mdl), 0,
nullptr, VisRTX::CompilationType::INSTANCE);
42 catch(
const std::exception&)
44 std::cerr <<
"CreateMDLMaterial failed! Falling back to BasicMaterial.\n";
45 this->material =
nullptr;
49 this->material = rtx->CreateBasicMaterial();
52 assert(this->material !=
nullptr);
57 this->material->Release();
62 assert(this->material !=
nullptr);
67 if (this->
type ==
"OBJMaterial" && this->material->GetType() == VisRTX::MaterialType::BASIC)
69 VisRTX::BasicMaterial* basicMaterial =
dynamic_cast<VisRTX::BasicMaterial*
>(this->material);
70 assert(basicMaterial);
78 basicMaterial->SetDiffuse(this->
Get3f({
"Kd" }, VisRTX::Vec3f(0.8f, 0.8f, 0.8f)));
79 basicMaterial->SetSpecular(this->
Get3f({
"Ks" }, VisRTX::Vec3f(0.0f, 0.0f, 0.0f)));
80 basicMaterial->SetShininess(this->
Get1f({
"Ns" }, 10.0f));
81 basicMaterial->SetOpacity(this->
Get1f({
"d",
"alpha" }, 1.0f));
82 basicMaterial->SetTransparencyFilter(this->
Get3f({
"Tf" }, VisRTX::Vec3f(0.0f, 0.0f, 0.0f)));
84 Texture* diffuseTex = this->GetObject<Texture>({
"map_Kd",
"map_kd" });
86 basicMaterial->SetDiffuseTexture(diffuseTex->texture);
88 Texture* specularTex = this->GetObject<Texture>({
"map_Ks",
"map_ks" });
90 basicMaterial->SetSpecularTexture(specularTex->texture);
92 Texture* shininessTex = this->GetObject<Texture>({
"map_Ns",
"map_ns" });
94 basicMaterial->SetShininessTexture(shininessTex->texture);
96 Texture* opacityTex = this->GetObject<Texture>({
"map_d",
"map_alpha" });
98 basicMaterial->SetOpacityTexture(opacityTex->texture);
100 Texture* bumpTex = this->GetObject<Texture>({
"map_Bump",
"map_bump" });
102 basicMaterial->SetBumpMapTexture(bumpTex->texture);
108 else if (this->
type ==
"Luminous" && this->material->GetType() == VisRTX::MaterialType::BASIC)
110 VisRTX::BasicMaterial* basicMaterial =
dynamic_cast<VisRTX::BasicMaterial*
>(this->material);
111 assert(basicMaterial);
116 basicMaterial->SetEmissive(this->
Get3f({
"color" }, VisRTX::Vec3f(0.0f, 0.0f, 0.0f)));
117 basicMaterial->SetLuminosity(this->
Get1f({
"intensity" }, 0.0f));
123 else if (this->material->GetType() == VisRTX::MaterialType::MDL)
125 VisRTX::MDLMaterial* mdlMaterial =
dynamic_cast<VisRTX::MDLMaterial*
>(this->material);
134 #define PRINT_MATERIAL_PARAMETERS 0 135 #if PRINT_MATERIAL_PARAMETERS 136 static std::set<std::string> mdltypes_printed;
137 if (mdltypes_printed.find(this->type) == mdltypes_printed.end())
139 std::vector<std::string> availableParams;
140 for (uint32_t i = 0; i < mdlMaterial->GetParameterCount(); ++i)
142 availableParams.push_back(mdlMaterial->GetParameterName(i));
145 for (
const auto &
parameter : availableParams)
148 switch (mdlMaterial->GetParameterType(
parameter.c_str()))
150 case VisRTX::ParameterType::NONE:
151 parameterType =
"none";
break;
152 case VisRTX::ParameterType::COLOR:
153 parameterType =
"color";
break;
154 case VisRTX::ParameterType::DOUBLE:
155 parameterType =
"double";
break;
156 case VisRTX::ParameterType::FLOAT:
157 parameterType =
"float";
break;
158 case VisRTX::ParameterType::INT:
159 parameterType =
"int";
break;
160 case VisRTX::ParameterType::BOOL:
161 parameterType =
"bool";
break;
162 case VisRTX::ParameterType::TEXTURE:
163 parameterType =
"texture";
break;
165 std::cerr <<
"(mdl) " << this->
type <<
": " << parameterType <<
" " <<
parameter <<
"\n";
167 mdltypes_printed.insert(this->
type);
171 static std::set<std::string> ospparams_printed;
173 for (
auto param : ospparams_current)
176 if (ospparams_printed.find(complete) == ospparams_printed.end())
178 std::cerr <<
"(osp) " << complete <<
"\n";
179 ospparams_printed.insert(complete);
182 #endif //PRINT_MATERIAL_PARAMETERS 184 #define WARN_NOT_IMPLEMENTED() std::cerr<<"Warning: type \""<<paramType<<"\" not implemented (Material: "<<this->type<<", "<<paramName<<")\n"; 190 std::istringstream iss(param);
196 std::vector<std::string> names;
197 names.push_back(paramName);
201 static const std::map<std::pair<std::string, std::string>,
std::string> renameMap
203 { {
"OBJMaterial",
"map_kd" },
"map_Kd"},
204 { {
"OBJMaterial",
"map_bump" },
"map_Bump"},
205 { {
"Glass",
"etaInside" },
"eta"},
206 { {
"OBJMaterial",
"alpha" },
"d"},
207 { {
"ThinGlass",
"transmission" },
"attenuationColor"}
211 auto rename_it = renameMap.find(std::make_pair(this->
type, paramName));
212 if (rename_it != renameMap.end())
214 paramName = rename_it->second;
221 if (paramName.length() >= ospSuffix.length()
222 && paramName.compare(paramName.length() - ospSuffix.length(),
223 ospSuffix.length(), ospSuffix) == 0)
226 paramName.substr(0, paramName.length() - ospSuffix.length());
227 paramName = mdlPrefix +
name;
234 Data* iorData = this->GetObject<Data>(names);
239 std::cerr <<
"Error: unexpected data type in ior object\n";
245 const VisRTX::Vec3f *input = (
const VisRTX::Vec3f*)iorData->
GetData();
247 static const unsigned spectrum_size = 8;
248 static const float wavelength_begin = 430.f;
249 static const float wavelength_spacing = 35.f;
251 float eta[spectrum_size], k[spectrum_size];
254 unsigned iinput = 0u, iprev = 0u;
255 for (
unsigned iwl = 0u; iwl < spectrum_size; iwl++)
257 const float currentwl = wavelength_begin + (float)iwl * wavelength_spacing;
258 for (; iinput < n_input - 1 && input[iinput].x < currentwl; ++iinput)
262 if (input[iprev].x == input[iinput].x)
264 eta[iwl] = input[iprev].y;
265 k[iwl] = input[iprev].z;
269 const float t = (currentwl - input[iprev].x) / (input[iinput].x - input[iprev].x);
270 eta[iwl] = (1.f - t) * input[iprev].y + t * input[iinput].y;
271 k[iwl] = (1.f - t) * input[iprev].z + t * input[iinput].z;
276 static const float response_sRGB_r[spectrum_size] =
288 static const float response_sRGB_g[spectrum_size] =
300 static const float response_sRGB_b[spectrum_size] =
313 VisRTX::Vec3f eta3(0.f, 0.f, 0.f), k3(0.f, 0.f, 0.f);
314 for (
unsigned iwl = 0u; iwl < spectrum_size; ++iwl)
316 VisRTX::Vec3f response(response_sRGB_r[iwl], response_sRGB_g[iwl], response_sRGB_b[iwl]);
318 eta3.x += response.x * eta[iwl];
319 eta3.y += response.y * eta[iwl];
320 eta3.z += response.z * eta[iwl];
321 k3.x += response.x * k[iwl];
322 k3.y += response.y * k[iwl];
323 k3.z += response.z * k[iwl];
326 mdlMaterial->SetParameterColor(
"eta", eta3);
327 mdlMaterial->SetParameterColor(
"k", k3);
336 Texture* ospParam = this->GetObject<Texture>(names);
339 mdlMaterial->SetParameterTexture(paramName.c_str(), ospParam->texture);
343 std::cerr <<
"Object \"" << paramName <<
"\" of material type \"" << this->
type <<
"\" is not a texture.\n";
348 int ospParam = this->
Get1i(names);
350 if (mdlMaterial->GetParameterType(paramName.c_str()) == VisRTX::ParameterType::BOOL)
352 mdlMaterial->SetParameterBool(paramName.c_str(), ospParam > 0);
356 mdlMaterial->SetParameterInt(paramName.c_str(), ospParam);
361 float ospParam = this->
Get1f(names);
363 if (mdlMaterial->GetParameterType(paramName.c_str()) == VisRTX::ParameterType::BOOL)
365 mdlMaterial->SetParameterBool(paramName.c_str(), ospParam > 0.0f);
369 mdlMaterial->SetParameterFloat(paramName.c_str(), ospParam);
374 VisRTX::Vec2f ospParam = this->
Get2f(names);
379 VisRTX::Vec3i ospParam = this->
Get3i(names);
384 VisRTX::Vec3f ospParam = this->
Get3f(names);
385 mdlMaterial->SetParameterColor(paramName.c_str(), ospParam);
389 VisRTX::Vec4f ospParam = this->
Get4f(names);
398 mdlMaterial->Compile();
VisRTX::Vec4f Get4f(const std::vector< std::string > &ids, const VisRTX::Vec4f &defaultValue=VisRTX::Vec4f(), bool *found=nullptr) const
Material(const std::string &type)
RTWDataType GetDataType() const
VisRTX::Vec3f Get3f(const std::vector< std::string > &ids, const VisRTX::Vec3f &defaultValue=VisRTX::Vec3f(), bool *found=nullptr) const
VisRTX::Vec3i Get3i(const std::vector< std::string > &ids, const VisRTX::Vec3i &defaultValue=VisRTX::Vec3i(), bool *found=nullptr) const
int32_t Get1i(const std::vector< std::string > &ids, int32_t defaultValue=0, bool *found=nullptr) const
unsigned char OSPRay_mdl[]
const std::string GetString(const std::vector< std::string > &ids, const std::string &defaultValue="", bool *found=nullptr) const
VisRTX::Vec2f Get2f(const std::vector< std::string > &ids, const VisRTX::Vec2f &defaultValue=VisRTX::Vec2f(), bool *found=nullptr) const
float Get1f(const std::vector< std::string > &ids, float defaultValue=0.0f, bool *found=nullptr) const
std::set< std::string > GetAllParameters() const
size_t GetNumElements() const
#define WARN_NOT_IMPLEMENTED()