Advertisement
xerpi

Chunk faces working

Jul 16th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.01 KB | None | 0 0
  1. #include "Chunk.h"
  2.  
  3.  
  4.  
  5. Chunk::Chunk(int _indexX, int _indexY, int _indexZ, Chunk *_topC, Chunk *_bottomC, Chunk *_rightC, Chunk *_leftC, Chunk *_frontC, Chunk *_backC)
  6. {
  7.    indexX = _indexX;
  8.    indexY = _indexY;
  9.    indexZ = _indexZ;
  10.  
  11.    topC    = _topC;
  12.    bottomC = _bottomC;
  13.    rightC  = _rightC;
  14.    leftC   = _leftC;
  15.    frontC  = _frontC;
  16.    backC   = _backC;
  17.  
  18.    int x, y, z;
  19.    for(z = 0; z < CHUNK_SIZE; z++)
  20.    {
  21.       for(y = 0; y < CHUNK_SIZE; y++)
  22.       {
  23.          for(x = 0; x < CHUNK_SIZE; x++)
  24.          {
  25.             blockArray[x + y * CHUNK_SIZE + z * CHUNK_SIZE * CHUNK_SIZE] = new Block(0);
  26.                         if(indexY< 4)
  27.                blockArray[x + y * CHUNK_SIZE + z * CHUNK_SIZE * CHUNK_SIZE]->hasTransparency = true;
  28.          }
  29.       }
  30.    }
  31.  
  32.    dispList = memalign(32, DISPLIST_SIZE);
  33.    rebuildDispList();
  34. }
  35.  
  36.  
  37. Chunk::~Chunk()
  38. {
  39.    int x, y, z;
  40.    for(z = 0; z < CHUNK_SIZE; z++)
  41.    {
  42.       for(y = 0; y < CHUNK_SIZE; y++)
  43.       {
  44.          for(x = 0; x < CHUNK_SIZE; x++)
  45.          {
  46.             delete blockArray[x + y * CHUNK_SIZE + z * CHUNK_SIZE * CHUNK_SIZE];
  47.          }
  48.       }
  49.    }
  50. }
  51.  
  52.  
  53. void Chunk::drawBlocks()
  54. {
  55.    GX_CallDispList(dispList, dispListSize);
  56. }
  57.  
  58.  
  59.  
  60. Block *Chunk::getBlockAt(int x, int y, int z)
  61. {
  62.    if( (x < 0 || x >= CHUNK_SIZE) || (y < 0 || y >= CHUNK_SIZE) || (z < 0 || z >= CHUNK_SIZE)) {
  63.       return NULL;
  64.    }
  65.    return blockArray[x + y * CHUNK_SIZE + z * CHUNK_SIZE * CHUNK_SIZE];
  66. }
  67.  
  68.  
  69. void Chunk::rebuildDispList()
  70. {
  71.    memset(dispList, 0x0, DISPLIST_SIZE);
  72.    DCInvalidateRange(dispList, DISPLIST_SIZE);
  73.  
  74.    GX_BeginDispList(dispList, DISPLIST_SIZE);
  75.  
  76.    Chunk *p;
  77.    Block *b;
  78.    int x, y, z;
  79.    for(z = 0; z < CHUNK_SIZE; z++)
  80.    {
  81.       for(y = 0; y < CHUNK_SIZE; y++)
  82.       {
  83.          for(x = 0; x < CHUNK_SIZE; x++)
  84.          {
  85.             b = getBlockAt(x, y, z);
  86.             if(b->hasTransparency) {
  87.                continue;
  88.             }
  89.             //Top block neighbour
  90.                if(y < (CHUNK_SIZE-1)) {
  91.                   b = getBlockAt(x, y + 1, z);
  92.                   if(b->hasTransparency) {
  93.                      addFaceTop(x, y, z);
  94.                   }
  95.                }else {
  96.                   p = topC;
  97.                   if(p) {
  98.                      b = p->getBlockAt(x, 0, z);
  99.                      if(b->hasTransparency) {
  100.                         addFaceTop(x, y, z);
  101.                      }
  102.                   }
  103.                }
  104.             //Bottom block neighbour
  105.                if(y > 0) {
  106.                   b = getBlockAt(x, y - 1, z);
  107.                   if(b->hasTransparency) {
  108.                      addFaceBottom(x, y, z);
  109.                   }
  110.                }else {
  111.                   p = bottomC;
  112.                   if(p) {
  113.                      b = p->getBlockAt(x, (CHUNK_SIZE-1), z);
  114.                      if(b->hasTransparency) {
  115.                         addFaceBottom(x, y, z);
  116.                      }
  117.                   }
  118.                }
  119.  
  120.             //Left block neighbour
  121.                if(x > 0) {
  122.                   b = getBlockAt(x - 1, y, z);
  123.                   if(b->hasTransparency) {
  124.                      addFaceLeft(x, y, z);
  125.                   }
  126.                }else {
  127.                   p = leftC;
  128.                   if(p) {
  129.                      b = p->getBlockAt((CHUNK_SIZE-1), y, z);
  130.                      if(b->hasTransparency) {
  131.                         addFaceLeft(x, y, z);
  132.                      }
  133.                   }
  134.                }
  135.             //Right block neighbour
  136.                if(x < (CHUNK_SIZE-1)) {
  137.                   b = getBlockAt(x + 1, y, z);
  138.                   if(b->hasTransparency) {
  139.                      addFaceRight(x, y, z);
  140.                   }
  141.                }else {
  142.                   p = rightC;
  143.                   if(p) {
  144.                      b = p->getBlockAt(0, y, z);
  145.                      if(b->hasTransparency) {
  146.                         addFaceRight(x, y, z);
  147.                      }
  148.                   }
  149.                }
  150.             //Front block neighbour
  151.                if(z < (CHUNK_SIZE-1)) {
  152.                   b = getBlockAt(x, y, z + 1);
  153.                   if(b->hasTransparency) {
  154.                      addFaceFront(x, y, z);
  155.                   }
  156.                }else {
  157.                   p = frontC;
  158.                   if(p) {
  159.                      b = p->getBlockAt(x, y, 0);
  160.                      if(b->hasTransparency) {
  161.                         addFaceFront(x, y, z);
  162.                      }
  163.                   }
  164.                }
  165.             //Back block neighbour
  166.                if(z > 0) {
  167.                   b = getBlockAt(x, y, z - 1);
  168.                   if(b->hasTransparency) {
  169.                      addFaceBack(x, y, z);
  170.                   }
  171.                }else {
  172.                   p = backC;
  173.                   if(p) {
  174.                      b = p->getBlockAt(x, y, (CHUNK_SIZE-1));
  175.                      if(b->hasTransparency) {
  176.                         addFaceBack(x, y, z);
  177.                      }
  178.                   }
  179.                }
  180.          }
  181.       }
  182.    }
  183.  
  184.    dispListSize = GX_EndDispList();
  185. }
  186.  
  187. /* vertices 0-7
  188.  
  189. 0      GX_Position3f32(x, y, z + BLOCK_SIZE);
  190. 1      GX_Position3f32(x, y + BLOCK_SIZE, z + BLOCK_SIZE);
  191. 2      GX_Position3f32(x + BLOCK_SIZE, y, z + BLOCK_SIZE);
  192. 3      GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z + BLOCK_SIZE);
  193. 4      GX_Position3f32(x + BLOCK_SIZE, y, z);
  194. 5      GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z);
  195. 6      GX_Position3f32(x, y, z);
  196. 7      GX_Position3f32(x, y + BLOCK_SIZE, z);
  197.  
  198. */
  199.  
  200.  
  201. void Chunk::addFaceTop(float x, float y, float z)
  202. {
  203.    x += indexX * CHUNK_SIZE;
  204.    y += indexY * CHUNK_SIZE;
  205.    z += indexZ * CHUNK_SIZE;
  206.  
  207.    GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
  208.       GX_Position3f32(x, y + BLOCK_SIZE, z + BLOCK_SIZE);
  209.          GX_Color3f32(1.0f, 0.0f, 0.0f);
  210.       GX_Position3f32(x, y + BLOCK_SIZE, z);
  211.          GX_Color3f32(0.0f, 1.0f, 0.0f);
  212.       GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z + BLOCK_SIZE);
  213.          GX_Color3f32(0.0f, 0.0f, 1.0f);
  214.       GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z);
  215.          GX_Color3f32(1.0f, 0.0f, 1.0f);
  216.    GX_End();
  217. }
  218.  
  219.  
  220. void Chunk::addFaceBottom(float x, float y, float z)
  221. {
  222.    x += indexX * CHUNK_SIZE;
  223.    y += indexY * CHUNK_SIZE;
  224.    z += indexZ * CHUNK_SIZE;
  225.  
  226.    GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
  227.       GX_Position3f32(x, y, z);
  228.          GX_Color3f32(1.0f, 0.0f, 0.0f);
  229.       GX_Position3f32(x, y, z + BLOCK_SIZE);
  230.          GX_Color3f32(0.0f, 1.0f, 0.0f);
  231.       GX_Position3f32(x + BLOCK_SIZE, y, z);
  232.          GX_Color3f32(0.0f, 0.0f, 1.0f);
  233.       GX_Position3f32(x + BLOCK_SIZE, y, z + BLOCK_SIZE);
  234.          GX_Color3f32(1.0f, 0.0f, 1.0f);
  235.    GX_End();
  236. }
  237.  
  238.  
  239. void Chunk::addFaceRight(float x, float y, float z)
  240. {
  241.    x += indexX * CHUNK_SIZE;
  242.    y += indexY * CHUNK_SIZE;
  243.    z += indexZ * CHUNK_SIZE;
  244.  
  245.    GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
  246.       GX_Position3f32(x + BLOCK_SIZE, y, z + BLOCK_SIZE);
  247.          GX_Color3f32(1.0f, 0.0f, 0.0f);
  248.       GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z + BLOCK_SIZE);
  249.          GX_Color3f32(0.0f, 1.0f, 0.0f);
  250.       GX_Position3f32(x + BLOCK_SIZE, y, z);
  251.          GX_Color3f32(0.0f, 0.0f, 1.0f);
  252.       GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z);
  253.          GX_Color3f32(1.0f, 0.0f, 1.0f);
  254.    GX_End();
  255. }
  256.  
  257.  
  258. void Chunk::addFaceLeft(float x, float y, float z)
  259. {
  260.    x += indexX * CHUNK_SIZE;
  261.    y += indexY * CHUNK_SIZE;
  262.    z += indexZ * CHUNK_SIZE;
  263.  
  264.  
  265.    GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
  266.       GX_Position3f32(x, y, z);
  267.          GX_Color3f32(1.0f, 0.0f, 0.0f);
  268.       GX_Position3f32(x, y + BLOCK_SIZE, z);
  269.          GX_Color3f32(0.0f, 1.0f, 0.0f);
  270.       GX_Position3f32(x, y, z + BLOCK_SIZE);
  271.          GX_Color3f32(0.0f, 0.0f, 1.0f);
  272.       GX_Position3f32(x, y + BLOCK_SIZE, z + BLOCK_SIZE);
  273.          GX_Color3f32(1.0f, 0.0f, 1.0f);
  274.    GX_End();
  275. }
  276.  
  277.  
  278. void Chunk::addFaceFront(float x, float y, float z)
  279. {
  280.    x += indexX * CHUNK_SIZE;
  281.    y += indexY * CHUNK_SIZE;
  282.    z += indexZ * CHUNK_SIZE;
  283.  
  284.    GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
  285.       GX_Position3f32(x, y, z + BLOCK_SIZE);
  286.          GX_Color3f32(1.0f, 0.0f, 0.0f);
  287.       GX_Position3f32(x, y + BLOCK_SIZE, z + BLOCK_SIZE);
  288.          GX_Color3f32(0.0f, 1.0f, 0.0f);
  289.       GX_Position3f32(x + BLOCK_SIZE, y, z + BLOCK_SIZE);
  290.          GX_Color3f32(0.0f, 0.0f, 1.0f);
  291.       GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z + BLOCK_SIZE);
  292.          GX_Color3f32(1.0f, 0.0f, 1.0f);
  293.    GX_End();
  294. }
  295.  
  296.  
  297. void Chunk::addFaceBack(float x, float y, float z)
  298. {
  299.    x += indexX * CHUNK_SIZE;
  300.    y += indexY * CHUNK_SIZE;
  301.    z += indexZ * CHUNK_SIZE;
  302.    GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
  303.       GX_Position3f32(x + BLOCK_SIZE, y, z);
  304.          GX_Color3f32(1.0f, 0.0f, 0.0f);
  305.       GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z);
  306.          GX_Color3f32(0.0f, 1.0f, 0.0f);
  307.       GX_Position3f32(x, y, z);
  308.          GX_Color3f32(0.0f, 0.0f, 1.0f);
  309.       GX_Position3f32(x, y + BLOCK_SIZE, z);
  310.          GX_Color3f32(1.0f, 0.0f, 1.0f);
  311.    GX_End();
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement