Skip to main content
GameDev.net gamedev.net
🔒 Locked

Using textures in shaders

Started by mlt Nov 27, 2008 at 6:20 PM 2 replies 800+ views
Original Post
mlt
mlt
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.
mlt
mlt
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".
V-man
V-man
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.