PDA

View Full Version : Projective texturing



Mickey
11-18-2004, 12:49 PM
Hi!
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);
else
gl_FragColor = texture2DProj(g_projectorTexture, gl_TexCoord[0]);
}

Any ideas?

Yours sincerely,
Michael

tasseloff
11-30-2004, 04:49 PM
Hi,
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
http://www.gamedev.net/reference/articles/article2138.asp
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

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

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


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

tasseloff
12-01-2004, 10: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...

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

tasseloff
12-01-2004, 07: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