From 4ddcd9f810fa588ccf90442f7b4e5ddf385e85f2 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 26 Jul 2012 22:26:19 +0200 Subject: Change of paths in src/graphics - moved abstract core to src/graphics/core - moved proper graphics engine to src/graphics/engine --- src/graphics/core/README.txt | 6 + src/graphics/core/color.cpp | 103 +++++++++++ src/graphics/core/color.h | 98 ++++++++++ src/graphics/core/device.h | 414 +++++++++++++++++++++++++++++++++++++++++++ src/graphics/core/light.h | 91 ++++++++++ src/graphics/core/material.h | 47 +++++ src/graphics/core/texture.h | 237 +++++++++++++++++++++++++ src/graphics/core/vertex.h | 141 +++++++++++++++ 8 files changed, 1137 insertions(+) create mode 100644 src/graphics/core/README.txt create mode 100644 src/graphics/core/color.cpp create mode 100644 src/graphics/core/color.h create mode 100644 src/graphics/core/device.h create mode 100644 src/graphics/core/light.h create mode 100644 src/graphics/core/material.h create mode 100644 src/graphics/core/texture.h create mode 100644 src/graphics/core/vertex.h (limited to 'src/graphics/core') diff --git a/src/graphics/core/README.txt b/src/graphics/core/README.txt new file mode 100644 index 0000000..12beef9 --- /dev/null +++ b/src/graphics/core/README.txt @@ -0,0 +1,6 @@ +src/graphics/core + +Abstract core of graphics engine + +Core types, enums, structs and CDevice abstract class that define +the abstract graphics device used in graphics engine diff --git a/src/graphics/core/color.cpp b/src/graphics/core/color.cpp new file mode 100644 index 0000000..8dec0e4 --- /dev/null +++ b/src/graphics/core/color.cpp @@ -0,0 +1,103 @@ +// * This file is part of the COLOBOT source code +// * 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/. + +// color.cpp + +#include "graphics/core/color.h" + +#include "math/func.h" + + +Gfx::ColorHSV Gfx::RGB2HSV(Gfx::Color color) +{ + Gfx::ColorHSV result; + + float min = Math::Min(color.r, color.g, color.b); + float max = Math::Max(color.r, color.g, color.b); + + result.v = max; // intensity + + if ( max == 0.0f ) + { + result.s = 0.0f; // saturation + result.h = 0.0f; // undefined color! + } + else + { + float delta = max-min; + result.s = delta/max; // saturation + + if ( color.r == max ) // between yellow & magenta + { + result.h = (color.g-color.b)/delta; + } + else if ( color.g == max ) // between cyan & yellow + { + result.h = 2.0f+(color.b-color.r)/delta; + } + else // between magenta & cyan + { + result.h = 4.0f+(color.r-color.g)/delta; + } + + result.h *= 60.0f; // in degrees + if ( result.h < 0.0f ) result.h += 360.0f; + result.h /= 360.0f; // 0..1 + } + + return result; +} + +Gfx::Color Gfx::HSV2RGB(Gfx::ColorHSV color) +{ + Gfx::Color result; + + color.h = Math::Norm(color.h)*360.0f; + color.s = Math::Norm(color.s); + color.v = Math::Norm(color.v); + + if ( color.s == 0.0f ) // zero saturation? + { + result.r = color.v; + result.g = color.v; + result.b = color.v; // gray + } + else + { + if ( color.h == 360.0f ) color.h = 0.0f; + color.h /= 60.0f; + int i = (int)color.h; // integer part (0 .. 5) + float f = color.h-i; // fractional part + + float v = color.v; + float p = color.v*(1.0f-color.s); + float q = color.v*(1.0f-(color.s*f)); + float t = color.v*(1.0f-(color.s*(1.0f-f))); + + switch (i) + { + case 0: result.r=v; result.g=t; result.b=p; break; + case 1: result.r=q; result.g=v; result.b=p; break; + case 2: result.r=p; result.g=v; result.b=t; break; + case 3: result.r=p; result.g=q; result.b=v; break; + case 4: result.r=t; result.g=p; result.b=v; break; + case 5: result.r=v; result.g=p; result.b=q; break; + } + } + + return result; +} + diff --git a/src/graphics/core/color.h b/src/graphics/core/color.h new file mode 100644 index 0000000..907a3b9 --- /dev/null +++ b/src/graphics/core/color.h @@ -0,0 +1,98 @@ +// * This file is part of the COLOBOT source code +// * 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/. + +// color.h + +#pragma once + + +#include + + +namespace Gfx { + +/** + \struct Color + \brief RGBA color */ +struct Color +{ + //! Red, green, blue and alpha components + float r, g, b, a; + + //! Constructor; default values are (0,0,0,0) = black + Color(float aR = 0.0f, float aG = 0.0f, float aB = 0.0f, float aA = 0.0f) + : r(aR), g(aG), b(aB), a(aA) {} + + inline Gfx::Color Inverse() const + { + return Gfx::Color(1.0f - r, 1.0f - g, 1.0f - b, 1.0f - a); + } + + //! Returns the struct cast to \c float* array; use with care! + inline float* Array() + { + return (float*)this; + } + + //! Returns the struct cast to const float* array; use with care! + inline const float* Array() const + { + return (const float*)this; + } + + //! Returns a string (r, g, b, a) + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(" << r << ", " << g << ", " << b << ", " << a << ")"; + return s.str(); + } + + inline bool operator==(const Gfx::Color &other) const + { + return r == other.r && g == other.g && b == other.b && a == other.a; + } +}; + +/** + \struct ColorHSV + \brief HSV color */ +struct ColorHSV +{ + float h, s, v; + + ColorHSV(float aH = 0.0f, float aS = 0.0f, float aV = 0.0f) + : h(aH), s(aS), v(aV) {} + + //! Returns a string "(h, s, v)" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(" << h << ", " << s << ", " << v << ")"; + return s.str(); + } +}; + +//! Converts a RGB color to HSV color +Gfx::ColorHSV RGB2HSV(Gfx::Color color); + +//! Converts a HSV color to RGB color +Gfx::Color HSV2RGB(Gfx::ColorHSV color); + +}; // namespace Gfx + diff --git a/src/graphics/core/device.h b/src/graphics/core/device.h new file mode 100644 index 0000000..ae612b7 --- /dev/null +++ b/src/graphics/core/device.h @@ -0,0 +1,414 @@ +// * 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.h + +#pragma once + + +#include "graphics/core/color.h" +#include "graphics/core/light.h" +#include "graphics/core/material.h" +#include "graphics/core/texture.h" +#include "graphics/core/vertex.h" +#include "math/matrix.h" + +#include + + +class CImage; + + +namespace Gfx { + +/** + \struct DeviceConfig + \brief General config for graphics device + + These settings are common window options set by SDL. +*/ +struct DeviceConfig +{ + //! Screen width + int width; + //! Screen height + int height; + //! Bits per pixel + int bpp; + //! Full screen + bool fullScreen; + //! Resizeable window + bool resizeable; + //! Double buffering + bool doubleBuf; + //! No window frame (also set with full screen) + bool noFrame; + + //! Constructor calls LoadDefault() + DeviceConfig() { LoadDefault(); } + + //! Loads the default values + inline void LoadDefault() + { + width = 800; + height = 600; + bpp = 32; + fullScreen = false; + resizeable = false; + doubleBuf = true; + noFrame = false; + } +}; + + +/** + \enum TransformType + \brief Type of transformation in rendering pipeline + + These correspond to DirectX's three transformation matrices. */ +enum TransformType +{ + TRANSFORM_WORLD, + TRANSFORM_VIEW, + TRANSFORM_PROJECTION +}; + +/** + \enum RenderState + \brief Render states that can be enabled/disabled */ +enum RenderState +{ + RENDER_STATE_LIGHTING, + RENDER_STATE_TEXTURING, + RENDER_STATE_BLENDING, + RENDER_STATE_FOG, + RENDER_STATE_DEPTH_TEST, + RENDER_STATE_DEPTH_WRITE, + RENDER_STATE_ALPHA_TEST, + RENDER_STATE_CULLING, + RENDER_STATE_DITHERING +}; + +/** + \enum CompFunc + \brief Type of function used to compare values */ +enum CompFunc +{ + COMP_FUNC_NEVER, + COMP_FUNC_LESS, + COMP_FUNC_EQUAL, + COMP_FUNC_NOTEQUAL, + COMP_FUNC_LEQUAL, + COMP_FUNC_GREATER, + COMP_FUNC_GEQUAL, + COMP_FUNC_ALWAYS +}; + +/** + \enum BlendFunc + \brief Type of blending function */ +enum BlendFunc +{ + BLEND_ZERO, + BLEND_ONE, + BLEND_SRC_COLOR, + BLEND_INV_SRC_COLOR, + BLEND_DST_COLOR, + BLEND_INV_DST_COLOR, + BLEND_SRC_ALPHA, + BLEND_INV_SRC_ALPHA, + BLEND_DST_ALPHA, + BLEND_INV_DST_ALPHA, + BLEND_SRC_ALPHA_SATURATE +}; + +/** + \enum FogMode + \brief Type of fog calculation function */ +enum FogMode +{ + FOG_LINEAR, + FOG_EXP, + FOG_EXP2 +}; + +/** + \enum CullMode + \brief Culling mode for polygons */ +enum CullMode +{ + //! Cull clockwise side + CULL_CW, + //! Cull counter-clockwise side + CULL_CCW +}; + +/** + \enum ShadeModel + \brief Shade model used in rendering */ +enum ShadeModel +{ + SHADE_FLAT, + SHADE_SMOOTH +}; + +/** + \enum FillMode + \brief Polygon fill mode */ +enum FillMode +{ + //! Draw only points + FILL_POINT, + //! Draw only lines + FILL_LINES, + //! Draw full polygons + FILL_FILL +}; + +/** + \enum PrimitiveType + \brief Type of primitive to render + + Only these two types are used. */ +enum PrimitiveType +{ + PRIMITIVE_LINES, + PRIMITIVE_TRIANGLES, + PRIMITIVE_TRIANGLE_STRIP +}; + +/** + \enum IntersectPlane + \brief Intersection plane of projection volume + + These flags can be OR'd together. */ +enum IntersectPlane +{ + INTERSECT_PLANE_LEFT = 0x01, + INTERSECT_PLANE_RIGHT = 0x02, + INTERSECT_PLANE_TOP = 0x04, + INTERSECT_PLANE_BOTTOM = 0x08, + INTERSECT_PLANE_FRONT = 0x10, + INTERSECT_PLANE_BACK = 0x20, + INTERSECT_PLANE_ALL = INTERSECT_PLANE_LEFT | INTERSECT_PLANE_RIGHT | + INTERSECT_PLANE_TOP | INTERSECT_PLANE_BOTTOM | + INTERSECT_PLANE_FRONT | INTERSECT_PLANE_BACK +}; + +/* + +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() or CreateTexture() for some params + +Params from enum in struct TextureCreateParams or 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 + + It is based on DIRECT3DDEVICE class from DirectX to make it easier to port existing code. + It encapsulates the general graphics device state and provides a common interface + to graphics-specific functions which will be used throughout the program, + both in CEngine class and in UI classes. Note that it doesn't contain all functions from DirectX, + only those that were used in old code. + + */ +class CDevice +{ +public: + virtual ~CDevice() {} + + //! Initializes the device, setting the initial state + virtual bool Create() = 0; + //! Destroys the device, releasing every acquired resource + virtual void Destroy() = 0; + + //! Returns whether the device has been initialized + virtual bool GetWasInit() = 0; + //! Returns the last encountered error + virtual std::string GetError() = 0; + + //! Begins drawing the 3D scene + virtual void BeginScene() = 0; + //! Ends drawing the 3D scene + virtual void EndScene() = 0; + + //! Clears the screen to blank + virtual void Clear() = 0; + + //! Sets the transform matrix of given type + virtual void SetTransform(TransformType type, const Math::Matrix &matrix) = 0; + //! Returns the current transform matrix of given type + virtual const Math::Matrix& GetTransform(TransformType type) = 0; + //! Multiplies the current transform matrix of given type by given matrix + virtual void MultiplyTransform(TransformType type, const Math::Matrix &matrix) = 0; + + //! Sets the current material + virtual void SetMaterial(const Gfx::Material &material) = 0; + //! Returns the current material + virtual const Gfx::Material& GetMaterial() = 0; + + //! Returns the maximum number of lights available + virtual int GetMaxLightCount() = 0; + //! Sets the light at given index + virtual void SetLight(int index, const Gfx::Light &light) = 0; + //! Returns the current light at given index + virtual const Gfx::Light& GetLight(int index) = 0; + //! Enables/disables the light at given index + virtual void SetLightEnabled(int index, bool enabled) = 0; + //! Returns the current enable state of light at given index + virtual bool GetLightEnabled(int index) = 0; + + //! Creates a texture from image; the image can be safely removed after that + virtual Gfx::Texture CreateTexture(CImage *image, const Gfx::TextureCreateParams ¶ms) = 0; + //! Deletes a given texture, freeing it from video memory + virtual void DestroyTexture(const Gfx::Texture &texture) = 0; + //! Deletes all textures created so far + virtual void DestroyAllTextures() = 0; + + //! Returns the maximum number of multitexture stages + virtual int GetMaxTextureCount() = 0; + //! Sets the (multi)texture at given index + virtual void SetTexture(int index, const 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; + + //! 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; + //! Returns the current texture factor + virtual Gfx::Color GetTextureFactor() = 0; + + //! Renders primitive composed of vertices with single texture + virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::Vertex *vertices , int vertexCount) = 0; + //! Renders primitive composed of vertices with color information and single texture + virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexCol *vertices , int vertexCount) = 0; + //! Renders primitive composed of vertices with multitexturing (2 textures) + virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexTex2 *vertices, int vertexCount) = 0; + + //! Tests whether a sphere intersects the 6 clipping planes of projection volume + virtual int ComputeSphereVisibility(const Math::Vector ¢er, float radius) = 0; + + //! Enables/disables the given render state + virtual void SetRenderState(Gfx::RenderState state, bool enabled) = 0; + //! Returns the current setting of given render state + virtual bool GetRenderState(Gfx::RenderState state) = 0; + + //! Sets the function of depth test + virtual void SetDepthTestFunc(Gfx::CompFunc func) = 0; + //! Returns the current function of depth test + virtual Gfx::CompFunc GetDepthTestFunc() = 0; + + //! Sets the depth bias (constant value added to Z-coords) + virtual void SetDepthBias(float factor) = 0; + //! Returns the current depth bias + virtual float GetDepthBias() = 0; + + //! Sets the alpha test function and reference value + virtual void SetAlphaTestFunc(Gfx::CompFunc func, float refValue) = 0; + //! Returns the current alpha test function and reference value + virtual void GetAlphaTestFunc(Gfx::CompFunc &func, float &refValue) = 0; + + //! Sets the blending functions for source and destination operations + virtual void SetBlendFunc(Gfx::BlendFunc srcBlend, Gfx::BlendFunc dstBlend) = 0; + //! Returns the current blending functions for source and destination operations + virtual void GetBlendFunc(Gfx::BlendFunc &srcBlend, Gfx::BlendFunc &dstBlend) = 0; + + //! Sets the clear color + virtual void SetClearColor(const Gfx::Color &color) = 0; + //! Returns the current clear color + virtual Gfx::Color GetClearColor() = 0; + + //! Sets the global ambient color + virtual void SetGlobalAmbient(const Gfx::Color &color) = 0; + //! Returns the global ambient color + virtual Gfx::Color GetGlobalAmbient() = 0; + + //! Sets the fog parameters: mode, color, start distance, end distance and density (for exp models) + virtual void SetFogParams(Gfx::FogMode mode, const Gfx::Color &color, float start, float end, float density) = 0; + //! Returns the current fog parameters: mode, color, start distance, end distance and density (for exp models) + virtual void GetFogParams(Gfx::FogMode &mode, Gfx::Color &color, float &start, float &end, float &density) = 0; + + //! Sets the current cull mode + virtual void SetCullMode(Gfx::CullMode mode) = 0; + //! 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 + virtual Gfx::FillMode GetFillMode() = 0; +}; + +}; // namespace Gfx diff --git a/src/graphics/core/light.h b/src/graphics/core/light.h new file mode 100644 index 0000000..b787cb2 --- /dev/null +++ b/src/graphics/core/light.h @@ -0,0 +1,91 @@ +// * 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/. + +// light.h + +#pragma once + + +#include "graphics/core/color.h" +#include "math/vector.h" + + +namespace Gfx { + +/** + \enum LightType + \brief Type of light in 3D scene */ +enum LightType +{ + LIGHT_POINT, + LIGHT_SPOT, + LIGHT_DIRECTIONAL +}; + +/** + \struct Light + \brief Properties of light in 3D scene + + This structure was created as analog to DirectX's D3DLIGHT. */ +struct Light +{ + //! Type of light source + Gfx::LightType type; + //! Color of ambient light + Gfx::Color ambient; + //! Color of diffuse light + Gfx::Color diffuse; + //! Color of specular light + Gfx::Color specular; + //! Position in world space (for point & spot lights) + Math::Vector position; + //! Direction in world space (for directional & spot lights) + Math::Vector direction; + //! Constant attenuation factor + float attenuation0; + //! Linear attenuation factor + float attenuation1; + //! Quadratic attenuation factor + float attenuation2; + //! Angle of spotlight cone (0-90 degrees) + float spotAngle; + //! Intensity of spotlight (0 = uniform; 128 = most intense) + float spotIntensity; + + //! Constructor; calls LoadDefault() + Light() + { + LoadDefault(); + } + + //! Loads default values + void LoadDefault() + { + type = LIGHT_POINT; + ambient = Gfx::Color(0.4f, 0.4f, 0.4f); + diffuse = Gfx::Color(0.8f, 0.8f, 0.8f); + specular = Gfx::Color(1.0f, 1.0f, 1.0f); + position = Math::Vector(0.0f, 0.0f, 0.0f); + direction = Math::Vector(0.0f, 0.0f, 1.0f); + attenuation0 = 1.0f; + attenuation1 = attenuation2 = 0.0f; + spotAngle = 90.0f; + spotIntensity = 0.0f; + } +}; + +}; // namespace Gfx diff --git a/src/graphics/core/material.h b/src/graphics/core/material.h new file mode 100644 index 0000000..31b42f3 --- /dev/null +++ b/src/graphics/core/material.h @@ -0,0 +1,47 @@ +// * This file is part of the COLOBOT source code +// * 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/. + +// material.h + +#pragma once + + +#include "graphics/core/color.h" + + +namespace Gfx { + +/** + * \struct Material + * \brief Material of a surface + * + * This structure was created as analog to DirectX's D3DMATERIAL. + * + * It contains values of 3 material colors: diffuse, ambient and specular. + * In D3DMATERIAL there are other fields, but they are not used + * by the graphics engine. + */ +struct Material +{ + //! Diffuse color + Gfx::Color diffuse; + //! Ambient color + Gfx::Color ambient; + //! Specular color + Gfx::Color specular; +}; + +}; // namespace Gfx diff --git a/src/graphics/core/texture.h b/src/graphics/core/texture.h new file mode 100644 index 0000000..787c2bf --- /dev/null +++ b/src/graphics/core/texture.h @@ -0,0 +1,237 @@ +// * This file is part of the COLOBOT source code +// * 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/. + +// texture.h + +#pragma once + +namespace Gfx { + +/** + \enum TexImgFormat + \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 Texture minification 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 Texture magnification 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 +{ + //! Default operation on default params (modulate on computed & texture) + TEX_MIX_OPER_DEFAULT, + //! = Arg1 + TEX_MIX_OPER_REPLACE, + //! = Arg1 * Arg2 + TEX_MIX_OPER_MODULATE, + //! = Arg1 + Arg2 + TEX_MIX_OPER_ADD, + //! = Arg1 - Arg2 + TEX_MIX_OPER_SUBTRACT +}; + +/** + \enum TexMixArgument + \brief Multitexture mixing argument */ +enum TexMixArgument +{ + //! Color from current texture + TEX_MIX_ARG_TEXTURE, + //! 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 + bool mipmap; + //! Format of source image data + Gfx::TexImgFormat format; + //! Minification filter + Gfx::TexMinFilter minFilter; + //! Magnification filter + Gfx::TexMagFilter magFilter; + + //! Constructor; calls LoadDefault() + TextureCreateParams() + { LoadDefault(); } + + //! Loads the default values + inline void LoadDefault() + { + format = Gfx::TEX_IMG_RGB; + mipmap = false; + + minFilter = Gfx::TEX_MIN_FILTER_NEAREST; + magFilter = Gfx::TEX_MAG_FILTER_NEAREST; + } +}; + +/** + \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; + //! 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; + //! Wrap mode for 1st tex coord + Gfx::TexWrapMode wrapS; + //! Wrap mode for 2nd tex coord + Gfx::TexWrapMode wrapT; + + //! Constructor; calls LoadDefault() + TextureStageParams() + { LoadDefault(); } + + //! Loads the default values + inline void LoadDefault() + { + colorOperation = Gfx::TEX_MIX_OPER_DEFAULT; + colorArg1 = Gfx::TEX_MIX_ARG_COMPUTED_COLOR; + colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + + 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 + \brief Info about a texture + + Identifies (through id) a texture created in graphics engine. + Also contains some additional data. */ +struct Texture +{ + //! Whether the texture (ID) is valid + bool valid; + //! ID of the texture in graphics engine + unsigned int id; + //! Width of texture + int width; + //! Height of texture + int height; + //! Whether the texture has alpha channel + bool alpha; + + Texture() + { + valid = false; + id = 0; + width = height = 0; + alpha = false; + } + + //! Comparator for use in texture maps and sets + inline bool operator<(const Gfx::Texture &other) const + { + // Invalid textures are always "less than" every other texture + + if ( (!valid) && (!other.valid) ) + return false; + + if (!valid) + return true; + + if (!other.valid) + return false; + + return id < other.id; + } + + //! Comparator + inline bool operator==(const Gfx::Texture &other) const + { + if (valid != other.valid) + return false; + if ( (!valid) && (!other.valid) ) + return true; + + return id == other.id; + } +}; + +}; // namespace Gfx diff --git a/src/graphics/core/vertex.h b/src/graphics/core/vertex.h new file mode 100644 index 0000000..b7fab1c --- /dev/null +++ b/src/graphics/core/vertex.h @@ -0,0 +1,141 @@ +// * This file is part of the COLOBOT source code +// * 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/. + +// vertex.h + +#pragma once + + +#include "graphics/core/color.h" +#include "math/vector.h" +#include "math/point.h" + +#include + +namespace Gfx { + +/** + * \struct Vertex + * \brief Vertex of a primitive + * + * This structure was created as analog to DirectX's D3DVERTEX. + * + * It contains: + * - vertex coordinates (x,y,z) as Math::Vector, + * - normal coordinates (nx,ny,nz) as Math::Vector + * - texture coordinates (u,v) as Math::Point. + */ +struct Vertex +{ + Math::Vector coord; + Math::Vector normal; + Math::Point texCoord; + + Vertex(Math::Vector aCoord = Math::Vector(), + Math::Vector aNormal = Math::Vector(), + Math::Point aTexCoord = Math::Point()) + : coord(aCoord), normal(aNormal), texCoord(aTexCoord) {} + + + //! Returns a string "(c: [...], n: [...], tc: [...])" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(c: " << coord.ToString() << ", n: " << normal.ToString() + << ", tc: " << texCoord.ToString() << ")"; + return s.str(); + } +}; + +/** + * \struct VertexCol + * \brief Vertex with color information + * + * This structure was created as analog to DirectX's D3DLVERTEX. + * + * It contains: + * - vertex coordinates (x,y,z) as Math::Vector, + * - RGBA color as Gfx::Color, + * - RGBA specular color as Gfx::Color, + * - texture coordinates (u,v) as Math::Point. + */ +struct VertexCol +{ + Math::Vector coord; + Gfx::Color color; + Gfx::Color specular; + Math::Point texCoord; + + VertexCol(Math::Vector aCoord = Math::Vector(), + Gfx::Color aColor = Gfx::Color(), + Gfx::Color aSpecular = Gfx::Color(), + Math::Point aTexCoord = Math::Point()) + : coord(aCoord), color(aColor), specular(aSpecular), texCoord(aTexCoord) {} + + //! Returns a string "(c: [...], col: [...], sp: [...], tc: [...])" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(c: " << coord.ToString() << ", col: " << color.ToString() << ", sp: " + << specular.ToString() << ", tc: " << texCoord.ToString() << ")"; + return s.str(); + } +}; + + +/** + * \struct VertexTex2 + * \brief Vertex with secondary texture coordinates + * + * In addition to fields from Gfx::Vector, it contains + * secondary texture coordinates (u2, v2) as Math::Point + */ +struct VertexTex2 +{ + Math::Vector coord; + Math::Vector normal; + Math::Point texCoord; + Math::Point texCoord2; + + VertexTex2(Math::Vector aCoord = Math::Vector(), + Math::Vector aNormal = Math::Vector(), + Math::Point aTexCoord = Math::Point(), + Math::Point aTexCoord2 = Math::Point()) + : coord(aCoord), normal(aNormal), texCoord(aTexCoord), texCoord2(aTexCoord2) {} + + //! Sets the fields from Gfx::Vertex with texCoord2 = (0,0) + void FromVertex(const Gfx::Vertex &v) + { + coord = v.coord; + normal = v.normal; + texCoord = v.texCoord; + texCoord2 = Math::Point(); + } + + //! Returns a string "(c: [...], n: [...], tc: [...], tc2: [...])" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(c: " << coord.ToString() << ", n: " << normal.ToString() + << ", tc: " << texCoord.ToString() << ", tc2: " << texCoord2.ToString() << ")"; + return s.str(); + } +}; + +}; // namespace Gfx -- cgit v1.2.3-1-g7c22