二十二,加上各种贴图

使用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.mzph.cn/news/90959.shtml

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

相关文章

凹凸贴图如何提高物体的真实感

什么是凹凸贴图 凹凸贴图&#xff08;Bump Mapping&#xff09;是一种计算机图形学中的技术&#xff0c;用于在表面上模拟微小的凹凸形状&#xff0c;从而增加了物体的细节和真实感。它可以在不改变物体几何形状的情况下&#xff0c;通过修改光照的反应&#xff0c;使表面看起来…

Java编程技巧:文件上传、下载、预览

目录 1、上传文件1.1、代码1.2、postman测试截图 2、下载resources目录中的模板文件2.1、项目结构2.2、代码2.3、使用场景 3、预览文件3.1、项目结构3.2、代码3.3、使用场景 1、上传文件 1.1、代码 PostMapping("/uploadFile") public String uploadFile(Multipart…

idea Springboot 高校科研资源共享系统VS开发mysql数据库web结构java编程计算机网页源码maven项目

一、源码特点 springboot 高校科研资源共享系统是一套完善的信息系统&#xff0c;结合springboot框架和bootstrap完成本系统&#xff0c;对理解JSP java编程开发语言有帮助系统采用springboot框架&#xff08;MVC模式开发&#xff09;&#xff0c; 系统具有完整的源代码和数据…

嵌入式Linux应用开发-面向对象-分层-分离及总线驱动模型

嵌入式Linux应用开发-面向对象-分层-分离及总线驱动模型 第八章 驱动设计的思想&#xff1a;面向对象/分层/分离8.1 面向对象8.2 分层8.3 分离8.4 写示例代码8.5 课后作业 第九章 驱动进化之路&#xff1a;总线设备驱动模型9.1 驱动编写的 3种方法9.1.1 传统写法9.1.2 总线设备…

BUUCTF reverse wp 21 - 30

[ACTF新生赛2020]rome 无壳, 直接拖进IDA32 y键把v2改成char[49], n键重命名为iuput int func() {int result; // eaxint v1[4]; // [esp14h] [ebp-44h]char input[49]; // [esp24h] [ebp-34h] BYREFstrcpy(&input[23], "Qsw3sj_lz4_Ujwl");printf("Please…

《Upload-Labs》01. Pass 1~13

Upload-Labs 索引前言Pass-01题解 Pass-02题解总结 Pass-03题解总结 Pass-04题解 Pass-05题解总结 Pass-06题解总结 Pass-07题解总结 Pass-08题解总结 Pass-09题解 Pass-10题解 Pass-11题解 Pass-12题解总结 Pass-13题解 靶场部署在 VMware - Win7。 靶场地址&#xff1a;https…

基于Java的旅游管理系统设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言具体实现截图论文参考详细视频演示为什么选择我自己的网站自己的小程序&#xff08;小蔡coding&#xff09;有保障的售后福利 代码参考源码获取 前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计划导师、全栈领域优质创作…

FreeRTOS入门教程(任务优先级,Tick)

文章目录 前言一、什么是任务优先级二、FreeRTOS如何分辨出优先级最高可运行的任务三、FreeRTOS中的时钟节拍Tick四、什么是时间片五、相同优先级任务怎么进行切换六、任务优先级实验七、修改任务优先级总结 前言 本篇文章将带大家学习FreeRTOS中的任务优先级&#xff0c;并且…

GPT-4科研实践:数据可视化、统计分析、编程、机器学习数据挖掘、数据预处理、代码优化、科研方法论

查看原文>>>GPT4科研实践技术与AI绘图 GPT对于每个科研人员已经成为不可或缺的辅助工具&#xff0c;不同的研究领域和项目具有不同的需求。例如在科研编程、绘图领域&#xff1a;1、编程建议和示例代码: 无论你使用的编程语言是Python、R、MATLAB还是其他语言&#x…

【SSL】用Certbot生成免费HTTPS证书

1. 实验背景 服务器&#xff1a;CentOS7.x 示例域名&#xff1a; www.example.com 域名对应的web站点目录&#xff1a; /usr/local/openresty/nginx/html 2. 安装docker # yum -y install yum-utils# yum-config-manager --add-repo https://download.docker.com/linux/ce…

web:[极客大挑战 2019]LoveSQL

题目 打开页面显示如下 查看源代码&#xff0c;查到一个check.php&#xff0c;还是get传参 尝试账号密码输入 题目名为sql&#xff0c;用万能密码 1or 11# 或 admin or 11 给了一段乱码&#xff0c;也不是flag 查看字段数 /check.php?usernameadmin order by 3%23&pass…

Word | 简单可操作的快捷公式编号、右对齐和引用方法

1. 问题描述 在理工科论文的写作中&#xff0c;涉及到大量的公式输入&#xff0c;我们希望能够按照章节为公式进行编号&#xff0c;并且实现公式居中&#xff0c;编号右对齐的效果。网上有各种各样的方法来实现&#xff0c;操作繁琐和简单的混在一起&#xff0c;让没有接触过公…

Redis学习笔记(常用数据类型,发布订阅,事务和锁机制,持久化,集群,雪崩,缓存击穿,分布式锁)

一、NoSQL数据库简介 解决扩展性问题&#xff0c;如果需要对功能进行改变&#xff08;比如增删功能&#xff09;&#xff0c;用框架有一定的规范要求&#xff0c;无形中解决了扩展性问题。 Redis是一种典型的NoSQL数据库。 NoSQL的基础作用&#xff1a; 1. nginx负载均衡反向…

Armv9读取cache内容:Direct access to internal memory

10 访问cache Cortex-A720核心提供一种机制,通过IMPLEMENTATION DEFINED系统寄存器可以读取L1缓存、L2缓存和Translation Lookaside Buffer(TLB)。当缓存数据与系统内存数据之间的一致性异常时,您可以使用此机制来调查任何问题。 只有在EL3中才可以访问内部内存(cache)。…

排序:最佳归并树(优化外部排序中对磁盘的读写次数)

1.归并树的性质 每个初始归并段对应一个叶子结点&#xff0c;把归并段的块数作为叶子的权值归并树的WPL树中所有叶结点的带权路径长度之和归并过程中的磁盘I/O次数归并树的WPL*2 如下图&#xff1a; 每个初始归并段看作一个叶子结点&#xff0c;归并段的长度作为结点权值&a…

C/C++进程线程超详细详解

目录 前言 一、进程基础 1.进程概念 2.进程特征 3.进程状态&#xff08;如图清晰可见&#xff09; 4&#xff0c;进程的标识 实例代码如下&#xff1a; 5.进程的种类 实例shell脚本程序如下: 二、进程API 1.创建子进程 实例代码如下&#xff1a; 2.exec函数族 函数…

Socket网络编程练习题四:客户端上传文件(多线程版)

题目 想要服务器不停止&#xff0c;能接收很多客户上传的图片&#xff1f; 解决方案 可以使用循环或者多线程 但是循环不合理&#xff0c;最优解法是&#xff08;循环多线程&#xff09;改写 代码实战 客户端代码 package com.heima;import java.io.*; import java.net.S…

深度学习算法在工业视觉落地的思考

0.废话 距离上次的栈板识别的思考已经过去3个月&#xff0c;中间根据客户的需求和自己的思考&#xff0c;对软件又重新做了调整。但是整体上还是不满意。 0.1 老生常谈的工业视觉落地架构 对于软件架构&#xff0c;我实在没有太多的参考。没办法&#xff0c;公司根本不关心软…

使用华为eNSP组网试验⑵-通过端口地址进行静态路由

有了网络模拟器可以对很多网络应用场景进行模拟&#xff0c;既方便学习又有利于实际的网络实施。 之前因为没有用过&#xff0c;用过了才知道eNSP的好处。但是与思科模拟器不同&#xff0c;连接是自动连接&#xff0c;不能确定端口&#xff0c;比如使用指定的光纤端口或者RJ45的…

CSS详细基础(六)边框样式

本期是CSS基础的最后一篇~ 目录 一.border属性 二.边框属性复合写法 三.CSS修改表格标签 四.内边距属性 五.外边距属性 六.其他杂例 1.盒子元素水平居中 2.清除网页内外元素边距 3.外边距的合并与塌陷 4.padding不会撑大盒子的情况 七.综合案例——新浪导航栏仿真 …