Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Chunk.h"
- Chunk::Chunk(int _indexX, int _indexY, int _indexZ, Chunk *_topC, Chunk *_bottomC, Chunk *_rightC, Chunk *_leftC, Chunk *_frontC, Chunk *_backC)
- {
- indexX = _indexX;
- indexY = _indexY;
- indexZ = _indexZ;
- topC = _topC;
- bottomC = _bottomC;
- rightC = _rightC;
- leftC = _leftC;
- frontC = _frontC;
- backC = _backC;
- int x, y, z;
- for(z = 0; z < CHUNK_SIZE; z++)
- {
- for(y = 0; y < CHUNK_SIZE; y++)
- {
- for(x = 0; x < CHUNK_SIZE; x++)
- {
- blockArray[x + y * CHUNK_SIZE + z * CHUNK_SIZE * CHUNK_SIZE] = new Block(0);
- if(indexY< 4)
- blockArray[x + y * CHUNK_SIZE + z * CHUNK_SIZE * CHUNK_SIZE]->hasTransparency = true;
- }
- }
- }
- dispList = memalign(32, DISPLIST_SIZE);
- rebuildDispList();
- }
- Chunk::~Chunk()
- {
- int x, y, z;
- for(z = 0; z < CHUNK_SIZE; z++)
- {
- for(y = 0; y < CHUNK_SIZE; y++)
- {
- for(x = 0; x < CHUNK_SIZE; x++)
- {
- delete blockArray[x + y * CHUNK_SIZE + z * CHUNK_SIZE * CHUNK_SIZE];
- }
- }
- }
- }
- void Chunk::drawBlocks()
- {
- GX_CallDispList(dispList, dispListSize);
- }
- Block *Chunk::getBlockAt(int x, int y, int z)
- {
- if( (x < 0 || x >= CHUNK_SIZE) || (y < 0 || y >= CHUNK_SIZE) || (z < 0 || z >= CHUNK_SIZE)) {
- return NULL;
- }
- return blockArray[x + y * CHUNK_SIZE + z * CHUNK_SIZE * CHUNK_SIZE];
- }
- void Chunk::rebuildDispList()
- {
- memset(dispList, 0x0, DISPLIST_SIZE);
- DCInvalidateRange(dispList, DISPLIST_SIZE);
- GX_BeginDispList(dispList, DISPLIST_SIZE);
- Chunk *p;
- Block *b;
- int x, y, z;
- for(z = 0; z < CHUNK_SIZE; z++)
- {
- for(y = 0; y < CHUNK_SIZE; y++)
- {
- for(x = 0; x < CHUNK_SIZE; x++)
- {
- b = getBlockAt(x, y, z);
- if(b->hasTransparency) {
- continue;
- }
- //Top block neighbour
- if(y < (CHUNK_SIZE-1)) {
- b = getBlockAt(x, y + 1, z);
- if(b->hasTransparency) {
- addFaceTop(x, y, z);
- }
- }else {
- p = topC;
- if(p) {
- b = p->getBlockAt(x, 0, z);
- if(b->hasTransparency) {
- addFaceTop(x, y, z);
- }
- }
- }
- //Bottom block neighbour
- if(y > 0) {
- b = getBlockAt(x, y - 1, z);
- if(b->hasTransparency) {
- addFaceBottom(x, y, z);
- }
- }else {
- p = bottomC;
- if(p) {
- b = p->getBlockAt(x, (CHUNK_SIZE-1), z);
- if(b->hasTransparency) {
- addFaceBottom(x, y, z);
- }
- }
- }
- //Left block neighbour
- if(x > 0) {
- b = getBlockAt(x - 1, y, z);
- if(b->hasTransparency) {
- addFaceLeft(x, y, z);
- }
- }else {
- p = leftC;
- if(p) {
- b = p->getBlockAt((CHUNK_SIZE-1), y, z);
- if(b->hasTransparency) {
- addFaceLeft(x, y, z);
- }
- }
- }
- //Right block neighbour
- if(x < (CHUNK_SIZE-1)) {
- b = getBlockAt(x + 1, y, z);
- if(b->hasTransparency) {
- addFaceRight(x, y, z);
- }
- }else {
- p = rightC;
- if(p) {
- b = p->getBlockAt(0, y, z);
- if(b->hasTransparency) {
- addFaceRight(x, y, z);
- }
- }
- }
- //Front block neighbour
- if(z < (CHUNK_SIZE-1)) {
- b = getBlockAt(x, y, z + 1);
- if(b->hasTransparency) {
- addFaceFront(x, y, z);
- }
- }else {
- p = frontC;
- if(p) {
- b = p->getBlockAt(x, y, 0);
- if(b->hasTransparency) {
- addFaceFront(x, y, z);
- }
- }
- }
- //Back block neighbour
- if(z > 0) {
- b = getBlockAt(x, y, z - 1);
- if(b->hasTransparency) {
- addFaceBack(x, y, z);
- }
- }else {
- p = backC;
- if(p) {
- b = p->getBlockAt(x, y, (CHUNK_SIZE-1));
- if(b->hasTransparency) {
- addFaceBack(x, y, z);
- }
- }
- }
- }
- }
- }
- dispListSize = GX_EndDispList();
- }
- /* vertices 0-7
- 0 GX_Position3f32(x, y, z + BLOCK_SIZE);
- 1 GX_Position3f32(x, y + BLOCK_SIZE, z + BLOCK_SIZE);
- 2 GX_Position3f32(x + BLOCK_SIZE, y, z + BLOCK_SIZE);
- 3 GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z + BLOCK_SIZE);
- 4 GX_Position3f32(x + BLOCK_SIZE, y, z);
- 5 GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z);
- 6 GX_Position3f32(x, y, z);
- 7 GX_Position3f32(x, y + BLOCK_SIZE, z);
- */
- void Chunk::addFaceTop(float x, float y, float z)
- {
- x += indexX * CHUNK_SIZE;
- y += indexY * CHUNK_SIZE;
- z += indexZ * CHUNK_SIZE;
- GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
- GX_Position3f32(x, y + BLOCK_SIZE, z + BLOCK_SIZE);
- GX_Color3f32(1.0f, 0.0f, 0.0f);
- GX_Position3f32(x, y + BLOCK_SIZE, z);
- GX_Color3f32(0.0f, 1.0f, 0.0f);
- GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z + BLOCK_SIZE);
- GX_Color3f32(0.0f, 0.0f, 1.0f);
- GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z);
- GX_Color3f32(1.0f, 0.0f, 1.0f);
- GX_End();
- }
- void Chunk::addFaceBottom(float x, float y, float z)
- {
- x += indexX * CHUNK_SIZE;
- y += indexY * CHUNK_SIZE;
- z += indexZ * CHUNK_SIZE;
- GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
- GX_Position3f32(x, y, z);
- GX_Color3f32(1.0f, 0.0f, 0.0f);
- GX_Position3f32(x, y, z + BLOCK_SIZE);
- GX_Color3f32(0.0f, 1.0f, 0.0f);
- GX_Position3f32(x + BLOCK_SIZE, y, z);
- GX_Color3f32(0.0f, 0.0f, 1.0f);
- GX_Position3f32(x + BLOCK_SIZE, y, z + BLOCK_SIZE);
- GX_Color3f32(1.0f, 0.0f, 1.0f);
- GX_End();
- }
- void Chunk::addFaceRight(float x, float y, float z)
- {
- x += indexX * CHUNK_SIZE;
- y += indexY * CHUNK_SIZE;
- z += indexZ * CHUNK_SIZE;
- GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
- GX_Position3f32(x + BLOCK_SIZE, y, z + BLOCK_SIZE);
- GX_Color3f32(1.0f, 0.0f, 0.0f);
- GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z + BLOCK_SIZE);
- GX_Color3f32(0.0f, 1.0f, 0.0f);
- GX_Position3f32(x + BLOCK_SIZE, y, z);
- GX_Color3f32(0.0f, 0.0f, 1.0f);
- GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z);
- GX_Color3f32(1.0f, 0.0f, 1.0f);
- GX_End();
- }
- void Chunk::addFaceLeft(float x, float y, float z)
- {
- x += indexX * CHUNK_SIZE;
- y += indexY * CHUNK_SIZE;
- z += indexZ * CHUNK_SIZE;
- GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
- GX_Position3f32(x, y, z);
- GX_Color3f32(1.0f, 0.0f, 0.0f);
- GX_Position3f32(x, y + BLOCK_SIZE, z);
- GX_Color3f32(0.0f, 1.0f, 0.0f);
- GX_Position3f32(x, y, z + BLOCK_SIZE);
- GX_Color3f32(0.0f, 0.0f, 1.0f);
- GX_Position3f32(x, y + BLOCK_SIZE, z + BLOCK_SIZE);
- GX_Color3f32(1.0f, 0.0f, 1.0f);
- GX_End();
- }
- void Chunk::addFaceFront(float x, float y, float z)
- {
- x += indexX * CHUNK_SIZE;
- y += indexY * CHUNK_SIZE;
- z += indexZ * CHUNK_SIZE;
- GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
- GX_Position3f32(x, y, z + BLOCK_SIZE);
- GX_Color3f32(1.0f, 0.0f, 0.0f);
- GX_Position3f32(x, y + BLOCK_SIZE, z + BLOCK_SIZE);
- GX_Color3f32(0.0f, 1.0f, 0.0f);
- GX_Position3f32(x + BLOCK_SIZE, y, z + BLOCK_SIZE);
- GX_Color3f32(0.0f, 0.0f, 1.0f);
- GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z + BLOCK_SIZE);
- GX_Color3f32(1.0f, 0.0f, 1.0f);
- GX_End();
- }
- void Chunk::addFaceBack(float x, float y, float z)
- {
- x += indexX * CHUNK_SIZE;
- y += indexY * CHUNK_SIZE;
- z += indexZ * CHUNK_SIZE;
- GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
- GX_Position3f32(x + BLOCK_SIZE, y, z);
- GX_Color3f32(1.0f, 0.0f, 0.0f);
- GX_Position3f32(x + BLOCK_SIZE, y + BLOCK_SIZE, z);
- GX_Color3f32(0.0f, 1.0f, 0.0f);
- GX_Position3f32(x, y, z);
- GX_Color3f32(0.0f, 0.0f, 1.0f);
- GX_Position3f32(x, y + BLOCK_SIZE, z);
- GX_Color3f32(1.0f, 0.0f, 1.0f);
- GX_End();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement