Jump to content
  • Advertisement
Sign in to follow this  
mishal153

Simplest vertex lighting shader...see anything wrong ?

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

I have a triangle with vertices having positions as : (-1,0,1) , (0,2,1) , (1,0,1) The vertex normal for all 3 vertices is (0,0,-1). I assigned diffuse color to the 3 vertices, used a texture and wrote vertex and pixel shaders which work just fine. Vertex shader :
[SOURCE] 
// Vertex shader input structure
struct VS_INPUT
{
    float4 Position   : POSITION;
    float2 Texture    : TEXCOORD0;
    float4 DiffuseColor : COLOR0;
};


// Vertex shader output structure
struct VS_OUTPUT
{
    float4 Position   : POSITION;
    float2 Texture    : TEXCOORD0;
    float4 DiffuseColor : COLOR0;
};


// Global variables
float4x4 WorldViewProj;


// Name: Simple Vertex Shader
// Type: Vertex shader
// Desc: Vertex transformation and texture coord pass-through
//
VS_OUTPUT vs_main( in VS_INPUT In )
{
    VS_OUTPUT Out;                      //create an output vertex

    Out.Position = mul(In.Position,
                       WorldViewProj);  //apply vertex transformation
    Out.Texture  = In.Texture;          //copy original texcoords
    Out.DiffuseColor = In.DiffuseColor; 

    return Out;                         //return output vertex
}
[/SOURCE]
[/source] Pixel shader
[SOURCE]
// Pixel shader input structure
struct PS_INPUT
{
    float4 Position   : POSITION;
    float2 Texture    : TEXCOORD0;
    float4 DiffuseColor : COLOR0;
};


// Pixel shader output structure
struct PS_OUTPUT
{
    float4 Color   : COLOR0;
};


// Global variables
sampler2D Tex0;
float     blendRatio;

// Name: Simple Pixel Shader
// Type: Pixel shader
// Desc: Fetch texture and blend with constant color
//
PS_OUTPUT ps_main( in PS_INPUT In )
{
    PS_OUTPUT Out;                             //create an output pixel

    Out.Color = tex2D(Tex0, In.Texture);       //do a texture lookup
    Out.Color *= In.DiffuseColor*blendRatio;   //do a simple effect

    return Out;                                //return output pixel
}
[/SOURCE]
[/source] Now, instead of using the diffuse color i want to try simulating a light source. So I modified the vertex shader :
[SOURCE]
// Vertex shader input structure
struct VS_INPUT
{
    float4 Position   : POSITION;
    float2 Texture    : TEXCOORD0;
    float4 DiffuseColor : COLOR0;
    float3 Normal       : NORMAL;
};


// Vertex shader output structure
struct VS_OUTPUT
{
    float4 Position   : POSITION;
    float2 Texture    : TEXCOORD0;
    float4 DiffuseColor : COLOR0;
};


// Global variables
float4x4 WorldViewProj;
float3 LightPosition;
float4 LightColor = {0.0f, 0.0f, 1.0f, 1.0f};

// Name: Simple Vertex Shader
// Type: Vertex shader
// Desc: Vertex transformation and texture coord pass-through
//
VS_OUTPUT vs_main( in VS_INPUT In )
{
    VS_OUTPUT Out;                      //create an output vertex
    float3 lightDirection = normalize (In.Position - LightPosition);
    float lightContibution = dot (lightDirection, In.Normal);
    float4 colorContibutedbyLight = 0;
    colorContibutedbyLight = LightColor * lightContibution;

    Out.Position = mul(In.Position,
                       WorldViewProj);  //apply vertex transformation
    Out.Texture  = In.Texture;          //copy original texcoords
    Out.DiffuseColor = colorContibutedbyLight; 

    return Out;                         //return output vertex
}
[/SOURCE]
[/source] The new uniform variable here is 'LightPosition' which i set as (0,0.5,-1) But i do not get black triangle. Any suggestions ? [Edited by - mishal153 on April 16, 2010 1:06:57 AM]

