Jump to content
  • Advertisement
Sign in to follow this  
deltit

using the meshs vertex color in cg programs

This topic is 4412 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

Im trying to write a shader that displays the meshs vertex color but it doesnt work. This is the cg code:
//------------------------------------
struct vertexInput {
    float3 position				: POSITION;
    float3 normal				: NORMAL;
    float4 texCoord				: TEXCOORD0;
    float4 vColor				: COLOR0;
};

struct vertexOutput {
    float4 position			: POSITION;
    float4 texCoord			: TEXCOORD0;
    float4 vColor			: COLOR;
};


//------------------------------------
vertexOutput VS_TransformAndTexture(vertexInput IN) 
{
    vertexOutput OUT;
    OUT.position = mul( float4(IN.position.xyz , 1.0) , worldViewProj);
    OUT.texCoord = IN.texCoord;

    OUT.vColor = IN.vColor +0.4;
    OUT.vColor = float4(IN.normal, 1);
    
    return OUT;
}


//-----------------------------------
float4 PS_Textured( vertexOutput IN): COLOR
{
  return IN.vColor;
}

//-----------------------------------
technique textured
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_TransformAndTexture();
		PixelShader  = compile ps_2_0 PS_Textured();
    }
}
But the mesh shows only the grey value due to the +0.4 to vertex colors. Is "float4 vColor : COLOR0;" the wrong parameter to get the vertex colors of the mesh? This is what the mesh should look like: vc01ul3.jpg And this is what it looks like in fx composer with the shader. The mesh was exported as .x and inside the .x file theres a section for vertex colors containing 2061 entries with different values for rg and b. vc02sn7.jpg

Share this post


Link to post
Share on other sites
Advertisement
Quote:

OUT.vColor = IN.vColor +0.4;
OUT.vColor = float4(IN.normal, 1);

why are you assigning the normal vector to vColor if it's the color of the vertices that you want?

Share this post


Link to post
Share on other sites
Oh, im sorry. That line was just to test if passing the colour to the fragment shader works. With that line i get colours derived by the meshes normals, without it i get the result i described.

Share this post


Link to post
Share on other sites
what's the code for drawing the model? Did you make sure to set a color first or is the color for each vertex placed in a vertex array?

Share this post


Link to post
Share on other sites
I didnt write any code to draw the model, im importing the model as .x into nvidias fx composer. Then i apply the material to the mesh. The material works fine except that it doesnt get the meshs vertex colour. I made the mesh in 3dsmax and applied vertex colours and when i export it as .x, theres a section for vertex colour:
MeshVertexColors {
2601;
0;0.097814;0.097814;0.097814;1.000000;;,
1;0.000000;0.000000;0.000000;1.000000;;,
2;0.078684;0.078684;0.078684;1.000000;;,
..
..
2600;0.009636;0.009636;0.009636;1.000000;;;
}

Since i couldnt get it to work in fx composer, i tried it directly in 3dsmax now and it worked. This new shader blends textures according to vertex colours now. I dont know, why it didnt work in fx composer. If anyone has an idea, please tell me. Writing and testing shaders in fx composer is much more comfortable.


void vp_vcolor(
float3 iPosition : POSITION,
float3 iNormal : NORMAL,
float4 iColor : COLOR,
float2 iTexCoord01 : TEXCOORD0,
float2 iTexCoord02 : TEXCOORD1,
float2 iTexCoord03 : TEXCOORD2,
//float2 iTexCoord04 : TEXCOORD3,

out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 oTexCoord01 : TEXCOORD0,
out float2 oTexCoord02 : TEXCOORD1,
out float2 oTexCoord03 : TEXCOORD2,
//out float2 oTexCoord04 : TEXCOORD3,

uniform float4x4 worldViewProj
)
{
oPosition = mul(float4(iPosition.xyz, 1.0), worldViewProj);
oColor = iColor;

//TeCoords
oTexCoord01 = iTexCoord01;
oTexCoord02 = iTexCoord02;
oTexCoord03 = iTexCoord03;
//oTexCoord04 = iTexCoord04;
}

//-----------------------------------
void fp_vcolor(
float4 iPosition : POSITION,
float4 iColor : COLOR,
float2 iTexCoord01 : TEXCOORD0,
float2 iTexCoord02 : TEXCOORD1,
float2 iTexCoord03 : TEXCOORD2,
//float2 iTexCoord04 : TEXCOORD3,

out float4 oColor : COLOR,

uniform sampler2D texture01,
uniform sampler2D texture02,
uniform sampler2D texture03
//uniform sampler2D texture04
)
{
//oColor = tex2D(texture01, iTexCoord01)*(1-iColor.r)*(1-iColor.g)*(1-iColor.b);
oColor = tex2D(texture01, iTexCoord01)*iColor.r;
oColor = oColor + tex2D(texture02, iTexCoord02)*iColor.g;
oColor = oColor + tex2D(texture03, iTexCoord03)*iColor.b;

}

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!