Guest User

Untitled

a guest
Apr 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. /**
  2.  * Chunk class, 32x32x32 section of blocks
  3.  */
  4.  
  5. #ifndef __chunk_h__
  6. #define __chunk_h__
  7.  
  8. #include "Block.h"
  9. #include <vector>
  10. #include <iostream>
  11.  
  12. #define CHUNK_SIZE 16
  13. #define C_SURFACE_AREA 256
  14. #define stp(val) GLfloat(val) - CHUNK_SIZE/2.0f - B_HALF_SIZE
  15.  
  16. /// A pair of indices, makes up a row of non-null blocks
  17. typedef struct _indicePair {
  18.     int start, end;
  19. } IndicePair;
  20.  
  21. /// A quad of indices, makes up a quad of non-null blocks
  22. /// which will then be used to construct the merged face
  23. typedef struct _indiceQuad {
  24.     int startX, startY;
  25.     int endX, endY;
  26. } IndiceQuad;
  27.  
  28. /// Test whether one pair ascends from the next.
  29. /// eg. If chunk size is 16, then the pair (18, 24)
  30. /// is an ascension of (2, 8).
  31. #define IPAIR_Ascension(p1, p2) ( p1.start == p2.start + CHUNK_SIZE &&  \
  32.                                   p1.end == p2.end + CHUNK_SIZE )
  33.  
  34. /// Helper class for face merging
  35. class CPoint {
  36. public:
  37.     CPoint() { }
  38.    
  39.     CPoint(GLfloat x, GLfloat y, GLfloat z) {
  40.         pts[0] = x;
  41.         pts[1] = y;
  42.         pts[2] = z;
  43.     }
  44.  
  45.     GLfloat* arr(void) { return pts; }
  46.  
  47. private:
  48.     GLfloat pts[3];
  49. };
  50.  
  51. /// Merges adjacent blocks
  52. /// on the same level into one large face for
  53. /// performance improvements.
  54. typedef struct _mgFace {
  55.     CPoint pointList[4];
  56.     int xr, yr; // num times to repeat texture
  57. } MergedFace;
  58.  
  59. class Chunk {
  60. public:
  61.     Chunk();
  62.  
  63.     /// Build the chunk
  64.     void buildChunk(void);
  65.     /// Draw all blocks in the chunk
  66.     void drawAll(void);
  67.     /// Cleans up all blocks within the chunk
  68.     void destroyChunk(void);
  69.     /// Build merged faces
  70.     void buildMergedFaces(void);
  71.  
  72. private:
  73.     /// Draws a single block in the chunk
  74.     void draw(Block* block, int i, int j, int k);
  75.     /// Determines whether a block is on the edge of a chunk
  76.     bool isEdgeBlock(int x, int y, int z);
  77.     /// Construct merged face
  78.     void constructMergedFace(int startA, int sizeA, int startB, int sizeB, int orientation, int depth);
  79.     /// Split off a row into a list of paired indices (start and end)
  80.     std::vector<IndicePair> getSplitRow(std::vector<int> values);
  81.  
  82.     Block* blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
  83.     std::vector<MergedFace> mergeList;
  84. };
  85.  
  86. #endif
Add Comment
Please, Sign In to add comment