Skip to content

Commit c0d4777

Browse files
Half implement normal map handling for @Crisspl
1 parent 7006828 commit c0d4777

File tree

6 files changed

+22
-13
lines changed

6 files changed

+22
-13
lines changed

include/nbl/ext/MitsubaLoader/CElementBSDF.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ class CElementBSDF : public IElement
242242
struct BumpMap : MetaBSDF
243243
{
244244
CElementTexture* texture;
245+
bool wasNormal;
245246
};
246247
struct MixtureBSDF : MetaBSDF
247248
{

include/nbl/ext/MitsubaLoader/CMitsubaMaterialCompilerFrontend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CMitsubaMaterialCompilerFrontend
2323

2424
const SContext* m_loaderContext;
2525

26-
tex_ass_type getDerivMap(const CElementTexture* _element) const;
26+
tex_ass_type getDerivMap(const CElementBSDF::BumpMap& _bump) const;
2727
tex_ass_type getBlendWeightTex(const CElementTexture* _element) const;
2828

2929
std::pair<const CElementTexture*, float> getTexture_common(const CElementTexture* _element) const;

include/nbl/ext/MitsubaLoader/SContext.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace MitsubaLoader
6161
return imageCacheKey + "?view";
6262
}
6363

64-
static std::string derivMapCacheKey(const CElementTexture* bitmap)
64+
static std::string derivMapCacheKey(const CElementBSDF::BumpMap& _bump)
6565
{
6666
using namespace std::string_literals;
6767
static const char* wrap[5]
@@ -73,16 +73,18 @@ namespace MitsubaLoader
7373
"?one"
7474
};
7575

76+
const auto* bitmap = _bump.texture;
7677
std::string key = bitmap->bitmap.filename.svalue + "?deriv"s;
78+
key += _bump.wasNormal ? "n"s:"h"s;
7779
key += wrap[bitmap->bitmap.wrapModeU];
7880
key += wrap[bitmap->bitmap.wrapModeV];
7981

8082
return key;
8183
}
8284

83-
static std::string derivMapViewCacheKey(const CElementTexture* bitmap)
85+
static std::string derivMapViewCacheKey(const CElementBSDF::BumpMap& _bump)
8486
{
85-
return imageViewCacheKey(derivMapCacheKey(bitmap));
87+
return imageViewCacheKey(derivMapCacheKey(_bump));
8688
}
8789

8890
static std::string blendWeightViewCacheKey(const CElementTexture* bitmap)

