Advertisement
AlwaysGeeky

QubicleBinary.cpp

Oct 4th, 2014
1,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 38.27 KB | None | 0 0
  1. // ******************************************************************************
  2. //
  3. // Filename:    QubicleBinary.cpp
  4. // Project: Vox
  5. // Author:  Steven Ball
  6. //
  7. // Purpose:
  8. //
  9. // Revision History:
  10. //   Initial Revision - 10/07/14
  11. //
  12. // Copyright (c) 2005-2014, Steven Ball
  13. //
  14. // ******************************************************************************
  15.  
  16. #include "QubicleBinary.h"
  17.  
  18. #include "Random.h"
  19.  
  20.  
  21. const float QubicleBinary::BLOCK_RENDER_SIZE = 0.5f;
  22.  
  23.  
  24. QubicleBinary::QubicleBinary(OpenGLRenderer* pRenderer)
  25. {
  26.     m_pRenderer = pRenderer;
  27.  
  28.     Reset();
  29.  
  30.     m_renderWireFrame = false;
  31.  
  32.     pRenderer->CreateMaterial(Colour(0.7f, 0.7f, 0.7f, 1.0f), Colour(1.0f, 1.0f, 1.0f, 1.0f), Colour(1.0f, 1.0f, 1.0f, 1.0f), Colour(0.0f, 0.0f, 0.0f, 0.0f), 64, &m_materialID);
  33.  
  34.     float l_length = 0.5f;
  35.     float l_height = 0.5f;
  36.     float l_width = 0.5f;
  37. }
  38.  
  39. QubicleBinary::~QubicleBinary()
  40. {
  41.     Unload();
  42.  
  43.     Reset();
  44. }
  45.  
  46. void QubicleBinary::Unload()
  47. {
  48.     if(m_loaded)
  49.     {
  50.         for(unsigned int i = 0; i < m_numMatrices; i++)
  51.         {
  52.             m_pRenderer->ClearMesh(m_pMatrices[i].m_meshID);
  53.         }
  54.  
  55.         delete m_pMatrices;
  56.         m_pMatrices = NULL;    
  57.     }
  58. }
  59.  
  60. void QubicleBinary::Reset()
  61. {
  62.     m_version[0] = 0;
  63.     m_version[1] = 0;
  64.     m_version[2] = 0;
  65.     m_version[3] = 0;
  66.     m_colourFormat = 0;
  67.     m_zAxisOrientation = 0;
  68.     m_compressed = 0;
  69.     m_visibilityMaskEncoded = 0;
  70.     m_numMatrices = 0;
  71.  
  72.     m_loaded = false;
  73.  
  74.     m_pMatrices = NULL;
  75.  
  76.     m_renderWireFrame = false;
  77. }
  78.  
  79. unsigned int QubicleBinary::GetMaterial()
  80. {
  81.     return m_materialID;
  82. }
  83.  
  84. Matrix4x4 QubicleBinary::GetModelMatrix(int qubicleMatrixIndex)
  85. {
  86.     if(m_loaded == false)
  87.     {
  88.         return Matrix4x4();
  89.     }
  90.  
  91.     return m_pMatrices[qubicleMatrixIndex].m_modelMatrix;
  92. }
  93.  
  94. int QubicleBinary::GetMatrixIndexForName(const char* matrixName)
  95. {
  96.     for(unsigned int i = 0; i < m_numMatrices; i++)
  97.     {
  98.         if(strcmp(m_pMatrices[i].m_name, matrixName) == 0)
  99.         {
  100.             return i;
  101.         }
  102.     }
  103.  
  104.     return -1;
  105. }
  106.  
  107. void QubicleBinary::GetMatrixPosition(int index, int* aX, int* aY, int* aZ)
  108. {
  109.     *aX = m_pMatrices[index].m_matrixPosX;
  110.     *aY = m_pMatrices[index].m_matrixPosY;
  111.     *aZ = m_pMatrices[index].m_matrixPosZ;
  112. }
  113.  
  114. bool QubicleBinary::Import(const char* fileName)
  115. {
  116.     char qbFilename[256];
  117.     sprintf_s(qbFilename, fileName);;
  118.  
  119.     FILE* pQBfile = NULL;
  120.     fopen_s(&pQBfile, qbFilename, "rb");
  121.  
  122.     const unsigned int CODEFLAG = 2;
  123.     const unsigned int NEXTSLICEFLAG = 6;
  124.  
  125.     if (qbFilename != NULL)
  126.     {
  127.         int ok = 0;
  128.         ok = fread(&m_version[0], sizeof(char)*4, 1, pQBfile) == 1;
  129.         ok = fread(&m_colourFormat, sizeof(unsigned int), 1, pQBfile) == 1;
  130.         ok = fread(&m_zAxisOrientation, sizeof(unsigned int), 1, pQBfile) == 1;
  131.         ok = fread(&m_compressed, sizeof(unsigned int), 1, pQBfile) == 1;
  132.         ok = fread(&m_visibilityMaskEncoded, sizeof(unsigned int), 1, pQBfile) == 1;
  133.         ok = fread(&m_numMatrices, sizeof(unsigned int), 1, pQBfile) == 1;
  134.  
  135.         m_pMatrices = new QubicleMatrix[m_numMatrices];
  136.  
  137.         for(unsigned int i = 0; i < m_numMatrices; i++)
  138.         {
  139.             ok = fread((char *)&m_pMatrices[i].m_nameLength, sizeof(char), 1, pQBfile) == 1;
  140.             m_pMatrices[i].m_name = new char[m_pMatrices[i].m_nameLength+1];
  141.             ok = fread(&m_pMatrices[i].m_name[0], sizeof(char)*m_pMatrices[i].m_nameLength, 1, pQBfile) == 1;
  142.             m_pMatrices[i].m_name[m_pMatrices[i].m_nameLength] = 0;
  143.  
  144.             ok = fread(&m_pMatrices[i].m_matrixSizeX, sizeof(unsigned int), 1, pQBfile) == 1;
  145.             ok = fread(&m_pMatrices[i].m_matrixSizeY, sizeof(unsigned int), 1, pQBfile) == 1;
  146.             ok = fread(&m_pMatrices[i].m_matrixSizeZ, sizeof(unsigned int), 1, pQBfile) == 1;
  147.            
  148.             ok = fread(&m_pMatrices[i].m_matrixPosX, sizeof(int), 1, pQBfile) == 1;
  149.             ok = fread(&m_pMatrices[i].m_matrixPosY, sizeof(int), 1, pQBfile) == 1;
  150.             ok = fread(&m_pMatrices[i].m_matrixPosZ, sizeof(int), 1, pQBfile) == 1;
  151.  
  152.             m_pMatrices[i].m_pColour = new unsigned int**[m_pMatrices[i].m_matrixSizeX];
  153.  
  154.             m_pMatrices[i].m_boneIndex = -1;
  155.             m_pMatrices[i].m_meshID = -1;
  156.  
  157.             m_pMatrices[i].m_scale = 1.0f;
  158.             m_pMatrices[i].m_offsetX = 0.0f;
  159.             m_pMatrices[i].m_offsetY = 0.0f;
  160.             m_pMatrices[i].m_offsetZ = 0.0f;
  161.  
  162.             for(unsigned int j = 0; j < m_pMatrices[i].m_matrixSizeX; j++)
  163.             {
  164.                 m_pMatrices[i].m_pColour[j] = new unsigned int*[m_pMatrices[i].m_matrixSizeY];
  165.  
  166.                 for(unsigned int k = 0; k < m_pMatrices[i].m_matrixSizeY; k++)
  167.                 {
  168.                     m_pMatrices[i].m_pColour[j][k] = new unsigned int[m_pMatrices[i].m_matrixSizeZ];
  169.                 }
  170.             }
  171.  
  172.             if(m_compressed == 0)
  173.             {
  174.                 for(unsigned int z = 0; z < m_pMatrices[i].m_matrixSizeZ; z++)
  175.                 {
  176.                     for(unsigned int y = 0; y < m_pMatrices[i].m_matrixSizeY; y++)
  177.                     {
  178.                         for(unsigned int x = 0; x < m_pMatrices[i].m_matrixSizeX; x++)
  179.                         {
  180.                             unsigned int colour = 0;
  181.                             ok = fread(&colour, sizeof(unsigned int), 1, pQBfile) == 1;
  182.  
  183.                             //unsigned int alpha = (colour & 0xFF000000) >> 24;
  184.                             //unsigned int blue = (colour & 0x00FF0000) >> 16;
  185.                             //unsigned int green = (colour & 0x0000FF00) >> 8;
  186.                             //unsigned int red = (colour & 0x000000FF);
  187.  
  188.                             m_pMatrices[i].m_pColour[x][y][z] = colour;
  189.                         }
  190.                     }
  191.                 }
  192.             }
  193.             else
  194.             {
  195.                 unsigned int z = 0;
  196.  
  197.                 while (z < m_pMatrices[i].m_matrixSizeZ)
  198.                 {
  199.                     unsigned int index = 0;
  200.  
  201.                     while(true)
  202.                     {
  203.                         unsigned int data = 0;
  204.                         ok = fread(&data, sizeof(unsigned int), 1, pQBfile) == 1;
  205.  
  206.                         if (data == NEXTSLICEFLAG)
  207.                             break;
  208.                         else if (data == CODEFLAG)
  209.                         {
  210.                             unsigned int count = 0;
  211.                             ok = fread(&count, sizeof(unsigned int), 1, pQBfile) == 1;
  212.                             ok = fread(&data, sizeof(unsigned int), 1, pQBfile) == 1;
  213.  
  214.                             for(unsigned int j = 0; j < count; j++)
  215.                             {
  216.                                 unsigned int x = index % m_pMatrices[i].m_matrixSizeX;
  217.                                 unsigned int y = index / m_pMatrices[i].m_matrixSizeX;
  218.  
  219.                                 //unsigned int alpha = (data & 0xFF000000) >> 24;
  220.                                 //unsigned int blue = (data & 0x00FF0000) >> 16;
  221.                                 //unsigned int green = (data & 0x0000FF00) >> 8;
  222.                                 //unsigned int red = (data & 0x000000FF);
  223.  
  224.                                 m_pMatrices[i].m_pColour[x][y][z] = data;
  225.  
  226.                                 index++;
  227.                             }
  228.                         }
  229.                         else
  230.                         {
  231.                             unsigned int x = index % m_pMatrices[i].m_matrixSizeX;
  232.                             unsigned int y = index / m_pMatrices[i].m_matrixSizeX;
  233.  
  234.                             //unsigned int alpha = (data & 0xFF000000) >> 24;
  235.                             //unsigned int blue = (data & 0x00FF0000) >> 16;
  236.                             //unsigned int green = (data & 0x0000FF00) >> 8;
  237.                             //unsigned int red = (data & 0x000000FF);
  238.  
  239.                             m_pMatrices[i].m_pColour[x][y][z] = data;
  240.  
  241.                             index++;
  242.                         }
  243.                     }
  244.  
  245.                     z++;
  246.                 }
  247.             }
  248.         }
  249.  
  250.         fclose(pQBfile);
  251.  
  252.         CreateMesh();
  253.  
  254.         m_loaded = true;
  255.  
  256.         return true;
  257.     }
  258.  
  259.     return false;
  260. }
  261.  
  262. void QubicleBinary::GetColour(int matrixIndex, int x, int y, int z, float* r, float* g, float* b, float* a)
  263. {
  264.     unsigned colour = m_pMatrices[matrixIndex].m_pColour[x][y][z];
  265.     unsigned int alpha = (colour & 0xFF000000) >> 24;
  266.     unsigned int blue = (colour & 0x00FF0000) >> 16;
  267.     unsigned int green = (colour & 0x0000FF00) >> 8;
  268.     unsigned int red = (colour & 0x000000FF);
  269.  
  270.     *r = (float)(red / 255.0f);
  271.     *g = (float)(green / 255.0f);
  272.     *b = (float)(blue / 255.0f);
  273.     *a = 1.0f;
  274. }
  275.  
  276. unsigned int QubicleBinary::GetColourCompact(int matrixIndex, int x, int y, int z)
  277. {
  278.     return m_pMatrices[matrixIndex].m_pColour[x][y][z];
  279. }
  280.  
  281. bool QubicleBinary::GetActive(int matrixIndex, int x, int y, int z)
  282. {
  283.     unsigned colour = m_pMatrices[matrixIndex].m_pColour[x][y][z];
  284.     unsigned int alpha = (colour & 0xFF000000) >> 24;
  285.     unsigned int blue = (colour & 0x00FF0000) >> 16;
  286.     unsigned int green = (colour & 0x0000FF00) >> 8;
  287.     unsigned int red = (colour & 0x000000FF);
  288.  
  289.     if(alpha == 0)
  290.     {
  291.         return false;
  292.     }
  293.  
  294.     return true;
  295. }
  296.  
  297. bool IsMergedXNegative(int ***merged, int x, int y, int z) { return (merged[x][y][z] & MergedSide_X_Negative) == MergedSide_X_Negative; }
  298. bool IsMergedXPositive(int ***merged, int x, int y, int z) { return (merged[x][y][z] & MergedSide_X_Positive) == MergedSide_X_Positive; }
  299. bool IsMergedYNegative(int ***merged, int x, int y, int z) { return (merged[x][y][z] & MergedSide_Y_Negative) == MergedSide_Y_Negative; }
  300. bool IsMergedYPositive(int ***merged, int x, int y, int z) { return (merged[x][y][z] & MergedSide_Y_Positive) == MergedSide_Y_Positive; }
  301. bool IsMergedZNegative(int ***merged, int x, int y, int z) { return (merged[x][y][z] & MergedSide_Z_Negative) == MergedSide_Z_Negative; }
  302. bool IsMergedZPositive(int ***merged, int x, int y, int z) { return (merged[x][y][z] & MergedSide_Z_Positive) == MergedSide_Z_Positive; }
  303.  
  304. void QubicleBinary::CreateMesh()
  305. {
  306.     for(unsigned int matrixIndex = 0; matrixIndex < m_numMatrices; matrixIndex++)
  307.     {
  308.         int ***l_merged;
  309.  
  310.         l_merged = new int**[m_pMatrices[matrixIndex].m_matrixSizeX];
  311.  
  312.         for(unsigned int i = 0; i < m_pMatrices[matrixIndex].m_matrixSizeX; i++)
  313.         {
  314.             l_merged[i] = new int*[m_pMatrices[matrixIndex].m_matrixSizeY];
  315.  
  316.             for(unsigned int j = 0; j < m_pMatrices[matrixIndex].m_matrixSizeY; j++)
  317.             {
  318.                 l_merged[i][j] = new int[m_pMatrices[matrixIndex].m_matrixSizeZ];
  319.             }
  320.         }
  321.  
  322.         for(unsigned int x = 0; x < m_pMatrices[matrixIndex].m_matrixSizeX; x++)
  323.         {
  324.             for(unsigned int y = 0; y < m_pMatrices[matrixIndex].m_matrixSizeY; y++)
  325.             {
  326.                 for(unsigned int z = 0; z < m_pMatrices[matrixIndex].m_matrixSizeZ; z++)
  327.                 {
  328.                     l_merged[x][y][z] = MergedSide_None;
  329.                 }
  330.             }
  331.         }
  332.  
  333.         if(m_pMatrices[matrixIndex].m_meshID == -1)
  334.         {
  335.             m_pRenderer->CreateMesh(&m_pMatrices[matrixIndex].m_meshID, OGLMeshType_Textured);
  336.         }
  337.  
  338.         float r = 1.0f;
  339.         float g = 1.0f;
  340.         float b = 1.0f;
  341.         float a = 1.0f;
  342.  
  343.         for(unsigned int x = 0; x < m_pMatrices[matrixIndex].m_matrixSizeX; x++)
  344.         {
  345.             for(unsigned int y = 0; y < m_pMatrices[matrixIndex].m_matrixSizeY; y++)
  346.             {
  347.                 for(unsigned int z = 0; z < m_pMatrices[matrixIndex].m_matrixSizeZ; z++)
  348.                 {
  349.                     if(GetActive(matrixIndex, x, y, z) == false)
  350.                     {
  351.                         continue;
  352.                     }
  353.                     else
  354.                     {
  355.                         GetColour(matrixIndex, x, y, z, &r, &g, &b, &a);
  356.  
  357.                         a = 1.0f;
  358.  
  359.                         Vector3d p1(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  360.                         Vector3d p2(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  361.                         Vector3d p3(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  362.                         Vector3d p4(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  363.                         Vector3d p5(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  364.                         Vector3d p6(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  365.                         Vector3d p7(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  366.                         Vector3d p8(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  367.  
  368.                         Vector3d n1;
  369.                         unsigned int v1, v2, v3, v4;
  370.                         unsigned int t1, t2, t3, t4;
  371.  
  372.                         bool doXPositive = (IsMergedXPositive(l_merged, x, y, z) == false);
  373.                         bool doXNegative = (IsMergedXNegative(l_merged, x, y, z) == false);
  374.                         bool doYPositive = (IsMergedYPositive(l_merged, x, y, z) == false);
  375.                         bool doYNegative = (IsMergedYNegative(l_merged, x, y, z) == false);
  376.                         bool doZPositive = (IsMergedZPositive(l_merged, x, y, z) == false);
  377.                         bool doZNegative = (IsMergedZNegative(l_merged, x, y, z) == false);
  378.  
  379.                         // Front
  380.                         if(doZPositive && ((z == m_pMatrices[matrixIndex].m_matrixSizeZ-1) || z < m_pMatrices[matrixIndex].m_matrixSizeZ-1 && GetActive(matrixIndex, x, y, z+1) == false))
  381.                         {
  382.                             int endX = m_pMatrices[matrixIndex].m_matrixSizeX;
  383.                             int endY = m_pMatrices[matrixIndex].m_matrixSizeY;
  384.  
  385.                             UpdateMergedSide(l_merged, matrixIndex, x, y, z, &p1, &p2, &p3, &p4, x, y, endX, endY, true, true, false, false);
  386.  
  387.                             n1 = Vector3d(0.0f, 0.0f, 1.0f);
  388.                             v1 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p1, n1, r, g, b, a);
  389.                             t1 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 0.0f, 0.0f);
  390.                             v2 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p2, n1, r, g, b, a);
  391.                             t2 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 1.0f, 0.0f);
  392.                             v3 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p3, n1, r, g, b, a);
  393.                             t3 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 1.0f, 1.0f);
  394.                             v4 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p4, n1, r, g, b, a);
  395.                             t4 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 0.0f, 1.0f);
  396.  
  397.                             m_pRenderer->AddTriangleToMesh(m_pMatrices[matrixIndex].m_meshID, v1, v2, v3);
  398.                             m_pRenderer->AddTriangleToMesh(m_pMatrices[matrixIndex].m_meshID, v1, v3, v4);
  399.                         }
  400.  
  401.                         p1 = Vector3d(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  402.                         p2 = Vector3d(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  403.                         p3 = Vector3d(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  404.                         p4 = Vector3d(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  405.                         p5 = Vector3d(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  406.                         p6 = Vector3d(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  407.                         p7 = Vector3d(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  408.                         p8 = Vector3d(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  409.  
  410.                         // Back
  411.                         if(doZNegative && ((z == 0) || (z > 0 && GetActive(matrixIndex, x, y, z-1) == false)))
  412.                         {
  413.                             int endX = m_pMatrices[matrixIndex].m_matrixSizeX;
  414.                             int endY = m_pMatrices[matrixIndex].m_matrixSizeY;
  415.  
  416.                             UpdateMergedSide(l_merged, matrixIndex, x, y, z, &p6, &p5, &p8, &p7, x, y, endX, endY, false, true, false, false);
  417.  
  418.                             n1 = Vector3d(0.0f, 0.0f, -1.0f);
  419.                             v1 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p5, n1, r, g, b, a);
  420.                             t1 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 0.0f, 0.0f);
  421.                             v2 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p6, n1, r, g, b, a);
  422.                             t2 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 1.0f, 0.0f);
  423.                             v3 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p7, n1, r, g, b, a);
  424.                             t3 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 1.0f, 1.0f);
  425.                             v4 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p8, n1, r, g, b, a);
  426.                             t4 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 0.0f, 1.0f);
  427.  
  428.                             m_pRenderer->AddTriangleToMesh(m_pMatrices[matrixIndex].m_meshID, v1, v2, v3);
  429.                             m_pRenderer->AddTriangleToMesh(m_pMatrices[matrixIndex].m_meshID, v1, v3, v4);
  430.                         }
  431.  
  432.                         p1 = Vector3d(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  433.                         p2 = Vector3d(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  434.                         p3 = Vector3d(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  435.                         p4 = Vector3d(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  436.                         p5 = Vector3d(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  437.                         p6 = Vector3d(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  438.                         p7 = Vector3d(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  439.                         p8 = Vector3d(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  440.  
  441.                         // Right
  442.                         if(doXPositive && ((x == m_pMatrices[matrixIndex].m_matrixSizeX-1) || (x < m_pMatrices[matrixIndex].m_matrixSizeX-1 && GetActive(matrixIndex, x+1, y, z) == false)))
  443.                         {
  444.                             int endX = m_pMatrices[matrixIndex].m_matrixSizeZ;
  445.                             int endY = m_pMatrices[matrixIndex].m_matrixSizeY;
  446.  
  447.                             UpdateMergedSide(l_merged, matrixIndex, x, y, z, &p5, &p2, &p3, &p8, z, y, endX, endY, true, false, true, false);
  448.  
  449.                             n1 = Vector3d(1.0f, 0.0f, 0.0f);
  450.                             v1 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p2, n1, r, g, b, a);
  451.                             t1 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 0.0f, 0.0f);
  452.                             v2 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p5, n1, r, g, b, a);
  453.                             t2 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 1.0f, 0.0f);
  454.                             v3 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p8, n1, r, g, b, a);
  455.                             t3 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 1.0f, 1.0f);
  456.                             v4 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p3, n1, r, g, b, a);
  457.                             t4 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 0.0f, 1.0f);
  458.  
  459.                             m_pRenderer->AddTriangleToMesh(m_pMatrices[matrixIndex].m_meshID, v1, v2, v3);
  460.                             m_pRenderer->AddTriangleToMesh(m_pMatrices[matrixIndex].m_meshID, v1, v3, v4);
  461.                         }
  462.  
  463.                         p1 = Vector3d(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  464.                         p2 = Vector3d(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  465.                         p3 = Vector3d(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  466.                         p4 = Vector3d(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  467.                         p5 = Vector3d(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  468.                         p6 = Vector3d(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  469.                         p7 = Vector3d(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  470.                         p8 = Vector3d(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  471.  
  472.                         // Left
  473.                         if(doXNegative && ((x == 0) || (x > 0 && GetActive(matrixIndex, x-1, y, z) == false)))
  474.                         {
  475.                             int endX = m_pMatrices[matrixIndex].m_matrixSizeZ;
  476.                             int endY = m_pMatrices[matrixIndex].m_matrixSizeY;
  477.  
  478.                             UpdateMergedSide(l_merged, matrixIndex, x, y, z, &p6, &p1, &p4, &p7, z, y, endX, endY, false, false, true, false);
  479.  
  480.                             n1 = Vector3d(-1.0f, 0.0f, 0.0f);
  481.                             v1 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p6, n1, r, g, b, a);
  482.                             t1 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 0.0f, 0.0f);
  483.                             v2 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p1, n1, r, g, b, a);
  484.                             t2 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 1.0f, 0.0f);
  485.                             v3 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p4, n1, r, g, b, a);
  486.                             t3 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 1.0f, 1.0f);
  487.                             v4 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p7, n1, r, g, b, a);
  488.                             t4 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 0.0f, 1.0f);
  489.  
  490.                             m_pRenderer->AddTriangleToMesh(m_pMatrices[matrixIndex].m_meshID, v1, v2, v3);
  491.                             m_pRenderer->AddTriangleToMesh(m_pMatrices[matrixIndex].m_meshID, v1, v3, v4);
  492.                         }
  493.  
  494.                         p1 = Vector3d(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  495.                         p2 = Vector3d(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  496.                         p3 = Vector3d(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  497.                         p4 = Vector3d(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  498.                         p5 = Vector3d(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  499.                         p6 = Vector3d(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  500.                         p7 = Vector3d(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  501.                         p8 = Vector3d(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  502.  
  503.                         // Top
  504.                         if(doYPositive && ((y == m_pMatrices[matrixIndex].m_matrixSizeY-1) || (y < m_pMatrices[matrixIndex].m_matrixSizeY-1 && GetActive(matrixIndex, x, y+1, z) == false)))
  505.                         {
  506.                             int endX = m_pMatrices[matrixIndex].m_matrixSizeX;
  507.                             int endY = m_pMatrices[matrixIndex].m_matrixSizeZ;
  508.  
  509.                             UpdateMergedSide(l_merged, matrixIndex, x, y, z, &p7, &p8, &p3, &p4, x, z, endX, endY, true, false, false, true);
  510.  
  511.                             n1 = Vector3d(0.0f, 1.0f, 0.0f);
  512.                             v1 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p4, n1, r, g, b, a);
  513.                             t1 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 0.0f, 0.0f);
  514.                             v2 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p3, n1, r, g, b, a);
  515.                             t2 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 1.0f, 0.0f);
  516.                             v3 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p8, n1, r, g, b, a);
  517.                             t3 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 1.0f, 1.0f);
  518.                             v4 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p7, n1, r, g, b, a);
  519.                             t4 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 0.0f, 1.0f);
  520.  
  521.                             m_pRenderer->AddTriangleToMesh(m_pMatrices[matrixIndex].m_meshID, v1, v2, v3);
  522.                             m_pRenderer->AddTriangleToMesh(m_pMatrices[matrixIndex].m_meshID, v1, v3, v4);
  523.                         }
  524.  
  525.                         p1 = Vector3d(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  526.                         p2 = Vector3d(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  527.                         p3 = Vector3d(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  528.                         p4 = Vector3d(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE);
  529.                         p5 = Vector3d(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  530.                         p6 = Vector3d(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  531.                         p7 = Vector3d(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  532.                         p8 = Vector3d(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE);
  533.  
  534.                         // Bottom
  535.                         if(doYNegative && ((y == 0) || (y > 0 && GetActive(matrixIndex, x, y-1, z) == false)))
  536.                         {
  537.                             int endX = m_pMatrices[matrixIndex].m_matrixSizeX;
  538.                             int endY = m_pMatrices[matrixIndex].m_matrixSizeZ;
  539.  
  540.                             UpdateMergedSide(l_merged, matrixIndex, x, y, z, &p6, &p5, &p2, &p1, x, z, endX, endY, false, false, false, true);
  541.  
  542.                             n1 = Vector3d(0.0f, -1.0f, 0.0f);
  543.                             v1 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p6, n1, r, g, b, a);
  544.                             t1 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 0.0f, 0.0f);
  545.                             v2 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p5, n1, r, g, b, a);
  546.                             t2 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 1.0f, 0.0f);
  547.                             v3 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p2, n1, r, g, b, a);
  548.                             t3 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 1.0f, 1.0f);
  549.                             v4 = m_pRenderer->AddVertexToMesh(m_pMatrices[matrixIndex].m_meshID, p1, n1, r, g, b, a);
  550.                             t4 = m_pRenderer->AddTextureCoordinatesToMesh(m_pMatrices[matrixIndex].m_meshID, 0.0f, 1.0f);
  551.  
  552.                             m_pRenderer->AddTriangleToMesh(m_pMatrices[matrixIndex].m_meshID, v1, v2, v3);
  553.                             m_pRenderer->AddTriangleToMesh(m_pMatrices[matrixIndex].m_meshID, v1, v3, v4);
  554.                         }
  555.                     }
  556.                 }
  557.             }
  558.         }
  559.  
  560.         m_pRenderer->FinishMesh(m_pMatrices[matrixIndex].m_meshID, -1, m_materialID);
  561.  
  562.         // Delete the blocks
  563.         for (unsigned int i = 0; i < m_pMatrices[matrixIndex].m_matrixSizeX; i++)
  564.         {
  565.             for (unsigned int j = 0; j < m_pMatrices[matrixIndex].m_matrixSizeY; j++)
  566.             {
  567.                 delete [] l_merged[i][j];
  568.             }
  569.  
  570.             delete [] l_merged[i];
  571.         }
  572.         delete [] l_merged;
  573.     }
  574. }
  575.  
  576. void QubicleBinary::UpdateMergedSide(int ***merged, int matrixIndex, int blockx, int blocky, int blockz, Vector3d *p1, Vector3d *p2, Vector3d *p3, Vector3d *p4, int startX, int startY, int maxX, int maxY, bool positive, bool zFace, bool xFace, bool yFace)
  577. {
  578.     bool doMore = true;
  579.     unsigned int incrementX = 0;
  580.     unsigned int incrementZ = 0;
  581.     unsigned int incrementY = 0;
  582.  
  583.     int change = 1;
  584.     if(positive == false)
  585.     {
  586.         //change = -1;
  587.     }
  588.  
  589.     if(zFace || yFace)
  590.     {
  591.         incrementX = 1;
  592.         incrementY = 1;
  593.     }
  594.     if(xFace)
  595.     {
  596.         incrementZ = 1;
  597.         incrementY = 1;
  598.     }
  599.  
  600.     // 1st phase
  601.     int incrementer = 1;
  602.     while(doMore)
  603.     {
  604.         if(startX + incrementer >= maxX)
  605.         {
  606.             doMore = false;
  607.         }
  608.         else
  609.         {
  610.             bool doPhase1Merge = true;
  611.             float r1, r2, g1, g2, b1, b2, a1, a2;
  612.             GetColour(matrixIndex, blockx, blocky, blockz, &r1, &g1, &b1, &a1);
  613.             GetColour(matrixIndex, blockx + incrementX, blocky, blockz + incrementZ, &r2, &g2, &b2, &a2);
  614.             //if(m_pBlocks[blockx][blocky][blockz].GetBlockType() != m_pBlocks[blockx + incrementX][blocky][blockz + incrementZ].GetBlockType())
  615.             //{
  616.                 // Don't do any phase 1 merging if we don't have the same block type.
  617.             //  doPhase1Merge = false;
  618.             //  doMore = false;
  619.             //}
  620.             /*//else*/ if((r1 != r2 || g1 != g2 || b1 != b2 || a1 != a2) /*&& allMerge == false*/)
  621.             {
  622.                 // Don't do any phase 1 merging if we don't have the same colour variation
  623.                 doPhase1Merge = false;
  624.                 doMore = false;
  625.             }
  626.             else
  627.             {
  628.                 /*
  629.                 if((xFace && positive && blockx + incrementX+1 == CHUNK_SIZE) ||
  630.                    (xFace && !positive && blockx + incrementX == 0) ||
  631.                    (yFace && positive && blocky+1 == CHUNK_SIZE) ||
  632.                    (yFace && !positive && blocky == 0) ||
  633.                    (zFace && positive && blockz + incrementZ+1 == CHUNK_SIZE) ||
  634.                    (zFace && !positive && blockz + incrementZ == 0))
  635.                 {
  636.                     doPhase1Merge = false;
  637.                     doMore = false;
  638.                 }
  639.                 // Don't do any phase 1 merging if we find an inactive block or already merged block in our path
  640.                 else */if(xFace && positive && (blockx + incrementX+1) < m_pMatrices[matrixIndex].m_matrixSizeX && GetActive(matrixIndex, blockx + incrementX+1, blocky, blockz + incrementZ) == true)
  641.                 {
  642.                     doPhase1Merge = false;
  643.                     doMore = false;
  644.                 }
  645.                 else if(xFace && !positive && (blockx + incrementX) > 0 && GetActive(matrixIndex, blockx + incrementX-1, blocky, blockz + incrementZ) == true)
  646.                 {
  647.                     doPhase1Merge = false;
  648.                     doMore = false;
  649.                 }
  650.                 else if(yFace && positive && (blocky+1) < (int)m_pMatrices[matrixIndex].m_matrixSizeY && GetActive(matrixIndex, blockx + incrementX, blocky+1, blockz + incrementZ) == true)
  651.                 {
  652.                     doPhase1Merge = false;
  653.                     doMore = false;
  654.                 }
  655.                 else if(yFace && !positive && blocky > 0 && GetActive(matrixIndex, blockx + incrementX, blocky-1, blockz + incrementZ) == true)
  656.                 {
  657.                     doPhase1Merge = false;
  658.                     doMore = false;
  659.                 }
  660.                 else if(zFace && positive && (blockz + incrementZ+1) < m_pMatrices[matrixIndex].m_matrixSizeZ && GetActive(matrixIndex, blockx + incrementX, blocky, blockz + incrementZ+1) == true)
  661.                 {
  662.                     doPhase1Merge = false;
  663.                     doMore = false;
  664.                 }
  665.                 else if(zFace && !positive && (blockz + incrementZ) > 0 && GetActive(matrixIndex, blockx + incrementX, blocky, blockz + incrementZ-1) == true)
  666.                 {
  667.                     doPhase1Merge = false;
  668.                     doMore = false;
  669.                 }
  670.                 else if(GetActive(matrixIndex, blockx + incrementX, blocky, blockz + incrementZ) == false)
  671.                 {
  672.                     doPhase1Merge = false;
  673.                     doMore = false;
  674.                 }
  675.                 else
  676.                 {
  677.                     if(xFace)
  678.                     {
  679.                         doPhase1Merge = positive ? (IsMergedXPositive(merged, blockx + incrementX, blocky, blockz + incrementZ) == false) : (IsMergedXNegative(merged, blockx + incrementX, blocky, blockz + incrementZ) == false);
  680.                     }
  681.                     if(zFace)
  682.                     {
  683.                         doPhase1Merge = positive ? (IsMergedZPositive(merged, blockx + incrementX, blocky, blockz + incrementZ) == false) : (IsMergedZNegative(merged, blockx + incrementX, blocky, blockz + incrementZ) == false);
  684.                     }
  685.                     if(yFace)
  686.                     {
  687.                         doPhase1Merge = positive ? (IsMergedYPositive(merged, blockx + incrementX, blocky, blockz + incrementZ) == false) : (IsMergedYNegative(merged, blockx + incrementX, blocky, blockz + incrementZ) == false);
  688.                     }
  689.                 }
  690.  
  691.                 if(doPhase1Merge)
  692.                 {
  693.                     if(zFace || yFace)
  694.                     {
  695.                         (*p2).x += change * (BLOCK_RENDER_SIZE * 2.0f);
  696.                         (*p3).x += change * (BLOCK_RENDER_SIZE * 2.0f);
  697.                     }
  698.                     if(xFace)
  699.                     {
  700.                         (*p2).z += change * (BLOCK_RENDER_SIZE * 2.0f);
  701.                         (*p3).z += change * (BLOCK_RENDER_SIZE * 2.0f);
  702.                     }
  703.  
  704.                     if(positive)
  705.                     {
  706.                         if(zFace)
  707.                         {
  708.                             merged[blockx + incrementX][blocky][blockz + incrementZ] |= MergedSide_Z_Positive;
  709.                         }
  710.                         if(xFace)
  711.                         {
  712.                             merged[blockx + incrementX][blocky][blockz + incrementZ] |= MergedSide_X_Positive;
  713.                         }
  714.                         if(yFace)
  715.                         {
  716.                             merged[blockx + incrementX][blocky][blockz + incrementZ] |= MergedSide_Y_Positive;
  717.                         }
  718.                     }
  719.                     else
  720.                     {
  721.                         if(zFace)
  722.                         {
  723.                             merged[blockx + incrementX][blocky][blockz + incrementZ] |= MergedSide_Z_Negative;
  724.                         }
  725.                         if(xFace)
  726.                         {
  727.                             merged[blockx + incrementX][blocky][blockz + incrementZ] |= MergedSide_X_Negative;
  728.                         }
  729.                         if(yFace)
  730.                         {
  731.                             merged[blockx + incrementX][blocky][blockz + incrementZ] |= MergedSide_Y_Negative;
  732.                         }
  733.                     }
  734.                 }
  735.                 else
  736.                 {
  737.                     doMore = false;
  738.                 }
  739.             }
  740.         }
  741.  
  742.         if(zFace || yFace)
  743.         {
  744.             incrementX += change;
  745.         }
  746.         if(xFace)
  747.         {
  748.             incrementZ += change;
  749.         }
  750.  
  751.         incrementer += change;
  752.     }
  753.  
  754.  
  755.     // 2nd phase
  756.     int loop = incrementer;
  757.     incrementer = 0;
  758.     incrementer = incrementY;
  759.  
  760.     doMore = true;
  761.     while(doMore)
  762.     {
  763.         if(startY + incrementer >= maxY)
  764.         {
  765.             doMore = false;
  766.         }
  767.         else
  768.         {
  769.             for(int i = 0; i < loop-1; i++)
  770.             {
  771.                 // Don't do any phase 2 merging is we have any inactive blocks or already merged blocks on the row
  772.                 if(zFace)
  773.                 {
  774.                     float r1, r2, g1, g2, b1, b2, a1, a2;
  775.                     GetColour(matrixIndex, blockx, blocky, blockz, &r1, &g1, &b1, &a1);
  776.                     GetColour(matrixIndex, blockx + i, blocky + incrementY, blockz, &r2, &g2, &b2, &a2);
  777.  
  778.                     if(positive && (blockz+1) < (int)m_pMatrices[matrixIndex].m_matrixSizeZ && GetActive(matrixIndex, blockx + i, blocky + incrementY, blockz+1) == true)
  779.                     {
  780.                         doMore = false;
  781.                     }
  782.                     else if(!positive && blockz > 0 && GetActive(matrixIndex, blockx + i, blocky + incrementY, blockz-1) == true)
  783.                     {
  784.                         doMore = false;
  785.                     }
  786.                     else if(GetActive(matrixIndex, blockx + i, blocky + incrementY, blockz) == false || (positive ? (IsMergedZPositive(merged, blockx + i, blocky + incrementY, blockz) == true) : (IsMergedZNegative(merged, blockx + i, blocky + incrementY, blockz) == true)))
  787.                     {
  788.                         // Failed active or already merged check
  789.                         doMore = false;
  790.                     }
  791.                     /*else if(m_pBlocks[blockx][blocky][blockz].GetBlockType() != m_pBlocks[blockx + i][blocky + incrementY][blockz].GetBlockType())
  792.                     {
  793.                         // Failed block type check
  794.                         doMore = false;
  795.                     }
  796.                     */
  797.                     else if((r1 != r2 || g1 != g2 || b1 != b2 || a1 != a2) /*&& allMerge == false*/)
  798.                     {
  799.                         // Failed colour check
  800.                         doMore = false;
  801.                     }
  802.                 }
  803.                 if(xFace)
  804.                 {
  805.                     float r1, r2, g1, g2, b1, b2, a1, a2;
  806.                     GetColour(matrixIndex, blockx, blocky, blockz, &r1, &g1, &b1, &a1);
  807.                     GetColour(matrixIndex, blockx, blocky + incrementY, blockz + i, &r2, &g2, &b2, &a2);
  808.  
  809.                     if(positive && (blockx+1) < (int)m_pMatrices[matrixIndex].m_matrixSizeX && GetActive(matrixIndex, blockx+1, blocky + incrementY, blockz + i) == true)
  810.                     {
  811.                         doMore = false;
  812.                     }
  813.                     else if(!positive && (blockx) > 0 && GetActive(matrixIndex, blockx-1, blocky + incrementY, blockz + i) == true)
  814.                     {
  815.                         doMore = false;
  816.                     }
  817.                     else if(GetActive(matrixIndex, blockx, blocky + incrementY, blockz + i) == false || (positive ? (IsMergedXPositive(merged, blockx, blocky + incrementY, blockz + i) == true) : (IsMergedXNegative(merged, blockx, blocky + incrementY, blockz + i) == true)))
  818.                     {
  819.                         // Failed active or already merged check
  820.                         doMore = false;
  821.                     }
  822.                     /*else if(m_pBlocks[blockx][blocky][blockz].GetBlockType() != m_pBlocks[blockx][blocky + incrementY][blockz + i].GetBlockType())
  823.                     {
  824.                         // Failed block type check
  825.                         doMore = false;
  826.                     }
  827.                     */
  828.                     else if((r1 != r2 || g1 != g2 || b1 != b2 || a1 != a2) /*&& allMerge == false*/)
  829.                     {
  830.                         // Failed colour check
  831.                         doMore = false;
  832.                     }
  833.                 }
  834.                 if(yFace)
  835.                 {
  836.                     float r1, r2, g1, g2, b1, b2, a1, a2;
  837.                     GetColour(matrixIndex, blockx, blocky, blockz, &r1, &g1, &b1, &a1);
  838.                     GetColour(matrixIndex, blockx + i, blocky, blockz + incrementY, &r2, &g2, &b2, &a2);
  839.  
  840.                     if(positive && (blocky+1) < (int)m_pMatrices[matrixIndex].m_matrixSizeY && GetActive(matrixIndex, blockx + i, blocky+1, blockz + incrementY) == true)
  841.                     {
  842.                         doMore = false;
  843.                     }
  844.                     else if(!positive && blocky > 0 && GetActive(matrixIndex, blockx + i, blocky-1, blockz + incrementY) == true)
  845.                     {
  846.                         doMore = false;
  847.                     }
  848.                     else if(GetActive(matrixIndex, blockx + i, blocky, blockz + incrementY) == false || (positive ? (IsMergedYPositive(merged, blockx + i, blocky, blockz + incrementY) == true) : (IsMergedYNegative(merged, blockx + i, blocky, blockz + incrementY) == true)))
  849.                     {
  850.                         // Failed active or already merged check
  851.                         doMore = false;
  852.                     }
  853.                     /*else if(m_pBlocks[blockx][blocky][blockz].GetBlockType() != m_pBlocks[blockx + i][blocky][blockz + incrementY].GetBlockType())
  854.                     {
  855.                         // Failed block type check
  856.                         doMore = false;
  857.                     }
  858.                     */
  859.                     else if((r1 != r2 || g1 != g2 || b1 != b2 || a1 != a2) /*&& allMerge == false*/)
  860.                     {
  861.                         // Failed colour check
  862.                         doMore = false;
  863.                     }
  864.                 }
  865.             }
  866.  
  867.             if(doMore == true)
  868.             {
  869.                 if(zFace || xFace)
  870.                 {
  871.                     (*p3).y += change * (BLOCK_RENDER_SIZE * 2.0f);
  872.                     (*p4).y += change * (BLOCK_RENDER_SIZE * 2.0f);
  873.                 }
  874.                 if(yFace)
  875.                 {
  876.                     (*p3).z += change * (BLOCK_RENDER_SIZE * 2.0f);
  877.                     (*p4).z += change * (BLOCK_RENDER_SIZE * 2.0f);
  878.                 }
  879.  
  880.                 for(int i = 0; i < loop-1; i++)
  881.                 {
  882.                     if(positive)
  883.                     {
  884.                         if(zFace)
  885.                         {
  886.                             merged[blockx + i][blocky + incrementY][blockz] |= MergedSide_Z_Positive;
  887.                         }
  888.                         if(xFace)
  889.                         {
  890.                             merged[blockx][blocky + incrementY][blockz + i] |= MergedSide_X_Positive;
  891.                         }
  892.                         if(yFace)
  893.                         {
  894.                             merged[blockx + i][blocky][blockz + incrementY] |= MergedSide_Y_Positive;
  895.                         }
  896.                     }
  897.                     else
  898.                     {
  899.                         if(zFace)
  900.                         {
  901.                             merged[blockx + i][blocky + incrementY][blockz] |= MergedSide_Z_Negative;
  902.                         }
  903.                         if(xFace)
  904.                         {
  905.                             merged[blockx][blocky + incrementY][blockz + i] |= MergedSide_X_Negative;
  906.                         }
  907.                         if(yFace)
  908.                         {
  909.                             merged[blockx + i][blocky][blockz + incrementY] |= MergedSide_Y_Negative;
  910.                         }
  911.                     }
  912.                 }
  913.             }
  914.         }
  915.  
  916.         incrementY += change;
  917.         incrementer += change;
  918.     }
  919. }
  920.  
  921. int QubicleBinary::GetNumMatrices()
  922. {
  923.     return m_numMatrices;
  924. }
  925.  
  926. QubicleMatrix* QubicleBinary::GetQubicleMatrix(int index)
  927. {
  928.     return &m_pMatrices[index];
  929. }
  930.  
  931. const char* QubicleBinary::GetMatrixName(int index)
  932. {
  933.     return m_pMatrices[index].m_name;
  934. }
  935.  
  936. float QubicleBinary::GetMatrixScale(int index)
  937. {
  938.     return m_pMatrices[index].m_scale;
  939. }
  940.  
  941. Vector3d QubicleBinary::GetMatrixOffset(int index)
  942. {
  943.     return Vector3d(m_pMatrices[index].m_offsetX, m_pMatrices[index].m_offsetY, m_pMatrices[index].m_offsetZ);
  944. }
  945.  
  946. void QubicleBinary::SetupMatrixBones(MS3DAnimator* pSkeleton)
  947. {
  948.     for(unsigned int i = 0; i < m_numMatrices; i++)
  949.     {
  950.         int boneIndex = pSkeleton->GetModel()->GetBoneIndex(m_pMatrices[i].m_name);
  951.  
  952.         if(boneIndex != -1)
  953.         {
  954.             m_pMatrices[i].m_boneIndex = boneIndex;
  955.         }
  956.     }
  957. }
  958.  
  959. void QubicleBinary::SetScaleAndOffsetForMatrix(const char* matrixName, float scale, float xOffset, float yOffset, float zOffset)
  960. {
  961.     for(unsigned int i = 0; i < m_numMatrices; i++)
  962.     {
  963.         if(strcmp(m_pMatrices[i].m_name, matrixName) == 0)
  964.         {
  965.             m_pMatrices[i].m_scale = scale;
  966.             m_pMatrices[i].m_offsetX = xOffset;
  967.             m_pMatrices[i].m_offsetY = yOffset;
  968.             m_pMatrices[i].m_offsetZ = zOffset;
  969.         }
  970.     }
  971. }
  972.  
  973. float QubicleBinary::GetScale(const char* matrixName)
  974. {
  975.     for(unsigned int i = 0; i < m_numMatrices; i++)
  976.     {
  977.         if(strcmp(m_pMatrices[i].m_name, matrixName) == 0)
  978.         {
  979.             return m_pMatrices[i].m_scale;
  980.         }
  981.     }
  982.  
  983.     return 1.0f;
  984. }
  985.  
  986. Vector3d QubicleBinary::GetOffset(const char* matrixName)
  987. {
  988.     for(unsigned int i = 0; i < m_numMatrices; i++)
  989.     {
  990.         if(strcmp(m_pMatrices[i].m_name, matrixName) == 0)
  991.         {
  992.             return Vector3d(m_pMatrices[i].m_offsetX, m_pMatrices[i].m_offsetY, m_pMatrices[i].m_offsetZ);
  993.         }
  994.     }
  995.  
  996.     return Vector3d(0.0f, 0.0f, 0.0f);
  997. }
  998.  
  999. void QubicleBinary::SetWireFrameRender(bool wireframe)
  1000. {
  1001.     m_renderWireFrame = wireframe;
  1002. }
  1003.  
  1004. void QubicleBinary::Update(float dt)
  1005. {
  1006.  
  1007. }
  1008.  
  1009. void QubicleBinary::Render(bool renderOutline, bool refelction)
  1010. {
  1011.     m_pRenderer->PushMatrix();
  1012.         for(unsigned int i = 0; i < m_numMatrices; i++)
  1013.         {
  1014.             m_pRenderer->PushMatrix();
  1015.                 // Scale for external matrix scale value
  1016.                 m_pRenderer->ScaleWorldMatrix(m_pMatrices[i].m_scale, m_pMatrices[i].m_scale, m_pMatrices[i].m_scale);
  1017.  
  1018.                 // Translate for initial block offset
  1019.                 m_pRenderer->TranslateWorldMatrix(0.5f, 0.5f, 0.5f);
  1020.  
  1021.                 // Translate to center of model
  1022.                 m_pRenderer->TranslateWorldMatrix(-(float)m_pMatrices[i].m_matrixSizeX*0.5f, -(float)m_pMatrices[i].m_matrixSizeY*0.5f, -(float)m_pMatrices[i].m_matrixSizeZ*0.5f);
  1023.                
  1024.                 // Translate for external matrix offset value
  1025.                 m_pRenderer->TranslateWorldMatrix(m_pMatrices[i].m_offsetX, m_pMatrices[i].m_offsetY, m_pMatrices[i].m_offsetZ);
  1026.  
  1027.                 if(renderOutline)
  1028.                 {
  1029.                     m_pRenderer->DisableDepthTest();
  1030.                     m_pRenderer->SetLineWidth(5.0f);
  1031.                     m_pRenderer->SetCullMode(CM_FRONT);
  1032.                     m_pRenderer->SetRenderMode(RM_WIREFRAME);
  1033.                     m_pRenderer->ImmediateColourAlpha(1.0f, 1.0f, 0.0f, 1.0f);
  1034.                 }
  1035.                 else if(m_renderWireFrame)
  1036.                 {
  1037.                     m_pRenderer->SetLineWidth(1.0f);
  1038.                     m_pRenderer->SetRenderMode(RM_WIREFRAME);
  1039.                 }
  1040.                 else
  1041.                 {
  1042.                     m_pRenderer->SetRenderMode(RM_SOLID);
  1043.                 }
  1044.  
  1045.                 // Store the model matrix
  1046.                 if(refelction == false)
  1047.                 {
  1048.                     m_pRenderer->GetModelMatrix(&m_pMatrices[i].m_modelMatrix);
  1049.                 }
  1050.  
  1051.                 m_pRenderer->PushMatrix();
  1052.                     m_pRenderer->StartMeshRender();
  1053.  
  1054.                     // Texture manipulation (for shadow rendering)
  1055.                     {
  1056.                         Matrix4x4 worldMatrix;
  1057.                         m_pRenderer->GetModelMatrix(&worldMatrix);
  1058.  
  1059.                         m_pRenderer->PushTextureMatrix();
  1060.                         m_pRenderer->MultiplyWorldMatrix(worldMatrix);
  1061.                     }
  1062.  
  1063.                     if(renderOutline)
  1064.                     {
  1065.                         m_pRenderer->EndMeshRender();
  1066.                         m_pRenderer->RenderMesh_NoColour(m_pMatrices[i].m_meshID);
  1067.                     }
  1068.                     else
  1069.                     {
  1070.                         m_pRenderer->MeshStaticBufferRender(m_pMatrices[i].m_meshID);
  1071.                     }
  1072.  
  1073.                     // Texture manipulation (for shadow rendering)
  1074.                     {
  1075.                         m_pRenderer->PopTextureMatrix();
  1076.                     }
  1077.                     m_pRenderer->EndMeshRender();
  1078.                 m_pRenderer->PopMatrix();
  1079.  
  1080.                 if(renderOutline)
  1081.                 {
  1082.                     m_pRenderer->SetCullMode(CM_BACK);
  1083.                     m_pRenderer->EnableDepthTest(DT_LESS);
  1084.                 }
  1085.             m_pRenderer->PopMatrix();
  1086.         }
  1087.     m_pRenderer->PopMatrix();
  1088. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement