Sign in to follow this  
maxest

Little problems with mipmapping and FBO

Recommended Posts

I think it'll be the best if I just put the code here:
	void CRenderTarget::init(RenderTargetFormat renderTargetFormat, int width, int height)
	{
		this->width = width;
		this->height = height;

		glGenFramebuffersEXT(1, &id);
		glGenTextures(1, &textureColor);
		glGenTextures(1, &textureDepthStencil);

		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);

		glBindTexture(GL_TEXTURE_2D, textureColor);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glGenerateMipmapEXT(GL_TEXTURE_2D);
		if (renderTargetFormat == rtfRGBA)
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
		else
			glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA32F_ARB, width, height, 0, GL_ALPHA, GL_FLOAT, NULL);
		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, textureColor, 0);

		glBindTexture(GL_TEXTURE_2D, textureDepthStencil);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8_EXT, width, height, 0, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, NULL);
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, textureDepthStencil, 0);
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, textureDepthStencil, 0);

		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

		CLogger::addText("OK: render target created\n");
	}
And somewhere else in my renderer:
	void CRenderer::setTexture(int sampler, const CRenderTarget &renderTarget)
	{
		glActiveTexture(GL_TEXTURE0 + sampler);
		glBindTexture(GL_TEXTURE_2D, renderTarget.textureColor);
		glGenerateMipmapEXT(GL_TEXTURE_2D);
	}
And now I have problems with moving glGenerateMipmapEXT into renderTarget::init. It's unnecessary to call this function every setTexture call, so I wanted to move to renderTarget::init but when I do, I get black color where my FBO image should be. Can anyone, please, tell me, where glGenerateMipmap should go?

Share this post


Link to post
Share on other sites
First, magnification filter should be GL_LINEAR because no mipmap is used in magnification.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);



I think automatic mipmap generation is better than glGenerateMipmapEXT() in your init().
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);



This call enables that glTexImage2D() triggers mipmap generation automatically.

And, use glGenerateMipmapEXT() explicitly after you perform render-to-texture with FBO.

Share this post


Link to post
Share on other sites
Quote:

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

This doesn't just work - get black screen.
Besides, this way of turning mipmapping on has been marked as deprecated in the new fantastic OGL 3.0 spec ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this