[osg-users] [build] using 3Ds Model in osgFX::BumpMapping and the Texture UV Coords for diffuse are not loaded
Tiago Trocoli
trocolit2 at gmail.com
Mon Apr 4 09:12:40 PDT 2016
Hi Tobias
I implemented the OSG BumpMap with no need to use shaders, as follow:
function source
Code:
void bumpMapOSG(osg::Geode *geode, osg::Group *group, osg::Image *normal_image, osg::Image *difuse_image, double scale_x,
double scale_y) {
if (!normal_image || !difuse_image) {
std::cout << "IMAGE FAIL" << std::endl;
exit(0);
}
osg::StateSet* bumpState = new osg::StateSet();
// Set textures
osg::ref_ptr<osg::Texture2D> normal_texture(new osg::Texture2D());
osg::ref_ptr<osg::Texture2D> difuse_texture(new osg::Texture2D());
normal_texture->setImage(normal_image);
normal_texture->setDataVariance(osg::Object::DYNAMIC);
normal_texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
normal_texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
normal_texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
normal_texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
normal_texture->setResizeNonPowerOfTwoHint(false);
normal_texture->setMaxAnisotropy(8.0f);
difuse_texture->setImage(difuse_image);
difuse_texture->setDataVariance(osg::Object::DYNAMIC);
difuse_texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
difuse_texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
difuse_texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
difuse_texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
difuse_texture->setResizeNonPowerOfTwoHint(false);
difuse_texture->setMaxAnisotropy(8.0f);
const int TEXTURE_UNIT_NORMAL = 1;
const int TEXTURE_UNIT_DIFUSE = 2;
bumpState->setTextureAttributeAndModes(TEXTURE_UNIT_NORMAL, normal_texture, osg::StateAttribute::ON);
bumpState->setTextureAttributeAndModes(TEXTURE_UNIT_DIFUSE, difuse_texture, osg::StateAttribute::ON);
osg::ref_ptr<osg::Geometry> geometry = geode->asGeode()->getDrawable(0)->asGeometry();
osg::Vec2Array* tex_coord = dynamic_cast<osg::Vec2Array*>(geometry->getTexCoordArray(0));
for (unsigned int i = 0; i < tex_coord->getNumElements(); ++i)
(*tex_coord)[i].set((*tex_coord)[i].x() * scale_x, (*tex_coord)[i].y() * scale_y);
geometry->setStateSet(bumpState);
if (tex_coord) {
geometry->setTexCoordArray(TEXTURE_UNIT_NORMAL, tex_coord);
geometry->setTexCoordArray(TEXTURE_UNIT_DIFUSE, tex_coord);
} else {
std::cout << "MISS TEXTURE COORDINATE " << std::endl;
exit(0);
}
osgFX::BumpMapping* bump_mapping = new osgFX::BumpMapping();
bump_mapping->setEnabled(true);
bump_mapping->setLightNumber(0);
bump_mapping->setNormalMapTextureUnit(TEXTURE_UNIT_NORMAL);
bump_mapping->setDiffuseTextureUnit(TEXTURE_UNIT_DIFUSE);
bump_mapping->addChild(geode);
bump_mapping->prepareChildren();
group->addChild(bump_mapping);
}
[\code]
Thank you!
Cheers,
Tiago
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=66724#66724
More information about the osg-users
mailing list