From f95df35dc58e01b99ffddfc4ad394feaa4460b09 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 4 Jul 2012 00:04:53 +0200 Subject: Multitexturing support - added CImage class for loading/saving images and a simple test for it - added libpng library to build - added Gfx::Texture struct - updated the Gfx::CDevice interface to include new features - implemented the new features in Gfx::CGLDevice --- src/graphics/common/device.cpp | 17 ++++ src/graphics/common/device.h | 185 +++++++++++++++++++++++++++++++++++++++-- src/graphics/common/texture.h | 9 +- 3 files changed, 203 insertions(+), 8 deletions(-) (limited to 'src/graphics/common') diff --git a/src/graphics/common/device.cpp b/src/graphics/common/device.cpp index fcd4318..bb51699 100644 --- a/src/graphics/common/device.cpp +++ b/src/graphics/common/device.cpp @@ -31,3 +31,20 @@ void Gfx::DeviceConfig::LoadDefault() doubleBuf = true; noFrame = false; } + +void Gfx::TextureParams::LoadDefault() +{ + minFilter = Gfx::TEX_MIN_FILTER_NEAREST; + magFilter = Gfx::TEX_MAG_FILTER_NEAREST; + + wrapS = Gfx::TEX_WRAP_REPEAT; + wrapT = Gfx::TEX_WRAP_REPEAT; + + colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + colorArg1 = Gfx::TEX_MIX_ARG_CURRENT; + colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + + alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + alphaArg1 = Gfx::TEX_MIX_ARG_CURRENT; + alphaArg2 = Gfx::TEX_MIX_ARG_TEXTURE; +} diff --git a/src/graphics/common/device.h b/src/graphics/common/device.h index 961fb6b..3382c9a 100644 --- a/src/graphics/common/device.h +++ b/src/graphics/common/device.h @@ -30,6 +30,9 @@ #include +class CImage; + + namespace Gfx { /** @@ -144,6 +147,15 @@ enum CullMode CULL_CCW }; +/** + \enum ShadeModel + \brief Shade model used in rendering */ +enum ShadeModel +{ + SHADE_FLAT, + SHADE_SMOOTH +}; + /** \enum FillMode \brief Polygon fill mode */ @@ -169,6 +181,147 @@ enum PrimitiveType PRIMITIVE_TRIANGLE_STRIP }; +/** + \enum TexMinFilter + \brief Minification texture filter + + Corresponds to OpenGL modes but should translate to DirectX too. */ +enum TexMinFilter +{ + TEX_MIN_FILTER_NEAREST, + TEX_MIN_FILTER_LINEAR, + TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST, + TEX_MIN_FILTER_LINEAR_MIPMAP_NEAREST, + TEX_MIN_FILTER_NEAREST_MIPMAP_LINEAR, + TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR +}; + +/** + \enum TexMagFilter + \brief Magnification texture filter */ +enum TexMagFilter +{ + TEX_MAG_FILTER_NEAREST, + TEX_MAG_FILTER_LINEAR +}; + +/** + \enum TexWrapMode + \brief Wrapping mode for texture coords */ +enum TexWrapMode +{ + TEX_WRAP_CLAMP, + TEX_WRAP_REPEAT +}; + +/** + \enum TexMixOperation + \brief Multitexture mixing operation + */ +enum TexMixOperation +{ + TEX_MIX_OPER_MODULATE, + TEX_MIX_OPER_ADD +}; + +/** + \enum TexMixArgument + \brief Multitexture mixing argument + */ +enum TexMixArgument +{ + TEX_MIX_ARG_CURRENT, + TEX_MIX_ARG_TEXTURE, + TEX_MIX_ARG_DIFFUSE, + TEX_MIX_ARG_FACTOR +}; + +/** + \enum TextureParams + \brief Parameters for texture creation + */ +struct TextureParams +{ + //! Minification filter + Gfx::TexMinFilter minFilter; + //! Magnification filter + Gfx::TexMagFilter magFilter; + //! Wrap S coord mode + Gfx::TexWrapMode wrapS; + //! Wrap T coord mode + Gfx::TexWrapMode wrapT; + //! Mixing operation done on color values + Gfx::TexMixOperation colorOperation; + //! 1st argument of color operations + Gfx::TexMixArgument colorArg1; + //! 2nd argument of color operations + Gfx::TexMixArgument colorArg2; + //! Mixing operation done on alpha values + Gfx::TexMixOperation alphaOperation; + //! 1st argument of alpha operations + Gfx::TexMixArgument alphaArg1; + //! 2nd argument of alpha operations + Gfx::TexMixArgument alphaArg2; + + //! Constructor; calls LoadDefault() + TextureParams() + { LoadDefault(); } + + //! Loads the default values + void LoadDefault(); +}; + +/* + +Notes for rewriting DirectX code: + +>> SetRenderState() translates to many functions depending on param + +D3DRENDERSTATE_ALPHABLENDENABLE -> SetRenderState() with RENDER_STATE_BLENDING +D3DRENDERSTATE_ALPHAFUNC -> SetAlphaTestFunc() func +D3DRENDERSTATE_ALPHAREF -> SetAlphaTestFunc() ref +D3DRENDERSTATE_ALPHATESTENABLE -> SetRenderState() with RENDER_STATE_ALPHA_TEST +D3DRENDERSTATE_AMBIENT -> SetGlobalAmbient() +D3DRENDERSTATE_CULLMODE -> SetCullMode() +D3DRENDERSTATE_DESTBLEND -> SetBlendFunc() dest blending func +D3DRENDERSTATE_DITHERENABLE -> SetRenderState() with RENDER_STATE_DITHERING +D3DRENDERSTATE_FILLMODE -> SetFillMode() +D3DRENDERSTATE_FOGCOLOR -> SetFogParams() +D3DRENDERSTATE_FOGENABLE -> SetRenderState() with RENDER_STATE_FOG +D3DRENDERSTATE_FOGEND -> SetFogParams() +D3DRENDERSTATE_FOGSTART -> SetFogParams() +D3DRENDERSTATE_FOGVERTEXMODE -> SetFogParams() fog model +D3DRENDERSTATE_LIGHTING -> SetRenderState() with RENDER_STATE_LIGHTING +D3DRENDERSTATE_SHADEMODE -> SetShadeModel() +D3DRENDERSTATE_SPECULARENABLE -> doesn't matter (always enabled) +D3DRENDERSTATE_SRCBLEND -> SetBlendFunc() src blending func +D3DRENDERSTATE_TEXTUREFACTOR -> SetTextureFactor() +D3DRENDERSTATE_ZBIAS -> SetDepthBias() +D3DRENDERSTATE_ZENABLE -> SetRenderState() with RENDER_STATE_DEPTH_TEST +D3DRENDERSTATE_ZFUNC -> SetDepthTestFunc() +D3DRENDERSTATE_ZWRITEENABLE -> SetRenderState() with RENDER_STATE_DEPTH_WRITE + + +>> SetTextureStageState() translates to SetTextureParams() + +Params from enum in struct TextureParams + D3DTSS_ADDRESS -> Gfx::TexWrapMode wrapS, wrapT + D3DTSS_ALPHAARG1 -> Gfx::TexMixArgument alphaArg1 + D3DTSS_ALPHAARG2 -> Gfx::TexMixArgument alphaArg2 + D3DTSS_ALPHAOP -> Gfx::TexMixOperation alphaOperation + D3DTSS_COLORARG1 -> Gfx::TexMixArgument colorArg1 + D3DTSS_COLORARG2 -> Gfx::TexMixArgument colorArg2 + D3DTSS_COLOROP -> Gfx::TexMixOperation colorOperation + D3DTSS_MAGFILTER -> Gfx::TexMagFilter magFilter + D3DTSS_MINFILTER -> Gfx::TexMinFilter minFilter + D3DTSS_TEXCOORDINDEX -> doesn't matter (texture coords are set explicitly by glMultiTexCoordARB*) + +Note that D3DTSS_ALPHAOP or D3DTSS_COLOROP set to D3DTOP_DISABLE must translate to disabling the whole texture stage. +In DirectX, you shouldn't mix enabling one and disabling the other. +Also, if previous stage is disabled in DirectX, the later ones are disabled, too. In OpenGL, that is not the case. + +*/ + /** \class CDevice \brief Abstract interface of graphics device @@ -226,20 +379,33 @@ public: //! Returns the current enable state of light at given index virtual bool GetLightEnabled(int index) = 0; - // TODO: - // virtual Gfx::Texture* CreateTexture(CImage *image) = 0; - // virtual void DestroyTexture(Gfx::Texture *texture) = 0; + //! Creates a texture from image; the image can be safely removed after that + virtual Gfx::Texture* CreateTexture(CImage *image, bool alpha, bool mipMap) = 0; + //! Deletes a given texture, freeing it from video memory + virtual void DestroyTexture(Gfx::Texture *texture) = 0; + //! Deletes all textures created so far + virtual void DestroyAllTextures() = 0; - //! Returns the maximum number of multitexture units + //! Returns the maximum number of multitexture stages virtual int GetMaxTextureCount() = 0; //! Sets the (multi)texture at given index virtual void SetTexture(int index, Gfx::Texture *texture) = 0; //! Returns the (multi)texture at given index virtual Gfx::Texture* GetTexture(int index) = 0; + //! Enables/disables the given texture stage + virtual void SetTextureEnabled(int index, bool enabled) = 0; + //! Returns the current enable state of given texture stage + virtual bool GetTextureEnabled(int index) = 0; - // TODO: - // virtual void GetTextureStageState() = 0; - // virtual void SetTextureStageState() = 0; + //! Sets the current params of texture with given index + virtual void SetTextureParams(int index, const Gfx::TextureParams ¶ms) = 0; + //! Returns the current params of texture with given index + virtual Gfx::TextureParams GetTextureParams(int index) = 0; + + //! Sets the texture factor to the given color value + virtual void SetTextureFactor(Gfx::Color &color) = 0; + //! Returns the current texture factor + virtual Gfx::Color GetTextureFactor() = 0; //! Renders primitive composed of vertices with single texture virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::Vertex *vertices, int vertexCount) = 0; @@ -297,6 +463,11 @@ public: //! Returns the current cull mode virtual Gfx::CullMode GetCullMode() = 0; + //! Sets the shade model + virtual void SetShadeModel(Gfx::ShadeModel model) = 0; + //! Returns the current shade model + virtual Gfx::ShadeModel GetShadeModel() = 0; + //! Sets the current fill mode virtual void SetFillMode(Gfx::FillMode mode) = 0; //! Returns the current fill mode diff --git a/src/graphics/common/texture.h b/src/graphics/common/texture.h index ab894db..55d5c70 100644 --- a/src/graphics/common/texture.h +++ b/src/graphics/common/texture.h @@ -20,9 +20,16 @@ namespace Gfx { +/** \struct Texture*/ struct Texture { - // TODO + //! Whether the texture was loaded + bool valid; + //! Id of the texture in graphics engine + unsigned int id; + + Texture() + { valid = false; id = 0; } }; }; // namespace Gfx -- cgit v1.2.3-1-g7c22