1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ 2 3 #ifndef VERTEXARRAY_H 4 #define VERTEXARRAY_H 5 6 #include "myGL.h" 7 #include "System/Platform/errorhandler.h" 8 #include "System/Color.h" 9 10 #define VA_INIT_VERTEXES 1000 // please don't change this, some files rely on specific initial sizes 11 #define VA_INIT_STRIPS 100 12 13 14 struct VA_TYPE_0 { 15 float3 p; 16 }; 17 struct VA_TYPE_N { 18 float3 p; 19 float3 n; 20 }; 21 struct VA_TYPE_C { 22 float3 p; 23 SColor c; 24 }; 25 struct VA_TYPE_T { 26 float3 p; 27 float s, t; 28 }; 29 struct VA_TYPE_TN { 30 float3 p; 31 float s, t; 32 float3 n; 33 }; 34 struct VA_TYPE_TC { 35 float3 p; 36 float s, t; 37 SColor c; 38 }; 39 struct VA_TYPE_TNT { 40 float3 p; 41 float s, t; 42 float3 n; 43 float3 uv1; 44 float3 uv2; 45 }; 46 struct VA_TYPE_2d0 { 47 float x, y; 48 }; 49 struct VA_TYPE_2dT { 50 float x, y; 51 float s, t; 52 }; 53 struct VA_TYPE_2dTC { 54 float x, y; 55 float s, t; 56 SColor c; 57 }; 58 59 60 // number of elements (bytes / sizeof(float)) per vertex 61 #define VA_SIZE_0 (sizeof(VA_TYPE_0) / sizeof(float)) 62 #define VA_SIZE_C (sizeof(VA_TYPE_C) / sizeof(float)) 63 #define VA_SIZE_N (sizeof(VA_TYPE_N) / sizeof(float)) 64 #define VA_SIZE_T (sizeof(VA_TYPE_T) / sizeof(float)) 65 #define VA_SIZE_TN (sizeof(VA_TYPE_TN) / sizeof(float)) 66 #define VA_SIZE_TC (sizeof(VA_TYPE_TC) / sizeof(float)) 67 #define VA_SIZE_TNT (sizeof(VA_TYPE_TNT) / sizeof(float)) 68 #define VA_SIZE_2D0 (sizeof(VA_TYPE_2d0) / sizeof(float)) 69 #define VA_SIZE_2DT (sizeof(VA_TYPE_2dT) / sizeof(float)) 70 #define VA_SIZE_2DTC (sizeof(VA_TYPE_2dTC) / sizeof(float)) 71 72 73 74 75 class CVertexArray 76 { 77 public: 78 typedef void (*StripCallback)(void* data); 79 80 public: 81 CVertexArray(unsigned int maxVerts = 1 << 16); 82 virtual ~CVertexArray(); 83 84 bool IsReady() const; 85 void Initialize(); 86 inline void CheckInitSize(const unsigned int vertexes, const unsigned int strips = 0); 87 inline void EnlargeArrays(const unsigned int vertexes, const unsigned int strips = 0, const unsigned int stripsize = VA_SIZE_0); drawIndex()88 unsigned int drawIndex() const { return drawArrayPos - drawArray; } ResetPos()89 void ResetPos() { drawArrayPos = drawArray; } 90 91 // standard API 92 inline void AddVertex0(const float3& p); 93 inline void AddVertex0(float x, float y, float z); 94 inline void AddVertexN(const float3& p, const float3& n); 95 inline void AddVertexT(const float3& p, float tx, float ty); 96 inline void AddVertexC(const float3& p, const unsigned char* c); 97 inline void AddVertexTC(const float3& p, float tx, float ty, const unsigned char* c); 98 inline void AddVertexTN(const float3& p, float tx, float ty, const float3& n); 99 inline void AddVertexTNT(const float3& p, float tx, float ty, const float3& n, const float3& st, const float3& tt); 100 inline void AddVertex2d0(float x, float z); 101 inline void AddVertex2dT(float x, float y, float tx, float ty); 102 inline void AddVertex2dTC(float x, float y, float tx, float ty, const unsigned char* c); 103 104 // same as the AddVertex... functions just without automated CheckEnlargeDrawArray 105 inline void AddVertexQ0(float x, float y, float z); 106 inline void AddVertexQ0(const float3& f3); 107 inline void AddVertexQN(const float3& p, const float3& n); 108 inline void AddVertexQC(const float3& p, const unsigned char* c); 109 inline void AddVertexQT(const float3& p, float tx, float ty); 110 inline void AddVertexQTN(const float3& p, float tx, float ty, const float3& n); 111 inline void AddVertexQTNT(const float3& p, float tx, float ty, const float3& n, const float3& st, const float3& tt); 112 inline void AddVertexQTC(const float3& p, float tx, float ty, const unsigned char* c); 113 inline void AddVertexQ2d0(float x, float z); 114 inline void AddVertexQ2dT(float x, float y, float tx, float ty); 115 inline void AddVertexQ2dTC(float x, float y, float tx, float ty, const unsigned char* c); 116 117 // 3rd and newest API 118 // it appends a block of size * sizeof(T) at the end of the VA and returns the typed address to it GetTypedVertexArrayQ(const int size)119 template<typename T> T* GetTypedVertexArrayQ(const int size) { 120 T* r = reinterpret_cast<T*>(drawArrayPos); 121 drawArrayPos += (sizeof(T) / sizeof(float)) * size; 122 return r; 123 } GetTypedVertexArray(const int size)124 template<typename T> T* GetTypedVertexArray(const int size) { 125 EnlargeArrays(size, 0, (sizeof(T) / sizeof(float))); 126 return GetTypedVertexArrayQ<T>(size); 127 } 128 129 130 // Render the VA 131 void DrawArray0(const int drawType, unsigned int stride = sizeof(float) * VA_SIZE_0); 132 void DrawArray2d0(const int drawType, unsigned int stride = sizeof(float) * VA_SIZE_2D0); 133 void DrawArrayN(const int drawType, unsigned int stride = sizeof(float) * VA_SIZE_N); 134 void DrawArrayT(const int drawType, unsigned int stride = sizeof(float) * VA_SIZE_T); 135 void DrawArrayC(const int drawType, unsigned int stride = sizeof(float) * VA_SIZE_C); 136 void DrawArrayTC(const int drawType, unsigned int stride = sizeof(float) * VA_SIZE_TC); 137 void DrawArrayTN(const int drawType, unsigned int stride = sizeof(float) * VA_SIZE_TN); 138 void DrawArrayTNT(const int drawType, unsigned int stride = sizeof(float) * VA_SIZE_TNT); 139 void DrawArray2dT(const int drawType, unsigned int stride = sizeof(float) * VA_SIZE_2DT); 140 void DrawArray2dTC(const int drawType, unsigned int stride = sizeof(float) * VA_SIZE_2DTC); 141 void DrawArray2dT(const int drawType, StripCallback callback, void* data, unsigned int stride = sizeof(float) * VA_SIZE_2DT); 142 143 // same as EndStrip, but without automated EnlargeStripArray 144 void EndStrip(); 145 146 protected: 147 void DrawArrays(const GLenum mode, const unsigned int stride); 148 void DrawArraysCallback(const GLenum mode, const unsigned int stride, StripCallback callback, void* data); 149 inline void CheckEnlargeDrawArray(); 150 void EnlargeStripArray(); 151 void EnlargeDrawArray(); 152 inline void CheckEndStrip(); 153 154 protected: 155 float* drawArray; 156 float* drawArrayPos; 157 float* drawArraySize; 158 159 unsigned int* stripArray; 160 unsigned int* stripArrayPos; 161 unsigned int* stripArraySize; 162 163 unsigned int maxVertices; 164 }; 165 166 167 #include "VertexArray.inl" 168 169 #endif /* VERTEXARRAY_H */ 170