PDA

View Full Version : Projective texturing: mapping screenspace pos -> (u,v)



niarn
04-28-2004, 11:13 PM
I would like to automatically generate texture coordinates which maps a screenspace vertex position to a (u,v) position in a texture such that the texture is virtualy mapped to the entire viewport. I have been trying to get glTexGen do this for me but without luck. Would anybody please explain if and how this can be achieved with glTexGen?

Christian Schüler
04-29-2004, 05:41 AM
slingshot without proper gl-syntax handy:

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glTexgeni( GL_TEXTURE_GEN_S, GL_EYE_LINEAR );
glTexgeni( GL_TEXTURE_GEN_T, GL_EYE_LINEAR );
glTexgeni( GL_TEXTURE_GEN_Q, GL_EYE_LINEAR );

const double splane[] = { 1, 0, 0, 0 );
const double tplane[] = { 0, 1, 0, 0 );
const double qplane[] = { 0, 0, 1, 0 );

glTexgenv( GL_TEXTURE_GEN_S, GL_EYE_PLANE, splane );
glTexgenv( GL_TEXTURE_GEN_T, GL_EYE_PLANE, tplane );
glTexgenv( GL_TEXTURE_GEN_Q, GL_EYE_PLANE, qplane );

basically this tells GL that S = xe/ze and T = ye/ze. you would need to multiply this with your viewport aspect (replace the "1" in splane/tplane with a constant factor).

cheers

-NiCo-
04-29-2004, 06:42 AM
In case you're using fragment programs, you might want to avoid the texture coordinate generation and use the screenspace fragment position ( f[WPOS] in GL_NV_FRAGMENT_PROGRAM ) as the texture coordinate

example code
------------
TEX R0,f[WPOS],TEX0,RECT;

This code works for textures generated with GL_TEXTURE_RECTANGLE_NV as target, where the texture size equals the viewport size.
The code can be extended to POT textures generated with GL_TEXTURE_2D

example code
------------
glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_N V,0,1./viewportwidth,1./viewportheight,0.0,0.0);

MUL R0,f[WPOS],p[0];
TEX R0,R0,TEX0,2D;

Note: This code is written for Nvidia graphics cards, but equivalent extensions are available for Ati graphics cards.