Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //==============================REGION.H==================================
- #ifndef __REGION_H__
- #define __REGION_H__
- #define REGION_DIM 32
- #define REGION_VOLUME REGION_DIM*REGION_DIM*REGION_DIM
- #define FACE_X_POS (uint8)0
- #define FACE_X_NEG (uint8)1
- #define FACE_Y_POS (uint8)2
- #define FACE_Y_NEG (uint8)3
- #define FACE_Z_POS (uint8)4
- #define FACE_Z_NEG (uint8)5
- #include "globals.h"
- class CVBObject;
- class ShaderProgram;
- class World;
- class Region
- {
- private:
- uint16 _blockData[REGION_DIM][REGION_DIM][REGION_DIM]; //Contains a 3D array of uint16s that are the ids of the blocks in the region
- uint8 _visibilityData[REGION_DIM][REGION_DIM][REGION_DIM]; //Contains a 3D array of bytes that hold the visibility data for the faces
- uint8 _x, _y, _z; //The location of the region in the world in region space
- World* _world; //The world that this region is in
- CVBObject* _vertexData; //Contains the vertex data of the region
- CVBObject* _textureData; //Contains the UV coordinate data of the region
- CVBObject* _normalData; //Contains the normal data of the region
- bool _dirty; //If this region needs to have it's mesh rebuilt
- public:
- Region(World* w, uint8 x, uint8 y, uint8 z);
- ~Region();
- bool InBounds(uint8 x, uint8 y, uint8 z) const;
- uint16 GetBlockAt(uint8 x, uint8 y, uint8 z) const;
- void SetBlockAt(uint8 x, uint8 y, uint8 z, uint16 id);
- uint8 GetVisibilityAt(uint8 x, uint8 y, uint8 z) const;
- void SetVisibilityAt(uint8 x, uint8 y, uint8 z, uint8 face, bool visible);
- bool IsVisible(uint8 x, uint8 y, uint8 z, uint8 face) const;
- void Rebuild();
- void UpdateVisibility(uint8 x, uint8 y, uint8 z);
- unsigned int GetNumVertices();
- void Update();
- void Render();
- };
- #endif
- //=======================================REGION.CPP========================================
- #include "region.h"
- #include "world.h"
- #include <GL\glew.h>
- #include "block\block.h"
- #include "graphics\cvbo.h"
- #include "graphics\shader_program.h"
- #include "graphics\camera.h"
- Region::Region(World* w, uint8 x, uint8 y, uint8 z)
- {
- //Reset world/visibility data
- for (uint8 x = 0; x < REGION_DIM; x++)
- for (uint8 y = 0; y < REGION_DIM; y++)
- for (uint8 z = 0; z < REGION_DIM; z++)
- SetBlockAt(x, y, z, 0);
- World::BlockRenderProgram->SetActive();
- _vertexData = new CVBObject(); //Also creates VAO
- _vertexData->CreateVBO();
- _textureData = new CVBObject(_vertexData->getVAO());
- _textureData->CreateVBO();
- _vertexData->BindVAO();
- printf("Using VAO %u.\n", _vertexData->getVAO());
- _vertexData->BindVBO();
- World::BlockRenderProgram->EnableVertexAttribArray("vertexPosition");
- World::BlockRenderProgram->VertexAttribArrayPointer("vertexPosition", 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
- _textureData->BindVBO();
- World::BlockRenderProgram->EnableVertexAttribArray("vertexUV");
- World::BlockRenderProgram->VertexAttribArrayPointer("vertexUV", 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
- _textureData->UnbindVAO();
- _dirty = true;
- _x = x;
- _y = y;
- _z = z;
- _world = w;
- //TODO load/create world data
- Rebuild();
- fprintf(stdout, "Region created at: (%u, %u, %u).\n", (uint32)_x, (uint32)_y, (uint32)_z);
- }
- Region::~Region()
- {
- //TODO: Add world saving on region unload
- //Deallocate memory devoted to world data
- delete [] _blockData;
- delete [] _visibilityData;
- _vertexData->ReleaseVBO();
- _textureData->ReleaseVBO();
- _normalData->ReleaseVBO();
- delete _vertexData;
- delete _textureData;
- delete _normalData;
- }
- void Region::Render()
- {
- if (_dirty)
- Rebuild();
- glEnable(GL_TEXTURE_2D);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glEnable(GL_CULL_FACE);
- glFrontFace(GL_CW);
- glCullFace(GL_BACK);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, g_blockAtlasTextureId);
- _vertexData->BindVAO();
- glDrawArrays(GL_TRIANGLES, 0, _vertexData->GetDataSize() / 12);
- _vertexData->UnbindVAO();
- glDisable(GL_DEPTH_TEST);
- glDisable(GL_BLEND);
- glDisable(GL_CULL_FACE);
- glDisable(GL_TEXTURE_2D);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement