Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am creating an editor of sorts that allow you to create 3D voxel models. I just got started and have ran into a few errors. Here is what it is producing:

enter image description here

I think the problem has to do with the way I am handling my vertex format (VertexPositionNormalColor) shader or the way either of them are written:

    struct VertexPositionNormalColor {
        Vector3 position;
        Vector3 normal;

        Color color;

        public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration(
            new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
            new VertexElement(sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementUsage.Normal, 1),
            new VertexElement(sizeof(float) * 6, VertexElementFormat.Vector4, VertexElementUsage.Color, 0)
        );

        public VertexPositionNormalColor(Vector3 position, Vector3 normal, Color color) {
            this.position = position;
            this.normal = normal;
            this.color = color;
        }
    }

Shader code:

struct ColoredVertexToPixel  {
    float4 Position     : POSITION;
    float4 Color        : COLOR0;
};

struct ColoredPixelToFrame  {
    float4 Color        : COLOR0;
};

ColoredVertexToPixel ColoredVertexShader(float4 inPosition : POSITION, float3 inNormal : NORMAL, float4 inColor : COLOR0)  {
    ColoredVertexToPixel Output = (ColoredVertexToPixel)0;

    float4 worldPos = mul(inPosition, World);
    float4 viewPos = mul(worldPos, View);

    Output.Position = mul(viewPos, Projection);
    Output.Color = inColor;

    // TODO: Implement basic lighting

    return Output;
}

ColoredPixelToFrame ColoredPixelShader(ColoredVertexToPixel PSIn)  {
    ColoredPixelToFrame Output = (ColoredPixelToFrame)0;

    Output.Color = PSIn.Color;

    return Output;
}


technique ColoredBlock  {
    pass Pass0  {
        VertexShader = compile vs_2_0 ColoredVertexShader();
        PixelShader = compile ps_2_0 ColoredPixelShader();
    }
}

I also tryed the object creation code in another project with a different effect file and vertex format and it worked fine. This is defiantly a shader/vertex format error as I set each vertex with the exact same color, so maybe the color and position data in the shader is getting with each other?

Any ideas?

Thanks

share|improve this question

1 Answer 1

up vote 3 down vote accepted

First, and since you are using only one normal channel, I would change the UsageIndex of your VertexElementUsage.Normal from 1 to 0.

And for your color channel, your vertex buffer is expecting a VertexElementFormat.Vector4, that is, 4 float components. The type used for colors in XNA is a special 32-bit encoded value, so change it to VertexElementFormat.Color.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.