00001
00007 #ifndef _RENDERER_H_
00008 #define _RENDERER_H_
00009
00010 #include "../Common/Globals.h"
00011 #include "../Util/Set.h"
00012 #include "../Util/String.h"
00013 #include "../Math/Vector.h"
00014 #include "Image.h"
00015
00016 typedef int TextureID;
00017 typedef int ShaderID;
00018
00019 enum TEXTUREKIND {
00020 TEXTURE_2D = 0,
00021 TEXTURE_CUBEMAP = 1,
00022 };
00023
00024 struct Texture {
00025 unsigned int textureID;
00026 unsigned int width, height, depth;
00027 unsigned int format;
00028 TEXTUREKIND textureKind;
00029 bool clampS, clampT, clampR;
00030 Image *images[6];
00031 };
00032
00033 struct Shader {
00034 unsigned int programObj;
00035 unsigned int vertexShader;
00036 unsigned int fragmentShader;
00037 void *vertexShaderConstants;
00038 void *fragmentShaderConstants;
00039 };
00040
00041 struct FragmentShader {
00042 unsigned int shaderID;
00043 char *program;
00044 unsigned int length;
00045 unsigned int type;
00046 };
00047
00048 struct VertexShader {
00049 unsigned int shaderID;
00050 char *program;
00051 unsigned int length;
00052 };
00053
00054 #define MAX_TEXTURES 16
00055
00056 #define TEXTURE_NONE (-1)
00057 #define DEPTH_NONE (-1)
00058 #define BLENDING_NONE (-1)
00059 #define SHADER_NONE (-1)
00060
00061 #define UNDEFINED 0
00062
00063
00064 #define TEXTURE_CLAMP_S 0x1
00065 #define TEXTURE_CLAMP_T 0x2
00066 #define TEXTURE_CLAMP_R 0x4
00067 #define TEXTURE_CLAMP (TEXTURE_CLAMP_S | TEXTURE_CLAMP_T | TEXTURE_CLAMP_R)
00068
00069 #define TEXTURE_NORMALMAP 0x10
00070 #define TEXTURE_HEIGHTMAP 0x20
00071 #define TEXTURE_NORMALHEIGHTMAP (TEXTURE_NORMALMAP | TEXTURE_HEIGHTMAP)
00072
00073
00074
00075 #define DEPTH 0x01
00076 #define RED 0x02
00077 #define GREEN 0x04
00078 #define BLUE 0x08
00079 #define ALPHA 0x10
00080
00081 #define COLOR (RED | GREEN | BLUE | ALPHA)
00082 #define ALL (DEPTH | COLOR)
00083 #define NONE 0
00084
00085
00086 extern const int NEVER;
00087 extern const int LESS;
00088 extern const int EQUAL;
00089 extern const int LEQUAL;
00090 extern const int GREATER;
00091 extern const int NOTEQUAL;
00092 extern const int GEQUAL;
00093 extern const int ALWAYS;
00094
00095
00096 extern const int ZERO;
00097 extern const int ONE;
00098 extern const int SRC_COLOR;
00099 extern const int ONE_MINUS_SRC_COLOR;
00100 extern const int DST_COLOR;
00101 extern const int ONE_MINUS_DST_COLOR;
00102 extern const int SRC_ALPHA;
00103 extern const int ONE_MINUS_SRC_ALPHA;
00104 extern const int DST_ALPHA;
00105 extern const int ONE_MINUS_DST_ALPHA;
00106 extern const int SRC_ALPHA_SATURATE;
00107
00108 extern const int BLENDMODE_ADD;
00109 extern const int BLENDMODE_SUBTRACT;
00110 extern const int BLENDMODE_REVERSE_SUBSTRACT;
00111 extern const int BLENDMODE_MIN;
00112 extern const int BLENDMOXE_MAX;
00113
00114
00115 #define LF_SHADING 0x1
00116
00117
00118 class Renderer {
00119 public:
00120 Renderer();
00121 virtual ~Renderer();
00122 void reset();
00123 void resetToDefaults();
00124 void apply();
00125
00126 void lock(const unsigned int flag);
00127 void unlock(const unsigned int flag);
00128
00129 virtual void setDefaults() = 0;
00130
00131
00132 TextureID addTexture(const char *filename, unsigned int flags = 0);
00133 TextureID addTexture(Image *image, unsigned int flags = 0);
00134 TextureID addCubemap(const char *fNegX, const char *fPosX, const char *fNegY, const char *fPosY, const char *fNegZ, const char *fPosZ);
00135 TextureID addCubemap(Image *images[6]);
00136 void deleteTexture(const int i);
00137
00138
00139 ShaderID addShader(const char *filename, const char *extraDefines = NULL);
00140 void deleteShader(const int i);
00141
00142 virtual void changeShaderConstant1f(const char *name, const float constant) = 0;
00143 virtual void changeShaderConstant2f(const char *name, const Vec2 &constant) = 0;
00144 virtual void changeShaderConstant3f(const char *name, const Vec3 &constant) = 0;
00145 virtual void changeShaderConstant4f(const char *name, const Vec4 &constant) = 0;
00146
00147
00148 virtual void changeDepthFunc(const int depthFunc) = 0;
00149 virtual void changeBlending(const sFactor, const dFactor, const int blendMode = BLENDMODE_ADD) = 0;
00150 virtual void changeMask(const int mask) = 0;
00151
00152
00153 ShaderID addFragmentShader(unsigned int type, char *filename = NULL);
00154 ShaderID addFragmentShader(unsigned int type, unsigned int shaderID);
00155 ShaderID addVertexShader(char *filename);
00156 void deleteFragmentShader(const int index);
00157 void deleteVertexShader(const int index);
00158
00159 virtual void changeFragmentShader(const ShaderID shader) = 0;
00160 virtual void changeVertexShader(const ShaderID shader) = 0;
00161
00162 protected:
00163
00164 TextureID insertTexture(Texture &texture);
00165 virtual bool createTexture(Texture &texture) = 0;
00166 virtual void removeTexture(Texture &texture) = 0;
00167 virtual void changeTextureUnit(const int tex) = 0;
00168 virtual void changeTexture(const TextureID textureID) = 0;
00169
00170 virtual void assignNamedTextures() = 0;
00171
00172
00173 ShaderID insertShader(Shader &shader);
00174 virtual bool createShader(Shader &shader, const char *vertexShaderText, const char *fragmentShaderText) = 0;
00175 virtual void removeShader(Shader &shader) = 0;
00176 virtual void changeShader(const ShaderID shader) = 0;
00177
00178
00179
00180 ShaderID insertFragmentShader(FragmentShader &fragmentShader);
00181
00182 virtual void createFragmentShader(FragmentShader &fragmentShader) = 0;
00183 virtual void removeFragmentShader(FragmentShader &fragmentShader) = 0;
00184 virtual void uploadFragmentShader(FragmentShader &fragmentShader) = 0;
00185
00186 ShaderID insertVertexShader(VertexShader &vertexShader);
00187 virtual void createVertexShader(VertexShader &vertexShader) = 0;
00188 virtual void removeVertexShader(VertexShader &vertexShader) = 0;
00189 virtual void uploadVertexShader(VertexShader &vertexShader) = 0;
00190
00191 public:
00192 unsigned int lockFlags;
00193
00194 protected:
00195
00196
00197 Set <Texture> textures;
00198 int maxTextures;
00199 int currentTextureUnit;
00200 TextureID selectedTextures[MAX_TEXTURES];
00201 TextureID currentTextures[MAX_TEXTURES];
00202 const char *selectedTextureNames[MAX_TEXTURES];
00203 int nNamedTextures;
00204
00205
00206 Set <Shader> shaders;
00207 ShaderID currentShader, selectedShader;
00208
00209
00210 int selectedDepthFunc, currentDepthFunc;
00211 int selectedBlendMode, currentBlendMode;
00212 int selectedBlendSrc, currentBlendSrc;
00213 int selectedBlendDest, currentBlendDest;
00214 int selectedMask, currentMask;
00215
00216
00217 Set <VertexShader> vertexShaders;
00218 Set <FragmentShader> fragmentShaders;
00219 ShaderID selectedVertexShader, currentVertexShader;
00220 ShaderID selectedFragmentShader, currentFragmentShader;
00221
00222
00223 public:
00224
00225
00226 inline void setTexture(const char *name, const TextureID texID) {
00227 selectedTextureNames[nNamedTextures] = name;
00228 selectedTextures[nNamedTextures] = texID;
00229 nNamedTextures++;
00230 }
00231
00232 inline void setTextures(const TextureID t0) {
00233 selectedTextures[0] = t0;
00234 }
00235
00236 inline void setTextures(const TextureID t0, const TextureID t1) {
00237 selectedTextures[0] = t0;
00238 selectedTextures[1] = t1;
00239 }
00240
00241 inline void setTextures(const TextureID t0, const TextureID t1, const TextureID t2) {
00242 selectedTextures[0] = t0;
00243 selectedTextures[1] = t1;
00244 selectedTextures[2] = t2;
00245 }
00246
00247 inline void setTextures(const TextureID t0, const TextureID t1, const TextureID t2, const TextureID t3) {
00248 selectedTextures[0] = t0;
00249 selectedTextures[1] = t1;
00250 selectedTextures[2] = t2;
00251 selectedTextures[3] = t3;
00252 }
00253
00254 inline void setTextures(const TextureID t0, const TextureID t1, const TextureID t2, const TextureID t3, const TextureID t4) {
00255 selectedTextures[0] = t0;
00256 selectedTextures[1] = t1;
00257 selectedTextures[2] = t2;
00258 selectedTextures[3] = t3;
00259 selectedTextures[4] = t4;
00260 }
00261
00262 inline void setTextures(const TextureID t0, const TextureID t1, const TextureID t2, const TextureID t3, const TextureID t4, const TextureID t5) {
00263 selectedTextures[0] = t0;
00264 selectedTextures[1] = t1;
00265 selectedTextures[2] = t2;
00266 selectedTextures[3] = t3;
00267 selectedTextures[4] = t4;
00268 selectedTextures[5] = t5;
00269 }
00270
00271
00272 inline void setShader(const ShaderID shader) {
00273 selectedShader = shader;
00274 }
00275
00276
00277 inline void setDepthFunc(const int depthFunc) {
00278 selectedDepthFunc = depthFunc;
00279 }
00280
00281
00282 inline void setBlending(const int src, const int dest, const int blendMode = BLENDMODE_ADD) {
00283 selectedBlendSrc = src;
00284 selectedBlendDest = dest;
00285 selectedBlendMode = blendMode;
00286 }
00287
00288
00289 inline void setMask(const int mask) {
00290 selectedMask = mask;
00291 }
00292
00293 inline void setFragmentShader(const ShaderID shader) {
00294 selectedFragmentShader = shader;
00295 }
00296
00297 inline void setVertexShader(const ShaderID shader) {
00298 selectedVertexShader = shader;
00299 }
00300 };
00301
00302 #endif