1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef PATCH_H
#define PATCH_H
#include "Rendering/GL/myGL.h"
#include "System/Rectangle.h"
#include "System/type2.h"
#include <vector>
class CSMFGroundDrawer;
class CCamera;
// How many heightmap pixels a patch consists of
#define PATCH_SIZE 128
// Depth of variance tree: should be near SQRT(PATCH_SIZE) + 1
#define VARIANCE_DEPTH (12)
// How many TriTreeNodes should be allocated?
#define POOL_SIZE (500000)
/**
* TriTreeNode Struct
* Store the triangle tree data, but no coordinates!
*/
struct TriTreeNode
{
TriTreeNode()
: LeftChild(nullptr)
, RightChild(nullptr)
, BaseNeighbor(nullptr)
, LeftNeighbor(nullptr)
, RightNeighbor(nullptr)
{}
bool IsLeaf() const {
// All non-leaf nodes have both children, so just check for one
return (LeftChild == nullptr);
}
bool IsBranch() const {
// All non-leaf nodes have both children, so just check for one
return (RightChild != nullptr);
}
TriTreeNode* LeftChild;
TriTreeNode* RightChild;
TriTreeNode* BaseNeighbor;
TriTreeNode* LeftNeighbor;
TriTreeNode* RightNeighbor;
};
/**
* CTriNodePool class
* Allocs a pool of TriTreeNodes, so we can reconstruct the whole tree w/o to dealloc the old nodes.
* InitPools() creates for each worker thread its own pool to avoid locking.
*/
class CTriNodePool
{
public:
static void InitPools(bool shadowPass, size_t newPoolSize = POOL_SIZE);
static void FreePools(bool shadowPass);
static void ResetAll(bool shadowPass);
inline static CTriNodePool* GetPool(bool shadowPass);
public:
CTriNodePool(const size_t poolSize);
void Reset();
void Allocate(TriTreeNode*& left, TriTreeNode*& right);
bool OutOfNodes() const {
return (m_NextTriNode >= pool.size());
}
private:
std::vector<TriTreeNode> pool;
size_t m_NextTriNode; //< Index to next free TriTreeNode
};
/**
* Patch Class
* Store information needed at the Patch level
*/
class Patch
{
public:
enum RenderMode {
VBO = 1,
DL = 2,
VA = 3
};
public:
friend class CRoamMeshDrawer;
friend class CPatchInViewChecker;
Patch();
~Patch();
void Init(CSMFGroundDrawer* drawer, int worldX, int worldZ); //FIXME move this into the ctor
void Reset();
TriTreeNode* GetBaseLeft() { return &baseLeft; }
TriTreeNode* GetBaseRight() { return &baseRight; }
bool IsVisible(const CCamera*) const;
char IsDirty() const { return isDirty; }
int GetTriCount() const { return (indices.size() / 3); }
void UpdateHeightMap(const SRectangle& rect = SRectangle(0, 0, PATCH_SIZE, PATCH_SIZE));
bool Tessellate(const float3& campos, int viewradius, bool shadowPass);
void ComputeVariance();
void GenerateIndices();
void Upload();
void Draw();
void DrawBorder();
void SetSquareTexture() const;
public:
static void SwitchRenderMode(int mode = -1);
void UpdateVisibility(CCamera* cam);
static void UpdateVisibility(CCamera* cam, std::vector<Patch>& patches, const int numPatchesX);
protected:
void VBOUploadVertices();
private:
// recursive functions
void Split(TriTreeNode* tri);
void RecursTessellate(TriTreeNode* tri, const int2 left, const int2 right, const int2 apex, const int node);
void RecursRender(const TriTreeNode* tri, const int2 left, const int2 right, const int2 apex);
float RecursComputeVariance(
const int2 left,
const int2 rght,
const int2 apex,
const float3 hgts,
const int node
);
void RecursBorderRender(
CVertexArray* va,
const TriTreeNode* tri,
const int2 left,
const int2 rght,
const int2 apex,
int depth,
bool leftChild
);
float GetHeight(int2 pos);
void GenerateBorderIndices(CVertexArray* va);
private:
static RenderMode renderMode;
CSMFGroundDrawer* smfGroundDrawer;
//< Which variance we are currently using. [Only valid during the Tessellate and ComputeVariance passes]
float* currentVariance;
CTriNodePool* currentPool;
//< Does the Variance Tree need to be recalculated for this Patch?
bool isDirty;
bool vboVerticesUploaded;
float varianceMaxLimit;
float camDistLODFactor; //< defines the LOD falloff in camera distance
//< World coordinate offsets of this patch.
int2 coors;
TriTreeNode baseLeft; //< Left base triangle tree node
TriTreeNode baseRight; //< Right base triangle tree node
std::vector<float> varianceLeft; //< Left variance tree
std::vector<float> varianceRight; //< Right variance tree
// TODO: remove for both the Displaylist and the VBO implementations (only really needed for VA's)
std::vector<float> vertices;
std::vector<unsigned int> indices;
//< frame on which this patch was last visible, per pass
std::vector<unsigned int> lastDrawFrames;
GLuint triList;
GLuint vertexBuffer;
GLuint vertexIndexBuffer;
};
#endif
|