From 32043605153543bd72eb012ff310367299ad4e8f Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Fri, 6 Jul 2012 19:00:22 +0200 Subject: Refactoring in math & texture modules - moved texture-related structs to texture.h & code to texture.cpp - cleaned up texture test code - added Math:: namespace qualifiers to math modules for clarity --- src/CMakeLists.txt | 1 + src/graphics/common/device.cpp | 23 ---- src/graphics/common/device.h | 109 ----------------- src/graphics/common/texture.cpp | 43 +++++++ src/graphics/common/texture.h | 109 +++++++++++++++++ src/graphics/opengl/gldevice.cpp | 2 +- src/graphics/opengl/test/texture_test.cpp | 186 +----------------------------- src/math/func.h | 28 ++--- src/math/geometry.h | 111 +++++++++--------- src/math/matrix.h | 14 +-- src/math/vector.h | 12 +- 11 files changed, 239 insertions(+), 399 deletions(-) create mode 100644 src/graphics/common/texture.cpp (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c72bcd9..fada14c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -52,6 +52,7 @@ graphics/common/planet.cpp graphics/common/pyro.cpp graphics/common/terrain.cpp graphics/common/text.cpp +graphics/common/texture.cpp graphics/common/water.cpp graphics/opengl/gldevice.cpp graphics/opengl/glengine.cpp diff --git a/src/graphics/common/device.cpp b/src/graphics/common/device.cpp index 79102b2..fcd4318 100644 --- a/src/graphics/common/device.cpp +++ b/src/graphics/common/device.cpp @@ -31,26 +31,3 @@ void Gfx::DeviceConfig::LoadDefault() doubleBuf = true; noFrame = false; } - -void Gfx::TextureCreateParams::LoadDefault() -{ - alpha = false; - mipmap = false; - - minFilter = Gfx::TEX_MIN_FILTER_NEAREST; - magFilter = Gfx::TEX_MAG_FILTER_NEAREST; - - wrapS = Gfx::TEX_WRAP_REPEAT; - wrapT = Gfx::TEX_WRAP_REPEAT; -} - -void Gfx::TextureParams::LoadDefault() -{ - 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 41181aa..6a71a8a 100644 --- a/src/graphics/common/device.h +++ b/src/graphics/common/device.h @@ -181,115 +181,6 @@ 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 -}; - -/** - \struct TextureCreateParams - \brief Parameters for texture creation - */ -struct TextureCreateParams -{ - //! Whether the texture image contains alpha - bool alpha; - //! Whether to generate mipmaps - bool mipmap; - //! Minification filter - 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() - { LoadDefault(); } - - //! Loads the default values - void LoadDefault(); -}; - -/** - \struct TextureParams - \brief Parameters for texture creation - */ -struct TextureParams -{ - //! 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: diff --git a/src/graphics/common/texture.cpp b/src/graphics/common/texture.cpp new file mode 100644 index 0000000..50e71cd --- /dev/null +++ b/src/graphics/common/texture.cpp @@ -0,0 +1,43 @@ +// * 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.cpp + +#include "graphics/common/texture.h" + + +void Gfx::TextureCreateParams::LoadDefault() +{ + alpha = false; + mipmap = false; + + minFilter = Gfx::TEX_MIN_FILTER_NEAREST; + magFilter = Gfx::TEX_MAG_FILTER_NEAREST; + + wrapS = Gfx::TEX_WRAP_REPEAT; + wrapT = Gfx::TEX_WRAP_REPEAT; +} + +void Gfx::TextureParams::LoadDefault() +{ + 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/texture.h b/src/graphics/common/texture.h index 55d5c70..55b2fc2 100644 --- a/src/graphics/common/texture.h +++ b/src/graphics/common/texture.h @@ -20,6 +20,115 @@ namespace Gfx { +/** + \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 +}; + +/** + \struct TextureCreateParams + \brief Parameters for texture creation + */ +struct TextureCreateParams +{ + //! Whether the texture image contains alpha + bool alpha; + //! Whether to generate mipmaps + bool mipmap; + //! Minification filter + 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() + { LoadDefault(); } + + //! Loads the default values + void LoadDefault(); +}; + +/** + \struct TextureParams + \brief Parameters for texture creation + */ +struct TextureParams +{ + //! 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(); +}; + /** \struct Texture*/ struct Texture { diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index 3182dfc..f253568 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -135,7 +135,7 @@ bool Gfx::CGLDevice::Create() int maxTextures = 0; glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &maxTextures); - m_textures = std::vector (maxTextures, NULL); + m_textures = std::vector (maxTextures, (Gfx::Texture*)(NULL)); m_texturesEnabled = std::vector (maxTextures, false); m_texturesParams = std::vector(maxTextures, Gfx::TextureParams()); diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index 022cf87..764e127 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -7,11 +7,6 @@ #include #include -#include - - -#define DEV 1 - void Init(Gfx::CGLDevice *device) { @@ -68,8 +63,6 @@ void Render(Gfx::CGLDevice *device) { device->BeginScene(); - glFlush(); - Math::Matrix ortho; Math::LoadOrthoProjectionMatrix(ortho, -10, 10, -10, 10); device->SetTransform(Gfx::TRANSFORM_PROJECTION, ortho); @@ -119,172 +112,6 @@ void Render(Gfx::CGLDevice *device) device->EndScene(); } -void InitGL() -{ - CImage img1; - if (! img1.Load("tex1.png")) - { - std::string err = img1.GetError(); - GetLogger()->Error("texture 1 not loaded, error: %d!\n", err.c_str()); - } - CImage img2; - if (! img2.Load("tex2.png")) - { - std::string err = img2.GetError(); - GetLogger()->Error("texture 2 not loaded, error: %d!\n", err.c_str()); - } - - unsigned int textureHandle1 = 0; - - glActiveTexture(GL_TEXTURE0_ARB); - glEnable(GL_TEXTURE_2D); - - glPixelStorei(GL_UNPACK_ALIGNMENT,1); - - glGenTextures(1, &textureHandle1); - glBindTexture(GL_TEXTURE_2D, textureHandle1); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img1.GetData()->surface->w, img1.GetData()->surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img1.GetData()->surface->pixels); - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_REPLACE); - glTexEnvi(GL_TEXTURE_2D, GL_SRC0_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_RGB, GL_SRC_COLOR); - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_REPLACE); - glTexEnvi(GL_TEXTURE_2D, GL_SRC0_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - - - - unsigned int textureHandle2 = 0; - - glActiveTexture(GL_TEXTURE1_ARB); - glEnable(GL_TEXTURE_2D); - - glPixelStorei(GL_UNPACK_ALIGNMENT,1); - - glGenTextures(1, &textureHandle2); - glBindTexture(GL_TEXTURE_2D, textureHandle2); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img2.GetData()->surface->w, img2.GetData()->surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img2.GetData()->surface->pixels); - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_MODULATE); - glTexEnvi(GL_TEXTURE_2D, GL_SRC0_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_RGB, GL_SRC_COLOR); - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_MODULATE); - glTexEnvi(GL_TEXTURE_2D, GL_SRC0_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - - - - glMatrixMode(GL_PROJECTION); - glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, -1.0f, 1.0f); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); -} - -void RenderGL() -{ - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glLoadIdentity(); - - glColor3f(1.0f, 1.0f, 1.0f); - - glPushMatrix(); - glTranslatef(-4.0f, 4.0f, 0.0f); - - glActiveTextureARB(GL_TEXTURE0_ARB); - glEnable(GL_TEXTURE_2D); - glActiveTextureARB(GL_TEXTURE1_ARB); - glDisable(GL_TEXTURE_2D); - - glBegin(GL_QUADS); - { - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); - glVertex2f(-2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); - glVertex2f(2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); - glVertex2f(2.0f, 2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); - glVertex2f(-2.0f, 2.0f); - } - glEnd(); - - glPopMatrix(); - glPushMatrix(); - glTranslatef( 4.0f, 4.0f, 0.0f); - - glActiveTextureARB(GL_TEXTURE0_ARB); - glDisable(GL_TEXTURE_2D); - glActiveTextureARB(GL_TEXTURE1_ARB); - glEnable(GL_TEXTURE_2D); - - glBegin(GL_QUADS); - { - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); - glVertex2f(-2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); - glVertex2f(2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); - glVertex2f(2.0f, 2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); - glVertex2f(-2.0f, 2.0f); - } - glEnd(); - - glPopMatrix(); - glPushMatrix(); - glTranslatef( 0.0f, -4.0f, 0.0f); - - glActiveTextureARB(GL_TEXTURE0_ARB); - glEnable(GL_TEXTURE_2D); - glActiveTextureARB(GL_TEXTURE1_ARB); - glEnable(GL_TEXTURE_2D); - - glBegin(GL_QUADS); - { - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); - glVertex2f(-2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); - glVertex2f(2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); - glVertex2f(2.0f, 2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); - glVertex2f(-2.0f, 2.0f); - } - glEnd(); - - glPopMatrix(); - - glFlush(); -} - - int main() { CLogger(); @@ -322,24 +149,15 @@ int main() SDL_WM_SetCaption("Texture Test", "Texture Test"); - - #if DEV Gfx::CGLDevice *device = new Gfx::CGLDevice(); device->Create(); Init(device); - #else - InitGL(); - #endif bool done = false; while (! done) { - #if DEV Render(device); - #else - RenderGL(); - #endif SDL_GL_SwapBuffers(); @@ -348,12 +166,10 @@ int main() if (event.type == SDL_QUIT) done = true; - usleep(50000); + usleep(10000); } - #if DEV device->Destroy(); - #endif SDL_FreeSurface(surface); diff --git a/src/math/func.h b/src/math/func.h index 8f0e4ab..9a417dc 100644 --- a/src/math/func.h +++ b/src/math/func.h @@ -34,15 +34,15 @@ namespace Math /* @{ */ // start of group //! Compares \a a and \a b within \a tolerance -inline bool IsEqual(float a, float b, float tolerance = TOLERANCE) +inline bool IsEqual(float a, float b, float tolerance = Math::TOLERANCE) { return fabs(a - b) < tolerance; } //! Compares \a a to zero within \a tolerance -inline bool IsZero(float a, float tolerance = TOLERANCE) +inline bool IsZero(float a, float tolerance = Math::TOLERANCE) { - return IsEqual(a, 0.0f, tolerance); + return Math::IsEqual(a, 0.0f, tolerance); } //! Minimum @@ -59,12 +59,12 @@ inline float Min(float a, float b, float c) inline float Min(float a, float b, float c, float d) { - return Min( Min(a, b), Min(c, d) ); + return Math::Min( Math::Min(a, b), Math::Min(c, d) ); } inline float Min(float a, float b, float c, float d, float e) { - return Min( Min(a, b), Min(c, d), e ); + return Math::Min( Math::Min(a, b), Math::Min(c, d), e ); } //! Maximum @@ -76,17 +76,17 @@ inline float Max(float a, float b) inline float Max(float a, float b, float c) { - return Max( Max(a, b), c ); + return Math::Max( Math::Max(a, b), c ); } inline float Max(float a, float b, float c, float d) { - return Max( Max(a, b), Max(c, d) ); + return Math::Max( Math::Max(a, b), Math::Max(c, d) ); } inline float Max(float a, float b, float c, float d, float e) { - return Max( Max(a, b), Max(c, d), e ); + return Math::Max( Math::Max(a, b), Math::Max(c, d), e ); } //! Returns the normalized value (0 .. 1) @@ -130,7 +130,7 @@ inline float Rand() //! Returns a normalized angle, that is in other words between 0 and 2 * PI inline float NormAngle(float angle) { - angle = Mod(angle, PI*2.0f); + angle = Math::Mod(angle, PI*2.0f); if ( angle < 0.0f ) return PI*2.0f + angle; @@ -140,9 +140,9 @@ inline float NormAngle(float angle) //! Test if a angle is between two terminals inline bool TestAngle(float angle, float min, float max) { - angle = NormAngle(angle); - min = NormAngle(min); - max = NormAngle(max); + angle = Math::NormAngle(angle); + min = Math::NormAngle(min); + max = Math::NormAngle(max); if ( min > max ) return ( angle <= max || angle >= min ); @@ -163,8 +163,8 @@ inline float PropAngle(int a, int b, float p) /** A positive angle is counterclockwise (CCW). */ inline float Direction(float a, float g) { - a = NormAngle(a); - g = NormAngle(g); + a = Math::NormAngle(a); + g = Math::NormAngle(g); if ( a < g ) { diff --git a/src/math/geometry.h b/src/math/geometry.h index d5960b8..e56ff10 100644 --- a/src/math/geometry.h +++ b/src/math/geometry.h @@ -40,7 +40,7 @@ namespace Math //! Returns py up on the line \a a - \a b -inline float MidPoint(const Point &a, const Point &b, float px) +inline float MidPoint(const Math::Point &a, const Math::Point &b, float px) { if (IsEqual(a.x, b.x)) { @@ -53,7 +53,7 @@ inline float MidPoint(const Point &a, const Point &b, float px) } //! Tests whether the point \a p is inside the triangle (\a a,\a b,\a c) -inline bool IsInsideTriangle(Point a, Point b, Point c, Point p) +inline bool IsInsideTriangle(Math::Point a, Math::Point b, Math::Point c, Math::Point p) { float n, m; @@ -82,13 +82,13 @@ inline bool IsInsideTriangle(Point a, Point b, Point c, Point p) /** \a center center of rotation \a angle angle is in radians (positive is counterclockwise (CCW) ) \a p the point */ -inline Point RotatePoint(const Point ¢er, float angle, const Point &p) +inline Math::Point RotatePoint(const Math::Point ¢er, float angle, const Math::Point &p) { - Point a; + Math::Point a; a.x = p.x-center.x; a.y = p.y-center.y; - Point b; + Math::Point b; b.x = a.x*cosf(angle) - a.y*sinf(angle); b.y = a.x*sinf(angle) + a.y*cosf(angle); @@ -101,23 +101,23 @@ inline Point RotatePoint(const Point ¢er, float angle, const Point &p) //! Rotates a point around the origin (0,0) /** \a angle angle in radians (positive is counterclockwise (CCW) ) \a p the point */ -inline Point RotatePoint(float angle, const Point &p) +inline Math::Point RotatePoint(float angle, const Math::Point &p) { float x = p.x*cosf(angle) - p.y*sinf(angle); float y = p.x*sinf(angle) + p.y*cosf(angle); - return Point(x, y); + return Math::Point(x, y); } //! Rotates a vector (dist, 0). /** \a angle angle is in radians (positive is counterclockwise (CCW) ) \a dist distance to origin */ -inline Point RotatePoint(float angle, float dist) +inline Math::Point RotatePoint(float angle, float dist) { float x = dist*cosf(angle); float y = dist*sinf(angle); - return Point(x, y); + return Math::Point(x, y); } //! TODO documentation @@ -140,13 +140,13 @@ inline void RotatePoint(float cx, float cy, float angle, float &px, float &py) \a angleH,angleV rotation angles in radians (positive is counterclockwise (CCW) ) ) \a p the point \returns the rotated point */ -inline void RotatePoint(const Vector ¢er, float angleH, float angleV, Vector &p) +inline void RotatePoint(const Math::Vector ¢er, float angleH, float angleV, Math::Vector &p) { p.x -= center.x; p.y -= center.y; p.z -= center.z; - Vector b; + Math::Vector b; b.x = p.x*cosf(angleH) - p.z*sinf(angleH); b.y = p.z*sinf(angleV) + p.y*cosf(angleV); b.z = p.x*sinf(angleH) + p.z*cosf(angleH); @@ -159,18 +159,18 @@ inline void RotatePoint(const Vector ¢er, float angleH, float angleV, Vector \a angleH,angleV rotation angles in radians (positive is counterclockwise (CCW) ) ) \a p the point \returns the rotated point */ -inline void RotatePoint2(const Vector center, float angleH, float angleV, Vector &p) +inline void RotatePoint2(const Math::Vector center, float angleH, float angleV, Math::Vector &p) { p.x -= center.x; p.y -= center.y; p.z -= center.z; - Vector a; + Math::Vector a; a.x = p.x*cosf(angleH) - p.z*sinf(angleH); a.y = p.y; a.z = p.x*sinf(angleH) + p.z*cosf(angleH); - Vector b; + Math::Vector b; b.x = a.x; b.y = a.z*sinf(angleV) + a.y*cosf(angleV); b.z = a.z*cosf(angleV) - a.y*sinf(angleV); @@ -196,7 +196,7 @@ inline float RotateAngle(float x, float y) /** \a center the center point \a p1,p2 the two points \returns The angle in radians (positive is counterclockwise (CCW) ) */ -inline float RotateAngle(const Point ¢er, const Point &p1, const Point &p2) +inline float RotateAngle(const Math::Point ¢er, const Math::Point &p1, const Math::Point &p2) { if (PointsEqual(p1, center)) return 0; @@ -221,11 +221,12 @@ inline float RotateAngle(const Point ¢er, const Point &p1, const Point &p2) /** \a from origin \a at view direction \a worldUp up vector */ -inline void LoadViewMatrix(Matrix &mat, const Vector &from, const Vector &at, const Vector &worldUp) +inline void LoadViewMatrix(Math::Matrix &mat, const Math::Vector &from, + const Math::Vector &at, const Math::Vector &worldUp) { // Get the z basis vector, which points straight ahead. This is the // difference from the eyepoint to the lookat point. - Vector view = at - from; + Math::Vector view = at - from; float length = view.Length(); assert(! IsZero(length) ); @@ -237,18 +238,18 @@ inline void LoadViewMatrix(Matrix &mat, const Vector &from, const Vector &at, co // vector onto the up vector. The projection is the y basis vector. float dotProduct = DotProduct(worldUp, view); - Vector up = worldUp - dotProduct * view; + Math::Vector up = worldUp - dotProduct * view; // If this vector has near-zero length because the input specified a // bogus up vector, let's try a default up vector if ( IsZero(length = up.Length()) ) { - up = Vector(0.0f, 1.0f, 0.0f) - view.y * view; + up = Math::Vector(0.0f, 1.0f, 0.0f) - view.y * view; // If we still have near-zero length, resort to a different axis. if ( IsZero(length = up.Length()) ) { - up = Vector(0.0f, 0.0f, 1.0f) - view.z * view; + up = Math::Vector(0.0f, 0.0f, 1.0f) - view.z * view; assert(! IsZero(up.Length()) ); } @@ -259,7 +260,7 @@ inline void LoadViewMatrix(Matrix &mat, const Vector &from, const Vector &at, co // The x basis vector is found simply with the cross product of the y // and z basis vectors - Vector right = CrossProduct(up, view); + Math::Vector right = CrossProduct(up, view); // Start building the matrix. The first three rows contains the basis // vectors used to rotate the view to point at the lookat point @@ -286,7 +287,7 @@ inline void LoadViewMatrix(Matrix &mat, const Vector &from, const Vector &at, co \a aspect aspect ratio (width / height) \a nearPlane distance to near cut plane \a farPlane distance to far cut plane */ -inline void LoadProjectionMatrix(Matrix &mat, float fov = 1.570795f, float aspect = 1.0f, +inline void LoadProjectionMatrix(Math::Matrix &mat, float fov = 1.570795f, float aspect = 1.0f, float nearPlane = 1.0f, float farPlane = 1000.0f) { assert(fabs(farPlane - nearPlane) >= 0.01f); @@ -309,7 +310,7 @@ inline void LoadProjectionMatrix(Matrix &mat, float fov = 1.570795f, float aspec /** \a left,right coordinates for left and right vertical clipping planes \a bottom,top coordinates for bottom and top horizontal clipping planes \a zNear,zFar distance to nearer and farther depth clipping planes */ -inline void LoadOrthoProjectionMatrix(Matrix &mat, float left, float right, float bottom, float top, +inline void LoadOrthoProjectionMatrix(Math::Matrix &mat, float left, float right, float bottom, float top, float zNear = -1.0f, float zFar = 1.0f) { mat.LoadIdentity(); @@ -325,7 +326,7 @@ inline void LoadOrthoProjectionMatrix(Matrix &mat, float left, float right, floa //! Loads a translation matrix from given vector /** \a trans vector of translation*/ -inline void LoadTranslationMatrix(Matrix &mat, const Vector &trans) +inline void LoadTranslationMatrix(Math::Matrix &mat, const Math::Vector &trans) { mat.LoadIdentity(); /* (1,4) */ mat.m[12] = trans.x; @@ -335,7 +336,7 @@ inline void LoadTranslationMatrix(Matrix &mat, const Vector &trans) //! Loads a scaling matrix fom given vector /** \a scale vector with scaling factors for X, Y, Z */ -inline void LoadScaleMatrix(Matrix &mat, const Vector &scale) +inline void LoadScaleMatrix(Math::Matrix &mat, const Math::Vector &scale) { mat.LoadIdentity(); /* (1,1) */ mat.m[0 ] = scale.x; @@ -345,7 +346,7 @@ inline void LoadScaleMatrix(Matrix &mat, const Vector &scale) //! Loads a rotation matrix along the X axis /** \a angle angle in radians */ -inline void LoadRotationXMatrix(Matrix &mat, float angle) +inline void LoadRotationXMatrix(Math::Matrix &mat, float angle) { mat.LoadIdentity(); /* (2,2) */ mat.m[5 ] = cosf(angle); @@ -356,7 +357,7 @@ inline void LoadRotationXMatrix(Matrix &mat, float angle) //! Loads a rotation matrix along the Y axis /** \a angle angle in radians */ -inline void LoadRotationYMatrix(Matrix &mat, float angle) +inline void LoadRotationYMatrix(Math::Matrix &mat, float angle) { mat.LoadIdentity(); /* (1,1) */ mat.m[0 ] = cosf(angle); @@ -367,7 +368,7 @@ inline void LoadRotationYMatrix(Matrix &mat, float angle) //! Loads a rotation matrix along the Z axis /** \a angle angle in radians */ -inline void LoadRotationZMatrix(Matrix &mat, float angle) +inline void LoadRotationZMatrix(Math::Matrix &mat, float angle) { mat.LoadIdentity(); /* (1,1) */ mat.m[0 ] = cosf(angle); @@ -379,11 +380,11 @@ inline void LoadRotationZMatrix(Matrix &mat, float angle) //! Loads a rotation matrix along the given axis /** \a dir axis of rotation \a angle angle in radians */ -inline void LoadRotationMatrix(Matrix &mat, const Vector &dir, float angle) +inline void LoadRotationMatrix(Math::Matrix &mat, const Math::Vector &dir, float angle) { float cos = cosf(angle); float sin = sinf(angle); - Vector v = Normalize(dir); + Math::Vector v = Normalize(dir); mat.LoadIdentity(); @@ -401,9 +402,9 @@ inline void LoadRotationMatrix(Matrix &mat, const Vector &dir, float angle) } //! Calculates the matrix to make three rotations in the order X, Z and Y -inline void LoadRotationXZYMatrix(Matrix &mat, const Vector &angle) +inline void LoadRotationXZYMatrix(Math::Matrix &mat, const Math::Vector &angle) { - Matrix temp; + Math::Matrix temp; LoadRotationXMatrix(temp, angle.x); LoadRotationZMatrix(mat, angle.z); @@ -414,9 +415,9 @@ inline void LoadRotationXZYMatrix(Matrix &mat, const Vector &angle) } //! Calculates the matrix to make three rotations in the order Z, X and Y -inline void LoadRotationZXYMatrix(Matrix &mat, const Vector &angle) +inline void LoadRotationZXYMatrix(Math::Matrix &mat, const Math::Vector &angle) { - Matrix temp; + Math::Matrix temp; LoadRotationZMatrix(temp, angle.z); LoadRotationXMatrix(mat, angle.x); @@ -427,7 +428,7 @@ inline void LoadRotationZXYMatrix(Matrix &mat, const Vector &angle) } //! Returns the distance between projections on XZ plane of two vectors -inline float DistanceProjected(const Vector &a, const Vector &b) +inline float DistanceProjected(const Math::Vector &a, const Math::Vector &b) { return sqrtf( (a.x-b.x)*(a.x-b.x) + (a.z-b.z)*(a.z-b.z) ); @@ -435,10 +436,10 @@ inline float DistanceProjected(const Vector &a, const Vector &b) //! Returns the normal vector to a plane /** \param p1,p2,p3 points defining the plane */ -inline Vector NormalToPlane(const Vector &p1, const Vector &p2, const Vector &p3) +inline Math::Vector NormalToPlane(const Math::Vector &p1, const Math::Vector &p2, const Math::Vector &p3) { - Vector u = p3 - p1; - Vector v = p2 - p1; + Math::Vector u = p3 - p1; + Math::Vector v = p2 - p1; return Normalize(CrossProduct(u, v)); } @@ -446,7 +447,7 @@ inline Vector NormalToPlane(const Vector &p1, const Vector &p2, const Vector &p3 //! Returns a point on the line \a p1 - \a p2, in \a dist distance from \a p1 /** \a p1,p2 line start and end \a dist scaling factor from \a p1, relative to distance between \a p1 and \a p2 */ -inline Vector SegmentPoint(const Vector &p1, const Vector &p2, float dist) +inline Math::Vector SegmentPoint(const Math::Vector &p1, const Math::Vector &p2, float dist) { return p1 + (p2 - p1) * dist; } @@ -454,9 +455,10 @@ inline Vector SegmentPoint(const Vector &p1, const Vector &p2, float dist) //! Returns the distance between given point and a plane /** \param p the point \param a,b,c points defining the plane */ -inline float DistanceToPlane(const Vector &a, const Vector &b, const Vector &c, const Vector &p) +inline float DistanceToPlane(const Math::Vector &a, const Math::Vector &b, + const Math::Vector &c, const Math::Vector &p) { - Vector n = NormalToPlane(a, b, c); + Math::Vector n = NormalToPlane(a, b, c); float d = -(n.x*a.x + n.y*a.y + n.z*a.z); return fabs(n.x*p.x + n.y*p.y + n.z*p.z + d); @@ -465,10 +467,10 @@ inline float DistanceToPlane(const Vector &a, const Vector &b, const Vector &c, //! Checks if two planes defined by three points are the same /** \a plane1 array of three vectors defining the first plane \a plane2 array of three vectors defining the second plane */ -inline bool IsSamePlane(const Vector (&plane1)[3], const Vector (&plane2)[3]) +inline bool IsSamePlane(const Math::Vector (&plane1)[3], const Math::Vector (&plane2)[3]) { - Vector n1 = NormalToPlane(plane1[0], plane1[1], plane1[2]); - Vector n2 = NormalToPlane(plane2[0], plane2[1], plane2[2]); + Math::Vector n1 = NormalToPlane(plane1[0], plane1[1], plane1[2]); + Math::Vector n2 = NormalToPlane(plane2[0], plane2[1], plane2[2]); if ( fabs(n1.x-n2.x) > 0.1f || fabs(n1.y-n2.y) > 0.1f || @@ -483,7 +485,8 @@ inline bool IsSamePlane(const Vector (&plane1)[3], const Vector (&plane2)[3]) } //! Calculates the intersection "i" right "of" the plane "abc". -inline bool Intersect(const Vector &a, const Vector &b, const Vector &c, const Vector &d, const Vector &e, Vector &i) +inline bool Intersect(const Math::Vector &a, const Math::Vector &b, const Math::Vector &c, + const Math::Vector &d, const Math::Vector &e, Math::Vector &i) { float d1 = (d.x-a.x)*((b.y-a.y)*(c.z-a.z)-(c.y-a.y)*(b.z-a.z)) - (d.y-a.y)*((b.x-a.x)*(c.z-a.z)-(c.x-a.x)*(b.z-a.z)) + @@ -505,7 +508,7 @@ inline bool Intersect(const Vector &a, const Vector &b, const Vector &c, const V //! Calculates the intersection of the straight line passing through p (x, z) /** Line is parallel to the y axis, with the plane abc. Returns p.y. */ -inline bool IntersectY(const Vector &a, const Vector &b, const Vector &c, Vector &p) +inline bool IntersectY(const Math::Vector &a, const Math::Vector &b, const Math::Vector &c, Math::Vector &p) { float d = (b.x-a.x)*(c.z-a.z) - (c.x-a.x)*(b.z-a.z); float d1 = (p.x-a.x)*(c.z-a.z) - (c.x-a.x)*(p.z-a.z); @@ -520,9 +523,9 @@ inline bool IntersectY(const Vector &a, const Vector &b, const Vector &c, Vector } //! Calculates the end point -inline Vector LookatPoint(const Vector &eye, float angleH, float angleV, float length) +inline Math::Vector LookatPoint(const Math::Vector &eye, float angleH, float angleV, float length) { - Vector lookat = eye; + Math::Vector lookat = eye; lookat.z += length; RotatePoint(eye, angleH, angleV, lookat); @@ -531,7 +534,7 @@ inline Vector LookatPoint(const Vector &eye, float angleH, float angleV, float l } //! TODO documentation -inline Vector Transform(const Matrix &m, const Vector &p) +inline Math::Vector Transform(const Math::Matrix &m, const Math::Vector &p) { return MatrixVectorMultiply(m, p); } @@ -539,7 +542,7 @@ inline Vector Transform(const Matrix &m, const Vector &p) //! Calculates the projection of the point \a p on a straight line \a a to \a b. /** \a p point to project \a a,b two ends of the line */ -inline Vector Projection(const Vector &a, const Vector &b, const Vector &p) +inline Math::Vector Projection(const Math::Vector &a, const Math::Vector &b, const Math::Vector &p) { float k = DotProduct(b - a, p - a); k /= DotProduct(b - a, b - a); @@ -548,15 +551,15 @@ inline Vector Projection(const Vector &a, const Vector &b, const Vector &p) } //! Calculates point of view to look at a center two angles and a distance -inline Vector RotateView(Vector center, float angleH, float angleV, float dist) +inline Math::Vector RotateView(Math::Vector center, float angleH, float angleV, float dist) { - Matrix mat1, mat2; + Math::Matrix mat1, mat2; LoadRotationZMatrix(mat1, -angleV); LoadRotationYMatrix(mat2, -angleH); - Matrix mat = MultiplyMatrices(mat2, mat1); + Math::Matrix mat = MultiplyMatrices(mat2, mat1); - Vector eye; + Math::Vector eye; eye.x = 0.0f+dist; eye.y = 0.0f; eye.z = 0.0f; diff --git a/src/math/matrix.h b/src/math/matrix.h index 0315a33..7ee40e8 100644 --- a/src/math/matrix.h +++ b/src/math/matrix.h @@ -388,9 +388,9 @@ inline bool MatricesEqual(const Matrix &m1, const Matrix &m2, } //! Convenience function for getting transposed matrix -inline Matrix Transpose(const Matrix &m) +inline Math::Matrix Transpose(const Math::Matrix &m) { - Matrix result = m; + Math::Matrix result = m; result.Transpose(); return result; } @@ -399,7 +399,7 @@ inline Matrix Transpose(const Matrix &m) /** \a left left-hand matrix \a right right-hand matrix \returns multiplied matrices */ -inline Matrix MultiplyMatrices(const Matrix &left, const Matrix &right) +inline Math::Matrix MultiplyMatrices(const Math::Matrix &left, const Math::Matrix &right) { return left.Multiply(right); } @@ -413,25 +413,25 @@ inline Matrix MultiplyMatrices(const Matrix &left, const Matrix &right) The result, a 4x1 vector is then converted to 3x1 by dividing x,y,z coords by the fourth coord (w). */ -inline Vector MatrixVectorMultiply(const Matrix &m, const Vector &v, bool wDivide = false) +inline Math::Vector MatrixVectorMultiply(const Math::Matrix &m, const Math::Vector &v, bool wDivide = false) { float x = v.x * m.m[0 ] + v.y * m.m[4 ] + v.z * m.m[8 ] + m.m[12]; float y = v.x * m.m[1 ] + v.y * m.m[5 ] + v.z * m.m[9 ] + m.m[13]; float z = v.x * m.m[2 ] + v.y * m.m[6 ] + v.z * m.m[10] + m.m[14]; if (!wDivide) - return Vector(x, y, z); + return Math::Vector(x, y, z); float w = v.x * m.m[3 ] + v.y * m.m[7 ] + v.z * m.m[11] + m.m[15]; if (IsZero(w)) - return Vector(x, y, z); + return Math::Vector(x, y, z); x /= w; y /= w; z /= w; - return Vector(x, y, z); + return Math::Vector(x, y, z); } /* @} */ // end of group diff --git a/src/math/vector.h b/src/math/vector.h index baba6bb..41f11a8 100644 --- a/src/math/vector.h +++ b/src/math/vector.h @@ -205,7 +205,7 @@ struct Vector }; // struct Point //! Checks if two vectors are equal within given \a tolerance -inline bool VectorsEqual(const Vector &a, const Vector &b, float tolerance = TOLERANCE) +inline bool VectorsEqual(const Math::Vector &a, const Math::Vector &b, float tolerance = TOLERANCE) { return IsEqual(a.x, b.x, tolerance) && IsEqual(a.y, b.y, tolerance) @@ -213,7 +213,7 @@ inline bool VectorsEqual(const Vector &a, const Vector &b, float tolerance = TOL } //! Convenience function for getting normalized vector -inline Vector Normalize(const Vector &v) +inline Vector Normalize(const Math::Vector &v) { Vector result = v; result.Normalize(); @@ -221,25 +221,25 @@ inline Vector Normalize(const Vector &v) } //! Convenience function for calculating dot product -inline float DotProduct(const Vector &left, const Vector &right) +inline float DotProduct(const Math::Vector &left, const Math::Vector &right) { return left.DotMultiply(right); } //! Convenience function for calculating cross product -inline Vector CrossProduct(const Vector &left, const Vector &right) +inline Vector CrossProduct(const Math::Vector &left, const Math::Vector &right) { return left.CrossMultiply(right); } //! Convenience function for calculating angle (in radians) between two vectors -inline float Angle(const Vector &a, const Vector &b) +inline float Angle(const Math::Vector &a, const Math::Vector &b) { return a.Angle(b); } //! Returns the distance between the ends of two vectors -inline float Distance(const Vector &a, const Vector &b) +inline float Distance(const Math::Vector &a, const Math::Vector &b) { return sqrtf( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + -- cgit v1.2.3-1-g7c22