Jump to content

  • Log In with Google      Sign In   
  • Create Account


Texture Coordinate Problem With Index Vertex Buffer


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 ryan20fun   Members   -  Reputation: 975

Like
0Likes
Like

Posted 03 April 2012 - 09:40 AM

Hi All Again.

im having a problem with texture coordinates and indexed geometry.

ive created 4 vertices and set the position / normal / tex coord.
but the second triangle seems to have stretched the texture wierdly.

here is a picture of the results:
untitled.JPG

how do i setup my vertices so that the texture coordinates work properly ?

here is the code:

vertex structure:
struct TexCoord
{
    float U,// horizontal
        V;// vertical
};

struct MyVertex
{
    D3DXVECTOR3 Position;

    D3DXVECTOR3 Normal;
    TexCoord Tex1, Tex2;

    MyVertex()
    {
        ZeroMemory( this, sizeof( MyVertex ) );
    }
};

#define D3DFVF_CUSTOMVERTEX ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX2 )


vertice creation code:
MyVertex* vertices = NULL;
unsigned* indices = NULL;

MyVertex vert1, vert2, vert3, vert4;
vert1.Position = D3DXVECTOR3( 0, 0, 0 );
vert2.Position = D3DXVECTOR3( 0, 255, 0 );
vert3.Position = D3DXVECTOR3( 255, 255, 0 );
vert4.Position = D3DXVECTOR3( 255, 0, 0 );

vert1.Normal = -GetNormal( vert1.Position, vert2.Position, vert3.Position );
vert1.Tex1.U = 0;
vert1.Tex1.V = 1;

vert2.Normal = -GetNormal( vert1.Position, vert2.Position, vert3.Position );
vert2.Tex1.U = 0;
 vert2.Tex1.V = 0;

vert3.Normal = -GetNormal( vert1.Position, vert2.Position, vert3.Position );
vert3.Tex1.U = 1;
vert3.Tex1.V = 0;

vert4.Normal = -GetNormal( vert1.Position, vert3.Position, vert4.Position );
vert4.Tex1.U = 0;
vert4.Tex1.V = 1;

dwNumberOfVertices = 4;

 dwNumberOfIndices = 6;

indices = TNEW unsigned[ 6 ];
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 3;
indices[4] = 0;
indices[5] = 2;

vertex / index buffer creation code:
// create the vertex buffer
        hr = D3DDevice->CreateVertexBuffer( dwNumberOfVertices * sizeof( MyVertex ), 0, D3DFVF_CUSTOMVERTEX , D3DPOOL_MANAGED, &pVertexBuffer, TNULL );

        if ( FAILED( hr ) )
            MessageBoxA( NULL,"Error while creating VertexBuffer","FillVertices()",MB_OK);

        // fill the vertex buffer
        VOID* p_Vertices;
        if ( FAILED( pVertexBuffer->Lock( 0, dwNumberOfVertices * sizeof( MyVertex ), ( void** )&p_Vertices, 0) ) )
            MessageBoxA(NULL,"Error trying to lock","FillVertices()",MB_OK);
        else
        {
            memcpy( p_Vertices, vertices, dwNumberOfVertices * sizeof( MyVertex ) );
            pVertexBuffer->Unlock();
        }
       
        // create the index buffer
        if ( FAILED( D3DDevice->CreateIndexBuffer( ( dwNumberOfVertices - 2 ) * 6 * sizeof( unsigned ), D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_MANAGED, &pIndexBuffer, TNULL ) ) )
            MessageBoxA(NULL,"Error while creating IndexBuffer","FillIndices()",MB_OK);

        // fill the index buffer
        VOID* p_Indices;
        if ( FAILED( pIndexBuffer->Lock( 0, sizeof( unsigned ), ( void** )&p_Indices, 0 ) ) )
            MessageBoxA(NULL,"Error trying to lock","FillIndices()",MB_OK);
        else
        {
            memcpy( p_Indices, indices, ( dwNumberOfVertices - 2 ) * 6 * sizeof( unsigned ) );
            pIndexBuffer->Unlock();
        }

rendering code:
D3DDevice->SetStreamSource( 0, pVertexBuffer, 0, sizeof( MyVertex ) );
D3DDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
D3DDevice->SetIndices( pIndexBuffer );
D3DDevice->DrawIndexedPrimitive
 (
	 D3DPT_TRIANGLELIST,        // PrimitiveType
	 0,                        // BaseVertexIndex
	 0,                        // MinIndex
	 dwNumberOfVertices,        // NumVertices
	 0,                        // StartIndex
	 2                        // PrimitiveCount
);

what am i doing wrong ?

Thanks In Advance.
Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

Sponsor:

#2 ryan20fun   Members   -  Reputation: 975

Like
0Likes
Like

Posted 04 April 2012 - 02:36 AM

oops, the code you think will not be the problem normally turns out to be the problem.
the problem was the UV coord of the fourth vertex.
Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS