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 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef LUA_MATERIAL_H
#define LUA_MATERIAL_H
#include <string>
#include <vector>
#include <set>
using std::string;
using std::vector;
using std::set;
#include "LuaOpenGLUtils.h"
#include "LuaUnitMaterial.h" // for LuaMatRef
#include "Rendering/GL/myGL.h"
#include "Rendering/ShadowHandler.h"
class CUnit;
class LuaMatTexSetRef;
class LuaMatUnifSetRef;
/******************************************************************************/
/******************************************************************************/
class LuaMatShader {
public:
enum Type {
LUASHADER_NONE = 0,
LUASHADER_GL = 1,
LUASHADER_3DO = 2,
LUASHADER_S3O = 3
};
public:
LuaMatShader() : type(LUASHADER_NONE), openglID(0) {}
void Finalize();
void Execute(const LuaMatShader& prev, bool deferredPass) const;
void Print(const string& indent) const;
static int Compare(const LuaMatShader& a, const LuaMatShader& b);
bool operator <(const LuaMatShader& mt) const {
return Compare(*this, mt) < 0;
}
bool operator==(const LuaMatShader& mt) const {
return Compare(*this, mt) == 0;
}
bool operator!=(const LuaMatShader& mt) const {
return Compare(*this, mt) != 0;
}
public:
Type type;
GLuint openglID;
};
/******************************************************************************/
class LuaMatTexSet {
public:
static const int maxTexUnits = 16;
public:
LuaMatTexSet() : texCount(0) {}
public:
int texCount;
LuaMatTexture textures[LuaMatTexture::maxTexUnits];
};
/******************************************************************************/
class LuaMaterial {
public:
LuaMaterial()
: type(LuaMatType(-1)), // invalid
order(0), texCount(0),
preList(0), postList(0),
useCamera(true), culling(0),
cameraLoc(-1), cameraInvLoc(-1), cameraPosLoc(-1),
sunPosLoc(-1), shadowLoc(-1), shadowParamsLoc(-1)
{}
void Finalize();
void Execute(const LuaMaterial& prev, bool deferredPass) const;
void Print(const string& indent) const;
static int Compare(const LuaMaterial& a, const LuaMaterial& b);
bool operator <(const LuaMaterial& m) const {
return Compare(*this, m) < 0;
}
bool operator==(const LuaMaterial& m) const {
return Compare(*this, m) == 0;
}
bool operator!=(const LuaMaterial& m) const {
return Compare(*this, m) != 0;
}
public:
LuaMatType type;
int order; // for manually adjusting rendering order
LuaMatShader standardShader;
LuaMatShader deferredShader;
int texCount;
LuaMatTexture textures[LuaMatTexture::maxTexUnits];
GLuint preList;
GLuint postList;
bool useCamera;
GLenum culling;
GLint cameraLoc; // view matrix
GLint cameraInvLoc; // inverse view matrix
GLint cameraPosLoc;
GLint sunPosLoc;
GLint shadowLoc; // shadow matrix
GLint shadowParamsLoc;
static const LuaMaterial defMat;
};
/******************************************************************************/
/******************************************************************************/
class LuaMatBin : public LuaMaterial {
friend class LuaMatHandler;
public:
void Clear() { units.clear(); }
const std::vector<CUnit*>& GetUnits() const { return units; }
void Ref();
void UnRef();
void AddUnit(CUnit* unit) { units.push_back(unit); }
void Print(const string& indent) const;
private:
LuaMatBin(const LuaMaterial& _mat) : LuaMaterial(_mat), refCount(0) {}
LuaMatBin(const LuaMatBin&);
LuaMatBin& operator=(const LuaMatBin&);
private:
int refCount;
std::vector<CUnit*> units;
};
/******************************************************************************/
struct LuaMatBinPtrLessThan {
bool operator()(const LuaMatBin* a, const LuaMatBin* b) const {
const LuaMaterial* ma = static_cast<const LuaMaterial*>(a);
const LuaMaterial* mb = static_cast<const LuaMaterial*>(b);
return (*ma < *mb);
}
};
typedef set<LuaMatBin*, LuaMatBinPtrLessThan> LuaMatBinSet;
/******************************************************************************/
class LuaMatHandler {
public:
LuaMatRef GetRef(const LuaMaterial& mat);
inline const LuaMatBinSet& GetBins(LuaMatType type) const {
return binTypes[type];
}
void ClearBins();
void ClearBins(LuaMatType type);
void FreeBin(LuaMatBin* bin);
void PrintBins(const string& indent, LuaMatType type) const;
void PrintAllBins(const string& indent) const;
public:
void (*setup3doShader)(bool);
void (*reset3doShader)(bool);
void (*setupS3oShader)(bool);
void (*resetS3oShader)(bool);
private:
LuaMatHandler();
LuaMatHandler(const LuaMatHandler&);
LuaMatHandler operator=(const LuaMatHandler&);
~LuaMatHandler();
private:
LuaMatBinSet binTypes[LUAMAT_TYPE_COUNT];
LuaMaterial* prevMat;
public:
static LuaMatHandler handler;
};
/******************************************************************************/
/******************************************************************************/
extern LuaMatHandler& luaMatHandler;
#endif /* LUA_MATERIAL_H */
|