osgPBR(十五)镜面IBL--查看不同级别的HDR环境贴图

首先,设置可以使用Mipmap,启用三线性过滤,设置最大级别和最小级别

osg::ref_ptr<osg::TextureCubeMap> tcm = new osg::TextureCubeMap;
tcm->setTextureSize(128, 128);

tcm->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
tcm->setUseHardwareMipMapGeneration(true);
tcm->setMinLOD(0);
tcm->setMaxLOD(4	);

然后在shader中,用textureLod分别查看各个级别

"gl_FragColor = textureLod(tex0,dir,4);\n"//0,1,2,3,4

运行如下




代码如下:

#include <osg/TextureCubeMap>

#include <osg/TexGen>

#include <osg/TexEnvCombine>

#include <osgUtil/ReflectionMapGenerator>

#include <osgDB/ReadFile>

#include <osgViewer/Viewer>

#include <osg/NodeVisitor>

#include <osg/ShapeDrawable>

static const char * vertexShader =

{

"in vec3 aPos;\n"

"varying vec3 outPos;"

"void main(void)\n"

"{\n"

"outPos = aPos;\n"

" gl_Position = ftransform();\n"

"}\n"

};

static const char *psShader =

{

"varying vec3 outPos;"

"uniform samplerCube tex0;"

"void main(void)\n"

"{\n"

"float x = outPos.r;\n"

"float y = outPos.g;\n"

"float z = outPos.b;\n"

"vec3 dir = vec3(x,y,z);\n"

//"gl_FragColor = vec4(x,y,z,1);\n"

//"gl_FragColor = texture(tex0,dir);\n"

"gl_FragColor = textureLod(tex0,dir,2);\n"//0,1,2,3,4

"}\n"

};

class MyNodeVisitor : public osg::NodeVisitor

{

public:

MyNodeVisitor() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)

{

}
void apply(osg::Geode& geode)
{
	int count = geode.getNumDrawables();
	for (int i = 0; i < count; i++)
	{
		osg::ref_ptr<osg::Geometry> geometry = geode.getDrawable(i)->asGeometry();
		if (!geometry.valid())
		{
			continue;
		}
		osg::Array* vertexArray = geometry->getVertexArray();
		geometry->setVertexAttribArray(1, vertexArray);

	}
	traverse(geode);
}

};

int main()

{

osg::ref_ptrosg::TextureCubeMap tcm = new osg::TextureCubeMap;

tcm->setTextureSize(128, 128);

tcm->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);

tcm->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);

tcm->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);

tcm->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);

tcm->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);

std::string strImagePosX = "D:/hdr/Right face camera.bmp";
osg::ref_ptr<osg::Image> imagePosX = osgDB::readImageFile(strImagePosX);
tcm->setImage(osg::TextureCubeMap::POSITIVE_X, imagePosX);
std::string strImageNegX = "D:/hdr/Left face camera.bmp";
osg::ref_ptr<osg::Image> imageNegX = osgDB::readImageFile(strImageNegX);
tcm->setImage(osg::TextureCubeMap::NEGATIVE_X, imageNegX);

std::string strImagePosY = "D:/hdr/Front face camera.bmp";;
osg::ref_ptr<osg::Image> imagePosY = osgDB::readImageFile(strImagePosY);
tcm->setImage(osg::TextureCubeMap::POSITIVE_Y, imagePosY);
std::string strImageNegY = "D:/hdr/Back face camera.bmp";;
osg::ref_ptr<osg::Image> imageNegY = osgDB::readImageFile(strImageNegY);
tcm->setImage(osg::TextureCubeMap::NEGATIVE_Y, imageNegY);

std::string strImagePosZ = "D:/hdr/Top face camera.bmp";
osg::ref_ptr<osg::Image> imagePosZ = osgDB::readImageFile(strImagePosZ);
tcm->setImage(osg::TextureCubeMap::POSITIVE_Z, imagePosZ);

std::string strImageNegZ = "D:/hdr/Bottom face camera.bmp";
osg::ref_ptr<osg::Image> imageNegZ = osgDB::readImageFile(strImageNegZ);
tcm->setImage(osg::TextureCubeMap::NEGATIVE_Z, imageNegZ);
tcm->setUseHardwareMipMapGeneration(true);
tcm->setMinLOD(0);
tcm->setMaxLOD(4);

osg::ref_ptr<osg::Box> box = new osg::Box(osg::Vec3(0, 0, 0), 10);
osg::ref_ptr<osg::ShapeDrawable> drawable = new osg::ShapeDrawable(box);
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(drawable);
MyNodeVisitor nv;
geode->accept(nv);
osg::ref_ptr<osg::StateSet> stateset = geode->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0, tcm, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);

//shader

osg::ref_ptr<osg::Shader> vs1 = new osg::Shader(osg::Shader::VERTEX, vertexShader);
osg::ref_ptr<osg::Shader> ps1 = new osg::Shader(osg::Shader::FRAGMENT, psShader);
osg::ref_ptr<osg::Program> program1 = new osg::Program;
program1->addShader(vs1);
program1->addShader(ps1);
program1->addBindAttribLocation("aPos", 1);

osg::ref_ptr<osg::Uniform> tex0Uniform = new osg::Uniform("tex0", 0);
stateset->addUniform(tex0Uniform);
stateset->setAttribute(program1, osg::StateAttribute::ON);

osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
viewer->setSceneData(geode);
viewer->realize();
return viewer->run();

}

相关推荐
读万卷书不如行万里路呀19 天前
数字地球程序框架搭建(三)
osg·加载模型不显示·加载模型显示位置不对
长沙红胖子Qt1 个月前
OSG开发笔记(三十二):深入理解相机视口、制作支持与主视图同步变换旋转的相机HUD
qt·osg·三维开发·hud相机·前景hud旋转坐标
长沙红胖子Qt3 个月前
项目实战:Qt+OSG爆破动力学仿真三维引擎测试工具v1.1.0(加载.K模型,子弹轨迹模拟动画,支持windows、linux、国产麒麟系统)
qt·osg·动力三维仿真·三维.k文件解析
charlee444 个月前
CMake构建学习笔记10-OsgQt库的构建
c++·qt·学习·cmake·构建·osg
bdbox@qq.com9 个月前
关于OSGConv转换osgb为obj时,无法指定输出贴图路径的问题及解决
c++·贴图·实景三维·osg·倾斜摄影·osgb
PHP代码10 个月前
osg qt5.15 osg3.6.3 osgEarth3.1 编译爬山
开发语言·qt·osg
荆楚闲人1 年前
CMake生成osg的FFMPEG插件及Windows下不生成VS工程问题解决
ffmpeg·osg
bcbobo21cn1 年前
OSG读取和添加节点学习
node·osg
杨石兴1 年前
lesson01-在osgEarth上实时的贴地显示无人机或卫星传回的图片
osgearth·osg
欧特克_Glodon1 年前
OSG编程指南<十七>:OSG光照与材质
材质·osg·osg光照·osg材质