Vertex shader question - how to calculate color based on the texture

Started by
1 comment, last by dolphin2011 12 years ago
The below is some vertex shader file written by me based on some books.
The question - is the alternative way to get input color instead of keeping global AmbientMtrl and DiffuseMtrl? The idea from the book is to set AmbientMtrl and DiffuseMtrl and then after calculating light set output color according to the below formula:
output.diffuse = (AmbientMtrl * AmbientLightIntensity) + (s * (DiffuseLightIntensity * DiffuseMtrl));

But maybe there is alternative method, based on TEXCOORD or some other approach???


uniform extern float4x4 MatWorld; //or uniform extern x, y, z;
uniform extern float4x4 MatWorldInvTrans;
uniform extern float4x4 MatView;
uniform extern float4x4 MatViewProj;

uniform extern float4x4 F[35]; //Final transformation matrix for each bone (bind space -> root space)

uniform extern vector LightDirection; //Light direction

//Material
uniform extern vector AmbientMtrl;
uniform extern vector DiffuseMtrl;
vector DiffuseLightIntensity = {1.0f, 1.0f, 1.0f, 1.0f};
vector AmbientLightIntensity = {0.3f, 0.3f, 0.3f, 1.0f};

struct VS_INPUT
{
float3 position : POSITION; //bind space
float3 normal : NORMAL;
float2 coord : TEXCOORD;
float3 weights : BLENDWEIGHT;
int4 boneIndices : BLENDINDICES;
};

struct VS_OUTPUT
{
vector position : POSITION;
vector diffuse : COLOR;
//float3 normal : NORMAL;
//float2 tex : TEXCOORD;
};

VS_OUTPUT Main(VS_INPUT input)
{
VS_OUTPUT output = (VS_OUTPUT)0;

//output position
//Transform root space -> root space acc. to weights
float weights3 = 1.0f - input.weights[0] - input.weights[1] - input.weights[2];
float4 pos = input.weights[0] * mul(float4(input.position, 1.0f), F[input.boneIndices[0]]);
pos += input.weights[1] * mul(float4(input.position, 1.0f), F[input.boneIndices[1]]);
pos += input.weights[2] * mul(float4(input.position, 1.0f), F[input.boneIndices[2]]);
pos += weights3 * mul(float4(input.position, 1.0f), F[input.boneIndices[3]]);
pos.w = 1.0f;

// Transform to World space
pos = mul(pos, MatWorld); //or pos.x += x; pos.y += y; pos.z += z;

// Transform to homogeneous clip space
output.position = mul(pos, MatViewProj);



//Output diffuse
//Transform root space -> root space acc. to weights
float4 normal = input.weights[0] * mul(float4(input.normal, 0.0f), F[input.boneIndices[0]]);
normal += input.weights[1] * mul(float4(input.normal, 0.0f), F[input.boneIndices[1]]);
normal += input.weights[2] * mul(float4(input.normal, 0.0f), F[input.boneIndices[2]]);
normal += weights3 * mul(float4(input.normal, 0.0f), F[input.boneIndices[3]]);
normal.w = 0.0f;

normal = normalize(normal);

// Transform to World space
normal = mul(normal, MatWorldInvTrans);

// Light calculation
LightDirection.w = 0.0f;
LightDirection = mul(LightDirection, MatView);
normal = mul(normal, MatView);

float s = dot(LightDirection, normal);

if(s < 0.0f) s = 0.0f;

output.diffuse = (AmbientMtrl * AmbientLightIntensity) +
(s * (DiffuseLightIntensity * DiffuseMtrl));


return output;
}
Advertisement
Hi,

You may pass a color value as part of the vertex input structure if you want, as you may pass any other value.


[color=#000088]struct[color=#000000] VS_INPUT
[color=#666600]{
[color=#000000]float3 position [color=#666600]:[color=#000000] POSITION[color=#666600];[color=#000000] [color=#880000]//bind space
[color=#000000]float3 normal [color=#666600]:[color=#000000] NORMAL[color=#666600];
[color=#000000]float2 coord [color=#666600]:[color=#000000] TEXCOORD[color=#666600];
[color=#000000]float3 weights [color=#666600]:[color=#000000] BLENDWEIGHT[color=#666600];
[color=#000000]int4 boneIndices [color=#666600]:[color=#000000] BLENDINDICES[color=#666600];
float4 color : COLOR;
[color=#666600]};

Of course, this requires modifications on the program side code and vertex structure too.

Best regards!
you didn't use a pixel shader,and you can pass an texture shader resource to the pixel shader, then samlpe it with "tex.Sample(texture coordinate)",the texel color should be used as the ambient and diffuse color of the material.

This topic is closed to new replies.

Advertisement