Share this post


Link to post
Share on other sites
Advertisement
Yes, I have provided vertex normal as part of the vertex and also put that in vertex declaration. (i do not have access to the actual code right now, will post it tomorrow).

Share this post


Link to post
Share on other sites
Here is the vertex structure and vertex declaration:

[SOURCE]
struct MyVertex
{
public Vector3 Position;
public Color4 Color;
public Vector2 TextureCoordinates1;
public Vector3 Normal;
}

MyVertex[] _vertices = new MyVertex[3];

_vertices[0].Position.X = -1.0f;
_vertices[0].Position.Y = 0.0f;
_vertices[0].Position.Z = 1.0f;
_vertices[0].Color = new Color4(Color.White);
_vertices[0].TextureCoordinates1.X = 0;
_vertices[0].TextureCoordinates1.Y = 1;
_vertices[0].Normal.X = 0;
_vertices[0].Normal.Y = 0;
_vertices[0].Normal.Z = -1;

_vertices[1].Position.X = 0.0f;
_vertices[1].Position.Y = 2.0f;
_vertices[1].Position.Z = 1.0f;
_vertices[1].Color = new Color4(Color.Green);
_vertices[1].TextureCoordinates1.X = 0.5f;
_vertices[1].TextureCoordinates1.Y = 0.0f;
_vertices[1].Normal.X = 0;
_vertices[1].Normal.Y = 0;
_vertices[1].Normal.Z = -1;

_vertices[2].Position.X = 1.0f;
_vertices[2].Position.Y = 0.0f;
_vertices[2].Position.Z = 1.0f;
_vertices[2].Color = new Color4(Color.Blue);
_vertices[2].TextureCoordinates1.X = 1;
_vertices[2].TextureCoordinates1.Y = 1;
_vertices[2].Normal.X = 0;
_vertices[2].Normal.Y = 0;
_vertices[2].Normal.Z = -1;

VertexElement [] vertexElements = new VertexElement[5];

vertexElements[0].Usage = DeclarationUsage.Position;
vertexElements[0].Type = DeclarationType.Float3;
vertexElements[0].Stream = 0;
vertexElements[0].Method = DeclarationMethod.Default;
vertexElements[0].UsageIndex = 0;
vertexElements[0].Offset = 0;

vertexElements[1].Usage = DeclarationUsage.Color;
vertexElements[1].Type = DeclarationType.Float4;
vertexElements[1].Stream = 0;
vertexElements[1].Method = DeclarationMethod.Default;
vertexElements[1].UsageIndex = 0;
vertexElements[1].Offset = (short) Vector3.SizeInBytes;

vertexElements[2].Usage = DeclarationUsage.TextureCoordinate;
vertexElements[2].Type = DeclarationType.Float2;
vertexElements[2].Stream = 0;
vertexElements[2].Method = DeclarationMethod.Default;
vertexElements[2].UsageIndex = 0;
vertexElements[2].Offset = (short)(Vector3.SizeInBytes + Marshal.SizeOf(typeof(Color4)));

vertexElements[3].Usage = DeclarationUsage.Normal;
vertexElements[3].Type = DeclarationType.Float3;
vertexElements[3].Stream = 0;
vertexElements[3].Method = DeclarationMethod.Default;
vertexElements[3].UsageIndex = 0;
vertexElements[3].Offset = (short)(Vector3.SizeInBytes + Marshal.SizeOf(typeof(Color4)) + Vector2.SizeInBytes);

vertexElements[4] = VertexElement.VertexDeclarationEnd;
[/SOURCE]


I am using SlimDX so the syntax looks different than C++.

Share this post


Link to post
Share on other sites
Well i narrowed down the problem. One of the problems was this .
And the other problem was that normal and light direction were in opposite directions thus creating a -ve dot product.

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!