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'm trying to learn DirectX and SlimDX but I'm having a terrible time getting started.

I'm taking the Triangle tutorial and trying to add color and transformations.

Starting with controlling color, I've updated the .fx file to the following:

cbuffer cbPerObject
{
    float4x4 gWVP;
};

struct vsInput 
{
    float4 positionLS   : POSITION;
    float4 color        : COLOR;
};

struct vsOutput
{
    float4 positionCS   : POSITION;
    float4 color        : COLOR;
};

struct psInput 
{
    float4 positionCS   : POSITION;
    float4 color        : COLOR;
};

vsOutput VShader(vsInput input)
{
    vsOutput output;

    output.positionCS = input.positionLS; //mul(position, gWVP);
    output.color = input.color;

    return output;
}

float4 PShader(psInput input) : SV_TARGET
{
    return input.color;
}

And the code updates limited to the following lines:

// create test vertex data, making sure to rewind the stream afterward
var vertices = new DataStream(16 * 6, true, true);
vertices.Write(new Vector4(0.0f, 0.5f, 0.5f, 1f));  // What is W (of POSITION)
vertices.Write(new Color4(.8f, 1f, 0f, 0f));        // What should it's value be?
vertices.Write(new Vector4(0.5f, -0.5f, 0.5f, 1f));
vertices.Write(new Color4(.8f, 0f, 1f, 0f));
vertices.Write(new Vector4(-0.5f, -0.5f, 0.5f, 1f));
vertices.Write(new Color4(.8f, 0f, 0f, 1f));
vertices.Position = 0;

// create the vertex layout and buffer
// Thanks to Mārtiņš Možeiko for fixing these next lines
var elements = new[] { 
    new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
    new InputElement("COLOR", 0, Format.R32G32B32A32_Float, InputElement.AppendAligned, 0) 
};

var layout = new InputLayout(device, inputSignature, elements);
var vertexBuffer = new SlimDX.Direct3D11.Buffer(device, 
    vertices, 
    (int)vertices.Length, 
    ResourceUsage.Default, 
    BindFlags.VertexBuffer, 
    CpuAccessFlags.None, 
    ResourceOptionFlags.None, 
    0);

After a few other attempts at using SV_POSITION, COLOR, and SV_TARGET in the wrong spots, it finally executes without error; however, the triangle does not appear.

  1. Please help me get the triangle working again with this updated shader.
  2. Sample code for SetConstantBuffer() for populating gWVP for standard perspective.

Thanks!

share|improve this question
add comment

3 Answers 3

elements array sould look like this:

// create the vertex layout and buffer
var elements = new[] { 
    new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
    new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) 
};

or

// create the vertex layout and buffer
var elements = new[] { 
    new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
    new InputElement("COLOR", 0, Format.R32G32B32A32_Float, InputElement.AppendAligned, 0) 
};

The last element is not an index of element. It is offset in bytes from beginning of vertex.

share|improve this answer
    
Oh, sorry, I edited my post to correct code. See the MiniTri11 example code in SlimDX for full example: code.google.com/p/slimdx/source/browse/trunk/samples/Direct3D11/… –  Mārtiņš Možeiko Feb 8 '12 at 22:40
    
Ok, I've incorporated that fix, but the triangle still doesn't appear. Any other ideas? Thanks. –  uosɐſ Feb 8 '12 at 23:19
    
Then the problem is somewhere else and not in InputLayout creation. –  Mārtiņš Možeiko Feb 8 '12 at 23:44
    
Yeah, so I'm still back to the original situation. Given that those are the only changes I'm making to the sample, what could be causing the triangle to not be appearing with this augmented shader? –  uosɐſ Feb 9 '12 at 18:01
add comment

What helped me was changing the InputElement format of the Position.

From

Format.R32G32B32A32_Float

to

Format.R32G32B32_Float
share|improve this answer
add comment

I had same problem and it was caused by a bad stride between vertex elements.

Check your vertexBufferBinding:

device.ImmediateContext.InputAssembler.SetVertexBuffers(0, new 
    VertexBufferBinding(vertexBuffer, 32, 0));
share|improve this answer
add comment

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.