二十二,加上各种贴图

使用pbr的各种贴图,albedo,金属度,ao,法线,粗糙度,可以更好的控制各个片元
1,先加上纹理坐标
texCoords->push_back(osg::Vec2(xSegment, ySegment));
geom->setVertexAttribArray(3, texCoords, osg::Array::BIND_PER_VERTEX);

program->addBindAttribLocation("aTexCoords", 3);
in vec2 aTexCoords;	
varying vec2 TexCoords;
TexCoords = aTexCoords;	

然后加上各种贴图

"uniform sampler2D albedoMap;																	   \n"
"uniform sampler2D normalMap;																	   \n"
"uniform sampler2D metallicMap;																	   \n"
"uniform sampler2D roughnessMap;																   \n"
"uniform sampler2D aoMap;																		   \n""vec3 getNormalFromMap()																			\n"
"{																									\n"
"	vec3 tangentNormal = texture(normalMap, TexCoords).xyz * 2.0 - 1.0;								\n"
"																									\n"
"	vec3 Q1 = dFdx(WorldPos);																		\n"
"	vec3 Q2 = dFdy(WorldPos);																		\n"
"	vec2 st1 = dFdx(TexCoords);																		\n"
"	vec2 st2 = dFdy(TexCoords);																		\n"
"																									\n"
"	vec3 N = normalize(Normal);																		\n"
"	vec3 T = normalize(Q1*st2.t - Q2*st1.t);														\n"
"	vec3 B = -normalize(cross(N, T));																\n"
"	mat3 TBN = mat3(T, B, N);																		\n"
"																									\n"
"	return normalize(TBN * tangentNormal);															\n"
"}																									\n""vec3 albedo = pow(texture(albedoMap, TexCoords).rgb, vec3(2.2));								  \n""float metallic = texture(metallicMap, TexCoords).r;											  \n""float roughness = texture(roughnessMap, TexCoords).r;											  \n""float ao = texture(aoMap, TexCoords).r;														  \n""vec3 N = getNormalFromMap();		\n"osg::ref_ptr<osg::Texture2D> albedoTexture = albedoVector[row];osg::ref_ptr<osg::Texture2D> normalTexture = normalVector[row];osg::ref_ptr<osg::Texture2D> metallicTexture = metallicVector[row];osg::ref_ptr<osg::Texture2D> roughnessTexture = roughnessVector[row];osg::ref_ptr<osg::Texture2D> aoTexture = aoVector[row];osg::ref_ptr<osg::Uniform> albedoUniform = new osg::Uniform("albedoMap", 7);osg::ref_ptr<osg::Uniform> normalUniform = new osg::Uniform("normalMap", 8);osg::ref_ptr<osg::Uniform> metallicUniform = new osg::Uniform("metallicMap", 9);osg::ref_ptr<osg::Uniform> roughnessUniform = new osg::Uniform("roughnessMap", 10);osg::ref_ptr<osg::Uniform> aoUniform = new osg::Uniform("aoMap", 11);stateset->setTextureAttributeAndModes(7, albedoTexture, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);stateset->setTextureAttributeAndModes(8, normalTexture, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);stateset->setTextureAttributeAndModes(9, metallicTexture, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);stateset->setTextureAttributeAndModes(10, roughnessTexture, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);stateset->setTextureAttributeAndModes(11, aoTexture, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);stateset->addUniform(albedoUniform);stateset->addUniform(normalUniform);stateset->addUniform(metallicUniform);stateset->addUniform(roughnessUniform);stateset->addUniform(aoUniform);

其余的不变,运行结果如下
在这里插入图片描述
代码如下

//通过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”
“in vec2 aTexCoords; \n”
“varying vec3 WorldPos; \n”
“varying vec3 Normal; \n”
“varying vec2 TexCoords; \n”
“uniform mat4 normalMatrix; \n”
“void main() \n”
“{ \n”
" WorldPos = aPos; \n"
" Normal = vec3(normalMatrix * vec4(aNormal,1.0)); \n"
" TexCoords = aTexCoords; \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”
“varying vec2 TexCoords; \n”
“uniform sampler2D albedoMap; \n”
“uniform sampler2D normalMap; \n”
“uniform sampler2D metallicMap; \n”
“uniform sampler2D roughnessMap; \n”
“uniform sampler2D aoMap; \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"
"vec3 getNormalFromMap()																			\n"
"{																									\n"
"	vec3 tangentNormal = texture(normalMap, TexCoords).xyz * 2.0 - 1.0;								\n"
"																									\n"
"	vec3 Q1 = dFdx(WorldPos);																		\n"
"	vec3 Q2 = dFdy(WorldPos);																		\n"
"	vec2 st1 = dFdx(TexCoords);																		\n"
"	vec2 st2 = dFdy(TexCoords);																		\n"
"																									\n"
"	vec3 N = normalize(Normal);																		\n"
"	vec3 T = normalize(Q1*st2.t - Q2*st1.t);														\n"
"	vec3 B = -normalize(cross(N, T));																\n"
"	mat3 TBN = mat3(T, B, N);																		\n"
"																									\n"
"	return normalize(TBN * tangentNormal);															\n"
"}																									\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"// material properties"vec3 albedo = pow(texture(albedoMap, TexCoords).rgb, vec3(2.2));								  \n""float metallic = texture(metallicMap, TexCoords).r;											  \n""float roughness = texture(roughnessMap, TexCoords).r;											  \n""float ao = texture(aoMap, TexCoords).r;														  \n""vec3 N = getNormalFromMap();		\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;
osg::ref_ptrosg::Vec2Array texCoords = new osg::Vec2Array;
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));texCoords->push_back(osg::Vec2(xSegment, ySegment));}
}
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);
geom->setVertexAttribArray(3, texCoords, 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);//Pbr贴图
std::string strPBRDir = "D:/tutorial/LearnOpenGL-master/LearnOpenGL-master/resources/textures/pbr/";
//种类
std::vector<std::string> categoryVector;
categoryVector.clear();
categoryVector.push_back("gold");
categoryVector.push_back("grass");
categoryVector.push_back("plastic");
categoryVector.push_back("rusted_iron");
categoryVector.push_back("wall");std::vector<osg::ref_ptr<osg::Texture2D>> roughnessVector;
roughnessVector.clear();
roughnessVector.resize(5);
for (int i = 0; i < 5; i++)
{std::string str = strPBRDir + categoryVector[i] + "/roughness.bmp";osg::ref_ptr<osg::Image > img = osgDB::readImageFile(str);roughnessVector[i] = new osg::Texture2D;roughnessVector[i]->setImage(img);
}
std::vector<osg::ref_ptr<osg::Texture2D>> normalVector;
normalVector.clear();
normalVector.resize(5);
for (int i = 0; i < 5; i++)
{std::string str = strPBRDir + categoryVector[i] + "/normal.bmp";osg::ref_ptr<osg::Image > img = osgDB::readImageFile(str);normalVector[i] = new osg::Texture2D;normalVector[i]->setImage(img);
}
std::vector<osg::ref_ptr<osg::Texture2D>> metallicVector;
metallicVector.clear();
metallicVector.resize(5);
for (int i = 0; i < 5; i++)
{std::string str = strPBRDir + categoryVector[i] + "/metallic.bmp";osg::ref_ptr<osg::Image > img = osgDB::readImageFile(str);metallicVector[i] = new osg::Texture2D;metallicVector[i]->setImage(img);
}std::vector<osg::ref_ptr<osg::Texture2D>> albedoVector;
albedoVector.clear();
albedoVector.resize(5);
for (int i = 0; i < 5; i++)
{std::string str = strPBRDir + categoryVector[i] + "/albedo.bmp";osg::ref_ptr<osg::Image > img = osgDB::readImageFile(str);albedoVector[i] = new osg::Texture2D;albedoVector[i]->setImage(img);
}
std::vector<osg::ref_ptr<osg::Texture2D>> aoVector;
aoVector.clear();
aoVector.resize(5);
for (int i = 0; i < 5; i++)
{std::string str = strPBRDir + categoryVector[i] + "/ao.bmp";osg::ref_ptr<osg::Image > img = osgDB::readImageFile(str);aoVector[i] = new osg::Texture2D;aoVector[i]->setImage(img);
}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;
//漫反射比率int nrRows = 5;
int nrColumns = 5;
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++)
{osg::ref_ptr<osg::Texture2D> albedoTexture = albedoVector[row];osg::ref_ptr<osg::Texture2D> normalTexture = normalVector[row];osg::ref_ptr<osg::Texture2D> metallicTexture = metallicVector[row];osg::ref_ptr<osg::Texture2D> roughnessTexture = roughnessVector[row];osg::ref_ptr<osg::Texture2D> aoTexture = aoVector[row];osg::ref_ptr<osg::Uniform> albedoUniform = new osg::Uniform("albedoMap", 7);osg::ref_ptr<osg::Uniform> normalUniform = new osg::Uniform("normalMap", 8);osg::ref_ptr<osg::Uniform> metallicUniform = new osg::Uniform("metallicMap", 9);osg::ref_ptr<osg::Uniform> roughnessUniform = new osg::Uniform("roughnessMap", 10);osg::ref_ptr<osg::Uniform> aoUniform = new osg::Uniform("aoMap", 11);for (int col = 0; col < nrColumns; col++){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);stateset->setTextureAttributeAndModes(7, albedoTexture, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);stateset->setTextureAttributeAndModes(8, normalTexture, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);stateset->setTextureAttributeAndModes(9, metallicTexture, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);stateset->setTextureAttributeAndModes(10, roughnessTexture, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);stateset->setTextureAttributeAndModes(11, aoTexture, 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->addBindAttribLocation("aTexCoords", 3);program->addShader(vs);program->addShader(ps);//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(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->addUniform(albedoUniform);stateset->addUniform(normalUniform);stateset->addUniform(metallicUniform);stateset->addUniform(roughnessUniform);stateset->addUniform(aoUniform);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.rhkb.cn/news/144582.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

WebPack-打包工具

从图中我们可以看出&#xff0c;Webpack 可以将多种静态资源 js、css、less 转换成一个静态文件&#xff0c;减少了页面的请求. 下面举个例子 &#xff1a; main.js 我们只命名导出一个变量 export const name"老六"index.js import { name } from "./tset/…

聊聊并发编程——Condition

目录 一.synchronized wait/notify/notifyAll 线程通信 二.Lock Condition 实现线程通信 三.Condition实现通信分析 四.JUC工具类的示例 一.synchronized wait/notify/notifyAll 线程通信 关于线程间的通信&#xff0c;简单举例下&#xff1a; 1.创建ThreadA传入共享…

Vue之ElementUI实现登陆及注册

目录 ​编辑 前言 一、ElementUI简介 1. 什么是ElementUI 2. 使用ElementUI的优势 3. ElementUI的应用场景 二、登陆注册前端界面开发 1. 修改端口号 2. 下载ElementUI所需的js依赖 2.1 添加Element-UI模块 2.2 导入Element-UI模块 2.3 测试Element-UI是否能用 3.编…

【VUE复习·9】v-for 基础用法(循环渲染也叫列表渲染)

总览 1.v-for 都能循环什么 2.用法 一、v-for 都能遍历什么 能循环的东西包括&#xff1a;数组、对象、字符串&#xff08;和java里面的3个引用数据类型一样&#xff09;、纯粹循环数量&#xff08;少用&#xff09; 二、用法 1.用法1&#xff1a;简单循环&#xff08;遍历…

Activiz 9.2 for Linux Crack

Activiz 9.2 在 C#、.Net 和 Unity 软件中为您的 3D 内容释放可视化工具包的强大功能。 ActiViz 允许您轻松地将 3D 可视化集成到您的应用程序中。 ActiViz 功能 用 C# 封装的 3D 可视化软件系统 允许在 .NET 环境中快速开发可投入生产的交互式3D 应用程序 支持窗口演示基础 (…

SI3262:国产NFC+MCU+防水触摸按键三合一SoC芯片

目录 SI3262简介特点结构框图芯片特性 SI3262简介 Si3262是高度集成ACD低功耗MCUNFC15通道防水触摸按键的SoC芯片。 其MCU模块具有低功耗、Low Pin Count、宽电压工作范围&#xff0c;集成了13/14/15/16位精度的ADC、LVD、UART、SPI、I2C、TIMER、WUP、IWDG、RTC、TSC等丰富的…

紫光展锐6nm国产5G处理器T820_国产手机芯片5G方案

紫光展锐T820是一款采用先进6nm EUV工艺的芯片&#xff0c;采用134三丛集八核心CPU架构&#xff0c;由1个主频为 2.7GHz 的 Arm Cortex-A76 大核和 3个主频为2.3GHz 的Arm Cortex-A76大核以及4个主频为2.1GHz的 Arm Cortex-A55组成 &#xff0c;支持高达3MB 三级缓存&#xff0…

jvm内存分配与回收策略

自动内存管理 解决两个问题 自动给对象分配内存 对象一般堆上分配&#xff08;而实际上也有可能经过即时编译后被拆散为标量类型并间接地在栈上分配&#xff09; 新生对象通常会分配在新生代&#xff0c;少数情况下&#xff08;例如对象大小超过一定阈值&#xff09;也可能…

[C++网络协议] 优于select的epoll

1.epoll函数为什么优于select函数 select函数的缺点&#xff1a; 调用select函数后&#xff0c;要针对所有文件描述符进行循环处理。每次调用select函数&#xff0c;都需要向该函数传递监视对象信息。 对于缺点2&#xff0c;是提高性能的最大障碍。因为&#xff0c;套接字是…

Python爬虫实战案例——第六例

文章中所有内容仅供学习交流使用&#xff0c;不用于其他任何目的&#xff01;严禁将文中内容用于任何商业与非法用途&#xff0c;由此产生的一切后果与作者无关。若有侵权&#xff0c;请联系删除。 目标&#xff1a;去哪儿网指定城市人气值最高的15个景点评论数据采集 地址&a…

微信小程序开发基础(二)基本组件

本帖开始介绍小程序中的一些基本组件~ 微信小程序是一种轻量、快速、跨平台的应用程序&#xff0c;是微信公众号的重要组成部分。随着微信小程序的普及&#xff0c;越来越多的开发者和企业开始使用微信小程序来搭建自己的应用&#xff0c;但是对于初次接触微信小程序的开发者…

排序:败者树和置换选择排序(解决外部排序中的优化问题)

1.算法目的&#xff08;败者树&#xff09; 解决多路平衡归并带来的问题。 在外部排序中&#xff0c;使用k路平衡归并策略, 选出一个最小元素需要对比关键字(k-1)次&#xff0c; 导致内部归并所需时间增加。&#xff08;可用“败者树”进行优化&#xff09; 2.败者树的定义 …

【C++11】多线程

多线程创建线程thread提供的成员函数获取线程id的方式线程函数参数的问题线程join场景和detach 互斥量库&#xff08;mutex&#xff09;mutexrecursive_mutexlock_guard 和 unique_lock 原子性操作库&#xff08;atomic&#xff09;条件变量库&#xff08;condition_varuable&a…

一文读懂集合竞价,建议收藏,读完少交学费

集合竞价每个时间段交易规则及作用都不一样&#xff0c;了解集合竞价的规则&#xff0c;有利于琢磨主力的意图。 大部分同学都不是很关心集合竞价&#xff0c;也不知道如何利用集合竞价买卖股票。如上图所示&#xff0c;有同学在9点15看着股票涨停&#xff0c;立马冲进去&…

【切片】基础不扎实引发的问题

本次文章主要是来聊聊关于切片传值需要注意的问题&#xff0c;如果不小心&#xff0c;则很容易引发线上问题&#xff0c;如果不够理解&#xff0c;可能会出现奇奇怪怪的现象 问题情况&#xff1a; 小 A 负责一个模块功能的实现&#xff0c;在调试代码的时候可能不仔细&#x…

UE蓝图学习(从Unity3D而来)

一、UE组件对比Unity3D&#xff0c;从Unity3D过渡来学的角度出发&#xff0c;可以理解为在 空物体下放置子物体。UE没有U3D那种可以将组件挂在自身空物体上面。 二、UE 蓝图的情境提示&#xff0c;必须先找到相应的类型&#xff0c;对象对象、事件事件&#xff0c;才能找到相应…

云原生Kubernetes:Pod控制器

目录 一、理论 1.Pod控制器 2.Deployment 控制器 3.SatefulSet 控制器 4.DaemonSet 控制器 5.Job 控制器 6.CronJob 控制器 二、实验 1.Deployment 控制器 2.SatefulSet 控制器 3.DaemonSet 控制器 4.Job 控制器 5.CronJob 控制器 三、问题 1. showmount -e 报错…

QT窗口的设置、按钮的创建和对象树的概念

目录 设置窗口属性 按钮的创建 对象树 对象树的概念 构建和析构的顺序问题 设置窗口属性 在Qt官方手册中查找QWidget相关信息 或者在QT软件帮助一栏直接搜索QWidget 即可找到一些要寻找的设置属性的函数 将代码写在构造函数中 widget.cpp #include "widget.h"…

1200*A. Flipping Game(前缀和)

解析&#xff1a; 100数据量&#xff0c;两层遍历每个区间&#xff0c;然后前缀和计算1的个数&#xff0c;维护最大值即可。 #include<bits/stdc.h> using namespace std; #define int long long const int N110; int n,a[N],res,sum[N]; signed main(){scanf("%ll…

保姆级 -- Zookeeper超详解

1. Zookeeper 是什么(了解) Zookeeper 是一个 分布式协调服务 的开源框架, 主要用来解决分布式集群中应用系统的一致性问题, 例如怎样避免同时操作同一数据造成脏读的问题. ZooKeeper 本质上是 一个分布式的小文件存储系统 . 提供基于类似于文件系统的目录树方式的数据存储, …