Original Post
Im trying to render/generate the indices/vertices for a 2d tilemap...Ive managed to get them to render but they are really messed up and i cant work out whats wrong.Heres the functions
Generating the vertices
Rendering the vertices
Thanks in advance
Ash.
int CTileMap::Resize(int NumXTiles, int NumYTiles)
{
//re-create
m_NumXTiles = NumXTiles;
m_NumYTiles = NumYTiles;
m_NumVertices = (m_NumXTiles+1)*(m_NumYTiles+1);
m_NumPolygons = m_NumXTiles*m_NumYTiles*2;
m_NumIndices = m_NumYTiles*m_NumXTiles*6;
if(m_Tiles)
{
for(int n = 0; n < m_NumXTiles; n++)
{
delete m_Tiles[n];
}
delete [] m_Tiles;
m_Tiles = NULL;
}
m_Tiles = new CTile*[m_NumXTiles];
for(int n = 0; n < m_NumXTiles; n++)
{
m_Tiles[n] = new CTile[NumYTiles];
}
for(int y = 0; y < m_NumYTiles; y++)
{
for(int x = 0; x < m_NumXTiles; x++)
{
m_Tiles[y][x].TileHandle = 0; //invalid
}
}
//free the vbuffer
if(m_pVBuffer)
{
delete m_pVBuffer;
}
m_pVBuffer = new CVertexBuffer();
//recreate vertex buffer
if(!m_pVBuffer->Create(m_NumVertices,sizeof(s3DVertex),_3DVERTEX)) g_Log.Error(__LINE__,__FILE__,"Error Creating VBuffer");
//generate the tilemap vertices
s3DVertex *verts = new s3DVertex[m_NumVertices];
int cury = 0;
int curx = 0;
for( cury = 0; cury < m_NumYTiles+1; cury++)
{
for( curx = 0; curx < m_NumXTiles+1; curx++)
{
verts[cury*curx].diffuse = D3DCOLOR_XRGB(255,100,255);
verts[cury*curx].u = (1/m_TileSet->GetWidth())*m_TileSet->GetTileWidth();
verts[cury*curx].v = (1/m_TileSet->GetHeight())*m_TileSet->GetTileHeight();
verts[cury*curx].x = curx * m_TileSet->GetTileWidth();
verts[cury*curx].y = cury * m_TileSet->GetTileHeight();
verts[cury*curx].z = 0.5f;
}
}
if(!m_pVBuffer->Set(0,m_NumVertices,verts)) g_Log.Error(__LINE__,__FILE__,"Error setting vertices");
//create the index buffer
if(m_pIBuffer)
{
m_pIBuffer->Free();
}
else
{
m_pIBuffer = new CIndexBuffer();
}
if(!m_pIBuffer->Create(m_NumIndices)) g_Log.Error(__LINE__,__FILE__,"Creating index buffer");
//generate the indices
WORD * indices = new WORD[m_NumIndices];
int curIndex = 0;
g_Log.Print("Number of X Tiles:%d Number of Y Tiles:%d", m_NumXTiles,m_NumYTiles);
cury = 0;
curx = 0;
//generate the indices uh oh..
for( cury = 0; cury < m_NumYTiles; cury++)
{
for( curx = 0; curx < m_NumXTiles; curx++)
{
int topLeft = ((m_NumXTiles+1)*cury)+curx;
int bottomLeft = topLeft+(m_NumXTiles+1);
g_Log.Print("Current X = %d \n Current Y = %d", curx,cury);
g_Log.Print("Bottom Left = %d \n Top Left = %d", bottomLeft,topLeft);
//left triangle
indices[curIndex++] = topLeft; //top left
indices[curIndex++] = bottomLeft; //bottom left
indices[curIndex++] = topLeft+1; //top right
//right triangle
indices[curIndex++] = topLeft+1; //top right
indices[curIndex++] = bottomLeft; //bottom left
indices[curIndex++] = bottomLeft+1; //bottom right
}
}
m_pIBuffer->Set(indices);
return 0;
}
int CTileMap::Draw(int OffsetX, int OffsetY)
{
D3DXMATRIX matTrans;
D3DXMatrixIdentity(&matTrans);
D3DXMATRIX matRot;
D3DXMatrixRotationZ(&matRot,D3DXToRadian(0));
D3DXMATRIX matMult;
D3DXMatrixMultiply(&matMult, &matTrans, &matRot);
D3DXMATRIX matTransback;
D3DXMatrixTranslation(&matTransback,-OffsetX,-OffsetY,0.5);
D3DXMATRIX matFinal;
D3DXMatrixMultiply(&matFinal, &matMult , & matTransback);
g_Graphics->GetDeviceCOM()->SetTransform(D3DTS_WORLD,&matFinal);
//set fvf
m_Err = g_Graphics->GetDeviceCOM()->SetFVF(_3DVERTEX);
if(m_Err != D3D_OK)
{
g_Log.DXError(__LINE__,__FILE__,m_Err);
}
//set the stream source
m_Err = g_Graphics->GetDeviceCOM()->SetStreamSource(0,m_pVBuffer->GetVertexBufferCOM(), 0, sizeof(s3DVertex));
if(m_Err != D3D_OK)
{
g_Log.DXError(__LINE__,__FILE__,m_Err);
}
//set the indices
m_Err = g_Graphics->GetDeviceCOM()->SetTexture(0,m_TileSet->GetTiles());
if(m_Err != D3D_OK)
{
g_Log.DXError(__LINE__,__FILE__,m_Err);
}
//set the indices
m_Err = g_Graphics->GetDeviceCOM()->SetIndices(m_pIBuffer->GetIndicesCOM());
if(m_Err != D3D_OK)
{
g_Log.DXError(__LINE__,__FILE__,m_Err);
}
//draw the primitive
m_Err = g_Graphics->GetDeviceCOM()->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,m_NumVertices,0,m_NumPolygons);
if(m_Err != D3D_OK)
{
g_Log.DXError(__LINE__,__FILE__,m_Err);
}
return 0;
}