Render to a texture

Started by
1 comment, last by silvermace 15 years, 9 months ago
Hi, When I render to a texture, I have a problem. I have checked out the data in texture, and it's correct. But the texture cannot be rendered.I change the texture with a texture generating from a BMP file, It works. Here is some source: <div class="source">

GLuint Texture;
glGenTextures(1, &Texture);
glBindTexture(GL_TEXTURE_2D, Texture);
glTexImage2D(GL_TEXTURE_2D,0,3,windowWidth,windowHeight,0,GL_RGB,GL_UNSIGNED_BYTE,NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

rendering...

glBindTexture(GL_TEXTURE_2D,Texture);
glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGB,0,0,windowWidth,windowHeight,0);

These codes are rendering to a texture. <div class="source">

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f);
glVertex2f(-x,-y);
glTexCoord2f(1.0f,0.0f);
glVertex2f(x,-y);
glTexCoord2f(1.0f,1.0f);
glVertex2f(x,y);
glTexCoord2f(0.0f,1.0f);
glVertex2f(-x,y);
glEnd();

Run this code I only get a white QUAD without texture. If I use a texture generating from a BMP file instead, I get the QUAD with texture. Is there any step missing in rendering to a texture?
Advertisement
The size of your window probably isn't a power of two or even square at all.
Your window size must be a power of two.
Someone who uses a, euhm..., delta!?
Quote:Original post by delta user
The size of your window probably isn't a power of two or even square at all.
Your window size must be a power of two.


That entirely depends on your hardware setup see: EXT_texture_non_power_of_two.

firstly, why aren't you using Frame buffer Objects? same setup complexity but much easier to use and more debuggable not to mention much more performant.

use glGetError after your calls to glTexImage and glCopyTexImage; actually use it as much as possible when developing rendering code, its a massive timesaver thats often overlooked. You could be missing a required glTexParameteri argument or calling those methods while in the wrong GL state.

Lastly, try read back the texture into CPU memory and see if the actual pixel data matches up, e.g. set the clear colour to green, read back the framebuffer into the texture and see if the texture data actually contains just green pixesl. you can use glGetTexImage2D for this?
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website

This topic is closed to new replies.

Advertisement