Bladeren bron

feat: 添加通过帧缓冲实现高斯模糊

NiceTry12138 10 maanden geleden
bovenliggende
commit
a5d397f55e

+ 1 - 1
图形学/OpenGL学习/src/OpenGLDemo/OpenGLDemo/imgui.ini

@@ -29,7 +29,7 @@ Size=418,267
 Collapsed=0
 
 [Window][Model]
-Pos=459,52
+Pos=335,51
 Size=396,162
 Collapsed=0
 

+ 23 - 1
图形学/OpenGL学习/src/OpenGLDemo/OpenGLDemo/res/shader/FrameBuffer/fb.frag

@@ -5,7 +5,29 @@ layout(location = 0) out vec4 o_color;
 in vec2 v_TexCoord;
 
 uniform sampler2D screenTexture;
+uniform float u_BlurSize;  // 模糊步长(通常为 1.0 / 纹理宽度 或 1.0 / 纹理高度)
 
 void main() {
-    o_color = vec4(v_TexCoord, 0.0f, 1.0f); 
+   // 高斯核权重(5次采样,权重和为1)
+   float weights[5] = float[](
+       0.227027,   // 中心点权重
+       0.1945946,  // ±1 偏移
+       0.1216216,  // ±2 偏移
+       0.054054,   // ±3 偏移
+       0.016216    // ±4 偏移
+   );
+   
+    // 初始颜色(中心点)
+   vec4 col = texture(screenTexture, v_TexCoord) * weights[0];
+   
+   // 横向模糊(左右各4次采样)
+   for(int i = 1; i < 5; i++) {
+       // 向右采样
+       col += texture(screenTexture, v_TexCoord + vec2(u_BlurSize * i, 0.0)) * weights[i];
+       // 向左采样 
+       col += texture(screenTexture, v_TexCoord - vec2(u_BlurSize * i, 0.0)) * weights[i];
+   }
+   
+   o_color = vec4(col.rgb, 1.0f);
+   
 }

+ 10 - 4
图形学/OpenGL学习/src/OpenGLDemo/OpenGLDemo/src/testModule/TestFrameBuffer.cpp

@@ -23,10 +23,18 @@ void TestFrameBuffer::OnEnter(GLFWwindow* window)
 
 	m_planeShader.Init("res/shader/FrameBuffer/fb.vert", "res/shader/FrameBuffer/fb.frag");
 	m_planeShader.SetUniform1i("screenTexture", 10);
+	m_planeShader.SetUniform1f("u_BlurSize", 1.0f / RSI->ViewportWidth);
+	m_planeShader.SetUniform1f("m_blurTimes", m_blurTimes);
 	m_planeShader.UnBind();
 
+	InitFBO();
+
+	m_ModelRotate = glm::vec3(-90.0f, 0.0f, 0.0f);
+
 	m_Light.Init();
 	m_LightPos = glm::vec3(5.0f);
+
+	m_blurSize = RSI->ViewportWidth;
 }
 
 void TestFrameBuffer::OnExit(GLFWwindow* window)
@@ -74,6 +82,7 @@ void TestFrameBuffer::Render(GLFWwindow* window)
 		glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
 		glClear(GL_COLOR_BUFFER_BIT);
 
+		m_planeShader.Bind();
 		glBindVertexArray(m_VAOPlane);
 		glActiveTexture(GL_TEXTURE10);
 		glBindTexture(GL_TEXTURE_2D, m_textureBuffer);
@@ -87,11 +96,8 @@ void TestFrameBuffer::UpdateImGUI(GLFWwindow* window)
 
 	ImGui::Begin("Model");
 
-	ImGui::SliderFloat3("Light Position", &m_LightPos.x, -10.0f, 10.0f);
-	ImGui::SliderFloat("Model Scale", &m_ModelScale, 0.1f, 2.0f);
-	ImGui::SliderFloat3("Model Rotate", &m_ModelRotate.x, -180.0f, 180.0f);
-	ImGui::SliderInt("Model Shineness", &m_Shineness, 0, 64);
 	ImGui::Checkbox("Show FBO Plane?", &m_ShowFBO);
+	ImGui::SliderInt("Blue Times ", &m_blurTimes, 1, 5);
 
 	if (ImGui::Button("Close Window"))
 		glfwSetWindowShouldClose(window, true);

+ 4 - 0
图形学/OpenGL学习/src/OpenGLDemo/OpenGLDemo/src/testModule/TestFrameBuffer.h

@@ -41,7 +41,11 @@ private:
 	GLuint m_FBO{ GL_ZERO };					// Frame Buffer Object 帧缓冲对象
 	GLuint m_VAOPlane{ GL_ZERO };				// 一个平面的 VAO 用于绘制帧缓冲效果
 	GLuint m_textureBuffer{ GL_ZERO };			// 用于存储帧缓冲的 贴图
+	GLuint m_depthTextureBuffer{ GL_ZERO };			// 用于存储帧缓冲的 贴图
 	Shader m_planeShader;
+
+	float m_blurSize;
+	int m_blurTimes = 1;
 	
 	bool m_ShowFBO{ true };						// 是否渲染到 FBO 上