SHARE
TWEET

region.h/.cpp

a guest Oct 30th, 2013 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //==============================REGION.H==================================
  2. #ifndef __REGION_H__
  3. #define __REGION_H__
  4.  
  5. #define REGION_DIM 32
  6. #define REGION_VOLUME REGION_DIM*REGION_DIM*REGION_DIM
  7.  
  8. #define FACE_X_POS (uint8)0
  9. #define FACE_X_NEG (uint8)1
  10. #define FACE_Y_POS (uint8)2
  11. #define FACE_Y_NEG (uint8)3
  12. #define FACE_Z_POS (uint8)4
  13. #define FACE_Z_NEG (uint8)5
  14.  
  15. #include "globals.h"
  16.  
  17. class CVBObject;
  18. class ShaderProgram;
  19. class World;
  20.  
  21. class Region
  22. {
  23. private:
  24.         uint16 _blockData[REGION_DIM][REGION_DIM][REGION_DIM]; //Contains a 3D array of uint16s that are the ids of the blocks in the region
  25.         uint8 _visibilityData[REGION_DIM][REGION_DIM][REGION_DIM]; //Contains a 3D array of bytes that hold the visibility data for the faces
  26.  
  27.         uint8 _x, _y, _z; //The location of the region in the world in region space
  28.         World* _world; //The world that this region is in
  29.  
  30.         CVBObject* _vertexData; //Contains the vertex data of the region
  31.         CVBObject* _textureData; //Contains the UV coordinate data of the region
  32.         CVBObject* _normalData; //Contains the normal data of the region
  33.         bool _dirty; //If this region needs to have it's mesh rebuilt
  34.  
  35. public:
  36.         Region(World* w, uint8 x, uint8 y, uint8 z);
  37.         ~Region();
  38.  
  39.         bool InBounds(uint8 x, uint8 y, uint8 z) const;
  40.         uint16 GetBlockAt(uint8 x, uint8 y, uint8 z) const;
  41.         void SetBlockAt(uint8 x, uint8 y, uint8 z, uint16 id);
  42.         uint8 GetVisibilityAt(uint8 x, uint8 y, uint8 z) const;
  43.         void SetVisibilityAt(uint8 x, uint8 y, uint8 z, uint8 face, bool visible);
  44.         bool IsVisible(uint8 x, uint8 y, uint8 z, uint8 face) const;
  45.  
  46.         void Rebuild();
  47.         void UpdateVisibility(uint8 x, uint8 y, uint8 z);
  48.         unsigned int GetNumVertices();
  49.  
  50.         void Update();
  51.         void Render();
  52. };
  53.  
  54. #endif
  55.  
  56. //=======================================REGION.CPP========================================
  57. #include "region.h"
  58. #include "world.h"
  59.  
  60. #include <GL\glew.h>
  61.  
  62. #include "block\block.h"
  63. #include "graphics\cvbo.h"
  64. #include "graphics\shader_program.h"
  65. #include "graphics\camera.h"
  66.  
  67. Region::Region(World* w, uint8 x, uint8 y, uint8 z)
  68. {
  69.         //Reset world/visibility data
  70.         for (uint8 x = 0; x < REGION_DIM; x++)
  71.                 for (uint8 y = 0; y < REGION_DIM; y++)
  72.                         for (uint8 z = 0; z < REGION_DIM; z++)
  73.                                 SetBlockAt(x, y, z, 0);
  74.  
  75.         World::BlockRenderProgram->SetActive();
  76.  
  77.         _vertexData = new CVBObject(); //Also creates VAO
  78.         _vertexData->CreateVBO();
  79.         _textureData = new CVBObject(_vertexData->getVAO());
  80.         _textureData->CreateVBO();
  81.  
  82.         _vertexData->BindVAO();
  83.         printf("Using VAO %u.\n", _vertexData->getVAO());
  84.         _vertexData->BindVBO();
  85.         World::BlockRenderProgram->EnableVertexAttribArray("vertexPosition");
  86.         World::BlockRenderProgram->VertexAttribArrayPointer("vertexPosition", 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  87.         _textureData->BindVBO();
  88.         World::BlockRenderProgram->EnableVertexAttribArray("vertexUV");
  89.         World::BlockRenderProgram->VertexAttribArrayPointer("vertexUV", 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
  90.         _textureData->UnbindVAO();
  91.  
  92.         _dirty = true;
  93.  
  94.         _x = x;
  95.         _y = y;
  96.         _z = z;
  97.         _world = w;
  98.  
  99.         //TODO load/create world data
  100.  
  101.         Rebuild();
  102.  
  103.         fprintf(stdout, "Region created at: (%u, %u, %u).\n", (uint32)_x, (uint32)_y, (uint32)_z);
  104. }
  105.  
  106. Region::~Region()
  107. {
  108.         //TODO: Add world saving on region unload
  109.  
  110.         //Deallocate memory devoted to world data
  111.         delete [] _blockData;
  112.         delete [] _visibilityData;
  113.  
  114.         _vertexData->ReleaseVBO();
  115.         _textureData->ReleaseVBO();
  116.         _normalData->ReleaseVBO();
  117.  
  118.         delete _vertexData;
  119.         delete _textureData;
  120.         delete _normalData;
  121. }
  122.  
  123. void Region::Render()
  124. {
  125.         if (_dirty)
  126.                 Rebuild();
  127.  
  128.         glEnable(GL_TEXTURE_2D);
  129.         glEnable(GL_DEPTH_TEST);
  130.         glEnable(GL_BLEND);
  131.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  132.         glEnable(GL_CULL_FACE);
  133.         glFrontFace(GL_CW);
  134.         glCullFace(GL_BACK);
  135.  
  136.         glActiveTexture(GL_TEXTURE0);
  137.         glBindTexture(GL_TEXTURE_2D, g_blockAtlasTextureId);
  138.  
  139.         _vertexData->BindVAO();
  140.         glDrawArrays(GL_TRIANGLES, 0, _vertexData->GetDataSize() / 12);
  141.         _vertexData->UnbindVAO();
  142.  
  143.         glDisable(GL_DEPTH_TEST);
  144.         glDisable(GL_BLEND);
  145.         glDisable(GL_CULL_FACE);
  146.         glDisable(GL_TEXTURE_2D);
  147. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top