Using textures in shaders

Started by
2 comments, last by V-man 15 years, 4 months ago
Lets assume I would like to render an Object A with a shader using a texture T (to tone the color). I supply T and the (u,v) coordinates to the vertex program (I know this might not make any sense). An example would be something like this:

struct vertex_input
{
  float4 position  : POSITION;
  float2 uv        : TEXCOORD0;

};


struct vertex_output
{
  float4 position : POSITION;
  float4 color    : COLOR;
};


vertex_output main(  
        vertex_input       IN 
      , uniform float4x4   ModelViewProj  
      , uniform sampler2D  texture
)
{
	
	vertex_output OUT;
        OUT.position = mul(ModelViewProj, IN.position);
        float tone = tex2D(texture, IN.uv);    

	OUT.color =(0.5, 0.5, 0.5)*tone;
	OUT.color.a = 1;
        return OUT;
}    


But when the object is rendered are the same index (u,v) not used for all the vertices? Even though uv is supplied for each vertex I don't understand which value it has for each vertex.
Advertisement
Each vertex has its own texcoord
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
But whats inside that texcoord? When it enters the vertex program I have from the view from the vertex program no control of the content of the texcoord.

When I do a lookup in a texture using that texcoord I have no idea where in the texture I am "looking".
I am not exactly sure what you are trying to say.

Normally, you would supply texture coordinates. Either through the glBegin/glTexCoord/glVertex/glEnd method which can be considered dead end now or via the glTexCoordPointer.

In.uv is suppose to be texcoord. If you are unsure and you want to do some debugging, make a floating point render target and change your shader slightly.

OUT.color = float4(IN.uv, 0.0, 0.0);

and in your fragment shader, output that color.
PS, I'm not a Cg user so I don't know if the code will compile or not.

What gets rendered are the texcoords of all the pixels.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement