Jump to content
  • Advertisement
Sign in to follow this  

Using textures in shaders

This topic is 3612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post

Link to post
Share on other sites
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".

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!