Skip to content

Commit 1ba4572

Browse files
author
Bruce Cherniak
committed
Extend obj/mtl importer to allow texture "wrapMode"
- As has been done with other parameters, mtl parsing has been extended to allow OSPRay specific values for "wrapMode". - map_<textureName>.wrapMode can be specified as: "repeat" (default), "mirror", "clamp" ex: "map_baseColor.wrapMode mirror clamp" (a single value sets both s and t coordinates the same)
1 parent f7308dd commit 1ba4572

File tree

1 file changed

+81
-8
lines changed

1 file changed

+81
-8
lines changed

sg/importer/OBJ.cpp

+81-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include "sg/scene/geometry/Geometry.h"
1010
#include "sg/Util.h"
1111

12+
using namespace rkcommon::utility;
13+
using namespace rkcommon::containers;
14+
1215
namespace ospray {
1316
namespace sg {
1417

@@ -35,7 +38,7 @@ namespace ospray {
3538

3639
static inline void parseParameterString(std::string typeAndValueString,
3740
std::string &paramType,
38-
rkcommon::utility::Any &paramValue)
41+
Any &paramValue)
3942
{
4043
std::stringstream typeAndValueStream(typeAndValueString);
4144
std::string paramValueString;
@@ -192,6 +195,56 @@ namespace ospray {
192195
return {};
193196
}
194197

198+
void addTextureWrapModes(std::shared_ptr<Texture2D> ospTex,
199+
std::string mapName,
200+
std::map<std::string, std::string> unknown_parameter)
201+
{
202+
// Parse texture wrapMode
203+
// Check whether this map has a wrapMode parameter add it to the
204+
// texture node if it does.
205+
// Need to find based on "param.first" and not paramName,
206+
// paramName may have been modified to convert naming style
207+
auto it = unknown_parameter.find(mapName + ".wrapMode");
208+
if (it != unknown_parameter.end()) {
209+
auto wrapModeStr = it->second;
210+
211+
bool error = false;
212+
213+
// Convert from wrapMode string to OSPTextureWrapMode enum
214+
std::function<OSPTextureWrapMode(std::string)> convertWrapMode =
215+
[&](std::string wrapMode) {
216+
static std::map<std::string, OSPTextureWrapMode> WrapModesMap = {
217+
{"repeat", OSP_TEXTURE_WRAP_REPEAT}, // default
218+
{"mirror", OSP_TEXTURE_WRAP_MIRRORED_REPEAT},
219+
{"clamp", OSP_TEXTURE_WRAP_CLAMP_TO_EDGE}};
220+
auto ospMode = OSP_TEXTURE_WRAP_REPEAT;
221+
if (WrapModesMap.count(wrapMode))
222+
ospMode = WrapModesMap[wrapMode];
223+
else
224+
error |= true;
225+
return ospMode;
226+
};
227+
228+
auto modes = split(wrapModeStr, ' ');
229+
if (modes.size() >= 1 && modes.size() <= 2) {
230+
auto s = convertWrapMode(modes[0]);
231+
if (modes.size() == 1) {
232+
// If only one param, set s and it affects both s and t
233+
ospTex->createChild("wrapMode", "uint32_t", uint32_t(s));
234+
} else {
235+
// else set both s and t params
236+
auto t = convertWrapMode(modes[1]);
237+
ospTex->createChild("wrapMode", "vec2ui", vec2ui(s, t));
238+
}
239+
} else
240+
error = true;
241+
242+
if (error)
243+
std::cerr << "#ospsg: obj parsing errors(s)... Uknown wrapMode "
244+
<< it->first << " " << wrapModeStr << std::endl;
245+
}
246+
}
247+
195248
static std::vector<NodePtr> createMaterials(const OBJData &objData,
196249
FileName fileName)
197250
{
@@ -263,8 +316,8 @@ namespace ospray {
263316
preferLinear,
264317
nearestFilter);
265318

266-
// If texture is UDIM, set appropriate scale/translation
267319
if (map_misc) {
320+
// If texture is UDIM, set appropriate scale/translation
268321
auto &ospTex = *map_misc->nodeAs<Texture2D>();
269322
if (ospTex.hasUDIM()) {
270323
auto scale = ospTex.getUDIM_scale();
@@ -275,11 +328,16 @@ namespace ospray {
275328
paramNodes.push_back(createNode(
276329
paramName + ".translation", "vec2f", translation));
277330
}
331+
332+
// Need to find based on "param.first" and not paramName,
333+
// paramName may have been modified to convert naming style
334+
addTextureWrapModes(map_misc, param.first, m.unknown_parameter);
335+
278336
paramNodes.push_back(map_misc);
279337
}
280338

281339
} else {
282-
rkcommon::utility::Any paramValue;
340+
Any paramValue;
283341
parseParameterString(paramValueStr, paramType, paramValue);
284342
// Principled::thin is the only non-float material parameter and
285343
// needs to be converted to a "bool" paramType, imported as a float
@@ -291,6 +349,11 @@ namespace ospray {
291349
paramType = "rgb";
292350
}
293351

352+
// texture wrapMode is handled entirely above, with the texture
353+
// creation.
354+
if (paramName.find(".wrapMode") != std::string::npos)
355+
continue;
356+
294357
try {
295358
auto newParam = createNode(paramName, paramType, paramValue);
296359
paramNodes.push_back(newParam);
@@ -322,36 +385,46 @@ namespace ospray {
322385
if (!m.diffuse_texname.empty()) {
323386
// sRGB gamma encoded, texture format may override this preference
324387
auto tex = createSGTex("map_kd", m.diffuse_texname, containingPath);
325-
if (tex)
388+
if (tex) {
389+
addTextureWrapModes(tex, "map_kd", m.unknown_parameter);
326390
mat.add(tex);
391+
}
327392
}
328393

329394
if (!m.specular_texname.empty()) {
330395
// sRGB gamma encoded, texture format may override this preference
331396
auto tex = createSGTex("map_ks", m.specular_texname, containingPath);
332-
if (tex)
397+
if (tex) {
398+
addTextureWrapModes(tex, "map_ks", m.unknown_parameter);
333399
mat.add(tex);
400+
}
334401
}
335402

336403
if (!m.specular_highlight_texname.empty()) {
337404
auto tex = createSGTex(
338405
"map_ns", m.specular_highlight_texname, containingPath, true);
339-
if (tex)
406+
if (tex) {
407+
addTextureWrapModes(tex, "map_nd", m.unknown_parameter);
340408
mat.add(tex);
409+
}
341410
}
342411

343412
if (!m.bump_texname.empty()) {
344413
auto tex =
345414
createSGTex("map_bump", m.bump_texname, containingPath, true);
346-
if (tex)
415+
if (tex) {
416+
addTextureWrapModes(tex, "map_bump", m.unknown_parameter);
347417
mat.add(tex);
418+
}
348419
}
349420

350421
if (!m.alpha_texname.empty()) {
351422
auto tex =
352423
createSGTex("map_d", m.alpha_texname, containingPath, true);
353-
if (tex)
424+
if (tex) {
425+
addTextureWrapModes(tex, "map_d", m.unknown_parameter);
354426
mat.add(tex);
427+
}
355428
}
356429
for (auto &param : paramNodes)
357430
mat.add(param);

0 commit comments

Comments
 (0)