Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int createToolMesh(int zeroBasedToolID, TOOLVERTEX *buffer)
- {
- float toolPosX = 0.0f;
- float toolPosY = 0.0f;
- float toolPosZ = 0.0f;
- int verticesPerTool = TOOL_TEXTURE_SIZE * TOOL_TEXTURE_SIZE * 36;
- ZeroMemory(buffer, verticesPerTool * sizeof(TOOLVERTEX));
- D3DLOCKED_RECT lrect;
- RECT rc;
- rc.top = TOOL_TEXTURE_SIZE * (zeroBasedToolID / TOOL_TEXTURES_PER_ROW);
- rc.left = TOOL_TEXTURE_SIZE * ((zeroBasedToolID + TOOL_TEXTURES_PER_ROW) % TOOL_TEXTURES_PER_ROW);
- rc.bottom = rc.top + TOOL_TEXTURE_SIZE;
- rc.right = rc.left + TOOL_TEXTURE_SIZE;
- if(FAILED(cGame->cTextureManager.getTexture("Tools.bmp")->LockRect(0, &lrect, &rc, D3DLOCK_READONLY | D3DLOCK_NO_DIRTY_UPDATE)))
- {
- drawTool = false;
- return 0;
- }
- int i = 0;
- //loop through each texel and create cubes for non-zero-alpha texels
- for(int y = 0; y < TOOL_TEXTURE_SIZE; ++y)
- {
- for(int x = 0; x < TOOL_TEXTURE_SIZE; ++x)
- {
- if(*((unsigned char*)((unsigned int)lrect.pBits + y * lrect.Pitch + x * 4 + 3)) == 0)
- {
- //alpha is zero, skip this cube
- continue;
- }
- else
- {
- DWORD colour = D3DCOLOR_XRGB(*((unsigned char*)((unsigned int)lrect.pBits + y * lrect.Pitch + x * 4 + 2)),
- *((unsigned char*)((unsigned int)lrect.pBits + y * lrect.Pitch + x * 4 + 1)),
- *((unsigned char*)((unsigned int)lrect.pBits + y * lrect.Pitch + x * 4)));
- #pragma region Vertice Definitions
- //FRONT FACE
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y);
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- //BACK FACE
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y);
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- //check edges
- if(*((unsigned char*)((unsigned int)lrect.pBits + y * lrect.Pitch + (x - 1) * 4 + 3)) == 0)
- {
- //LEFT FACE
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- }
- if(*((unsigned char*)((unsigned int)lrect.pBits + y * lrect.Pitch + (x + 1) * 4 + 3)) == 0)
- {
- //RIGHT FACE
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- }
- if((*((unsigned char*)((unsigned int)lrect.pBits + (y + 1) * lrect.Pitch + x * 4 + 3)) == 0) || (y == 31))
- {
- //BOTTOM FACE
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- }
- if((*((unsigned char*)((unsigned int)lrect.pBits + (y - 1) * lrect.Pitch + x * 4 + 3)) == 0) || (y == 0))
- {
- //TOP FACE
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ;
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
- buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
- buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
- buffer[i].colour = colour;
- ++i;
- }
- #pragma endregion
- }
- }
- }
- cGame->cTextureManager.getTexture("Tools.bmp")->UnlockRect(0);
- toolPrimitiveCount = i / 3;
- return i; //returns number of vertices
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement