From 42963b341f3fbc055c494a0dc0c97d395fa69562 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 25 Jul 2012 00:27:01 +0200 Subject: Refactoring of texture code - refactored texture structs & functions - added note about OpenGL extensions - removed device.cpp as unnecessary - minor changes in CEngine --- src/graphics/common/device.cpp | 33 ---------------- src/graphics/common/device.h | 19 ++++++--- src/graphics/common/engine.cpp | 90 +++++++++++++++++++++--------------------- src/graphics/common/engine.h | 2 +- src/graphics/common/texture.h | 82 ++++++++++++++++++++++++-------------- 5 files changed, 114 insertions(+), 112 deletions(-) delete mode 100644 src/graphics/common/device.cpp (limited to 'src/graphics/common') diff --git a/src/graphics/common/device.cpp b/src/graphics/common/device.cpp deleted file mode 100644 index fcd4318..0000000 --- a/src/graphics/common/device.cpp +++ /dev/null @@ -1,33 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program is free software: you can redistribute it and/or modify -// * it under the terms of the GNU General Public License as published by -// * the Free Software Foundation, either version 3 of the License, or -// * (at your option) any later version. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/. - -// device.cpp - - -#include "graphics/common/device.h" - - -void Gfx::DeviceConfig::LoadDefault() -{ - width = 800; - height = 600; - bpp = 32; - fullScreen = false; - resizeable = false; - doubleBuf = true; - noFrame = false; -} diff --git a/src/graphics/common/device.h b/src/graphics/common/device.h index 410596d..7aa4ce3 100644 --- a/src/graphics/common/device.h +++ b/src/graphics/common/device.h @@ -62,7 +62,16 @@ struct DeviceConfig DeviceConfig() { LoadDefault(); } //! Loads the default values - void LoadDefault(); + inline void LoadDefault() + { + width = 800; + height = 600; + bpp = 32; + fullScreen = false; + resizeable = false; + doubleBuf = true; + noFrame = false; + } }; @@ -326,10 +335,10 @@ public: //! Returns the current enable state of given texture stage virtual bool GetTextureEnabled(int index) = 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 params for texture stage with given index + virtual void SetTextureStageParams(int index, const Gfx::TextureStageParams ¶ms) = 0; + //! Returns the current params of texture stage with given index + virtual Gfx::TextureStageParams GetTextureStageParams(int index) = 0; //! Sets the texture factor to the given color value virtual void SetTextureFactor(const Gfx::Color &color) = 0; diff --git a/src/graphics/common/engine.cpp b/src/graphics/common/engine.cpp index d8c4f4e..dd1d23f 100644 --- a/src/graphics/common/engine.cpp +++ b/src/graphics/common/engine.cpp @@ -52,13 +52,13 @@ Gfx::CEngine::CEngine(CInstanceManager *iMan, CApplication *app) m_iMan->AddInstance(CLASS_ENGINE, this); m_app = app; - /*m_light = new Gfx::CLight(m_iMan, this); - m_text = new Gfx::CText(m_iMan, this); - m_particle = new Gfx::CParticle(m_iMan, this); - m_water = new Gfx::CWater(m_iMan, this); - m_cloud = new Gfx::CCloud(m_iMan, this); - m_lightning = new Gfx::CLightning(m_iMan, this); - m_planet = new Gfx::CPlanet(m_iMan, this);*/ + m_light = NULL; + m_text = NULL; + m_particle = NULL; + m_water = NULL; + m_cloud = NULL; + m_lightning = NULL; + m_planet = NULL; m_sound = NULL; m_terrain = NULL; @@ -185,27 +185,6 @@ Gfx::CEngine::~CEngine() m_app = NULL; m_device = NULL; - /*delete m_light; - m_light = NULL; - - delete m_text; - m_text = NULL; - - delete m_particle; - m_particle = NULL; - - delete m_water; - m_water = NULL; - - delete m_cloud; - m_cloud = NULL; - - delete m_lightning; - m_lightning = NULL; - - delete m_planet; - m_planet = NULL;*/ - m_sound = NULL; m_terrain = NULL; } @@ -220,16 +199,18 @@ std::string Gfx::CEngine::GetError() return m_error; } -bool Gfx::CEngine::BeforeCreateInit() -{ - // TODO - return true; -} - bool Gfx::CEngine::Create() { m_wasInit = true; + /*m_light = new Gfx::CLight(m_iMan, this); + m_text = new Gfx::CText(m_iMan, this); + m_particle = new Gfx::CParticle(m_iMan, this); + m_water = new Gfx::CWater(m_iMan, this); + m_cloud = new Gfx::CCloud(m_iMan, this); + m_lightning = new Gfx::CLightning(m_iMan, this); + m_planet = new Gfx::CPlanet(m_iMan, this);*/ + m_matWorldInterface.LoadIdentity(); m_matViewInterface.LoadIdentity(); Math::LoadOrthoProjectionMatrix(m_matProjInterface, 0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); @@ -241,6 +222,27 @@ void Gfx::CEngine::Destroy() { // TODO + /*delete m_light; + m_light = NULL; + + delete m_text; + m_text = NULL; + + delete m_particle; + m_particle = NULL; + + delete m_water; + m_water = NULL; + + delete m_cloud; + m_cloud = NULL; + + delete m_lightning; + m_lightning = NULL; + + delete m_planet; + m_planet = NULL;*/ + m_wasInit = false; } @@ -265,7 +267,7 @@ bool Gfx::CEngine::AfterDeviceSetInit() params.minFilter = Gfx::TEX_MIN_FILTER_NEAREST; params.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; params.mipmap = false; - CreateTexture("mouse.png", params); + m_miceTexture = CreateTexture("mouse.png", params); return true; } @@ -360,12 +362,12 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) m_device->SetTextureEnabled(0, true); m_device->SetTextureFactor(color); - Gfx::TextureParams params; + Gfx::TextureStageParams params; params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE; params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR; params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - m_device->SetTextureParams(0, params); + m_device->SetTextureStageParams(0, params); } else if (state & Gfx::ENG_RSTATE_TTEXTURE_WHITE) // The transparent white texture? { @@ -379,12 +381,12 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) m_device->SetTextureEnabled(0, true); m_device->SetTextureFactor(color.Inverse()); - Gfx::TextureParams params; + Gfx::TextureStageParams params; params.colorOperation = Gfx::TEX_MIX_OPER_ADD; params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE; params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR; params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - m_device->SetTextureParams(0, params); + m_device->SetTextureStageParams(0, params); } else if (state & Gfx::ENG_RSTATE_TCOLOR_BLACK) // The transparent black color? { @@ -398,7 +400,7 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) m_device->SetTextureFactor(color); m_device->SetTextureEnabled(0, true); - m_device->SetTextureParams(0, Gfx::TextureParams()); + m_device->SetTextureStageParams(0, Gfx::TextureStageParams()); } else if (state & Gfx::ENG_RSTATE_TCOLOR_WHITE) // The transparent white color? { @@ -412,7 +414,7 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) m_device->SetTextureFactor(color.Inverse()); m_device->SetTextureEnabled(0, true); - m_device->SetTextureParams(0, Gfx::TextureParams()); + m_device->SetTextureStageParams(0, Gfx::TextureStageParams()); } else if (state & Gfx::ENG_RSTATE_TDIFFUSE) // diffuse color as transparent? { @@ -454,7 +456,7 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); m_device->SetTextureEnabled(0, true); - m_device->SetTextureParams(0, Gfx::TextureParams()); + m_device->SetTextureStageParams(0, Gfx::TextureStageParams()); /*m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); @@ -620,8 +622,8 @@ void Gfx::CEngine::DrawMouse() material.diffuse = Gfx::Color(1.0f, 1.0f, 1.0f); material.ambient = Gfx::Color(0.5f, 0.5f, 0.5f); - SetMaterial(material); - SetTexture("mouse.png"); + m_device->SetMaterial(material); + m_device->SetTexture(0, m_miceTexture); int index = static_cast(m_mouseType); diff --git a/src/graphics/common/engine.h b/src/graphics/common/engine.h index de4a115..0413816 100644 --- a/src/graphics/common/engine.h +++ b/src/graphics/common/engine.h @@ -516,7 +516,6 @@ public: bool GetWasInit(); std::string GetError(); - bool BeforeCreateInit(); bool Create(); void Destroy(); @@ -970,6 +969,7 @@ protected: std::map m_revTexNameMap; Gfx::EngineMouse m_mice[Gfx::ENG_MOUSE_COUNT]; + Gfx::Texture m_miceTexture; Math::Point m_mouseSize; Gfx::EngineMouseType m_mouseType; Math::Point m_mousePos; diff --git a/src/graphics/common/texture.h b/src/graphics/common/texture.h index 83986ea..787c2bf 100644 --- a/src/graphics/common/texture.h +++ b/src/graphics/common/texture.h @@ -25,15 +25,21 @@ namespace Gfx { \brief Format of image data */ enum TexImgFormat { + //! Try to determine automatically (may not work) + TEX_IMG_AUTO, + //! RGB triplet, 3 bytes TEX_IMG_RGB, + //! BGR triplet, 3 bytes TEX_IMG_BGR, + //! RGBA triplet, 4 bytes TEX_IMG_RGBA, + //! BGRA triplet, 4 bytes TEX_IMG_BGRA }; /** \enum TexMinFilter - \brief Minification texture filter + \brief Texture minification filter Corresponds to OpenGL modes but should translate to DirectX too. */ enum TexMinFilter @@ -48,7 +54,7 @@ enum TexMinFilter /** \enum TexMagFilter - \brief Magnification texture filter */ + \brief Texture magnification filter */ enum TexMagFilter { TEX_MAG_FILTER_NEAREST, @@ -66,30 +72,42 @@ enum TexWrapMode /** \enum TexMixOperation - \brief Multitexture mixing operation - */ + \brief Multitexture mixing operation */ enum TexMixOperation { + //! Default operation on default params (modulate on computed & texture) + TEX_MIX_OPER_DEFAULT, + //! = Arg1 + TEX_MIX_OPER_REPLACE, + //! = Arg1 * Arg2 TEX_MIX_OPER_MODULATE, - TEX_MIX_OPER_ADD + //! = Arg1 + Arg2 + TEX_MIX_OPER_ADD, + //! = Arg1 - Arg2 + TEX_MIX_OPER_SUBTRACT }; /** \enum TexMixArgument - \brief Multitexture mixing argument - */ + \brief Multitexture mixing argument */ enum TexMixArgument { - TEX_MIX_ARG_CURRENT, + //! Color from current texture TEX_MIX_ARG_TEXTURE, - TEX_MIX_ARG_DIFFUSE, + //! Color computed by previous texture unit (current in DirectX; previous in OpenGL) + TEX_MIX_ARG_COMPUTED_COLOR, + //! (Source) color of textured fragment (diffuse in DirectX; primary color in OpenGL) + TEX_MIX_ARG_SRC_COLOR, + //! Constant color (texture factor in DirectX; texture env color in OpenGL) TEX_MIX_ARG_FACTOR }; /** \struct TextureCreateParams \brief Parameters for texture creation - */ + + These params define how particular texture is created and later displayed. + They must be specified at texture creation time and cannot be changed later. */ struct TextureCreateParams { //! Whether to generate mipmaps @@ -100,10 +118,6 @@ struct TextureCreateParams Gfx::TexMinFilter minFilter; //! Magnification filter Gfx::TexMagFilter magFilter; - //! Wrap S coord mode - Gfx::TexWrapMode wrapS; - //! Wrap T coord mode - Gfx::TexWrapMode wrapT; //! Constructor; calls LoadDefault() TextureCreateParams() @@ -117,17 +131,16 @@ struct TextureCreateParams minFilter = Gfx::TEX_MIN_FILTER_NEAREST; magFilter = Gfx::TEX_MAG_FILTER_NEAREST; - - wrapS = Gfx::TEX_WRAP_REPEAT; - wrapT = Gfx::TEX_WRAP_REPEAT; } }; /** - \struct TextureParams - \brief Parameters for texture creation - */ -struct TextureParams + \struct TextureStageParams + \brief Parameters for a texture unit + + These params define the behavior of texturing units (stages). + They can be changed freely and are feature of graphics engine, not any particular texture. */ +struct TextureStageParams { //! Mixing operation done on color values Gfx::TexMixOperation colorOperation; @@ -141,30 +154,41 @@ struct TextureParams Gfx::TexMixArgument alphaArg1; //! 2nd argument of alpha operations Gfx::TexMixArgument alphaArg2; + //! Wrap mode for 1st tex coord + Gfx::TexWrapMode wrapS; + //! Wrap mode for 2nd tex coord + Gfx::TexWrapMode wrapT; //! Constructor; calls LoadDefault() - TextureParams() + TextureStageParams() { LoadDefault(); } //! Loads the default values inline void LoadDefault() { - colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - colorArg1 = Gfx::TEX_MIX_ARG_CURRENT; + colorOperation = Gfx::TEX_MIX_OPER_DEFAULT; + colorArg1 = Gfx::TEX_MIX_ARG_COMPUTED_COLOR; colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; - alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - alphaArg1 = Gfx::TEX_MIX_ARG_CURRENT; + alphaOperation = Gfx::TEX_MIX_OPER_DEFAULT; + alphaArg1 = Gfx::TEX_MIX_ARG_COMPUTED_COLOR; alphaArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + + wrapS = wrapT = Gfx::TEX_WRAP_REPEAT; } }; -/** \struct Texture*/ +/** + \struct Texture + \brief Info about a texture + + Identifies (through id) a texture created in graphics engine. + Also contains some additional data. */ struct Texture { - //! Whether the texture was loaded + //! Whether the texture (ID) is valid bool valid; - //! Id of the texture in graphics engine + //! ID of the texture in graphics engine unsigned int id; //! Width of texture int width; -- cgit v1.2.3-1-g7c22