Screen coordinates in the programs

I’m trying to do some basic screen space hatching (same mapping as OGL automatic texcoo generation using GL_EYE_THING).

My implementation doesn’t work well …

The vertex program :

# ---------------- SCREEN COORDINATES ---------------- #

OUTPUT  outScreenCoo         = result.texcoord[6] ;
PARAM   mvp[4]               = { state.matrix.mvp } ;
PARAM   Constants            = {0.5, 1.0, 5.0, 2.0} ;
TEMP    ScrCoo ;

DP4     ScrCoo.x, mvp[0], inPosition ;   
DP4     ScrCoo.y, mvp[1], inPosition ;
MAD     outScreenCoo, ScrCoo, Constants.x, Constants.x ;

and for the fragment program :

# -------------------- HATCH TEXTURE ------------------- #

ATTRIB  inScreenCoo         = fragment.texcoord[6] ;
TEMP    R1 ;

TEX     R1, inScreenCoo, texture[4], 2D ;

I get some output, but the mapping coordinates are definetely not correct … where did I go wrong ?

Thanks,
SeskaPeel.

Actually, I fixed some stuff (normalized the output), and I got some nearly correct values.

But the texture is still warped in the lower right and upper left corner … any idea ?

VP :

ATTRIB  inPosition           = vertex.position ;
PARAM   mvp[4]               = { state.matrix.mvp } ;
OUTPUT  outScreenCoo         = result.texcoord[4] ;
PARAM   Constants            = {0.5, 1.0, 5.0, 2.0} ;

DP4     outScreenCoo.x, mvp[0], inPosition ;   
DP4     outScreenCoo.y, mvp[1], inPosition ;
DP4     outScreenCoo.z, mvp[2], inPosition ;
DP4     outScreenCoo.w, mvp[3], inPosition ;

FP :

ATTRIB  inScreenCoo         = fragment.texcoord[4] ;
PARAM   Constants           = {0.5, 1.0, 5.0, 2.0} ;
TEMP    R1 ;

DP3     R1.w, inScreenCoo, inScreenCoo ;
RSQ     R1.w, R1.w ;
MUL     R1, inScreenCoo, R1.w ;
MAD     R1, R1, Constants.x, Constants.x ;
TEX     R1, R1, texture[4], 2D ;

Thanks,
SeskaPeel.

Try TXP in the fragment program. And don’t normalize.

It works fine, thanks.
I first tried to divide by w, but I substituted the RCP with a RSQ …

I tried to divide in the vertex program but I had some nasty warping effects due to interpolation.

here is the method I choosed

VP :

OUTPUT  outScreenCoo         = result.texcoord[4] ;
PARAM   Constants            = {0.5, 1.0, 0.0, 4.0} ;
TEMP    ScrCoo ;
DP4     ScrCoo.x, mvp[0], inPosition ;   
DP4     ScrCoo.y, mvp[1], inPosition ;
DP4     ScrCoo.w, mvp[3], inPosition ;
MAD     outScreenCoo, ScrCoo, Constants.yyyw, ScrCoo.wwwz ;

FP :

ATTRIB  inScreenCoo         = fragment.texcoord[4] ;
TEMP    R1 ;
TXP     R1, inScreenCoo, texture[4], 2D ;

Thanks,
SeskaPeel.

That’s expected. The perspective divide is needed per fragment. Dividing per vertex and interpolating the result is quite different.

A lerp(A,B,0.5) B
s:=0 s:=1 s:=2
w:=1 w:=3 w:=5


s/w=0 0.2 s/w=0.4

0.2!=1/3!

I first tried to divide by w, but I substituted the RCP with a RSQ …
My bad, I didn’t even see that. The real “mistake” was that you did the MAD after the normalization step.

Actually, normalizing the texcoord in the fragment program and then doing TEX is equivalent to TXP. I’d still expect TXP to generally perform better.

edited again because the first edit ate the first part of my post …

[This message has been edited by zeckensack (edited 02-24-2004).]

[This message has been edited by zeckensack (edited 02-24-2004).]

Explanations of my programs :

V(x,y,z,w) is the position after being mvp’ed
P(s,t) is the screen coordinate I’m trying to get, in the range [0, 1]

s = (x / w) * 0.5 + 0.5
s = (x + w) / (2 * w)

the vertex prog outputs to the frag prog
(x+q, y+q, 0, 2w), and the frag prog performs the divide in the TXP.

Values are big and close enough to give a nice approximation of the lerp(A/w1, B/w2, l) == lerp(A, B, l) / lerp (w1, w2, l)

It interpolates near to perfect and let the fragment program cool down.