Render To Texture...
Hi there.
Been fiddling with a thing that I cant understand:
When I render to a texture, nothing gets copied to the texture....
i.e if I set a specific texture as a "render target", then it remains the same afterwards.. :/
I also tried copy-and-paste tactics with Nehe code, didn't work when using their code either... although their demo runs and compiles just fine (I got a GF6)
anyone knows?
Could you post some code ? That part the texture is binded to be rendering target and how do you create this texture ?
I sure can... :)
//This is standard NeHe code, except the sizex,sizey variables:GLuint EmptyTexture(int sizex,int sizey) // Create an empty texture{ GLuint txtnumber; // Texture ID unsigned int* data; // Stored data // Create storage space for texture data (128x128x4) data = (unsigned int*)new GLuint[((sizex * sizey)* 4 * sizeof(unsigned int))]; ZeroMemory(data,((sizex * sizey)* 4 * sizeof(unsigned int))); // Clear storage memory glGenTextures(1, &txtnumber); // Create 1 texture glBindTexture(GL_TEXTURE_2D, txtnumber); // Bind the texture glTexImage2D(GL_TEXTURE_2D, 0, 4, sizex, sizey, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); // Build texture using information in data glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); delete [] data; // Release data return txtnumber; // Return the texture ID}void StartRenderToTexture(int sizex,int sizey){glPushMatrix(); glViewport(0,0,sizex,sizey); // Set our viewport (Match texture size)}void EndRenderToTexture(int Texture,int sizex,int sizey){ glBindTexture(GL_TEXTURE_2D,Texture); // Bind to the blur texture // Copy our viewPort to the blur texture (From 0,0 to 128,128... no border) glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 0, 0, sizex, sizey, 0); glViewport(0, 0, ScreenWidth, ScreenHeight); // Reset the current viewportglPopMatrix();}//init:RenderToTextureTarget = EmptyTexture(256,256);'void Draw(){//normal perspective mode:StartRenderToTexture(256,256);RenderTerrain();EndRenderToTexture(RenderToTextureTarget,256,256);//ortho glBindTexture(GL_TEXTURE_2D,textures[RenderToTextureTarget].texID); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex2f(ScreenWidth/2-128,ScreenHeight/2-128); // Bottom left glTexCoord2f(1,0); glVertex2f(ScreenWidth/2 +128,ScreenHeight/2-128); // Bottom right glTexCoord2f(1,1); glVertex2f(ScreenWidth/2+128, ScreenHeight/2+128); // Top right glTexCoord2f(0,1); glVertex2f(ScreenWidth/2-128, ScreenHeight/2+128); // Top left glEnd();}
You aren't actually rendering to a texture but copying the contents of the frame buffer to the texture using glCopyTexImage().
make sure that you set your read buffer to your write buffer.
If you're rendering to the back buffer you'll need a glReadBuffer(GL_BACK).
first get the current read buffer via glGetIntegerv.
then set your read buffer to your current write buffer.
glCopyTex...
set the read buffer back.
also, if you're rendering a lot to the frame buffer use a glFinish() before you do your glCopyTexImage to ensure that drawing has finished and you get what you expect in your texture.
make sure that you set your read buffer to your write buffer.
If you're rendering to the back buffer you'll need a glReadBuffer(GL_BACK).
first get the current read buffer via glGetIntegerv.
then set your read buffer to your current write buffer.
glCopyTex...
set the read buffer back.
also, if you're rendering a lot to the frame buffer use a glFinish() before you do your glCopyTexImage to ensure that drawing has finished and you get what you expect in your texture.
Hmm... that sounds like the thing I should use, but how would that look in code? :)
I tried simply putting glReadBuffer(GL_BACK) before glcopytex, but made no difference... :/
I tried simply putting glReadBuffer(GL_BACK) before glcopytex, but made no difference... :/
EndRenderToTexture(RenderToTextureTarget,256,256);
//ortho
glBindTexture(GL_TEXTURE_2D,textures[RenderToTextureTarget].texID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(ScreenWidth/2-128,ScreenHeight/2-128); // Bottom left
glTexCoord2f(1,0); glVertex2f(ScreenWidth/2 +128,ScreenHeight/2-128); // Bottom right
glTexCoord2f(1,1); glVertex2f(ScreenWidth/2+128, ScreenHeight/2+128); // Top right
glTexCoord2f(0,1); glVertex2f(ScreenWidth/2-128, ScreenHeight/2+128); // Top left
glEnd();
You're sending RenderToTextureTarget as your texID to EndRenderToTexture.
Then you're binding textures[RenderToTextureTarget].texID and drawing a quad.
Are these supposed to be the same texture?
are you sure you didn't mean to pass textures[RenderToTextureTarget].texID.
also, are you setting textures[RenderToTextureTarget].texID to the value returned by EmptyTexture?
I'm thinking perhaps your texture bindings are to blame at this point.
To be absolutely sure that you're reading from the right buffer you could call
glGetIntegerv(GL_WRITE_BUFFER,buffer) and then use glReadBuffer(buffer).
//ortho
glBindTexture(GL_TEXTURE_2D,textures[RenderToTextureTarget].texID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(ScreenWidth/2-128,ScreenHeight/2-128); // Bottom left
glTexCoord2f(1,0); glVertex2f(ScreenWidth/2 +128,ScreenHeight/2-128); // Bottom right
glTexCoord2f(1,1); glVertex2f(ScreenWidth/2+128, ScreenHeight/2+128); // Top right
glTexCoord2f(0,1); glVertex2f(ScreenWidth/2-128, ScreenHeight/2+128); // Top left
glEnd();
You're sending RenderToTextureTarget as your texID to EndRenderToTexture.
Then you're binding textures[RenderToTextureTarget].texID and drawing a quad.
Are these supposed to be the same texture?
are you sure you didn't mean to pass textures[RenderToTextureTarget].texID.
also, are you setting textures[RenderToTextureTarget].texID to the value returned by EmptyTexture?
I'm thinking perhaps your texture bindings are to blame at this point.
To be absolutely sure that you're reading from the right buffer you could call
glGetIntegerv(GL_WRITE_BUFFER,buffer) and then use glReadBuffer(buffer).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement