Currently in the process of 'learning' D3D, however I've stumbled across a small issue that I can't seem to figure out. I'm trying to load an image file which contains multiple textures (think of a tile sheet), and draw one of those textures, I can do it however it's a really unsightly method and I'm guessing there has to be something easier that I haven't managed to find.
This is my code basically:
Now using the example of a 1024x1024 image which contains 4 textures of 512x512 each, I could change the u and v (my texture coordinates) to be u = 0.0f and v = 0.5f to load the top left corner of the second image, however this is going to become complex and erroneous if I start using say 3 images (0.333333333f anyone?), so is there something more friendly I can use than this?
This is my code basically:
Code:
void blitD3D(IDirect3DTexture9 *texture, RECT *rDest, D3DCOLOR vertexColour, float rotate)
{
TLVERTEX *vertices;
//lock the vertex buffer so only this thread can access it
vertexBuffer->Lock(0, 0, (void**)&vertices, NULL);
//vertices go clockwise
vertices[0].colour = vertexColour;
vertices[0].x = (float) rDest->left - 0.5f;
vertices[0].y = (float) rDest->top - 0.5f;
vertices[0].z = 0.0f;
vertices[0].rhw = 1.0f;
vertices[0].u = 0.0f;
vertices[0].v = 0.0f;
vertices[1].colour = vertexColour;
vertices[1].x = (float) rDest->right - 0.5f;
vertices[1].y = (float) rDest->top - 0.5f;
vertices[1].z = 0.0f;
vertices[1].rhw = 1.0f;
vertices[1].u = 1.0f;
vertices[1].v = 0.0f;
vertices[2].colour = vertexColour;
vertices[2].x = (float) rDest->right - 0.5f;
vertices[2].y = (float) rDest->bottom - 0.5f;
vertices[2].z = 0.0f;
vertices[2].rhw = 1.0f;
vertices[2].u = 1.0f;
vertices[2].v = 1.0f;
vertices[3].colour = vertexColour;
vertices[3].x = (float) rDest->left - 0.5f;
vertices[3].y = (float) rDest->bottom - 0.5f;
vertices[3].z = 0.0f;
vertices[3].rhw = 1.0f;
vertices[3].u = 0.0f;
vertices[3].v = 1.0f;
if(rotate)
{
//rotation code here
}
//unlock the vertex buffer for external use
vertexBuffer->Unlock();
d3ddev->SetTexture(0, texture);
d3ddev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
}
Now using the example of a 1024x1024 image which contains 4 textures of 512x512 each, I could change the u and v (my texture coordinates) to be u = 0.0f and v = 0.5f to load the top left corner of the second image, however this is going to become complex and erroneous if I start using say 3 images (0.333333333f anyone?), so is there something more friendly I can use than this?