////////FRAGMENT:
uniform sampler2D myTexture;
void main (void)
{
gl_FragColor = texture2DProj(myTexture, gl_TexCoord[0]);
}
/////////VERTEX:
uniform mat4 Mprojtex;
void main(void)
{
gl_TexCoord[0] = Mprojtex * ftransform();
gl_Position = ftransform();
}
// Calculating the texture matrix which i called Mprojtex and which i send to my shaders
// doing Mprojtex = Mr*MProj*Model
// Mr being my "scale and bias", Model being Modelview, MProj being Projection matrix
float Mr[16] = { 0.5, 0, 0, 0,
0, 0.5, 0, 0,
0, 0, 0.5, 0,
0.5, 0.5, 0.5, 1 };
float Mprojtex[16], Model[16], MProj[16];
glMatrixMode(GL_MODELVIEW);
glGetFloatv(GL_MODELVIEW_MATRIX, Model);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glGetFloatv(GL_PROJECTION_MATRIX, MProj);
glLoadMatrixf(Mr); // Here i basically load the Mr matrix and multiply it by the projection matrix and modelview matrix
glMultMatrixf(MProj);
glMultMatrixf(Model);
glGetFloatv(GL_PROJECTION_MATRIX, Mprojtex);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glUseProgramObjectARB(ShaderObject);
GLint texLoc = glGetUniformLocationARB(ShaderObject, "myTexture");
glUniform1iARB(texLoc, 0);
GLint MprojLoc = glGetUniformLocationARB(ShaderObject, "Mprojtex");
glUniform2fvARB(MprojLoc, 16, Mprojtex);
glBindTexture(GL_TEXTURE_2D, render_texture);
glEnable(GL_TEXTURE_2D);
glBegin( GL_QUADS );
glVertex3f(0, 0, 30);
glVertex3f(0, 100, 30);
glVertex3f( 100, 100, 30);
glVertex3f( 100, 0, 30);
glEnd();
glDisable(GL_TEXTURE_2D);
glUseProgramObjectARB(0);