Search code examples
c++openglshadertextures

Render to a texture, then render result to screen in OpenGL


I need to render an OpenGL scene to a texture in order to then manipulate that texture in a shader. I've solved this by using Framebuffer Objects, which I think I understand fairly well by now. At many points in my effect pipeline, I need to render a fullscreen quad and texture it with the dynamically rendered texture, which is where my problem is.

This is what my scene looks like: https://www.mathematik.uni-marburg.de/~thomak/planet.jpg

I render this to a texture and map that texture to a fullscreen quad. However, the resulting image is distorted in this way: https://www.mathematik.uni-marburg.de/~thomak/planettexture.jpg

Here is the code that renders the quad and sets the texture coordinates:

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glBegin(GL_QUADS);

glTexCoord2i(0, 0);
glVertex3i(-1, -1, -1);    
glTexCoord2i(0, 1);
glVertex3i( 1, -1, -1); 
glTexCoord2i(1, 1);   
glVertex3i( 1,  1, -1); 
glTexCoord2i(1, 0);   
glVertex3i(-1,  1, -1);

glEnd();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

And the shader code is here:

sampler2D BlitSamp = sampler_state
{
  MinFilter = LINEAR;
  MagFilter = LINEAR;
  MipFilter = LINEAR;
  AddressU = Clamp;
  AddressV = Clamp;
};

float4 AlphaClearPS(float2 texcoords : TEXCOORD0) : COLOR
{
  return float4(tex2D(BlitSamp, texcoords).rgb, 1.0f);
}

Where BlitSamp is the texture I rendered to and then passed to the shader. What could be going on here?


Solution

  • It's possible that your tex-coords are off. Your code, my comments:

    glTexCoord2i(0, 0);     //Bottom-Left
    glVertex3i(-1, -1, -1); //Bottom-Left    
    
    glTexCoord2i(0, 1);     //Top-Left
    glVertex3i( 1, -1, -1); //Bottom-Right??? 
    
    glTexCoord2i(1, 1);     //Top-Right
    glVertex3i( 1,  1, -1); //Top-Right
    
    glTexCoord2i(1, 0);     //Bottom-Right
    glVertex3i(-1,  1, -1); //Bottom-Left??