Advertisement
Guest User

3D Mesh Creation From 2D Texture

a guest
Aug 24th, 2013
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.03 KB | None | 0 0
  1. int createToolMesh(int zeroBasedToolID, TOOLVERTEX *buffer)
  2. {
  3.     float toolPosX = 0.0f;
  4.     float toolPosY = 0.0f;
  5.     float toolPosZ = 0.0f;
  6.     int verticesPerTool = TOOL_TEXTURE_SIZE * TOOL_TEXTURE_SIZE * 36;
  7.     ZeroMemory(buffer, verticesPerTool * sizeof(TOOLVERTEX));
  8.    
  9.     D3DLOCKED_RECT lrect;
  10.     RECT rc;
  11.     rc.top = TOOL_TEXTURE_SIZE * (zeroBasedToolID / TOOL_TEXTURES_PER_ROW);
  12.     rc.left = TOOL_TEXTURE_SIZE * ((zeroBasedToolID + TOOL_TEXTURES_PER_ROW) % TOOL_TEXTURES_PER_ROW);
  13.     rc.bottom = rc.top + TOOL_TEXTURE_SIZE;
  14.     rc.right = rc.left + TOOL_TEXTURE_SIZE;
  15.  
  16.     if(FAILED(cGame->cTextureManager.getTexture("Tools.bmp")->LockRect(0, &lrect, &rc, D3DLOCK_READONLY | D3DLOCK_NO_DIRTY_UPDATE)))
  17.     {
  18.         drawTool = false;
  19.         return 0;
  20.     }
  21.    
  22.     int i = 0;
  23.  
  24.     //loop through each texel and create cubes for non-zero-alpha texels
  25.     for(int y = 0; y < TOOL_TEXTURE_SIZE; ++y)
  26.     {
  27.         for(int x = 0; x < TOOL_TEXTURE_SIZE; ++x)
  28.         {
  29.             if(*((unsigned char*)((unsigned int)lrect.pBits + y * lrect.Pitch + x * 4 + 3)) == 0)
  30.             {
  31.                 //alpha is zero, skip this cube
  32.                 continue;
  33.             }
  34.             else
  35.             {
  36.                 DWORD colour = D3DCOLOR_XRGB(*((unsigned char*)((unsigned int)lrect.pBits + y * lrect.Pitch + x * 4 + 2)),
  37.                         *((unsigned char*)((unsigned int)lrect.pBits + y * lrect.Pitch + x * 4 + 1)),
  38.                         *((unsigned char*)((unsigned int)lrect.pBits + y * lrect.Pitch + x * 4)));
  39.  
  40. #pragma region Vertice Definitions
  41.                 //FRONT FACE
  42.                 buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  43.                 buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  44.                 buffer[i].z = toolPosZ;
  45.                 buffer[i].colour = colour;
  46.                 ++i;
  47.  
  48.                 buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  49.                 buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  50.                 buffer[i].z = toolPosZ;
  51.                 buffer[i].colour = colour;
  52.                 ++i;
  53.  
  54.                 buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  55.                 buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  56.                 buffer[i].z = toolPosZ;
  57.                 buffer[i].colour = colour;
  58.                 ++i;
  59.  
  60.                 buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x);
  61.                 buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y);
  62.                 buffer[i].z = toolPosZ;
  63.                 buffer[i].colour = colour;
  64.                 ++i;
  65.  
  66.                 buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x);
  67.                 buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  68.                 buffer[i].z = toolPosZ;
  69.                 buffer[i].colour = colour;
  70.                 ++i;
  71.  
  72.                 buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  73.                 buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  74.                 buffer[i].z = toolPosZ;
  75.                 buffer[i].colour = colour;
  76.                 ++i;
  77.  
  78.  
  79.                 //BACK FACE
  80.                 buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  81.                 buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  82.                 buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  83.                 buffer[i].colour = colour;
  84.                 ++i;
  85.  
  86.                 buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  87.                 buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  88.                 buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  89.                 buffer[i].colour = colour;
  90.                 ++i;
  91.  
  92.                 buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  93.                 buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  94.                 buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  95.                 buffer[i].colour = colour;
  96.                 ++i;
  97.  
  98.                 buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x);
  99.                 buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y);
  100.                 buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  101.                 buffer[i].colour = colour;
  102.                 ++i;
  103.  
  104.                 buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  105.                 buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  106.                 buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  107.                 buffer[i].colour = colour;
  108.                 ++i;
  109.  
  110.                 buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x);
  111.                 buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  112.                 buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  113.                 buffer[i].colour = colour;
  114.                 ++i;
  115.  
  116.  
  117.                 //check edges
  118.                 if(*((unsigned char*)((unsigned int)lrect.pBits + y * lrect.Pitch + (x - 1) * 4 + 3)) == 0)
  119.                 {
  120.                     //LEFT FACE
  121.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  122.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  123.                     buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  124.                     buffer[i].colour = colour;
  125.                     ++i;
  126.  
  127.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  128.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  129.                     buffer[i].z = toolPosZ;
  130.                     buffer[i].colour = colour;
  131.                     ++i;
  132.  
  133.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  134.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  135.                     buffer[i].z = toolPosZ;
  136.                     buffer[i].colour = colour;
  137.                     ++i;
  138.  
  139.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  140.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  141.                     buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  142.                     buffer[i].colour = colour;
  143.                     ++i;
  144.  
  145.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  146.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  147.                     buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  148.                     buffer[i].colour = colour;
  149.                     ++i;
  150.  
  151.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  152.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  153.                     buffer[i].z = toolPosZ;
  154.                     buffer[i].colour = colour;
  155.                     ++i;
  156.                 }
  157.  
  158.                 if(*((unsigned char*)((unsigned int)lrect.pBits + y * lrect.Pitch + (x + 1) * 4 + 3)) == 0)
  159.                 {
  160.                     //RIGHT FACE
  161.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  162.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  163.                     buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  164.                     buffer[i].colour = colour;
  165.                     ++i;
  166.  
  167.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  168.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  169.                     buffer[i].z = toolPosZ;
  170.                     buffer[i].colour = colour;
  171.                     ++i;
  172.  
  173.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  174.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  175.                     buffer[i].z = toolPosZ;
  176.                     buffer[i].colour = colour;
  177.                     ++i;
  178.  
  179.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  180.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  181.                     buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  182.                     buffer[i].colour = colour;
  183.                     ++i;
  184.  
  185.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  186.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  187.                     buffer[i].z = toolPosZ;
  188.                     buffer[i].colour = colour;
  189.                     ++i;
  190.  
  191.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  192.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  193.                     buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  194.                     buffer[i].colour = colour;
  195.                     ++i;
  196.                 }
  197.                
  198.                 if((*((unsigned char*)((unsigned int)lrect.pBits + (y + 1) * lrect.Pitch + x * 4 + 3)) == 0) || (y == 31))
  199.                 {
  200.                     //BOTTOM FACE
  201.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  202.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  203.                     buffer[i].z = toolPosZ;
  204.                     buffer[i].colour = colour;
  205.                     ++i;
  206.  
  207.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  208.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  209.                     buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  210.                     buffer[i].colour = colour;
  211.                     ++i;
  212.  
  213.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  214.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  215.                     buffer[i].z = toolPosZ;
  216.                     buffer[i].colour = colour;
  217.                     ++i;
  218.  
  219.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  220.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  221.                     buffer[i].z = toolPosZ;
  222.                     buffer[i].colour = colour;
  223.                     ++i;
  224.  
  225.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  226.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  227.                     buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  228.                     buffer[i].colour = colour;
  229.                     ++i;
  230.  
  231.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  232.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(y + 1);
  233.                     buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  234.                     buffer[i].colour = colour;
  235.                     ++i;
  236.                 }
  237.                
  238.                 if((*((unsigned char*)((unsigned int)lrect.pBits + (y - 1) * lrect.Pitch + x * 4 + 3)) == 0) || (y == 0))
  239.                 {
  240.                     //TOP FACE
  241.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  242.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  243.                     buffer[i].z = toolPosZ;
  244.                     buffer[i].colour = colour;
  245.                     ++i;
  246.  
  247.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  248.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  249.                     buffer[i].z = toolPosZ;
  250.                     buffer[i].colour = colour;
  251.                     ++i;
  252.  
  253.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  254.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  255.                     buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  256.                     buffer[i].colour = colour;
  257.                     ++i;
  258.  
  259.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  260.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  261.                     buffer[i].z = toolPosZ;
  262.                     buffer[i].colour = colour;
  263.                     ++i;
  264.  
  265.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)(x + 1);
  266.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  267.                     buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  268.                     buffer[i].colour = colour;
  269.                     ++i;
  270.  
  271.                     buffer[i].x = toolPosX + (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)x;
  272.                     buffer[i].y = toolPosY - (1.0f / (float)TOOL_TEXTURE_SIZE) * (float)y;
  273.                     buffer[i].z = toolPosZ + (3.0f / (float)TOOL_TEXTURE_SIZE);
  274.                     buffer[i].colour = colour;
  275.                     ++i;
  276.                 }
  277. #pragma endregion
  278.             }
  279.         }
  280.     }
  281.  
  282.     cGame->cTextureManager.getTexture("Tools.bmp")->UnlockRect(0);
  283.  
  284.     toolPrimitiveCount = i / 3;
  285.  
  286.     return i; //returns number of vertices
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement