getting the colour at a given location in a texture

Started by
2 comments, last by xargon123 14 years, 5 months ago
Hello, This is more of a CG question but I did not know which forum it should belong to. So, apologies for posting on the wrong forum. Please feel free to remove the thread. I have a simple particle system implemented on the GPU (one that I borrowed from the CG book) and I am trying to make some simple changes to it. Now, my pixel shader takes a texture as an input and what I am trying to do is figure out the colour of the texture at the current location. My plan was to change the brightness of the particles based on the underlying colour. So, my vertex shader looks as follows:

struct fragment
{
       float4  position    : POSITION;
       float4  colour      : COLOR0;
       float2  texcoord0   : TEXCOORD0;
       float   pointSize   : PSIZE;
};


fragment main(float3 pInitial   : POSITION,
             float3 vInitial   : TEXCOORD0,
             float tInitial    : TEXCOORD1,
             uniform float     globalTime,
             uniform float     particleSize,
             uniform float4x4  modelViewProj)
{
   fragment OUT;
   float t = globalTime - tInitial;
   float4 final = float4(pInitial, 1) + (float4(vInitial, 0) + float4(0, (-4.9f), 0, 0) * t * t;
   OUT.position = mul(modelViewProj, final);
   OUT.texcoord0 = OUT.position; // store the position for the pixel shader
   OUT.pointSize = particleSize;
   return OUT;
}

So, I store the position (x, y) value (in the range -1, 1) in the texcoord0 parameter and my plan is to sample the colour at this position and use it as the particle colour. My pixel shader looks as follows:

struct fragment
{
       float4 position  : POSITION;
       float4 colour0    : COLOR0;
       float2 texcoord0 : TEXCOORD0;
};

struct pixel
{
       float4 colour : COLOR;
};



pixel main( fragment IN,
                       uniform sampler2D inputTexture,
                       uniform float brightness,
                       uniform float contrast,
                       uniform float gamma)
{
   pixel OUT;
   OUT.colour = tex2D(inputTexture, IN.texcoord0);
   return OUT;
}
What I was expecting was the particle to have the sampled colour but what I actually see is that the texture gets mapped to the particle. I would actually like it to take a simple colour value rather than doing texture mapping. I have played around with the code in many ways but am unable to get the desired functionality. I would really appreciate any help as this is driving me up the wall! Thanks, xarg
Advertisement
I can't remember the exact details offhand, but I think the use of pointsprites sets the UV to be 0-1 across the sprite itself, which is probably why you're seeing it textured.

You'll need to to do something to turn that off, or perhaps try a different texcoord channel or something. Check the docs again - they're in their somewhere as I have a similar problem once. I don't use them anymore else I'd look in the code :)
------------------------------Great Little War Game
The texture coords will be interpolated, so if each vertex of the triangle outputs a different value to texcoord0 (which is seems to be doing, as it's based on position), then you can expect a range of values to be read from the texture. This in normal texture mapping. If you want a single colour for the entire triangle, make sure its vertices output it.
Thanks for the reply.

The particle system uses point sprites which, I thought, should not be doing this. So, for each particle (which is rendered as a point sprite), we have a unique 2D position and I would like to sample the color at that position.

Thanks,

/x

This topic is closed to new replies.

Advertisement