# Texture Coordinate Problem With Index Vertex Buffer

This topic is 2757 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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:
 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 ?

##### Share on other sites
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.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26