Jump to content
  • Advertisement
Sign in to follow this  
Rasmadrak

Render To Texture...

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

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?

Share this post


Link to post
Share on other sites
Advertisement
Could you post some code ? That part the texture is binded to be rendering target and how do you create this texture ?

Share this post


Link to post
Share on other sites
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 viewport

glPopMatrix();
}

//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();


}




Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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... :/

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Thanks a bunch :)

it was the texture loading code that didn't work... When I loaded a regular TGA file instead of using EmptyTexture() it worked perfectly... didn't even have to set the readbuffer... :) But now I've learned that as well! :D

thanks!

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!