Question

I am working on a simple 3D app that will draw a cube using SlimDX. Currently it should draw a single triangle on the screen, but instead it does nothing.

If I examine things using the graphics debugger feature in VS2012, I can see that the output from the vertex shader stage is a triangle as expected. But then we skip the pixel shader and go straight to the OutputMerger.

I've set cullmode to none but it still refuses to run the pixel shader. Here is the code that initializes the projection and view matrices.

        m_ProjectionMatrix = Matrix.PerspectiveFovLH(0.7853982f, //1.570796f, // this is 90 degrees in radians
                                                     (float) m_Form.Width / (float) m_Form.Height,
                                                     m_Viewport.MinZ,
                                                     m_Viewport.MaxZ);

        m_ViewMatrix = Matrix.LookAtLH(new Vector3(3, 1, -10),
                                       new Vector3(0, 0, 0),
                                       new Vector3(0, 1, 0));

The triangle itself is positioned at the origin as I have not applied any translation to it. Here is the vertex data, which is stored in a custom vertex stucture. I'm only using the position data for now until I can solve this problem. Then I will mess with the other values I've added. But here is the vertex data:

    Vertex[] vertexData =
    {
        new Vertex() { Position = new Vector4(-1.0f, 1.0f, 0.0f, 0.0f), Color = new Color4(1.0f, 0.0f, 0.0f, 1.0f), TexCoord = new Vector2(0, 0) },
        new Vertex() { Position = new Vector4(1.0f, 1.0f, 0.0f, 0.0f), Color = new Color4(1.0f, 0.0f, 1.0f, 0.0f), TexCoord = new Vector2(1, 0) },
        new Vertex() { Position = new Vector4(0.0f, -1.0f, 0.0f, 0.0f), Color = new Color4(1.0f, 1.0f, 0.0f, 0.0f), TexCoord = new Vector2(0.5f, 1) },
    };

And here is the shader code:

cbuffer cbPerResize : register( b0 )
{
    matrix Projection;
};

cbuffer cbPerFrame : register( b1 )
{
    matrix View;
};

cbuffer cbPerObject : register( b2 )
{
    matrix World;
};


Texture2D ObjTexture;
SamplerState ObjSamplerState;


//--------------------------------------------------------------------------------------
struct VS_INPUT
{
    float4 Pos : POSITION;
    float4 Color : COLOR;
    float2 TexCoord: TEXCOORD;
};

struct PS_INPUT
{
    float4 Pos : SV_POSITION;
    float4 Color : COLOR;
    float2 TexCoord: TEXCOORD;
};






PS_INPUT Vertex_Shader( VS_INPUT input )
{
    PS_INPUT output = (PS_INPUT) 0;

    output.Color = input.Color;
    output.TexCoord = input.TexCoord;

    output.Pos = mul( input.Pos, World );
    output.Pos = mul( output.Pos, View );
    output.Pos = mul( output.Pos, Projection );

    return output;
}

float4 Pixel_Shader_Color(PS_INPUT input) : SV_TARGET
{
    return float4(0.0f, 0.0f, 1.0f, 1.0f);   
}

Here is the drawing code in my RenderScene() method. The world matrix for the cube is just the identity matrix currently:

    // Clear the screen before we draw the next frame.
    m_DeviceContext.ClearRenderTargetView(m_RenderTargetView, m_ClearColor);


    // Send the cube's world matrix to the changes per object constant buffer.
    m_DataStream.Position = 0;
    m_DataStream.Write(Matrix.Transpose(m_CubeWorldMatrix));
    m_DataStream.Position = 0;
    m_Device.ImmediateContext.UpdateSubresource(new DataBox(0, 0, m_DataStream),
                                                m_CbChangesPerObject,
                                                0);


    // Draw the triangle that we created in our vertex buffer.
    m_DeviceContext.Draw(3, 0);


    // Present the frame we just rendered to the user.
    m_SwapChain.Present(0,
                        PresentFlags.None);

Here is output from the InputAssembler stage. The vertex value seem to be screwy.

# Visual Studio - VsPix
# Version: 11
# Vertex shader mesh
#
# Generated data
# object insideShape
g insideShape
v -Infinity Infinity NaN
v Infinity Infinity NaN
v NaN -Infinity NaN
f 1 2 3

This is one of those very irritating problems that you just can't seem to solve. And in my experience these types of bugs usually end up being caused by something very small... lol

I have checked the constant buffers and it looks like they are getting set correctly. I am stumped here... What is going on? Any help would be much appreciated!

Was it helpful?

Solution

From what it seems you are using m_Viewport.MinZ and m_Viewport.MaxZ as your near/far plane for your projection matrix.

In Viewport they are generally 0->1, but values in projection matrix is different, so if you set MaxZ = 1 in your projection, you triangle will be clipped (as your lookat z is -10).

Try to set your projection to near = 0.5 and far = 200 (something like that).

EDIT: Also I noticed that your vertex position is -1.0f, 1.0f, 0.0f, 0.0f

Last component should be 1.0f

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top