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


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

    osg::ref_ptr prefilterMap_0 = new osg::TextureCubeMap;
    setImagesByLevel(prefilterMap_0, 0);
    osg::ref_ptr prefilterMap_1 = new osg::TextureCubeMap;
    setImagesByLevel(prefilterMap_1, 1);
    osg::ref_ptr prefilterMap_2 = new osg::TextureCubeMap;
    setImagesByLevel(prefilterMap_2, 2);
    osg::ref_ptr prefilterMap_3 = new osg::TextureCubeMap;
    setImagesByLevel(prefilterMap_3, 3);
    osg::ref_ptr prefilterMap_4 = new osg::TextureCubeMap;
    setImagesByLevel(prefilterMap_4, 4);
    
    osg::ref_ptr prefilter0Uniform = new osg::Uniform("prefilterMap0", 0);
    osg::ref_ptr prefilter1Uniform = new osg::Uniform("prefilterMap1",1);
    osg::ref_ptr prefilter2Uniform = new osg::Uniform("prefilterMap2", 2);
    osg::ref_ptr prefilter3Uniform = new osg::Uniform("prefilterMap3", 3);
    osg::ref_ptr prefilter4Uniform = new osg::Uniform("prefilterMap4", 4);
    osg::ref_ptr tex0Uniform = new osg::Uniform("irradianceMap", 5);
    osg::ref_ptr 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 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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

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

    #include
    #include
    #include
    #include

    #include
    #include
    #include

    #include
    #include

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    #include

    #include
    #include
    #include
    #include
    #include
    #include
    #include

    #include

    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"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    };

    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 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(indices->size());
    indices->setMode(GL_TRIANGLE_STRIP);
    
    osg::ref_ptr 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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    }

    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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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 imagePosX = osgDB::readImageFile(strImagePosX);
    textureCubemap->setImage(osg::TextureCubeMap::POSITIVE_X, imagePosX);
    
    std::string strImageNegX = strDir + "Left face camera.bmp";
    osg::ref_ptr imageNegX = osgDB::readImageFile(strImageNegX);
    textureCubemap->setImage(osg::TextureCubeMap::NEGATIVE_X, imageNegX);
    
    std::string strImagePosY = strDir + "Front face camera.bmp";;
    osg::ref_ptr imagePosY = osgDB::readImageFile(strImagePosY);
    textureCubemap->setImage(osg::TextureCubeMap::POSITIVE_Y, imagePosY);
    std::string strImageNegY = strDir + "Back face camera.bmp";;
    osg::ref_ptr imageNegY = osgDB::readImageFile(strImageNegY);
    textureCubemap->setImage(osg::TextureCubeMap::NEGATIVE_Y, imageNegY);
    
    std::string strImagePosZ = strDir + "Top face camera.bmp";
    osg::ref_ptr imagePosZ = osgDB::readImageFile(strImagePosZ);
    textureCubemap->setImage(osg::TextureCubeMap::POSITIVE_Z, imagePosZ);
    std::string strImageNegZ = strDir + "Bottom face camera.bmp";
    osg::ref_ptr imageNegZ = osgDB::readImageFile(strImageNegZ);
    textureCubemap->setImage(osg::TextureCubeMap::NEGATIVE_Z, imageNegZ);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

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

    osg::ref_ptr prefilterMap_0 = new osg::TextureCubeMap;
    setImagesByLevel(prefilterMap_0, 0);
    osg::ref_ptr prefilterMap_1 = new osg::TextureCubeMap;
    setImagesByLevel(prefilterMap_1, 1);
    osg::ref_ptr prefilterMap_2 = new osg::TextureCubeMap;
    setImagesByLevel(prefilterMap_2, 2);
    osg::ref_ptr prefilterMap_3 = new osg::TextureCubeMap;
    setImagesByLevel(prefilterMap_3, 3);
    osg::ref_ptr prefilterMap_4 = new osg::TextureCubeMap;
    setImagesByLevel(prefilterMap_4, 4);
    
    //漫反射贴图
    osg::ref_ptr 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 imagePosX = osgDB::readImageFile(strImagePosX);
    	irradianceTextureCubeMap->setImage(osg::TextureCubeMap::POSITIVE_X, imagePosX);
    
    	std::string strImageNegX = "D:/irradiance/Left face camera.bmp";
    	osg::ref_ptr imageNegX = osgDB::readImageFile(strImageNegX);
    	irradianceTextureCubeMap->setImage(osg::TextureCubeMap::NEGATIVE_X, imageNegX);
    
    	std::string strImagePosY = "D:/irradiance/Front face camera.bmp";;
    	osg::ref_ptr imagePosY = osgDB::readImageFile(strImagePosY);
    	irradianceTextureCubeMap->setImage(osg::TextureCubeMap::POSITIVE_Y, imagePosY);
    	std::string strImageNegY = "D:/irradiance/Back face camera.bmp";;
    	osg::ref_ptr imageNegY = osgDB::readImageFile(strImageNegY);
    	irradianceTextureCubeMap->setImage(osg::TextureCubeMap::NEGATIVE_Y, imageNegY);
    
    	std::string strImagePosZ = "D:/irradiance/Top face camera.bmp";
    	osg::ref_ptr imagePosZ = osgDB::readImageFile(strImagePosZ);
    	irradianceTextureCubeMap->setImage(osg::TextureCubeMap::POSITIVE_Z, imagePosZ);
    	std::string strImageNegZ = "D:/irradiance/Bottom face camera.bmp";
    	osg::ref_ptr imageNegZ = osgDB::readImageFile(strImageNegZ);
    	irradianceTextureCubeMap->setImage(osg::TextureCubeMap::NEGATIVE_Z, imageNegZ);
    }
    //积分贴图
    osg::ref_ptr textureBRDFLUT = new osg::Texture2D;
    {
    	std::string strBRDFLUTImageName = "d:/hdr/lut/brdflut.bmp";
    	osg::ref_ptr brdfLUTImage = osgDB::readImageFile(strBRDFLUTImageName);
    	textureBRDFLUT->setImage(brdfLUTImage);
    }
    
    
    osg::ref_ptr prefilter0Uniform = new osg::Uniform("prefilterMap0", 0);
    osg::ref_ptr prefilter1Uniform = new osg::Uniform("prefilterMap1",1);
    osg::ref_ptr prefilter2Uniform = new osg::Uniform("prefilterMap2", 2);
    osg::ref_ptr prefilter3Uniform = new osg::Uniform("prefilterMap3", 3);
    osg::ref_ptr prefilter4Uniform = new osg::Uniform("prefilterMap4", 4);
    osg::ref_ptr tex0Uniform = new osg::Uniform("irradianceMap", 5);
    osg::ref_ptr brdfLUTUniform = new osg::Uniform("brdfLUT", 6);
    
    osg::ref_ptr viewer = new osgViewer::Viewer;
    
    osg::ref_ptr camera = viewer->getCamera();
    osg::ref_ptr 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 camPosUniform = new osg::Uniform("camPos", eye);
    osg::ref_ptr viewMatrixUniform = new osg::Uniform("view", viewMatrix);
    viewMatrixUniform->setUpdateCallback(new ViewMatrixCallback(camera));
    osg::ref_ptr projMatrixUniform = new osg::Uniform("projection", projMatrix);
    projMatrixUniform->setUpdateCallback(new ProjectMatrixCallback(camera));
    osg::ref_ptr grp = new osg::Group;
    //漫反射比率
    osg::Vec3f albedo(0.5f, 0.0f, 0.0f);
    osg::ref_ptr albedoUniform = new osg::Uniform("albedo", albedo);
    float ao = 1.0f;
    osg::ref_ptr aoUniform = new osg::Uniform("ao", ao);
    
    int nrRows = 7;
    int nrColumns = 7;
    float spacing = 2.5;
    float ballRadius = 1.0f;
    
    osg::ref_ptr 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 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 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 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 geode = renderSphere(ballCenter);
    		{
    			osg::ref_ptr 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 vs = new osg::Shader(osg::Shader::VERTEX, vertexShader_PBR);
    			osg::ref_ptr ps = new osg::Shader(osg::Shader::FRAGMENT, psShader_PBR);
    			osg::ref_ptr program = new osg::Program;
    			program->addBindAttribLocation("aPos", 1);
    			program->addBindAttribLocation("aNormal", 2);
    			program->addShader(vs);
    			program->addShader(ps);
    
    			osg::ref_ptr metallicUniform = new osg::Uniform("metallic", metallic);
    			osg::ref_ptr roughnessUniform = new osg::Uniform("roughness", roughness);
    			//osg::ref_ptr 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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194

    }

  • 相关阅读:
    【广州华锐互动】VR虚拟党建云展馆:带你沉浸式领略红色文化
    【LeetCode】每日一题 2023_11_9 逃离火灾(bfs 练习)
    csp-202206
    常见荧光染料修饰多种基团及其激发和发射波长数据一览数据
    AIoT通用组件服务攻略之设备“收纳”好帮手——分组管理
    [附源码]计算机毕业设计springboot天狗电子商城系统
    Python tkinter -- 第12章 Scale属性
    05. DataTemplate
    【ACM学习】【STL】顺序容器的特性比较(不包括array)
    QFSFileEngine::open: No file name specified解决方案
  • 原文地址:https://blog.csdn.net/directx3d_beginner/article/details/133387598