View Full Version : Projective texturing

11-18-2004, 11:49 AM
I'm having some problems implementing projective texturing using GLSL.

------------ Vertex Shader ------------
uniform mat4 g_projectorMatrix;

void main(void)
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = g_projectorMatrix * gl_Vertex;

------------ Fragment Shader ------------
uniform sampler2D g_projectorTexture;

void main(void)
gl_FragColor = texture2DProj(g_projectorTexture, gl_TexCoord[0]);


The scene consists of the following quad:

glVertex3f(-10.0f, -10.0f, -30.0f);
glVertex3f(10.0f, -10.0f, -30.0f);
glVertex3f(10.0f, 10.0f, -30.0f);
glVertex3f(-10.0f, 10.0f, -30.0f);

g_projectorMatrix is setup using the following code:

float projectorFOV = 1.57f;
float projectorAspectRatio = 1.0f;
float projectorNear = 1.0f;
float projectorFar = 1000.0f;

float f = 1.0f / tan(projectorFOV * 0.5f);

Matrix4x4f projection(
f / projectorAspectRatio, 0.0f, 0.0f, 0.0f,
0.0f, f, 0.0f, 0.0f,
0.0f, 0.0f, (projectorFar + projectorNear) / (projectorNear - projectorFar), (-2.0f * projectorFar * projectorNear) / ( projectorNear - projectorFar), 0.0f, 0.0f, -1.0f, 0.0f);

Matrix4x4f scaleAndBias(
0.5f, 0.0f, 0.0f, 0.5f,
0.0f, 0.5f, 0.0f, 0.5f,
0.0f, 0.0f, 0.5f, 0.5f,
0.0f, 0.0f, 0.0f, 1.0f);

Matrix4x4f projectorMatrix = scaleAndBias * projection;

Since the projector is positioned at 0, 0, 0 looking down -Z the projector "view" matrix is ignored.

What am I missing here?

It seems like the q-coordinate of gl_TexCoord[0] equals 0 since the following fragment shader produce a yellow quad:

uniform sampler2D g_projectorTexture;

void main(void)
if(gl_TexCoord[0].q == 0.0)
gl_FragColor = vec4(1.0, 1.0, 0.0, 0.0);
gl_FragColor = texture2DProj(g_projectorTexture, gl_TexCoord[0]);

Any ideas?

Yours sincerely,

11-30-2004, 03:49 PM
i have written the exact same vertex/fragment shaders as you and am in the same trouble, i just can't get the projective texture to work right :p i took most of my info from
so maybe it'll help you, if you haven't already checked there......if you find anything let me know i'll sure be interested
good luck

11-30-2004, 07:25 PM
dont know what your matrix struct is like but i do 0.5 on the 12,13,14 elements

12-01-2004, 06:38 AM
well, here's what i have so far:

uniform sampler2D myTexture;

void main (void)
gl_FragColor = texture2DProj(myTexture, gl_TexCoord[0]);
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];

glGetFloatv(GL_MODELVIEW_MATRIX, Model);
glLoadMatrixf(Mr); // Here i basically load the Mr matrix and multiply it by the projection matrix and modelview matrix
glGetFloatv(GL_PROJECTION_MATRIX, Mprojtex);

GLint texLoc = glGetUniformLocationARB(ShaderObject, "myTexture");
glUniform1iARB(texLoc, 0);
GLint MprojLoc = glGetUniformLocationARB(ShaderObject, "Mprojtex");
glUniform2fvARB(MprojLoc, 16, Mprojtex);

glBindTexture(GL_TEXTURE_2D, render_texture);

glBegin( GL_QUADS );
glVertex3f(0, 0, 30);
glVertex3f(0, 100, 30);
glVertex3f( 100, 100, 30);
glVertex3f( 100, 0, 30);
glUseProgramObjectARB(0);you see anything wrong that i don't see???
there seems to be a texture applied, but its like a plain color...maybe the coords are just not right or something :p

12-01-2004, 09:21 AM
ok, i've managed to get my projection working, but without the shaders....so i definitely have the Mprojtex matrix right, now anyone see anything wrong with my shaders?? or maybe the way i send my array to them, im not 100% sure its right :p
anyway the error can only be somewhere in the shaders or in the way i send it my texture and Mprojtex matrix...

12-01-2004, 05:47 PM
use glUniformMatrix4fvARB(..) to pass the matrix

12-01-2004, 06:56 PM
yeah actually i changed that afterwards but still i get the same result :(
whats weird is that the projection is alright if i do it by definig the glTexCoord3f without the shaders...however if i try it with shaders its just not happening :p