src/nbl/ext/MitsubaLoader/CElementBSDF.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ CElementFactory::return_type CElementFactory::createElement<CElementBSDF>(const
4040
{"coating", CElementBSDF::Type::COATING},
4141
{"roughcoating", CElementBSDF::Type::ROUGHCOATING},
4242
{"bumpmap", CElementBSDF::Type::BUMPMAP},
43+
{"normalmap", CElementBSDF::Type::BUMPMAP},
4344
{"phong", CElementBSDF::Type::PHONG},
4445
{"ward", CElementBSDF::Type::WARD},
4546
{"mixturebsdf", CElementBSDF::Type::MIXTURE_BSDF},
@@ -96,6 +97,7 @@ CElementFactory::return_type CElementFactory::createElement<CElementBSDF>(const
9697
break;
9798
case CElementBSDF::Type::BUMPMAP:
9899
obj->bumpmap = CElementBSDF::BumpMap();
100+
obj->bumpmap.wasNormal = strcmp(type,"bumpmap")!=0;
99101
break;
100102
case CElementBSDF::Type::PHONG:
101103
obj->phong = CElementBSDF::Phong();

src/nbl/ext/MitsubaLoader/CMitsubaLoader.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,12 @@ static core::smart_refctd_ptr<asset::ICPUImageView> createImageView(core::smart_
270270

271271
return asset::ICPUImageView::create(std::move(params));
272272
}
273-
static core::smart_refctd_ptr<asset::ICPUImage> createDerivMap(asset::ICPUImage* _heightMap, asset::ICPUSampler* _smplr)
273+
static core::smart_refctd_ptr<asset::ICPUImage> createDerivMap(asset::ICPUImage* _heightMap, asset::ICPUSampler* _smplr, bool fromNormalMap)
274274
{
275275
const auto& sp = _smplr->getParams();
276276

277+
if (fromNormalMap) // TODO: use the normalmap to derivative map filter and utils from the glTF PR
278+
os::Printer::log("Normalmaps not implemented yet! Treating with Bumpmap creation pipeline, expect results to be off!",ELL_ERROR);
277279
return asset::CDerivativeMapCreator::createDerivativeMapFromHeightMap(
278280
_heightMap,
279281
static_cast<asset::ICPUSampler::E_TEXTURE_CLAMP>(sp.TextureWrapU),
@@ -1269,11 +1271,12 @@ auto CMitsubaLoader::genBSDFtreeTraversal(SContext& ctx, const CElementBSDF* _bs
12691271
// TODO check and restore if dummy (image and sampler)
12701272
auto bumpmap = std::get<0>(bm)->getCreationParameters().image;
12711273
auto sampler = std::get<1>(bm);
1272-
const std::string key = ctx.derivMapCacheKey(bumpmap_element);
1274+
const std::string key = ctx.derivMapCacheKey(bsdf->bumpmap);
12731275

12741276
if (!getBuiltinAsset<asset::ICPUImage, asset::IAsset::ET_IMAGE>(key.c_str(), m_assetMgr))
12751277
{
1276-
auto derivmap = createDerivMap(bumpmap.get(), sampler.get());
1278+
// TODO: @Crisspl retrieve the normalization factor from the deriv map filter, then adjust the scale accordingly!
1279+
auto derivmap = createDerivMap(bumpmap.get(),sampler.get(),bsdf->bumpmap.wasNormal);
12771280
asset::SAssetBundle imgBundle(nullptr,{ derivmap });
12781281
ctx.override_->insertAssetIntoCache(std::move(imgBundle), key, ctx.inner, 0u);
12791282
auto derivmap_view = createImageView(std::move(derivmap));

src/nbl/ext/MitsubaLoader/CMitsubaMaterialCompilerFrontend.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ namespace ext
1313
namespace MitsubaLoader
1414
{
1515

16-
auto CMitsubaMaterialCompilerFrontend::getDerivMap(const CElementTexture* _element) const -> tex_ass_type
16+
auto CMitsubaMaterialCompilerFrontend::getDerivMap(const CElementBSDF::BumpMap& _bump) const -> tex_ass_type
1717
{
18+
const CElementTexture* texture = nullptr;
1819
float scale = 1.f;
19-
std::tie(_element, scale) = getTexture_common(_element);
20-
std::string key = m_loaderContext->derivMapCacheKey(_element);
21-
if (_element->type != CElementTexture::BITMAP)
20+
std::tie(texture, scale) = getTexture_common(_bump.texture);
21+
std::string key = m_loaderContext->derivMapCacheKey(_bump);
22+
if (texture->type != CElementTexture::BITMAP)
2223
return { nullptr, nullptr, 0.f };
2324

24-
return getTexture(key, _element, scale);
25+
return getTexture(key, texture, scale);
2526
}
2627

2728
auto CMitsubaMaterialCompilerFrontend::getBlendWeightTex(const CElementTexture* _element) const -> tex_ass_type
@@ -250,7 +251,7 @@ namespace MitsubaLoader
250251
//no other source supported for now (uncomment in the future) [far future TODO]
251252
//node->source = IR::CGeomModifierNode::ESRC_TEXTURE;
252253

253-
std::tie(node->texture.image, node->texture.sampler, node->texture.scale) = getDerivMap(_bsdf->bumpmap.texture);
254+
std::tie(node->texture.image, node->texture.sampler, node->texture.scale) = getDerivMap(_bsdf->bumpmap);
254255
}
255256
break;
256257
case CElementBSDF::COATING:

0 commit comments

Comments
 (0)