二十一,结合直射光和间接光绘制小球

news2024/11/25 22:57:53

走到这一步,可以说,将直接光和间接光都走完了,要把这些结合起来了。
与learn opengl中不同的是,预过滤环境贴图没有用Mipmap,而是把五个不同粗糙度的所有纹理都加进来。

osg::ref_ptr<osg::TextureCubeMap> prefilterMap_0 = new osg::TextureCubeMap;
setImagesByLevel(prefilterMap_0, 0);
osg::ref_ptr<osg::TextureCubeMap> prefilterMap_1 = new osg::TextureCubeMap;
setImagesByLevel(prefilterMap_1, 1);
osg::ref_ptr<osg::TextureCubeMap> prefilterMap_2 = new osg::TextureCubeMap;
setImagesByLevel(prefilterMap_2, 2);
osg::ref_ptr<osg::TextureCubeMap> prefilterMap_3 = new osg::TextureCubeMap;
setImagesByLevel(prefilterMap_3, 3);
osg::ref_ptr<osg::TextureCubeMap> prefilterMap_4 = new osg::TextureCubeMap;
setImagesByLevel(prefilterMap_4, 4);

osg::ref_ptr<osg::Uniform> prefilter0Uniform = new osg::Uniform("prefilterMap0", 0);
osg::ref_ptr<osg::Uniform> prefilter1Uniform = new osg::Uniform("prefilterMap1",1);
osg::ref_ptr<osg::Uniform> prefilter2Uniform = new osg::Uniform("prefilterMap2", 2);
osg::ref_ptr<osg::Uniform> prefilter3Uniform = new osg::Uniform("prefilterMap3", 3);
osg::ref_ptr<osg::Uniform> prefilter4Uniform = new osg::Uniform("prefilterMap4", 4);
osg::ref_ptr<osg::Uniform> tex0Uniform = new osg::Uniform("irradianceMap", 5);
osg::ref_ptr<osg::Uniform> brdfLUTUniform = new osg::Uniform("brdfLUT", 6);

			stateset->addUniform(tex0Uniform);
			stateset->addUniform(brdfLUTUniform);
			stateset->addUniform(prefilter0Uniform);
			stateset->addUniform(prefilter1Uniform);
			stateset->addUniform(prefilter2Uniform);
			stateset->addUniform(prefilter3Uniform);
			stateset->addUniform(prefilter4Uniform);

			osg::ref_ptr<osg::StateSet> stateset = geode->getOrCreateStateSet();
			stateset->setTextureAttributeAndModes(0, prefilterMap_0, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
			stateset->setTextureAttributeAndModes(1, prefilterMap_1, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
			stateset->setTextureAttributeAndModes(2, prefilterMap_2, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
			stateset->setTextureAttributeAndModes(3, prefilterMap_3, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
			stateset->setTextureAttributeAndModes(4, prefilterMap_4, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
			stateset->setTextureAttributeAndModes(5, irradianceTextureCubeMap, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
			stateset->setTextureAttributeAndModes(6, textureBRDFLUT, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);

在这里插入图片描述
代码如下:
//通过Liblas读取.las文件,并在osg中显示出来,用shader,先在片元着色器指定使用绿色
#include
#include

#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include <osg/CoordinateSystemNode>

#include <osg/Switch>
#include <osg/Types>
#include <osgText/Text>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/SphericalManipulator>

#include <osgGA/Device>

#include
#include <osg/Shader>
#include <osg/BlendFunc>
#include <osg/blendColor>
#include <osg/Point>
#include <osg/Shapedrawable>
#include <osgUtil/SmoothingVisitor>

#include <osg/TextureCubeMap>

static const char * vertexShader_PBR =
{
“in vec3 aPos; \n”
“in vec3 aNormal; \n”
“varying vec3 WorldPos; \n”
“varying vec3 Normal; \n”
“uniform mat4 normalMatrix; \n”
“void main() \n”
“{ \n”
" WorldPos = aPos; \n"
" Normal = vec3(normalMatrix * vec4(aNormal,1.0)); \n"
" gl_Position = ftransform(); \n"
“}\n”
};

static const char *psShader_PBR =
{
“#version 330 core \n”
“out vec4 FragColor; \n”
“varying vec3 WorldPos; \n”
“varying vec3 Normal; \n”
“uniform vec3 albedo; \n”
“uniform float metallic; \n”
“uniform float roughness; \n”
“uniform float ao; \n”
//IBL
“uniform samplerCube irradianceMap;”
“uniform sampler2D brdfLUT; \n”
“uniform samplerCube prefilterMap0; \n”
“uniform samplerCube prefilterMap1; \n”
“uniform samplerCube prefilterMap2; \n”
“uniform samplerCube prefilterMap3; \n”
“uniform samplerCube prefilterMap4; \n”

"uniform vec3 lightPositions[4];																  \n"
"uniform vec3 lightColors[4];																	  \n"
"uniform vec3 camPos;																			  \n"
"const float PI = 3.14159265359;																  \n"
"float DistributionGGX(vec3 N, vec3 H, float roughness)											  \n"
"{																								  \n"
"	float a = roughness*roughness;																  \n"
"	float a2 = a*a;																				  \n"
"	float NdotH = max(dot(N, H), 0.0);															  \n"
"	float NdotH2 = NdotH*NdotH;																	  \n"
"	float nom = a2;																				  \n"
"	float denom = (NdotH2 * (a2 - 1.0) + 1.0);													  \n"
"	denom = PI * denom * denom;																	  \n"
"	return nom / denom;																			  \n"
"}																								  \n"
"float GeometrySchlickGGX(float NdotV, float roughness)											  \n"
"{																								  \n"
"	float r = (roughness + 1.0);																  \n"
"	float k = (r*r) / 8.0;																		  \n"
"	float nom = NdotV;																			  \n"
"	float denom = NdotV * (1.0 - k) + k;														  \n"
"	return nom / denom;																			  \n"
"}																								  \n"
"float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)									  \n"
"{																								  \n"
"	float NdotV = max(dot(N, V), 0.0);															  \n"
"	float NdotL = max(dot(N, L), 0.0);															  \n"
"	float ggx2 = GeometrySchlickGGX(NdotV, roughness);											  \n"
"	float ggx1 = GeometrySchlickGGX(NdotL, roughness);											  \n"
"	return ggx1 * ggx2;																			  \n"
"}																								  \n"
"vec3 fresnelSchlick(float cosTheta, vec3 F0)													  \n"
"{																								  \n"
"	return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);							  \n"
"}																								  \n"
"vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness)									  \n"
"{																										  \n"
"	return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);		  \n"
"}																										  \n"
"void main()																						\n"
"{																									\n"
"	vec3 N = normalize(Normal);																		\n"
"	vec3 V = normalize(camPos - WorldPos);															\n"
" vec3 R = reflect(-V, N);\n"
"	vec3 F0 = vec3(0.04);																			\n"
"	F0 = mix(F0, albedo, metallic);																	\n"
"	vec3 Lo = vec3(0.0);																			\n"
"	for (int i = 0; i < 4; ++i)																		\n"
"	{																								\n"
"		vec3 L = normalize(lightPositions[i] - WorldPos);											\n"
"		vec3 H = normalize(V + L);																	\n"
"		float distance = length(lightPositions[i] - WorldPos);										\n"
"		float attenuation = 1.0 / (distance * distance);											\n"
"		vec3 radiance = lightColors[i] * attenuation;												\n"
"		float NDF = DistributionGGX(N, H, roughness);												\n"
"		float G = GeometrySmith(N, V, L, roughness);												\n"
"		vec3 F = fresnelSchlick(clamp(dot(H, V), 0.0, 1.0), F0);									\n"
"		vec3 numerator = NDF * G * F;																\n"
"		float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; 				\n"
"		vec3 specular = numerator / denominator;													\n"
"		vec3 kS = F;																				\n"
"		vec3 kD = vec3(1.0) - kS;																	\n"
"		kD *= 1.0 - metallic;																		\n"
"		float NdotL = max(dot(N, L), 0.0);															\n"
"		Lo += (kD * albedo / PI + specular) * radiance * NdotL;  									\n"
"	}																								\n"
// ambient lighting (we now use IBL as the ambient term)
"vec3 F = fresnelSchlickRoughness(max(dot(N, V), 0.0), F0, roughness);							 \n"
"vec3 kS = F;																					 \n"
"vec3 kD = 1.0 - kS;"
"  kD *= 1.0 - metallic;	\n"
"vec3 irradiance = texture(irradianceMap, N).rgb;"
"vec3 diffuse = irradiance * albedo;"
// sample both the pre-filter map and the BRDF lut and combine them together as per the Split-Sum approximation to get the IBL specular part.
"const float MAX_REFLECTION_LOD = 4.0;																	  \n"
"float level = roughness * MAX_REFLECTION_LOD;\n"
"vec3 prefilteredColor = vec3(0,0,0);\n"
"if(level >= 4)\n"
"{"
	"prefilteredColor = texture(prefilterMap4, R).rgb;\n"
"}"
"else if(level >= 3)\n"
"{"
	"prefilteredColor = texture(prefilterMap3, R).rgb;\n"
"}"
"else if(level >= 2)\n"
"{"
	"prefilteredColor = texture(prefilterMap2, R).rgb;\n"
"}"
"else if(level >= 1)\n"
"{"
	"prefilteredColor = texture(prefilterMap1, R).rgb;\n"
"}"
"else if(level >= 0)\n"
"{"
	"prefilteredColor = texture(prefilterMap0, R).rgb;\n"
"}"
"vec2 brdf = texture(brdfLUT, vec2(max(dot(N, V), 0.0), roughness)).rg;									  \n"
"vec3 specular = prefilteredColor * (F * brdf.x + brdf.y);												  \n"
"vec3 ambient = (kD * diffuse + specular) * ao;															  \n"
"	vec3 color = ambient + Lo;																		\n"
"	color = color / (color + vec3(1.0));															\n"
"	color = pow(color, vec3(1.0 / 2.2));															\n"
"	FragColor = vec4(color, 1.0);																	\n"
//"	FragColor = vec4(1.0,0.0,0.0, 1.0);																	\n"
"}																									\n"

};

osg::ref_ptrosg::Geode CreateSphereGeode()
{
osg::ref_ptrosg::Geode geode = new osg::Geode;
osg::ref_ptrosg::Vec3Array vertices = new osg::Vec3Array(6);
(*vertices)[0].set(0.0f, 0.0f, 1.0f);
(*vertices)[1].set(-0.5f, -0.5f, 0.0f);
(*vertices)[2].set(0.5f, -0.5f, 0.0f);
(*vertices)[3].set(0.5f, 0.5f, 0.0f);
(*vertices)[4].set(-0.5f, 0.5f, 0.0f);
(*vertices)[5].set(0.0f, 0.0f, -1.0f);
osg::ref_ptrosg::DrawElementsUInt indices = new osg::DrawElementsUInt(GL_TRIANGLES, 24);
(*indices)[0] = 0; (*indices)[1] = 1; (*indices)[2] = 2;
(*indices)[3] = 0; (*indices)[4] = 2; (*indices)[5] = 3;
(*indices)[6] = 0; (*indices)[7] = 3; (*indices)[8] = 4;
(*indices)[9] = 0; (*indices)[10] = 4; (*indices)[11] = 1;
(*indices)[12] = 5; (*indices)[13] = 2; (*indices)[14] = 1;
(*indices)[15] = 5; (*indices)[16] = 3; (*indices)[17] = 2;
(*indices)[18] = 5; (*indices)[19] = 4; (*indices)[20] = 3;
(*indices)[21] = 5; (*indices)[22] = 1; (*indices)[23] = 4;
osg::ref_ptrosg::Geometry geom = new osg::Geometry;
geom->setVertexArray(vertices.get());
geom->addPrimitiveSet(indices.get());
osgUtil::SmoothingVisitor::smooth(*geom);
geode->addDrawable(geom);
return geode;

}

osg::ref_ptrosg::Geode renderSphere(osg::Vec3f pos)
{
osg::ref_ptrosg::Geode geode = new osg::Geode;
const unsigned int X_SEGMENTS = 64;
const unsigned int Y_SEGMENTS = 64;
const float PI = 3.14159265359f;
osg::ref_ptrosg::Vec3Array vertices = new osg::Vec3Array;
osg::ref_ptrosg::Vec3Array normalArray = new osg::Vec3Array;
for (unsigned int x = 0; x <= X_SEGMENTS; ++x)
{
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{
float xSegment = (float)x / (float)X_SEGMENTS;
float ySegment = (float)y / (float)Y_SEGMENTS;
float xPos = std::cos(xSegment * 2.0f * PI) * std::sin(ySegment * PI);
float yPos = std::cos(ySegment * PI);
float zPos = std::sin(xSegment * 2.0f * PI) * std::sin(ySegment * PI);

		vertices->push_back(osg::Vec3(xPos, yPos, zPos) + pos);
		normalArray->push_back(osg::Vec3(xPos, yPos, zPos));
	}
}
osg::ref_ptr<osg::DrawElementsUInt> indices = new osg::DrawElementsUInt();
bool oddRow = false;
for (unsigned int y = 0; y < Y_SEGMENTS; ++y)
{
	if (!oddRow) // even rows: y == 0, y == 2; and so on
	{
		for (unsigned int x = 0; x <= X_SEGMENTS; ++x)
		{
			indices->push_back(y       * (X_SEGMENTS + 1) + x);
			indices->push_back((y + 1) * (X_SEGMENTS + 1) + x);
		}
	}
	else
	{
		for (int x = X_SEGMENTS; x >= 0; --x)
		{
			indices->push_back((y + 1) * (X_SEGMENTS + 1) + x);
			indices->push_back(y       * (X_SEGMENTS + 1) + x);
		}
	}
	oddRow = !oddRow;
}
int indexCount = static_cast<unsigned int>(indices->size());
indices->setMode(GL_TRIANGLE_STRIP);

osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
geom->setVertexArray(vertices.get());
geom->addPrimitiveSet(indices.get());
geom->setNormalArray(normalArray, osg::Array::BIND_PER_VERTEX);

geom->setVertexAttribArray(1, vertices, osg::Array::BIND_PER_VERTEX);
geom->setVertexAttribArray(2, normalArray, osg::Array::BIND_PER_VERTEX);

//osgUtil::SmoothingVisitor::smooth(*geom);
geode->addDrawable(geom);
return geode;

}

class EyePointCallback : public osg::UniformCallback
{
public:
EyePointCallback(osg::ref_ptrosg::Camera camera)
{
_camera = camera;
}

virtual void operator() (osg::Uniform* uniform, osg::NodeVisitor* nv)
{
	osg::Vec3 eye, center, up;
	_camera->getViewMatrixAsLookAt(eye, center, up);
	uniform->set(eye);
}

private:
osg::ref_ptrosg::Camera _camera;
};

class ProjectMatrixCallback : public osg::UniformCallback
{
public:
ProjectMatrixCallback(osg::ref_ptrosg::Camera camera)
{
_camera = camera;
}

virtual void operator() (osg::Uniform* uniform, osg::NodeVisitor* nv)
{
	osg::Matrixd projectionMatrix = _camera->getProjectionMatrix();
	uniform->set(projectionMatrix);
}

private:
osg::ref_ptrosg::Camera _camera;
};
class ViewMatrixCallback : public osg::UniformCallback
{
public:
ViewMatrixCallback(osg::ref_ptrosg::Camera camera)
{
_camera = camera;
}

virtual void operator() (osg::Uniform* uniform, osg::NodeVisitor* nv)
{
	osg::Matrixd viewMatrix = _camera->getViewMatrix();
	uniform->set(viewMatrix);
}

private:
osg::ref_ptrosg::Camera _camera;
};
void setImagesByLevel(osg::ref_ptrosg::TextureCubeMap textureCubemap, int level)
{

textureCubemap->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
textureCubemap->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
textureCubemap->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
textureCubemap->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
textureCubemap->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);

std::string strDir = "D:/hdr/Prefilter/" + std::to_string(level) + "/";
std::string strImagePosX = strDir + "Right face camera.bmp";
osg::ref_ptr<osg::Image> imagePosX = osgDB::readImageFile(strImagePosX);
textureCubemap->setImage(osg::TextureCubeMap::POSITIVE_X, imagePosX);

std::string strImageNegX = strDir + "Left face camera.bmp";
osg::ref_ptr<osg::Image> imageNegX = osgDB::readImageFile(strImageNegX);
textureCubemap->setImage(osg::TextureCubeMap::NEGATIVE_X, imageNegX);

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

std::string strImagePosZ = strDir + "Top face camera.bmp";
osg::ref_ptr<osg::Image> imagePosZ = osgDB::readImageFile(strImagePosZ);
textureCubemap->setImage(osg::TextureCubeMap::POSITIVE_Z, imagePosZ);
std::string strImageNegZ = strDir + "Bottom face camera.bmp";
osg::ref_ptr<osg::Image> imageNegZ = osgDB::readImageFile(strImageNegZ);
textureCubemap->setImage(osg::TextureCubeMap::NEGATIVE_Z, imageNegZ);

}
int main()
{
//预过滤HDR环境贴图(第0-4层)

osg::ref_ptr<osg::TextureCubeMap> prefilterMap_0 = new osg::TextureCubeMap;
setImagesByLevel(prefilterMap_0, 0);
osg::ref_ptr<osg::TextureCubeMap> prefilterMap_1 = new osg::TextureCubeMap;
setImagesByLevel(prefilterMap_1, 1);
osg::ref_ptr<osg::TextureCubeMap> prefilterMap_2 = new osg::TextureCubeMap;
setImagesByLevel(prefilterMap_2, 2);
osg::ref_ptr<osg::TextureCubeMap> prefilterMap_3 = new osg::TextureCubeMap;
setImagesByLevel(prefilterMap_3, 3);
osg::ref_ptr<osg::TextureCubeMap> prefilterMap_4 = new osg::TextureCubeMap;
setImagesByLevel(prefilterMap_4, 4);

//漫反射贴图
osg::ref_ptr<osg::TextureCubeMap> irradianceTextureCubeMap = new osg::TextureCubeMap;
{
	irradianceTextureCubeMap->setTextureSize(512, 512);
	irradianceTextureCubeMap->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
	irradianceTextureCubeMap->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
	irradianceTextureCubeMap->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
	irradianceTextureCubeMap->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
	irradianceTextureCubeMap->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);

	std::string strImagePosX = "D:/irradiance/Right face camera.bmp";
	osg::ref_ptr<osg::Image> imagePosX = osgDB::readImageFile(strImagePosX);
	irradianceTextureCubeMap->setImage(osg::TextureCubeMap::POSITIVE_X, imagePosX);

	std::string strImageNegX = "D:/irradiance/Left face camera.bmp";
	osg::ref_ptr<osg::Image> imageNegX = osgDB::readImageFile(strImageNegX);
	irradianceTextureCubeMap->setImage(osg::TextureCubeMap::NEGATIVE_X, imageNegX);

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

	std::string strImagePosZ = "D:/irradiance/Top face camera.bmp";
	osg::ref_ptr<osg::Image> imagePosZ = osgDB::readImageFile(strImagePosZ);
	irradianceTextureCubeMap->setImage(osg::TextureCubeMap::POSITIVE_Z, imagePosZ);
	std::string strImageNegZ = "D:/irradiance/Bottom face camera.bmp";
	osg::ref_ptr<osg::Image> imageNegZ = osgDB::readImageFile(strImageNegZ);
	irradianceTextureCubeMap->setImage(osg::TextureCubeMap::NEGATIVE_Z, imageNegZ);
}
//积分贴图
osg::ref_ptr<osg::Texture2D> textureBRDFLUT = new osg::Texture2D;
{
	std::string strBRDFLUTImageName = "d:/hdr/lut/brdflut.bmp";
	osg::ref_ptr<osg::Image> brdfLUTImage = osgDB::readImageFile(strBRDFLUTImageName);
	textureBRDFLUT->setImage(brdfLUTImage);
}


osg::ref_ptr<osg::Uniform> prefilter0Uniform = new osg::Uniform("prefilterMap0", 0);
osg::ref_ptr<osg::Uniform> prefilter1Uniform = new osg::Uniform("prefilterMap1",1);
osg::ref_ptr<osg::Uniform> prefilter2Uniform = new osg::Uniform("prefilterMap2", 2);
osg::ref_ptr<osg::Uniform> prefilter3Uniform = new osg::Uniform("prefilterMap3", 3);
osg::ref_ptr<osg::Uniform> prefilter4Uniform = new osg::Uniform("prefilterMap4", 4);
osg::ref_ptr<osg::Uniform> tex0Uniform = new osg::Uniform("irradianceMap", 5);
osg::ref_ptr<osg::Uniform> brdfLUTUniform = new osg::Uniform("brdfLUT", 6);

osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;

osg::ref_ptr<osg::Camera> camera = viewer->getCamera();
osg::ref_ptr<osg::Uniform> camPosUniform = new osg::Uniform("camPos", osg::Vec3f(0, 0, 0));
camPosUniform->setUpdateCallback(new EyePointCallback(camera));

osg::Matrix viewMatrix = camera->getViewMatrix();
osg::Matrix projMatrix = camera->getProjectionMatrix();
osg::Vec3f eye, center, up;
camera->getViewMatrixAsLookAt(eye, center, up);

//osg::ref_ptr<osg::Uniform> camPosUniform = new osg::Uniform("camPos", eye);
osg::ref_ptr<osg::Uniform> viewMatrixUniform = new osg::Uniform("view", viewMatrix);
viewMatrixUniform->setUpdateCallback(new ViewMatrixCallback(camera));
osg::ref_ptr<osg::Uniform> projMatrixUniform = new osg::Uniform("projection", projMatrix);
projMatrixUniform->setUpdateCallback(new ProjectMatrixCallback(camera));
osg::ref_ptr<osg::Group> grp = new osg::Group;
//漫反射比率
osg::Vec3f albedo(0.5f, 0.0f, 0.0f);
osg::ref_ptr<osg::Uniform> albedoUniform = new osg::Uniform("albedo", albedo);
float ao = 1.0f;
osg::ref_ptr<osg::Uniform> aoUniform = new osg::Uniform("ao", ao);

int nrRows = 7;
int nrColumns = 7;
float spacing = 2.5;
float ballRadius = 1.0f;

osg::ref_ptr<osg::Vec3Array> lightColors = new osg::Vec3Array;
lightColors->push_back(osg::Vec3(300.0f, 300.0f, 300.0f));
lightColors->push_back(osg::Vec3(300.0f, 300.0f, 300.0f));
lightColors->push_back(osg::Vec3(300.0f, 300.0f, 300.0f));
lightColors->push_back(osg::Vec3(300.0f, 300.0f, 300.0f));

osg::ref_ptr<osg::Uniform> lightColorsUniform = new osg::Uniform(osg::Uniform::FLOAT_VEC3, "lightColors", lightColors->size());
for (int i = 0; i < lightColors->size(); i++)
{
	lightColorsUniform->setElement(i, lightColors->at(i));
}
osg::ref_ptr<osg::Vec3Array> lightPositions = new osg::Vec3Array;
lightPositions->push_back(osg::Vec3(-10.0f, 10.0f, 10.0f));
lightPositions->push_back(osg::Vec3(10.0f, 10.0f, 10.0f));
lightPositions->push_back(osg::Vec3(-10.0f, -10.0f, 10.0f));
lightPositions->push_back(osg::Vec3(10.0f, -10.0f, 10.0f));

osg::ref_ptr<osg::Uniform> lightPositionsUniform = new osg::Uniform(osg::Uniform::FLOAT_VEC3, "lightPositions", lightPositions->size());
for (int i = 0; i < lightPositions->size(); i++)
{
	lightPositionsUniform->setElement(i, lightPositions->at(i));
}
for (int row = 0; row < nrRows; row++)
{
	float metallic = row * 1.0 / nrRows;
	for (int col = 0; col < nrColumns; col++)
	{
		float roughness = col * 1.0 / nrColumns;
		if (roughness <0.05)
		{
			roughness = 0.05;
		}
		if (roughness > 1.0)
		{
			roughness = 1.0;
		}
		osg::Vec3 ballCenter(
			(col - (nrColumns / 2)) * spacing,
			(row - (nrRows / 2)) * spacing,
			0.0f);
		osg::Matrix worldMatrix = osg::Matrix::translate(ballCenter);

		osg::Matrix inverse;
		inverse.invert(worldMatrix);
		osg::Matrix transPose;
		transPose.transpose(inverse);
		osg::ref_ptr<osg::Geode> geode = renderSphere(ballCenter);
		{
			osg::ref_ptr<osg::StateSet> stateset = geode->getOrCreateStateSet();
			stateset->setTextureAttributeAndModes(0, prefilterMap_0, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
			stateset->setTextureAttributeAndModes(1, prefilterMap_1, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
			stateset->setTextureAttributeAndModes(2, prefilterMap_2, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
			stateset->setTextureAttributeAndModes(3, prefilterMap_3, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
			stateset->setTextureAttributeAndModes(4, prefilterMap_4, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
			stateset->setTextureAttributeAndModes(5, irradianceTextureCubeMap, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
			stateset->setTextureAttributeAndModes(6, textureBRDFLUT, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);



			osg::ref_ptr<osg::Shader> vs = new osg::Shader(osg::Shader::VERTEX, vertexShader_PBR);
			osg::ref_ptr<osg::Shader> ps = new osg::Shader(osg::Shader::FRAGMENT, psShader_PBR);
			osg::ref_ptr<osg::Program> program = new osg::Program;
			program->addBindAttribLocation("aPos", 1);
			program->addBindAttribLocation("aNormal", 2);
			program->addShader(vs);
			program->addShader(ps);

			osg::ref_ptr<osg::Uniform> metallicUniform = new osg::Uniform("metallic", metallic);
			osg::ref_ptr<osg::Uniform> roughnessUniform = new osg::Uniform("roughness", roughness);
			//osg::ref_ptr<osg::Uniform> transposeInverseMatrixUniform = new osg::Uniform("normalMatrix", transPose);

			osg::Uniform* transposeInverseMatrixUniform = stateset->getOrCreateUniform("normalMatrix", osg::Uniform::FLOAT_MAT4);
			transposeInverseMatrixUniform->set(transPose);
			stateset->addUniform(albedoUniform);
			stateset->addUniform(metallicUniform);
			stateset->addUniform(roughnessUniform);
			stateset->addUniform(aoUniform);
			stateset->addUniform(lightPositionsUniform);
			stateset->addUniform(lightColorsUniform);
			stateset->addUniform(transposeInverseMatrixUniform);
			stateset->addUniform(viewMatrixUniform);
			stateset->addUniform(projMatrixUniform);
			stateset->addUniform(camPosUniform);
			stateset->addUniform(tex0Uniform);
			stateset->addUniform(brdfLUTUniform);
			stateset->addUniform(prefilter0Uniform);
			stateset->addUniform(prefilter1Uniform);
			stateset->addUniform(prefilter2Uniform);
			stateset->addUniform(prefilter3Uniform);
			stateset->addUniform(prefilter4Uniform);
			stateset->setAttribute(program, osg::StateAttribute::ON);
		}
		grp->addChild(geode);
	}

}
grp->addChild(renderSphere(osg::Vec3(-10.0f, 10.0f, 10.0f)));
grp->addChild(renderSphere(osg::Vec3(10.0f, 10.0f, 10.0f)));
grp->addChild(renderSphere(osg::Vec3(-10.0f, -10.0f, 10.0f)));
grp->addChild(renderSphere(osg::Vec3(10.0f, -10.0f, 10.0f)));

viewer->getCamera()->setClearColor(osg::Vec4(0.1f, 0.1f, 0.1f, 1.0f));
viewer->setSceneData(grp);
viewer->run();

return 0;

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1048428.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C# 继承

C# 继承 继承的类型实现继承虚方法隐藏方法调用函数的基类版本抽象类和抽象函数密封类和密封方法派生类的构造函数修饰符访问修饰符其他修饰符 接口 继承的类型 实现继承 表示一个类型派生于一个基类型&#xff0c;拥有该基类型的所有成员字段和函数。在实现继承中&#xff0c…

【接口测试学习】白盒测试 接口测试 自动化测试

一、什么是白盒测试 白盒测试是一种测试策略&#xff0c;这种策略允许我们检查程序的内部结构&#xff0c;对程序的逻辑结构进行检查&#xff0c;从中获取测试数据。白盒测试的对象基本是源程序&#xff0c;所以它又称为结构测试或逻辑驱动测试&#xff0c;白盒测试方法一般分为…

无代码赋能数字化,云表搭桥铺路链接“数据孤岛”

什么是信息孤岛 企业数字化转型过程中&#xff0c;信息孤岛是一个突出的问题。这种情况发生的原因是&#xff0c;企业内部使用了多种应用软件&#xff0c;时间一长&#xff0c;员工在不同的系统中积累了大量的企业数据资产。然而&#xff0c;由于这些系统之间的数据无法互通&am…

Cruise 的界面和模型文件路径介绍

文章目录 打开 Cruise自带模型所在路径自带模型分类Cruise 中的模型路径解析打开用户手册建模界面介绍打开模型 打开 Cruise 最新的几个 Cruise 软件都是集成到一个平台上的&#xff0c;名为 AVL Advanced Simulation Desktop。 自带模型所在路径 User 选项卡下的模型&#x…

canvas手写签名组件

效果图&#x1f447; 代码不多直接粘在这里 <template><div class"border"><canvasref"canvas"width"800"height"500"class"border-success"tabindex"0"mousedown"onMouseDown"/>&…

云可观测性安全平台——掌动智能

云可观测性安全平台是一个跨架构、跨平台的可观测性方案&#xff0c;实现对云环境下的细粒度数据可视化&#xff0c;满足安全部门对云内部安全领域的多场景诉求&#xff0c;包括敏感数据动态监管、云网攻击回溯分析、攻击横移风险监控、云异常流量分析。本文将介绍掌动智能云可…

【RabbitMQ实战】01 3分钟在Linux上安装RabbitMQ

本节采用docker安装RabbitMQ。采用的是bitnami的镜像。Bitnami是一个提供各种流行应用的Docker镜像和软件包的公司。采用docker的方式3分钟就可以把我们想安装的程序运行起来&#xff0c;不得不说真的很方便啊&#xff0c;好了&#xff0c;开搞。使用前提&#xff1a;Linux虚拟…

验证NIO的非阻塞模型

我们知道传统BIO模型在等待客户端连接时是阻塞的&#xff0c;读取数据时如果没有数据&#xff0c;也是阻塞的&#xff0c;而NIO则可以配置成非阻塞&#xff0c;废话不多说&#xff0c;直接看代码&#xff1a; import java.net.InetSocketAddress; import java.nio.ByteBuffer;…

OWASP Top 10漏洞解析(2)- A2:Cryptographic Failures 加密机制失效

作者&#xff1a;gentle_zhou 原文链接&#xff1a;OWASP Top 10漏洞解析&#xff08;2&#xff09;- A2:Cryptographic Failures 加密机制失效-云社区-华为云 Web应用程序安全一直是一个重要的话题&#xff0c;它不但关系到网络用户的隐私&#xff0c;财产&#xff0c;而且关…

服务器补丁管理软件

随着漏洞的不断上升&#xff0c;服务器修补是增强企业网络安全的典型特征。作为业务关键型机器&#xff0c;计划服务器维护的停机时间无疑是一件麻烦事。但是&#xff0c;借助高效的服务器补丁管理软件&#xff08;如 Patch Manager Plus&#xff09;&#xff0c;管理员可以利用…

港联证券:市场有望从2024年起进入大众化折叠屏手机时代

根据Counterpoint Research的近期全球折叠屏智能手机追踪陈述&#xff0c;2023年第二季度全球折叠屏智能手机商场同比添加10%&#xff0c;达到210万部。该增幅与全球智能手机商场形成了鲜明对比&#xff0c;后者在同一季度出货量下降9%&#xff0c;达2.68亿部。估量智能手机商场…

Smtp4dev 虚拟SMTP电子邮件服务

Smtp4dev&#xff08;https://github.com/rnwood/smtp4dev&#xff09;用于开发和测试的虚拟SMTP电子邮件服务器。可以让你在测试应用程序时&#xff0c;无需向真实客户发送邮件&#xff0c;也无需使用特殊配置设置复杂的真实电子邮件服务器 dotnet tool install -g Rnwood.Smt…

Linux 安全 - SUID机制

文章目录 一、文件权限位二、SUID简介 一、文件权限位 &#xff08;1&#xff09; $ ls -l text.txt -rw-rw-r-- 1 yl yl 0 Sep 28 16:25 text.txt其中第一个字段-rw-rw-r–&#xff0c;我们可以把它分为四部分看&#xff1a; -rw-rw-r--&#xff08;1&#xff09;- &a…

Pygame中监控鼠标动作的方法

在Pygame中监控键盘按键的方法_pygame获取键盘输入-CSDN博客中提到&#xff0c;通过在while True循环中获取队列中事件的方法监控键盘动作。监控鼠标动作的方法与监控键盘动作的方法相同。 相关连接1 队列与事件的相关知识&#xff0c;请参考 Pygame中监控键盘按键的方法_pyg…

从技能需求到就业前景,了解前端和后端开发的优缺点和个人选择

文章目录 每日一句正能量一、引言前端开发后端开发 二、两者的对比分析三、技能转换和跨领域工作四&#xff1a;介绍全栈开发后记 每日一句正能量 命运决定的不是你的人生&#xff0c;能决定你人生的只有自己。 一、引言 前端和后端是Web开发中两个不可或缺的领域。前端开发主…

No131.精选前端面试题,享受每天的挑战和学习

🤍 前端开发工程师(主业)、技术博主(副业)、已过CET6 🍨 阿珊和她的猫_CSDN个人主页 🕠 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 🍚 蓝桥云课签约作者、已在蓝桥云课上架的前后端实战课程《Vue.js 和 Egg.js 开发企业级健康管理项目》、《带你从入…

vulnhub靶机-DC系列-DC-3

文章目录 信息收集漏洞查找漏洞利用SQL注入John工具密码爆破反弹shell 提权 信息收集 主机扫描 arp-scan -l可以用netdiscover 它是一个主动/被动的ARP 侦查工具。使用Netdiscover工具可以在网络上扫描IP地址,检查在线主机或搜索为它们发送的ARP请求。 netdiscover -r 192.1…

Linux或Centos查看CPU和内存占用情况_top只能查看对应的命令_如何查看具体进程---linux工作笔记062

一般我们都是用top去查看,但是top查看的结果,不能看出,具体是哪个程序占用的,这就很苦恼.. 其实如果有时间的话,再去专门看一下网络安全和linux脚本以及命令方面的,比较系统的看一下比较好.现在积累的都是工作中用到的,比较零散的知识. 如果用top,比如说这里的java,就只能知道…

云服务器租用价格表概览_阿里云腾讯云华为云

云服务器租用价格多少钱一年&#xff1f;阿腾云分享阿里云、腾讯云和华为云的云服务器租用价格表&#xff1a;阿里云2核2G服务器108元一年起、腾讯云2核2G3M带宽轻量服务器95元一年、华为云2核2G3M云耀L实例89元一年起&#xff0c;阿腾云分享更多关于云服务器租用价格明细&…

eNSP网络学习-v05

IP容量 ip地址一共是32位&#xff0c;/24就表示他的网络号是24位。 也就是说共有 2^&#xff08;32-24&#xff09;-2 个主机&#xff08;因为主机为全0和1的保留不用&#xff0c;所以需要减2&#xff09;&#xff0c;共254个ip。 /24&#xff1a;2的8次方-2 &#xff1a;254 …