00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __GLFX_H__
00018 #define __GLFX_H__
00019
00020 #ifdef _WIN32
00021 # ifdef GLFX_LIBRARY
00022 # define GLFX_EXPORT __declspec(dllexport)
00023 # else
00024 # define GLFX_EXPORT __declspec(dllimport)
00025 # endif
00026 #else
00027 # define GLFX_EXPORT
00028 #endif
00029
00030 #ifndef GLFX_LIBRARY
00031 # ifdef _MSC_VER
00032 # ifdef _DEBUG
00033 # pragma comment(lib, "glfx-dbg")
00034 # else
00035 # pragma comment(lib, "glfx")
00036 # endif
00037 # endif
00038 #endif
00039
00041 namespace GLFX {
00042
00043
00044 class EffectManager;
00045
00047 struct IObject
00048 {
00051 virtual unsigned long AddRef() = 0;
00055 virtual unsigned long Release() = 0;
00056
00057 };
00058
00083 struct GLFX_EXPORT VertexComponent
00084 {
00086 GLenum array;
00088 unsigned long index;
00090 unsigned long count;
00092 GLenum type;
00093
00095 VertexComponent() {}
00101 VertexComponent(int arr, int id, int num, int tp) : array(arr), index(id), count(num), type(tp) {}
00106 static unsigned long GetByteSize(int type, int count);
00111 static unsigned long GetVertexSize(const VertexComponent* desc);
00115 static void EnableClientState(const VertexComponent* desc);
00119 static void DisableClientState(const VertexComponent* desc);
00126 static void SetPointers(const VertexComponent* desc, int stride, void* ptr);
00133 static const VertexComponent* FindComponentByArray(const VertexComponent* desc, GLenum array, int index);
00139 static unsigned long ComponentOffset(const VertexComponent* desc, const VertexComponent* component);
00140
00141 };
00142
00144 template<int ArrayT, int IndexT, int CountT, int TypeT>
00145 struct VertexComponentGen : public VertexComponent
00146 {
00147 VertexComponentGen() : VertexComponent(ArrayT, IndexT, CountT, TypeT) {}
00148 enum { IS_VALID = 1 };
00149 };
00150
00152 struct VertexComponentInvalidGen : public VertexComponent
00153 {
00154 VertexComponentInvalidGen() : VertexComponent(0, 0, 0, 0) {}
00155 enum { IS_VALID = 0 };
00156 };
00157
00159 typedef VertexComponentGen<GL_VERTEX_ARRAY, 0, 2, GL_FLOAT> VC_VERTEX_2F;
00161 typedef VertexComponentGen<GL_VERTEX_ARRAY, 0, 3, GL_FLOAT> VC_VERTEX_3F;
00163 typedef VertexComponentGen<GL_VERTEX_ARRAY, 0, 4, GL_FLOAT> VC_VERTEX_4F;
00165 typedef VertexComponentGen<GL_NORMAL_ARRAY, 0, 3, GL_FLOAT> VC_NORMAL_3F;
00167 typedef VertexComponentGen<GL_COLOR_ARRAY, 0, 3, GL_FLOAT> VC_COLOR_3F;
00169 typedef VertexComponentGen<GL_COLOR_ARRAY, 0, 4, GL_FLOAT> VC_COLOR_4F;
00171 typedef VertexComponentGen<GL_COLOR_ARRAY, 0, 3, GL_UNSIGNED_BYTE> VC_COLOR_3UB;
00173 typedef VertexComponentGen<GL_COLOR_ARRAY, 0, 4, GL_UNSIGNED_BYTE> VC_COLOR_4UB;
00175 typedef VertexComponentGen<GL_SECONDARY_COLOR_ARRAY, 0, 3, GL_FLOAT> VC_SECONDARY_COLOR_3F;
00177 typedef VertexComponentGen<GL_SECONDARY_COLOR_ARRAY, 0, 4, GL_FLOAT> VC_SECONDARY_COLOR_4F;
00179 typedef VertexComponentGen<GL_SECONDARY_COLOR_ARRAY, 0, 3, GL_UNSIGNED_BYTE> VC_SECONDARY_COLOR_3UB;
00181 typedef VertexComponentGen<GL_SECONDARY_COLOR_ARRAY, 0, 4, GL_UNSIGNED_BYTE> VC_SECONDARY_COLOR_4UB;
00183 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 0, 1, GL_FLOAT> VC_TEXTURE0_1F;
00185 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 0, 2, GL_FLOAT> VC_TEXTURE0_2F;
00187 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 0, 3, GL_FLOAT> VC_TEXTURE0_3F;
00189 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 0, 4, GL_FLOAT> VC_TEXTURE0_4F;
00191 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 1, 1, GL_FLOAT> VC_TEXTURE1_1F;
00193 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 1, 2, GL_FLOAT> VC_TEXTURE1_2F;
00195 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 1, 3, GL_FLOAT> VC_TEXTURE1_3F;
00197 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 1, 4, GL_FLOAT> VC_TEXTURE1_4F;
00199 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 2, 1, GL_FLOAT> VC_TEXTURE2_1F;
00201 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 2, 2, GL_FLOAT> VC_TEXTURE2_2F;
00203 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 2, 3, GL_FLOAT> VC_TEXTURE2_3F;
00205 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 2, 4, GL_FLOAT> VC_TEXTURE2_4F;
00207 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 3, 1, GL_FLOAT> VC_TEXTURE3_1F;
00209 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 3, 2, GL_FLOAT> VC_TEXTURE3_2F;
00211 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 3, 3, GL_FLOAT> VC_TEXTURE3_3F;
00213 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 3, 4, GL_FLOAT> VC_TEXTURE3_4F;
00215 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 4, 1, GL_FLOAT> VC_TEXTURE4_1F;
00217 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 4, 2, GL_FLOAT> VC_TEXTURE4_2F;
00219 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 4, 3, GL_FLOAT> VC_TEXTURE4_3F;
00221 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 4, 4, GL_FLOAT> VC_TEXTURE4_4F;
00223 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 5, 1, GL_FLOAT> VC_TEXTURE5_1F;
00225 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 5, 2, GL_FLOAT> VC_TEXTURE5_2F;
00227 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 5, 3, GL_FLOAT> VC_TEXTURE5_3F;
00229 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 5, 4, GL_FLOAT> VC_TEXTURE5_4F;
00231 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 6, 1, GL_FLOAT> VC_TEXTURE6_1F;
00233 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 6, 2, GL_FLOAT> VC_TEXTURE6_2F;
00235 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 6, 3, GL_FLOAT> VC_TEXTURE6_3F;
00237 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 6, 4, GL_FLOAT> VC_TEXTURE6_4F;
00239 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 7, 1, GL_FLOAT> VC_TEXTURE7_1F;
00241 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 7, 2, GL_FLOAT> VC_TEXTURE7_2F;
00243 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 7, 3, GL_FLOAT> VC_TEXTURE7_3F;
00245 typedef VertexComponentGen<GL_TEXTURE_COORD_ARRAY, 7, 4, GL_FLOAT> VC_TEXTURE7_4F;
00247 typedef VertexComponentInvalidGen VC_END;
00249 template<int IndexT> struct VC_ATTRIBUTE_1F : public VertexComponentGen<GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB, IndexT, 1, GL_FLOAT> {};
00251 template<int IndexT> struct VC_ATTRIBUTE_2F : public VertexComponentGen<GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB, IndexT, 2, GL_FLOAT> {};
00253 template<int IndexT> struct VC_ATTRIBUTE_3F : public VertexComponentGen<GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB, IndexT, 3, GL_FLOAT> {};
00255 template<int IndexT> struct VC_ATTRIBUTE_4F : public VertexComponentGen<GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB, IndexT, 4, GL_FLOAT> {};
00257 template<int IndexT> struct VC_ATTRIBUTE_1UB : public VertexComponentGen<GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB, IndexT, 1, GL_UNSIGNED_BYTE> {};
00259 template<int IndexT> struct VC_ATTRIBUTE_2UB : public VertexComponentGen<GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB, IndexT, 2, GL_UNSIGNED_BYTE> {};
00261 template<int IndexT> struct VC_ATTRIBUTE_3UB : public VertexComponentGen<GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB, IndexT, 3, GL_UNSIGNED_BYTE> {};
00263 template<int IndexT> struct VC_ATTRIBUTE_4UB : public VertexComponentGen<GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB, IndexT, 4, GL_UNSIGNED_BYTE> {};
00264
00324 template<class Comp0, class Comp1 = VC_END, class Comp2 = VC_END, class Comp3 = VC_END, class Comp4 = VC_END, class Comp5 = VC_END, class Comp6 = VC_END, class Comp7 = VC_END, class Comp8 = VC_END, class Comp9 = VC_END, class Comp10 = VC_END, class Comp11 = VC_END>
00325 struct VertexFormat
00326 {
00327 private:
00328 typedef VertexFormat<Comp0, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8, Comp9, Comp10, Comp11> MyType;
00329 public:
00330
00332 Comp0 c0;
00334 Comp1 c1;
00336 Comp2 c2;
00338 Comp3 c3;
00340 Comp4 c4;
00342 Comp5 c5;
00344 Comp6 c6;
00346 Comp7 c7;
00348 Comp8 c8;
00350 Comp9 c9;
00352 Comp10 c10;
00354 Comp11 c11;
00356 VC_END end;
00357
00360 static VertexComponent* Desc() {
00361 static const MyType def;
00362 return (VertexComponent*)&(def.c0);
00363 }
00364
00366 enum { Size = Comp0::IS_VALID + Comp1::IS_VALID + Comp2::IS_VALID + Comp3::IS_VALID + Comp4::IS_VALID +
00367 Comp5::IS_VALID + Comp6::IS_VALID + Comp7::IS_VALID + Comp8::IS_VALID + Comp9::IS_VALID +
00368 Comp10::IS_VALID + Comp11::IS_VALID };
00369 };
00370
00386 struct ITechnique
00387 {
00388
00393 virtual bool Begin() = 0;
00397 virtual bool End() = 0;
00402 virtual bool BeginPass(int num) = 0;
00405 virtual bool EndPass() = 0;
00408 virtual long GetPassCount() = 0;
00411 virtual const char* GetName() = 0;
00412
00413 };
00414
00417 struct IEffect : public IObject
00418 {
00419
00422 virtual long GetTechniqueCount() = 0;
00426 virtual ITechnique* GetTechnique(long num) = 0;
00431 virtual ITechnique* FindTechnique(const char* name) = 0;
00436 virtual long GetParameterId(const char* name) = 0;
00442 virtual void SetParameterFloat(long id, GLsizei components, GLsizei count, float* value) = 0;
00446 virtual void SetParameterTexture(long id, GLuint texture) = 0;
00449 virtual void ReloadTextures() = 0;
00450
00451 };
00452
00466 struct IFramebuffer : public IObject
00467 {
00468
00471 virtual bool BeginCapture() = 0;
00473 virtual void EndCapture() = 0;
00484 virtual bool AttachTexture(long attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset) = 0;
00487 virtual bool IsValid() = 0;
00490 virtual void SetDrawBuffer(long attachment) = 0;
00491
00492 };
00493
00495 struct IFont : public IObject
00496 {
00497
00499 enum {
00501 SINGLE_LINE,
00503 MULTI_LINE,
00505 CHAR_WRAP,
00509 WORD_WRAP
00510 };
00511
00514 virtual GLuint GetTexture() = 0;
00523 virtual void DrawChar(float x, float y, float z, int c, float* bounds) = 0;
00536 virtual void DrawString(float x, float y, float z, const char* strBegin,
00537 const char* strEnd, unsigned long mode, float lineWidth, float* bounds) = 0;
00540 virtual unsigned long GetGlyphHeight() = 0;
00543 virtual unsigned long GetGlyphBaseLine() = 0;
00552 virtual float ComputeStringWidth(const char* strBegin, const char* strEnd,
00553 unsigned long mode, float lineWidth) = 0;
00562 virtual unsigned long ComputeStringLineCount(const char* strBegin, const char* strEnd,
00563 unsigned long mode, float lineWidth) = 0;
00566 virtual void SetTabSize(unsigned long horizontal) = 0;
00567
00568 };
00569
00574 struct IVertexBuffer : public IObject
00575 {
00576
00579 virtual unsigned long GetSize() = 0;
00580
00583 virtual const VertexComponent* GetFormat() = 0;
00584
00588 virtual void* Lock(GLenum access) = 0;
00589
00591 virtual void Unlock() = 0;
00592
00594 virtual void Bind() = 0;
00595
00597 virtual void Unbind() = 0;
00598
00599 };
00600
00603 struct IIndexBuffer : public IObject
00604 {
00605
00608 virtual unsigned long GetSize() = 0;
00609
00612 virtual GLenum GetType() = 0;
00613
00617 virtual void* Lock(GLenum access) = 0;
00618
00620 virtual void Unlock() = 0;
00621
00623 virtual void Bind() = 0;
00624
00626 virtual void Unbind() = 0;
00627
00634 virtual void DrawElements(GLenum mode, GLsizei first, GLsizei count) = 0;
00635
00636 };
00637
00641 struct IMesh : public IObject
00642 {
00645 virtual IVertexBuffer* GetVertexBuffer() = 0;
00646
00649 virtual IIndexBuffer* GetIndexBuffer() = 0;
00650
00653 virtual unsigned long GetPrimitiveGroupCount() = 0;
00654
00658 virtual unsigned long GetPrimitiveGroupStartIndex(unsigned long group) = 0;
00659
00663 virtual unsigned long GetPrimitiveGroupElementCount(unsigned long group) = 0;
00664
00667 virtual GLenum GetPrimitiveType() = 0;
00668
00673 virtual void SetPrimitiveGroup(unsigned long group, unsigned long start, unsigned long count) = 0;
00674
00677 virtual void DrawPrimitiveGroup(unsigned long group) = 0;
00678
00679 };
00680
00685 struct ISceneBuilder
00686 {
00687
00688 enum {
00690 NODE_SIMPLE=0,
00692 NODE_STATIC_MESH,
00694 NODE_LIGHT,
00696 NODE_CAMERA,
00697 };
00698
00699 enum {
00701 VERT_POSITION=0,
00703 VERT_NORMAL,
00705 VERT_COLOR,
00707 VERT_TEXTURE0,
00709 VERT_TEXTURE1,
00711 VERT_TEXTURE2,
00713 VERT_TEXTURE3,
00715 VERT_TEXTURE4,
00717 VERT_TEXTURE5,
00719 VERT_TEXTURE6,
00721 VERT_TEXTURE7
00722 };
00723
00724 enum {
00726 ANIM_POSITION=0,
00728 ANIM_ROTATION,
00730 ANIM_SCALE,
00731 };
00732
00733
00734
00738 virtual bool Initialize(EffectManager* fxMgr) = 0;
00739
00743 virtual bool Finalize() = 0;
00744
00751 virtual int CreateNode(const char* nodeName, int type) = 0;
00752
00757 virtual int CreateMaterial(const char* materialName) = 0;
00758
00762 virtual int CreateAnimation(const char* animName) = 0;
00763
00764
00765
00771 virtual void SetNodeParentByName(int nodeId, const char* parentName) = 0;
00772
00776 virtual void SetNodeTM(int nodeId, const float* tm) = 0;
00777
00778
00779
00786 virtual void SetMeshParameters(int nodeId, GLenum primitiveType, unsigned long vertexCount, unsigned long indexCount, unsigned long primitiveGroupCount) = 0;
00787
00801 virtual bool SetMeshVertexData(int nodeId, unsigned long type, unsigned long dim, const float* data) = 0;
00802
00807 virtual bool SetMeshFaceIndices(int nodeId, const unsigned long* data) = 0;
00808
00814 virtual void SetMeshPrimitiveGroup(int nodeId, unsigned long groupId, unsigned long start, unsigned long count) = 0;
00815
00820 virtual void SetMeshMaterial(int nodeId, unsigned long groupId, int materialId) = 0;
00821
00822
00823
00827 virtual void SetLightColor(int nodeId, const float* color) = 0;
00828
00835 virtual void SetLightAttenuation(int nodeId, float attnStart, float attnEnd) = 0;
00836
00837
00838
00842 virtual void SetCameraFieldOfViewX(int nodeId, float fovx) = 0;
00843
00844
00845
00849 virtual void SetMaterialAmbientColor(int materialId, const float* color) = 0;
00850
00854 virtual void SetMaterialDiffuseColor(int materialId, const float* color) = 0;
00855
00859 virtual void SetMaterialSpecularColor(int materialId, const float* color) = 0;
00860
00864 virtual void SetMaterialSpecularLevel(int materialId, float level) = 0;
00865
00870 virtual void SetMaterialTransparency(int materialId, float transparency) = 0;
00871
00875 virtual void SetMaterialDiffuseMap(int materialId, const char* name) = 0;
00876
00880 virtual void SetMaterialGlossMap(int materialId, const char* name) = 0;
00881
00885 virtual void SetMaterialGlowMap(int materialId, const char* name) = 0;
00886
00890 virtual void SetMaterialBumpMap(int materialId, const char* name) = 0;
00891
00892
00893
00899 virtual void SetAnimationParameters(int animId, float startTime, float frequency, unsigned long frameCount) = 0;
00900
00909 virtual void SetAnimationNodeKeyframes(int animId, int nodeId, unsigned long type, float* data) = 0;
00910
00911 };
00912
00915 struct ISceneLoader : public IObject
00916 {
00917
00922 virtual bool Initialize(EffectManager* fxMgr) = 0;
00923
00928 virtual bool IsFileSupported(const char* fileName) = 0;
00929
00934 virtual bool LoadScene(const char* fileName, ISceneBuilder* sceneBuilder) = 0;
00935
00936 };
00937
00938
00939 namespace Impl {
00940
00941 class EffectPool;
00942 class FontPool;
00943 class Utility;
00944 class TextureManager;
00945 class SceneLoaderManager;
00946
00947 }
00948
00955 class GLFX_EXPORT EffectManager
00956 {
00957 public:
00958
00960 enum {
00962 POOL_SOFTWARE=0,
00964 POOL_HARDWARE_VBO=1,
00966 POOL_HARDWARE=POOL_HARDWARE_VBO
00967 };
00968
00970 enum {
00972 LOG_DEBUG=1000,
00974 LOG_INFO=1200,
00976 LOG_NOTICE=1500,
00978 LOG_WARN=1700,
00980 LOG_ERROR=2000
00981 };
00982
00983 EffectManager();
00984 virtual ~EffectManager();
00985
00993 bool Initialize(bool currentStateDefault);
00996 void Finalize();
01001 IEffect* LoadFX(const char* name);
01005 Impl::EffectPool* GetEffectPool();
01009 void MessageFormat(long level, const char* fmt, ...);
01017 IFramebuffer* CreateFramebuffer(long width, long height, long depthBits, long stencilBits);
01021 #ifdef _DEBUG
01022 void CheckGLErrors(const char* location = 0);
01023 #else
01024 void CheckGLErrors(const char* location = 0) {}
01025 #endif
01028 Impl::FontPool* GetFontPool();
01034 IFont* LoadFont(const char* name, unsigned long fontSize);
01036 void UpdateFPS();
01040 float GetFPS();
01049 GLuint LoadTexture(GLenum target, const char* name);
01053 void DeleteTexture(GLuint texture);
01055 void ReloadTextures();
01058 void IncrTexRefCount(GLuint texture);
01065 IVertexBuffer* CreateVertexBuffer(VertexComponent* format, unsigned long size, GLenum usage, unsigned long pool);
01072 IIndexBuffer* CreateIndexBuffer(GLenum type, unsigned long size, GLenum usage, unsigned long pool);
01074 IMesh* CreateMesh(unsigned long numVert, unsigned long numIndex, unsigned long numGroup,
01075 GLenum primitiveType, VertexComponent* format, GLenum indexType, GLenum vertexUsage,
01076 GLenum indexUsage, unsigned long vertexPool, unsigned long indexPool);
01080 bool RegisterSceneLoader(ISceneLoader* sceneLoader);
01085 bool LoadScene(const char* fileName, ISceneBuilder* sceneBuilder);
01087 void LogGLInfo();
01088
01120 virtual bool FillTextureData(GLenum target, const char* name);
01127 virtual const char* MakeAbsolutePath(const char* file, const char* referer);
01133 virtual void Message(long level, const char* message);
01142 virtual void* OpenFile(const char* name, bool writeFile);
01149 virtual long ReadFile(void* file, void* buffer, long bytes);
01153 virtual void CloseFile(void* file);
01156 virtual bool Eof(void* file);
01162 virtual long SeekFile(void* file, long offset, long origin);
01165 virtual long TellFile(void* file);
01166
01167 private:
01168
01169 Impl::EffectPool* _effect;
01170 Impl::FontPool* _font;
01171 Impl::Utility* _util;
01172 Impl::TextureManager* _texture;
01173 Impl::SceneLoaderManager* _scene;
01174
01175 };
01176
01177 }
01178
01179 #endif // __GLFX_H__