Advertisement Jump to content
Sign in to follow this  
megav0xel

OpenGL Convolve mipmap chain in OpenGL

This topic is 719 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all.

 

I'm trying to convolve a mipmap chain of my screen texture using the following code. Unfortunately all mipmaps levels I get are black. 

 

C++

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, prevColorFrame1);
currentWidth = screenWidth;
currentHeight = screenHeight;
for (int i = 1; i<mipLevels; ++i) {
	Shader.SetUniform("MipSize", glm::vec2(currentWidth, currentHeight));
	currentWidth /= 2, currentHeight /= 2;
	currentWidth = currentWidth > 0 ? currentWidth : 1;
	currentHeight = currentHeight > 0 ? currentHeight : 1;
	glViewport(0, 0, currentWidth, currentHeight);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, i - 1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, i - 1);
	glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA16F, currentWidth, currentHeight, 0, GL_RGBA, GL_FLOAT, NULL);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, prevColorFrame1, i);
	DrawFullScreenQuad();
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, numLevels - 1);
glViewport(0, 0, screenWidth, screenHeight);

Here is my fragment shader.

#version 430

out vec4 FragColor;

uniform sampler2D colorBuffer;
uniform vec2 MipSize;

in vec2 TexCoords;

.........

void main()
{
......
    vec3 result = vec3(0.0);
    for(int i = 0; i < NumSamples; ++i)
    {
      vec2 offset = offsets[i]/MipSize;
      vec3 sampleColor = texture(colorBuffer, TexCoords + offset).rgb;
      result += sampleColor * weights[i];
    }
    FragColor = vec4(result, 1.0f);
}

Does anyone know how to manually generate mipmaps correctly in OpenGL? Any help would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement

FragColor = vec4(1,0,0,1); .... does this do anything? This validates your shader pipeline is working and textures are bound correctly.

 

Also try texture2DLod() instead to force a specific LOD, which should make these lines irrelevant I believe.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, i - 1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, i - 1);

 

I don't see where you set MIN/MAG texture filter parameters so your sampling may be messedup, though it should have defaults set I believe.

 

I still use glGenerateMipMap to generate mip maps. Could give it a try.

Share this post


Link to post
Share on other sites

FragColor = vec4(1,0,0,1); .... does this do anything? This validates your shader pipeline is working and textures are bound correctly.

 

Also try texture2DLod() instead to force a specific LOD, which should make these lines irrelevant I believe.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, i - 1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, i - 1);

 

I don't see where you set MIN/MAG texture filter parameters so your sampling may be messedup, though it should have defaults set I believe.

 

I still use glGenerateMipMap to generate mip maps. Could give it a try.

Thanks for reply!

 

I have solved the problem. I mistakenly set the attachment parameter of glFramebufferTexture2D() to GL_DEPTH_ATTACHMENT instead of GL_COLOR_ATTACHMENT. Now my code works correctly.

Thanks for your instructions anyway.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!