"render To Texture" With Mipmaps On Opengl Es 2.0

Started by
3 comments, last by Ed Welch 7 years, 9 months ago

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.

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.

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

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.

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

This topic is closed to new replies.

Advertisement