How to compute the texture coordinates in a vertex shader ?


I have a problem (I think) with the texture coordinate generation a la glTexGen.

I want to achieve the same functionality using vertex shaders (because I think turning on vertex shaders disables the functionality of glTexGen)

Here is my program :

struct OUT_Struct
float4 position : POSITION;
float4 color : COLOR;
float4 texCoord:TEXCOORD0;


OUT_Struct main(float4 position : POSITION,
float4 color:COLOR,

      uniform float4x4 modelViewProj,
  uniform float4x4 textureMatrix,//here I have the rows used in glTexGen
  uniform float4x4 modelViewI,//the inverse of modelview
  uniform float4  eye//the eye coordinates


OUT_Struct OUT;

// Transform position from object space to clip space

OUT.position = mul(modelViewProj, position);

OUT.color = color;

float4x4 tmp = textureMatrix * modelViewI;

OUT.texCoord = mul(tmp, eye);

return OUT;


Is that ok ?

I’ve been doing some researching I looked over the gl documentation (microsoft’s was kind of blurry) and I came out with

OUT.position = mul(modelViewProj, position);

OUT.texCoord.x = mul(mul(glstate.texgen[0].eye.s,glstate.matrix.inverse.modelview[0]),OUT.position);

That seems to be what is documented.
Is that OK ?

Please, I am struggling with this problem…and I am a novice with shaders

if the vertex positions that you’re passing into your vertex shader are in object space, and your texture matrix is composed as follows:
texMatrix = biasMatrix * projMatrix * viewMatrix * modelMatrix;
then the texture coordinates will be as follows:
OUT.texCoord = mul(textureMatrix, position);

you were correct that:
OUT.position = mul(modelViewProj, position);

[This message has been edited by lost hope (edited 12-04-2003).]

Part of the problem may also be that TexGen is done per fragment. Your vertex program is per vertex (duh) and interpolated.

I’ve solved my problem. The code is the following:

float4 eye_space_pos = mul(glstate.matrix.modelview[0], position );
OUT.texCoord.x = dot(glstate.texgen[0].eye.s,eye_space_pos);
OUT.texCoord.y = dot(glstate.texgen[0].eye.t,eye_space_pos);
OUT.texCoord.z = dot(glstate.texgen[0].eye.r,eye_space_pos);
OUT.texCoord.w = dot(glstate.texgen[0].eye.q,eye_space_pos);

Texture generation with glTexGen is done per vertex. The texture coordinates for each fragment are interpolated between the texture coordinates for the vertices that make up the triangle in which the fragment is…afaik