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

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

osg::ref_ptr 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
运行如下
osgPBR(十五)镜面IBL--查看不同级别的HDR环境贴图_第1张图片
osgPBR(十五)镜面IBL--查看不同级别的HDR环境贴图_第2张图片
osgPBR(十五)镜面IBL--查看不同级别的HDR环境贴图_第3张图片
osgPBR(十五)镜面IBL--查看不同级别的HDR环境贴图_第4张图片
osgPBR(十五)镜面IBL--查看不同级别的HDR环境贴图_第5张图片
代码如下:
#include
#include
#include
#include
#include
#include
#include
#include

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

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

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

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

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

//shader

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

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

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

}

你可能感兴趣的:(osg+pbr,osg)