|
环境贴图的表达,本示例程序中,使用立方图纹理表达环境高光,为了能清楚地看到效果,我们对程序的光源位置和颜色进行了改动,程序中的主要代码在create_specular_highlights()函数中,代码如下:
osg::StateSet *ss = node->getOrCreateStateSet();
//创建和设定立方图纹理的属性
osg::TextureCubeMap *tcm = new osg::TextureCubeMap;
//设定纹理的截取方式
tcm->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
tcm->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
tcm->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP);
//设置纹理的滤波方式
tcm->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
tcm->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
//生成6个高光图像
osgUtil::HighlightMapGenerator *mapgen = new osgUtil::HighlightMapGenerator(
osg::Vec3(1, 1, -1), //光源的方向
osg::Vec4(1, 0.0f, 0.0f, 1), //光源的颜色
1); //镜面指数
mapgen->generateMap();
//设置每个立方体的面所对应的图像
//右面
tcm->setImage(osg::TextureCubeMap::POSITIVE_X, mapgen->getImage(osg::TextureCubeMap::POSITIVE_X));
//左面
tcm->setImage(osg::TextureCubeMap::NEGATIVE_X, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_X));
//前面
tcm->setImage(osg::TextureCubeMap::POSITIVE_Y, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Y));
//后面
tcm->setImage(osg::TextureCubeMap::NEGATIVE_Y, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Y));
//上面
tcm->setImage(osg::TextureCubeMap::POSITIVE_Z, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Z));
//下面
tcm->setImage(osg::TextureCubeMap::NEGATIVE_Z, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Z));
//设置纹理属性
ss->setTextureAttributeAndModes(0, tcm, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
//生成纹理的方式
osg::TexGen *tg = new osg::TexGen;
//反射影射
tg->setMode(osg::TexGen::REFLECTION_MAP);
ss->setTextureAttributeAndModes(0, tg, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
////设置纹理组合贴图模式
osg::TexEnvCombine *te = new osg::TexEnvCombine;
te->setCombine_RGB(osg::TexEnvCombine::ADD);
te->setSource0_RGB(osg::TexEnvCombine::TEXTURE);
te->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
te->setSource1_RGB(osg::TexEnvCombine::PRIMARY_COLOR);
te->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
ss->setTextureAttributeAndModes(0, te, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
程序中主要是对纹理的操作比较多,如使用osg::TexGen和osg::TexEnvCombine类,有关这两个类的用法请参考opengl有关的内容,参数比较多,很容易混淆,改动高光图像的光源颜色和方向看一看有什么效果。
程序的效果如下图所示,程序中设定的是红色光源,反射的是红光。