XNA Creators Club Online
Page 1 of 1 (12 items)
Sort Posts: Previous Next

Newbie with Quads

Last post 05-04-2008 12:52 PM by David Hunt. 11 replies.
  • 05-02-2008 10:07 AM

    Newbie with Quads

    Hi everyone.

    My app is a simple modification from the tutorials on the SDK. Where the tutorial draws a triangle, I wanted to draw a Quad.

    Then, here is the code I use:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    SimpleVertex vertices[] =

    {

    D3DXVECTOR3( -0.5f, 0.5f, 0.5f ),

    D3DXVECTOR3( 0.5f, 0.5f, 0.5f ),

    D3DXVECTOR3( 0.5f, -0.5f, 0.5f),

    D3DXVECTOR3( -0.5f, -0.5f, 0.5f),

    };

    D3D10_BUFFER_DESC bd;

    bd.Usage = D3D10_USAGE_DEFAULT;

    bd.ByteWidth = sizeof( SimpleVertex ) * 3;

    bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;

    bd.CPUAccessFlags = 0;

    bd.MiscFlags = 0;

    D3D10_SUBRESOURCE_DATA InitData;

    InitData.pSysMem = vertices;

    d3ddev->CreateBuffer( &bd, &InitData, &g_pVertexBuffer );

    Everything is ok (not many places to do it wrong). But, when debugging since I still getting a triangle, the CreateBuffer just puts 3 vertex into the g_pVertexBuffer.

    So, what I am doing wrong?

  • 05-02-2008 10:23 AM In reply to

    Re: Newbie with Quads

    The code you posted is incomplete.

     

  • 05-02-2008 11:16 AM In reply to

    Re: Newbie with Quads

    Thank you for answer.

    The problem now is just with the creation of the vertex buffer, so I only post that part of my code (copy and pasted directly from my code).

    The only part missing is the Vertex struct definition, but I am going to paste all the code.

    struct SimpleVertex

    {

    D3DXVECTOR3 Pos;

    };

    ID3D10Buffer* g_pVertexBuffer;

    **********************

    DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;

    #if defined( DEBUG ) || defined( _DEBUG )

    // Set the D3D10_SHADER_DEBUG flag to embed debug information in the shaders.

    // Setting this flag improves the shader debugging experience, but still allows

    // the shaders to be optimized and to run exactly the way they will run in

    // the release configuration of this program.

    dwShaderFlags |= D3D10_SHADER_DEBUG;

    #endif

    HRESULT hr;

    hr=D3DX10CreateEffectFromFile(L"C:\\temp\\Tutorial02.fx", NULL, NULL, "fx_4_0", dwShaderFlags, 0,

    d3ddev, NULL, NULL, &efecto, NULL, NULL );

    if (hr==D3D10_ERROR_FILE_NOT_FOUND)

    MessageBox( NULL, L"The FX file cannot be located. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK );

    tecnica=efecto->GetTechniqueByName("Render");

    // Define the input layout

    D3D10_INPUT_ELEMENT_DESC layout[] =

    {

    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },

    };

    UINT numElements = sizeof(layout)/sizeof(layout[0]);

    // Create the input layout

    D3D10_PASS_DESC PassDesc;

    tecnica->GetPassByIndex( 0 )->GetDesc( &PassDesc );

    d3ddev->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pVertexLayout );

    d3ddev->IASetInputLayout( g_pVertexLayout );

    // Create vertex buffer

    SimpleVertex vertices[] =

    {

    D3DXVECTOR3( -0.5f, 0.5f, 0.5f ),

    D3DXVECTOR3( 0.5f, 0.5f, 0.5f ),

    D3DXVECTOR3( 0.5f, -0.5f, 0.5f),

    D3DXVECTOR3( -0.5f, -0.5f, 0.5f),

    };

    D3D10_BUFFER_DESC bd;

    bd.Usage = D3D10_USAGE_DEFAULT;

    bd.ByteWidth = sizeof( SimpleVertex ) * 3;

    bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;

    bd.CPUAccessFlags = 0;

    bd.MiscFlags = 0;

    D3D10_SUBRESOURCE_DATA InitData;

    InitData.pSysMem = vertices;

    d3ddev->CreateBuffer( &bd, &InitData, &g_pVertexBuffer );

    // Set vertex buffer

    UINT stride = sizeof( SimpleVertex );

    UINT offset = 0;

    d3ddev->IASetVertexBuffers( 0, 1, &g_pVertexBuffer, &stride, &offset );

    // Set primitive topology

    d3ddev->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

    return;

    The first part is the definition block. They are defined as global. From the ********* forward is the code itself. It is included in a function.

  • 05-02-2008 11:24 AM In reply to

    Re: Newbie with Quads

    bd.ByteWidth = sizeof( SimpleVertex ) * 3;

    should be

    bd.ByteWidth = sizeof( SimpleVertex ) * 4;

    to get four vertices into your buffer.

    Also, to draw a quad with four vertices you need to use a triangle strip instead of a triangle list. Otherwise, you would need 6 vertices to draw two triangles.

     

  • 05-02-2008 11:34 AM In reply to

    Re: Newbie with Quads

    David Hunt:

    bd.ByteWidth = sizeof( SimpleVertex ) * 3;

    should be

    bd.ByteWidth = sizeof( SimpleVertex ) * 4;

    to get four vertices into your buffer.

    Also, to draw a quad with four vertices you need to use a triangle strip instead of a triangle list. Otherwise, you would need 6 vertices to draw two triangles.

     

    It didn't work. When debuging, just the 3 first vertices are referenced by g_pVertexBuffer.

    I corrected the confussion with the primitive type now, thank you.

  • 05-02-2008 12:23 PM In reply to

    Re: Newbie with Quads

    trogo:

    It didn't work. When debuging, just the 3 first vertices are referenced by g_pVertexBuffer.

    I find that difficult to believe. If you indeed changed the '3' to a '4', there has to be four vertices in the vertex buffer. Did you also change the '3' to a '4' in your Draw call?

  • 05-02-2008 8:03 PM In reply to

    Re: Newbie with Quads

    David Hunt:
    If you indeed changed the '3' to a '4', there has to be four vertices in the vertex buffer. Did you also change the '3' to a '4' in your Draw call?

    Thank you. I had a 3 also in the Draw call, but the problem when debuging is that the pointer to the buffer that returns the CreateBuffer() only references the first 3 vertex, not the 4th.

    When compiling again once the Draw() call has been fixed, still appears a triangle on screen, and the pointer to the buffer remains with just 3 vertex.

  • 05-02-2008 9:07 PM In reply to

    Re: Newbie with Quads

    Can you post your entire source code in a pastebin somewhere and provide a link? There has to be something else going on.

     

  • 05-03-2008 3:21 PM In reply to

    Re: Newbie with Quads

    David Hunt:
    Can you post your entire source code in a pastebin somewhere and provide a link? There has to be something else going on.

    I have post the code almost complete http://pastebin.com/m1579d962

    The parts missing are in a DLL created to simplify my code. the DLL contains the SwapChain creation and the renderTargetView assignment, but copy directly from the tutorials on the SDK without any modifications.

  • 05-03-2008 8:56 PM In reply to

    Re: Newbie with Quads

    Answer

    One thing I noticed is you need to reverse the last two vertices. For a triangle strip quad, you need the order to be top-left, top-right, bottom-left, bottom-right. I think that might fix the problem.

     

  • 05-04-2008 6:47 AM In reply to

    Re: Newbie with Quads

    David Hunt:
    One thing I noticed is you need to reverse the last two vertices. For a triangle strip quad, you need the order to be top-left, top-right, bottom-left, bottom-right. I think that might fix the problem.

    Thank you very much for your patience.

    This finally does the job. Although I thought that in the Vertex Buffer there should be references to all my vertices on the original structure, when debuging, the pointer still keeps storing references to the 3 first vertices. So, I was on the wrong way.

    And I have learned 2 things: about the vertices order and about one site called pastebin.com. Thank you again.

  • 05-04-2008 12:52 PM In reply to

    Re: Newbie with Quads

    You're very welcome!

     

Page 1 of 1 (12 items) Previous Next