//init func
//----------------------
//FRONT
//----------------------
glGenFramebuffers(1, &m_FrameBufferFront);
glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBufferFront);
glGenTextures(1, &m_RenderedTextureFront);
glBindTexture(GL_TEXTURE_2D, m_RenderedTextureFront);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 800, 600, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_RenderedTextureFront, 0);
//----------------------
//BACK
//----------------------
glGenFramebuffers(1, &m_FrameBufferBack);
glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBufferBack);
glGenTextures(1, &m_RenderedTextureBack);
glBindTexture(GL_TEXTURE_2D, m_RenderedTextureBack);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 800, 600, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_RenderedTextureBack, 0);
glDrawBuffer(GL_NONE);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
return;
}
void CMeshManager::Draw_MeshManager()
{
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
//------------------------------
//FRONT PASS 1
//------------------------------
glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBufferFront);
glViewport(0,0,800,600);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f );
glClearDepth(1.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glUseProgram(m_ProgramHandleDepth);
glBindVertexArray(m_FsCube);
GLuint MatMVP = glGetUniformLocation(m_ProgramHandleDepth, "MVP");
if( MatMVP >= 0 )
{
glUniformMatrix4fv(MatMVP, 1, GL_FALSE, (const GLfloat*)&m_MatVP);
}
GLuint ZFar = glGetUniformLocation(m_ProgramHandleDepth, "ZFar");
if (ZFar >= 0)
{
glUniform1f(ZFar, m_ZFar);
}
glCullFace(GL_BACK);
glDrawElements(GL_TRIANGLES, 36,GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glUseProgram(0);
//------------------------------
//BACK PASS 2
//------------------------------
glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBufferBack);
glViewport(0,0,800,600);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f );
glClearDepth(1.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glUseProgram(m_ProgramHandleDepth);
glBindVertexArray(m_FsCube);
MatMVP = glGetUniformLocation(m_ProgramHandleDepth, "MVP");
if( MatMVP >= 0 )
{
glUniformMatrix4fv(MatMVP, 1, GL_FALSE, (const GLfloat*)&m_MatVP);
}
ZFar = glGetUniformLocation(m_ProgramHandleDepth, "ZFar");
if (ZFar >= 0)
{
glUniform1f(ZFar, m_ZFar);
}
glCullFace(GL_FRONT);
glDrawElements(GL_TRIANGLES,36,GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glUseProgram(0);
//---------------
//FOG PASS 3
//---------------
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0,800,600);
glEnable (GL_BLEND);
glBlendFunc (GL_ONE, GL_ONE);
glDisable (GL_DEPTH_TEST);
glCullFace(GL_BACK);
glClearColor(0.0f, 0.125f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(m_ProgramHandleFog);
glBindVertexArray(m_FsQuad);
glActiveTexture(GL_TEXTURE0);
glBindTexture( GL_TEXTURE_2D, m_RenderedTextureFront );
GLuint Tex = glGetUniformLocation(m_ProgramHandleFog, "ourTexture_Front");
if( Tex >= 0 )
{
glUniform1i(Tex, 0);
}
glActiveTexture(GL_TEXTURE1);
glBindTexture( GL_TEXTURE_2D, m_RenderedTextureBack );
Tex = glGetUniformLocation(m_ProgramHandleFog, "ourTexture_Back");
if( Tex >= 0 )
{
glUniform1i(Tex, 1);
}
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
glUseProgram(0);
}
//depth.vert
#version 330
layout (location = 0) in vec3 aPos;
out float TexDepth;
uniform mat4 MVP;
uniform float ZFar;
void main()
{
vec4 PositionT = MVP * vec4(aPos,1.0f);
gl_Position = PositionT;
TexDepth=PositionT.w/ZFar;
}
//depth.frag
#version 330
out float FragColor;
in float TexDepth;
void main()
{
gl_FragDepth = TexDepth;
}
//fog.frag
#version 330
out vec4 FragColor;
in vec2 tex;
uniform sampler2D ourTexture_Front;
uniform sampler2D ourTexture_Back;
uniform vec3 FogColor = {0.5f,0.5f,0.5f};
float FogFactor = 12.0f;
void main()
{
float front = texture(ourTexture_Front, tex).r;
float back = texture(ourTexture_Back, tex).r;
if(front == 1.0f)
front = 1.0f - front;
if(back == 1.0f)
back = 1.0f - back;
float k = (back-front) * FogFactor;
FragColor = vec4(FogColor*k, 1.0f);
}
I make volumetric fog. I draw a cube on the stage, in the first depth texture I record the depth of the front polygons of the cube, in the second depth texture I record the depth of the rear polygons of the cube. Then I set both textures in the shader, subtract the difference in depth from both textures - this is the intensity of the fog. I display the pixel with intensity on the screen.
The problem is that the code does not work correctly.
This code draws like this:
https://ibb.co/KXhksC1
It should be like this:
https://ibb.co/HnvWVsR
Who knows what's wrong?