Jump to content
  • Advertisement
Sign in to follow this  
Ed Welch

OpenGL "render To Texture" With Mipmaps On Opengl Es 2.0

This topic is 793 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

Does anyone know how to do "render to texture" with mipmaps on OpenGL ES 2.0?

This is the code that I have:

   // Get the original framebuffer object handle.
    glGetIntegerv(GL_FRAMEBUFFER_BINDING, &m_origFrameBuffer);
	// Create a frame buffer with only the color buffer attached
	glGenFramebuffers(1, &m_groundFrameBuffer);
	glBindFramebuffer(GL_FRAMEBUFFER, m_groundFrameBuffer);
	//Create the ground map texture
	glGenTextures(1, &m_textureID);
	glBindTexture(GL_TEXTURE_2D, m_textureID);
	// Set the textures parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	// Give an empty image to OpenGL ( the last "0" )
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_groundMapSize, m_groundMapSize, 
		0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
	glGenerateMipmap(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, 0);	// unbind texture
	glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_textureID, 0);

	int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

	if (status != GL_FRAMEBUFFER_COMPLETE)
	{
		throw RacException("ground framebuffer not created");
	}

This code doesn't work. I get a black texture when the mipmapping kicks in.

Share this post


Link to post
Share on other sites
Advertisement
At least in the code you posted you appear to only ever bind mipmap-level 0 to render on. If you want to glGenerateMipmap() to propagate the rendered state to the lower levels, you will have to call it after rendering. It's not really magic, it just takes the current content of the texture's base level and creates the mip map levels from that in the canonical way. Any changes after that in any level are not reflected in the other mipmap levels.

Share this post


Link to post
Share on other sites

At least in the code you posted you appear to only ever bind mipmap-level 0 to render on. If you want to glGenerateMipmap() to propagate the rendered state to the lower levels, you will have to call it after rendering. It's not really magic, it just takes the current content of the texture's base level and creates the mip map levels from that in the canonical way. Any changes after that in any level are not reflected in the other mipmap levels.

Do you mean call    

glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, m_textureID, 0);

?

I tried that. It doesn't work

 

Share this post


Link to post
Share on other sites
That binds mipmap level 0 to the render target. If you want to render to a different mipmap level, you need to bind that and then render to it.

Share this post


Link to post
Share on other sites

I figured out my mistake. I was calling  glGenerateMipmap after creation of the texture and then once again after I render to the texture, but I didn't release that I had to unbind the frame buffer first and then call glBindTexture:

// after rendering to texture
glBindFramebuffer(GL_FRAMEBUFFER, m_origFrameBuffer);
	glBindTexture(GL_TEXTURE_2D, m_textureID);
	glGenerateMipmap(GL_TEXTURE_2D);

Thanks for the help anyways

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!