Advertisement

Depth Error Bloom Texture

Started by January 05, 2019 02:31 AM
8 comments, last by WhiskyAndCode 6 years, 1 month ago

Hello everyone.
I was implementing bloom in a texture as below example:

Ovz5u.png.b030389bfbe42d88e3fd36dcf35242c6.png

T1.png.d00054f1393bbb1fa94bc5efc764a051.png 

tanlglow1.png.26c8ed6d046c25f7eb0dd33d658e8100.png

however, bloom is having depth problems:

ErroBloom.thumb.png.06b147f3f9a4410eae47392c64a21ffa.png

Texture:
image.png.9f674d599b2a61e073dfe93ae755c213.png

My code is:

Framebuffer (deferred shading):


unsigned int _texturePosition;
unsigned int _textureDiffuse;
unsigned int _textureNormal;
unsigned int _textureLight;
unsigned int _textureBloom;

unsigned int attachments[5] = { 
			GL_COLOR_ATTACHMENT0, //pos
			GL_COLOR_ATTACHMENT1, //diff
			GL_COLOR_ATTACHMENT2, //normal
			GL_COLOR_ATTACHMENT3, //light
			GL_COLOR_ATTACHMENT4  //Bloom
	};

glGenFramebuffers(1, &_frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, _frameBuffer);

glGenTextures(1, &_texturePosition);
glBindTexture(GL_TEXTURE_2D, _texturePosition);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texturePosition, 0);

glGenTextures(1, &_textureDiffuse);
glBindTexture(GL_TEXTURE_2D, _textureDiffuse);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, _textureDiffuse, 0);

glGenTextures(1, &_textureNormal);
glBindTexture(GL_TEXTURE_2D, _textureNormal);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, _textureNormal, 0);

glGenTextures(1, &_textureLight);
glBindTexture(GL_TEXTURE_2D, _textureLight);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, _textureLight, 0);

glGenTextures(1, &_textureBloom);
glBindTexture(GL_TEXTURE_2D, _textureBloom);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, GL_TEXTURE_2D, _textureBloom, 0);

glDrawBuffers(5, attachments);

My bloom Shader (fragment):


#version 330
                                                                        
in vec2 TexCoord;                                                                  
in vec3 Nrm;                                                                    
in vec3 WorldPos;    

layout (location = 0) out vec3 WorldPosOut;
layout (location = 1) out vec4 DiffuseOut;
layout (location = 2) out vec3 NormalOut;
layout (location = 3) out vec4 LightOut;
layout (location = 4) out vec4 BloomOut;

uniform sampler2D Texture;
uniform sampler2D TextureBloom;

uniform vec3 AmbientDirection;
uniform vec3 AmbientColor;
uniform float AmbientPower;

uniform float discardAlphaPNGValue;

void main()
{	
	vec4 diffuseTexture = vec4(texture2D(Texture, TexCoord));

	if(diffuseTexture.a < discardAlphaPNGValue){
		discard;
	}

    DiffuseOut      = diffuseTexture;
    WorldPosOut     = WorldPos;
    NormalOut       = normalize(Nrm);
	BloomOut		= texture2D(TextureBloom, TexCoord);

    float dt = 1.0 - (max(dot(NormalOut, -AmbientDirection),0.0) * 0.5);

    LightOut = vec4(AmbientColor * AmbientPower * dt, 1);
}

Using bloom shader:



//.........
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _frameBuffer);
glDrawBuffers(5, attachments);

//.........
glEnable(GL_DEPTH_TEST);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

_bloomShader.use();
_bloomShader.setMat4("View", view);
_bloomShader.setMat4("Proj", projection);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureTree2);
_bloomShader.setInt("Texture", 0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textureTreeBloom);
_bloomShader.setInt("TextureBloom", 1);

_bloomShader.setMat4("World", model);

_bloomShader.setVec3Glm("AmbientColor", AMBIENT_COLOR);
_bloomShader.setFloat("AmbientPower", AMBIENT_POWER);
_bloomShader.setVec3Glm("AmbientDirection", glm::vec3(1, 1, 1));

tree->draw(); //glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);

 

And final shader to combine:


out vec4 FragColor; 

uniform sampler2D ColorBuffer; //0
uniform sampler2D LightBuffer; //1
uniform sampler2D BloomBuffer; //2

in vec2 TexCoord;

void main()
{
	vec4 color = texture2D(ColorBuffer, TexCoord);
	vec4 bloom = texture2D(BloomBuffer, TexCoord);
	FragColor = (color * texture2D(LightBuffer, TexCoord) ) + bloom;

}

And to use:


glClearColor(0.52f, 0.80f, 0.92f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glCullFace(GL_BACK);

_finalCombine.use();

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _gBuffer._textureDiffuse);
_finalCombine.setInt("ColorBuffer", 0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, _gBuffer._textureLight);
_finalCombine.setInt("LightBuffer", 1);

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, _gBuffer._textureBloom);
_finalCombine.setInt("BloomBuffer", 2);


_fsQuad.draw(); //fullscreen quad to deferred shading

can anybody help me ?

Thank you

Additional information:
I'm inspiring myself in this tutorial:
https://learnopengl.com/Advanced-Lighting/Bloom

other example:
image.png.58a7f7ad4b2e0af014ed8154a39824aa.png

Bloom.jpg

 

Advertisement

I did not understand your problem

any of "example code" work for you or no? link on that tutorial have source https://learnopengl.com/code_viewer_gh.php?code=src/5.advanced_lighting/7.bloom/bloom.cpp is this work for you?

I see GL_NEAREST in code, when "example" use GL_LINEAR, maybe this is problem (I had problem because of this option, so maybe)

Hello Atri, thank you for your reply.
Actually the effect I was able to do, however, when I adapted my code to use framebuffer and deferred shading, then I could not, the effect is correct in my code, but the problem is the depth it is overlapping. 

only my texture representing the "bloom" is overlapping everything.

image.png.f20df4cba983c8a4d25705919248e228.png

image.png.3879abf9c7803023d4e320b7ba4d1203.png

sorry I see "too many reasons" for this... can not help, maybe my tips will be very stupid sorry

maybe the best way to debug it:

1. save "textures" that your engine generate for "bloom" effect, check them if they correct

2. send these two images as "textures" to your bloom-framebuffer, and look on result

so you will know where the problem, in the "engine" or in bloom shader

4 hours ago, WhiskyAndCode said:

Actually the effect I was able to do, however, when I adapted my code to use framebuffer and deferred shading, then I could not, the effect is correct in my code, but the problem is the depth it is overlapping. 

"bloom effect" is full screen shader, it can not make any problems to "rendering engine"...

is in "rendering engine", my texture is OK, look:

image.png.b4dc21d2d63e9c4614041ecf567e9ce5.png

it's just the depth that I can not control / correct.

Advertisement

then problem in bloom shader, test it with textures only, start from your linked tutorial...idk

Try clearing only color buffer (and check your depth states), maybe you're clearing depth buffer at some point and that's why the Bloom spills out..?


glClear(GL_COLOR_BUFFER_BIT);

Also, ambient light does not have a direction - what you're doing is more like a weak directional light ;)

 

.:vinterberg:.

Hello Vinterberg and atri, thank u again.

Now I found out what it was, I was not "feeding" (with vec4 (0)) the color attachment 4 on the other shaders.
I added out vec4 bloomOut and set vec4 (0) and it worked.

thank you all.

This topic is closed to new replies.

Advertisement