Sign in to follow this  
ryan20fun

Texture Coordinate Problem With Index Vertex Buffer

Recommended Posts

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:
[attachment=8016:untitled.JPG]

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

here is the code:

vertex structure:
[code]
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 )

[/code]

vertice creation code:
[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;
[/code]

vertex / index buffer creation code:
[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();
}
[/code]

rendering code:
[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
);
[/code]

what am i doing wrong ?

Thanks In Advance.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this