Skip to main content
GameDev.net gamedev.net
🔒 Locked

render to texture

Started by froni Jan 18, 2007 at 10:20 AM 2 replies 550+ views
Original Post
froni
froni
Hello! I have the following problem: I want to render my total screen(framebuffer) into a texture. This texture I try to apply on a quad. I try: //init func glGenTextures(3, &color_tex); glBindTexture(GL_TEXTURE_2D, color_tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //reshape func color =(unsigned int*)new GLuint[((width * height)* 4 * sizeof(unsigned int))]; glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, color); delete [] color; //display func glutSolidTeapot(3.0f); glBindTexture(GL_TEXTURE_2D, color_tex); glCopyTexSubImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 0, m_width, m_height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); float aspectRatio = (float) m_width / (float) m_height; gluPerspective( 60.0, // Field of view in degree aspectRatio, // Aspect ratio 0.1, // Z near 100.0); // Z far glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 0, 10, 0.0 ,0.0, 0.0, // XYZ view center 0.0, 1.0, 0.0); // Up is in positive Y direction glClear(GL_COLOR_BUFFER_BIT); glDisable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glBindTexture(GL_TEXTURE_2D, color_tex); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glEnable(GL_TEXTURE_2D); glFrontFace(GL_CW); glBegin(GL_QUADS); glVertex3f(-2.0,-1.0,0.0); glVertex3f(-2.0,1.0,0.0); glVertex3f(0.0,1.0,0.0); glVertex3f(0.0,-1.0,0.0); glEnd(); But the quad stay white!!!!!! Thanks for help!!! Froni
AndyEsser
AndyEsser
I haven't read through all your code, and I haven't tried rendering to texture yet myself. But i'm pretty sure you need some texture coordinates on the quad. Otherwise it has no idea how to apply the texture.
froni
froni
Sorry, I copy the wrong piece of code.

Of cause I have texture coordinates:

glFrontFace(GL_CW);
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex3f(-2.0,-1.0,0.0);
glTexCoord2f(0.0,1.0);
glVertex3f(-2.0,1.0,0.0);
glTexCoord2f(1.0,1.0);
glVertex3f(0.0,1.0,0.0);
glTexCoord2f(1.0,0.0);
glVertex3f(0.0,-1.0,0.0);
glEnd();

I realy don't know why I don't see my teapot???

Kalidor
Kalidor
Quote:
Original post by froni
//reshape func
color =(unsigned int*)new GLuint[((width * height)* 4 * sizeof(unsigned int))];
glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, color);
delete [] color;
[EDIT]
Whoops... forgot the main thing I wanted to mention. Are width and height both powers of two? Or does your video card support non-power-of-two (NPOT) textures? It's very common to have a frame-buffer of NPOT dimensions.
[/EDIT]

You're allocating more unsigned ints then are used; you don't need to include the size of the data type with operator new.

You're also telling OpenGL that you're passing an array of unsigned bytes (you either want color to be an array of unsigned chars or you want the type parameter to be GL_UNSIGNED_INT) so it's interpreting the (undefined) data you are passing to it incorrectly, which isn't too big a deal in this case because the array is large enough that it won't try to read out of bounds and because you don't care about what image data you are sending, you only care that the texture is created. And since you don't care what image data the texture is created with, you don't need to send any data at all. If the data parameter is a null pointer, the texture is created properly but with undefined image data.

You should also use one of the supported symbolic constants for the internalformat parameter instead of using 4. The more specific you are, the better (ie: prefer GL_RGBA8 over GL_RGBA if you want a 32-bit RGBA texture).
Quote:
Original post by froni
//display func
...
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 0, m_width, m_height);
...
The third parameter to glCopyTexSubImage2D is xoffset, not a format parameter. Since the texture is already created you don't need to specify a new internal format, and since you aren't passing any data you don't need to specify a data format. The function prototype for glCopyTexSubImage2D is...
Quote:
From the OpenGL Specs (with added prefixes)
void glCopyTexSubImage2D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height );

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.