From 836970f012f1f00dbe455abc22c66833e0e10718 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 25 Nov 2017 13:51:09 +0100 Subject: [PATCH] - moved the matrix code out of 'gl' because the model code also needs it. --- src/CMakeLists.txt | 2 +- src/gl/models/gl_models.cpp | 1 - src/gl/models/gl_models.h | 3 +- src/gl/renderer/gl_postprocessstate.h | 2 +- src/gl/renderer/gl_quaddrawer.cpp | 2 +- src/gl/renderer/gl_renderer.h | 2 +- src/gl/renderer/gl_renderstate.cpp | 17 +-- src/gl/renderer/gl_renderstate.h | 2 +- src/gl/scene/gl_portal.cpp | 1 - src/gl/scene/gl_walls.cpp | 1 - src/gl/shaders/gl_shader.cpp | 2 +- src/gl/stereo3d/gl_stereo3d.h | 2 +- src/gl/utility/gl_geometric.h | 99 ----------------- src/polyrenderer/scene/poly_model.h | 2 +- .../data/gl_matrix.cpp => r_data/matrix.cpp} | 23 +--- src/{gl/data/gl_matrix.h => r_data/matrix.h} | 102 +++++++++++++++++- src/r_data/models/models.h | 3 +- src/r_data/models/models_voxel.cpp | 1 + 18 files changed, 129 insertions(+), 138 deletions(-) rename src/{gl/data/gl_matrix.cpp => r_data/matrix.cpp} (98%) rename src/{gl/data/gl_matrix.h => r_data/matrix.h} (50%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 17fca731cb..39bd0933e9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -796,7 +796,6 @@ set( FASTMATH_SOURCES sound/oalsound.cpp sound/sndfile_decoder.cpp sound/mididevices/music_timiditypp_mididevice.cpp - gl/data/gl_matrix.cpp gl/utility/gl_clock.cpp gl/renderer/gl_2ddrawer.cpp gl/hqnx/init.cpp @@ -826,6 +825,7 @@ set( FASTMATH_SOURCES gl/system/gl_load.c gl/models/gl_models.cpp r_data/models/models.cpp + r_data/matrix.cpp ) set (PCH_SOURCES diff --git a/src/gl/models/gl_models.cpp b/src/gl/models/gl_models.cpp index 0e051a402a..0d3754b7f4 100644 --- a/src/gl/models/gl_models.cpp +++ b/src/gl/models/gl_models.cpp @@ -48,7 +48,6 @@ #include "gl/scene/gl_drawinfo.h" #include "gl/models/gl_models.h" #include "gl/textures/gl_material.h" -#include "gl/utility/gl_geometric.h" #include "gl/utility/gl_convert.h" #include "gl/renderer/gl_renderstate.h" #include "gl/shaders/gl_shader.h" diff --git a/src/gl/models/gl_models.h b/src/gl/models/gl_models.h index b60fabea66..636c1944cd 100644 --- a/src/gl/models/gl_models.h +++ b/src/gl/models/gl_models.h @@ -23,12 +23,13 @@ #pragma once #include "tarray.h" -#include "gl/utility/gl_geometric.h" #include "gl/data/gl_vertexbuffer.h" #include "p_pspr.h" #include "r_data/voxels.h" #include "r_data/models/models.h" +class GLSprite; + class FGLModelRenderer : public FModelRenderer { public: diff --git a/src/gl/renderer/gl_postprocessstate.h b/src/gl/renderer/gl_postprocessstate.h index 795a7d4ba9..cd45915aac 100644 --- a/src/gl/renderer/gl_postprocessstate.h +++ b/src/gl/renderer/gl_postprocessstate.h @@ -4,7 +4,7 @@ #include #include "gl/system/gl_interface.h" #include "gl/data/gl_data.h" -#include "gl/data/gl_matrix.h" +#include "r_data/matrix.h" #include "c_cvars.h" #include "r_defs.h" diff --git a/src/gl/renderer/gl_quaddrawer.cpp b/src/gl/renderer/gl_quaddrawer.cpp index d6a4f98a8b..3a31fe62b9 100644 --- a/src/gl/renderer/gl_quaddrawer.cpp +++ b/src/gl/renderer/gl_quaddrawer.cpp @@ -25,7 +25,7 @@ #include "gl/renderer/gl_renderer.h" #include "gl/renderer/gl_renderstate.h" #include "gl/renderer/gl_quaddrawer.h" -#include "gl/data/gl_matrix.h" +#include "r_data/matrix.h" /* ** For handling of dynamically created quads when no persistently mapped diff --git a/src/gl/renderer/gl_renderer.h b/src/gl/renderer/gl_renderer.h index e3924ad408..8b6ba46df5 100644 --- a/src/gl/renderer/gl_renderer.h +++ b/src/gl/renderer/gl_renderer.h @@ -5,7 +5,7 @@ #include "v_video.h" #include "vectors.h" #include "r_renderer.h" -#include "gl/data/gl_matrix.h" +#include "r_data/matrix.h" #include "gl/dynlights/gl_shadowmap.h" struct particle_t; diff --git a/src/gl/renderer/gl_renderstate.cpp b/src/gl/renderer/gl_renderstate.cpp index c37ccbb992..6dd78d5938 100644 --- a/src/gl/renderer/gl_renderstate.cpp +++ b/src/gl/renderer/gl_renderstate.cpp @@ -50,6 +50,11 @@ CVAR(Bool, gl_bandedswlight, false, CVAR_ARCHIVE) static VSMatrix identityMatrix(1); TArray gl_MatrixStack; +static void matrixToGL(const VSMatrix &mat, int loc) +{ + glUniformMatrix4fv(loc, 1, false, (float*)&mat); +} + //========================================================================== // // @@ -273,28 +278,28 @@ bool FRenderState::ApplyShader() } if (mTextureMatrixEnabled) { - mTextureMatrix.matrixToGL(activeShader->texturematrix_index); + matrixToGL(mTextureMatrix, activeShader->texturematrix_index); activeShader->currentTextureMatrixState = true; } else if (activeShader->currentTextureMatrixState) { activeShader->currentTextureMatrixState = false; - identityMatrix.matrixToGL(activeShader->texturematrix_index); + matrixToGL(identityMatrix, activeShader->texturematrix_index); } if (mModelMatrixEnabled) { - mModelMatrix.matrixToGL(activeShader->modelmatrix_index); + matrixToGL(mModelMatrix, activeShader->modelmatrix_index); VSMatrix norm; norm.computeNormalMatrix(mModelMatrix); - norm.matrixToGL(activeShader->normalmodelmatrix_index); + matrixToGL(norm, activeShader->normalmodelmatrix_index); activeShader->currentModelMatrixState = true; } else if (activeShader->currentModelMatrixState) { activeShader->currentModelMatrixState = false; - identityMatrix.matrixToGL(activeShader->modelmatrix_index); - identityMatrix.matrixToGL(activeShader->normalmodelmatrix_index); + matrixToGL(identityMatrix, activeShader->modelmatrix_index); + matrixToGL(identityMatrix, activeShader->normalmodelmatrix_index); } return true; } diff --git a/src/gl/renderer/gl_renderstate.h b/src/gl/renderer/gl_renderstate.h index 241461e133..92de96e9a6 100644 --- a/src/gl/renderer/gl_renderstate.h +++ b/src/gl/renderer/gl_renderstate.h @@ -26,7 +26,7 @@ #include #include "gl/system/gl_interface.h" #include "gl/data/gl_data.h" -#include "gl/data/gl_matrix.h" +#include "r_data/matrix.h" #include "gl/textures/gl_material.h" #include "c_cvars.h" #include "r_defs.h" diff --git a/src/gl/scene/gl_portal.cpp b/src/gl/scene/gl_portal.cpp index b5671923f4..2e5df1da9d 100644 --- a/src/gl/scene/gl_portal.cpp +++ b/src/gl/scene/gl_portal.cpp @@ -57,7 +57,6 @@ #include "gl/textures/gl_material.h" #include "gl/utility/gl_clock.h" #include "gl/utility/gl_templates.h" -#include "gl/utility/gl_geometric.h" //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- diff --git a/src/gl/scene/gl_walls.cpp b/src/gl/scene/gl_walls.cpp index 2ce769575b..018f8ab678 100644 --- a/src/gl/scene/gl_walls.cpp +++ b/src/gl/scene/gl_walls.cpp @@ -45,7 +45,6 @@ #include "gl/scene/gl_scenedrawer.h" #include "gl/textures/gl_material.h" #include "gl/utility/gl_clock.h" -#include "gl/utility/gl_geometric.h" #include "gl/utility/gl_templates.h" #include "gl/shaders/gl_shader.h" diff --git a/src/gl/shaders/gl_shader.cpp b/src/gl/shaders/gl_shader.cpp index 6c9ee416db..d551106a45 100644 --- a/src/gl/shaders/gl_shader.cpp +++ b/src/gl/shaders/gl_shader.cpp @@ -42,7 +42,7 @@ #include "gl/system/gl_interface.h" #include "gl/system/gl_debug.h" #include "gl/data/gl_data.h" -#include "gl/data/gl_matrix.h" +#include "r_data/matrix.h" #include "gl/renderer/gl_renderer.h" #include "gl/renderer/gl_renderstate.h" #include "gl/system/gl_cvars.h" diff --git a/src/gl/stereo3d/gl_stereo3d.h b/src/gl/stereo3d/gl_stereo3d.h index 81a72874f8..ee632b31cf 100644 --- a/src/gl/stereo3d/gl_stereo3d.h +++ b/src/gl/stereo3d/gl_stereo3d.h @@ -30,7 +30,7 @@ #include // needed for memcpy on linux, which is needed by VSMatrix copy ctor #include "tarray.h" -#include "gl/data/gl_matrix.h" +#include "r_data/matrix.h" #include "gl/renderer/gl_renderer.h" diff --git a/src/gl/utility/gl_geometric.h b/src/gl/utility/gl_geometric.h index 30c3e2dea9..5f5fad8e51 100644 --- a/src/gl/utility/gl_geometric.h +++ b/src/gl/utility/gl_geometric.h @@ -83,103 +83,4 @@ protected: float m_d; }; - -class Matrix3x4 // used like a 4x4 matrix with the last row always being (0,0,0,1) -{ - float m[3][4]; - -public: - - void MakeIdentity() - { - memset(m, 0, sizeof(m)); - m[0][0] = m[1][1] = m[2][2] = 1.f; - } - - void Translate(float x, float y, float z) - { - m[0][3] = m[0][0]*x + m[0][1]*y + m[0][2]*z + m[0][3]; - m[1][3] = m[1][0]*x + m[1][1]*y + m[1][2]*z + m[1][3]; - m[2][3] = m[2][0]*x + m[2][1]*y + m[2][2]*z + m[2][3]; - } - - void Scale(float x, float y, float z) - { - m[0][0] *=x; - m[1][0] *=x; - m[2][0] *=x; - - m[0][1] *=y; - m[1][1] *=y; - m[2][1] *=y; - - m[0][2] *=z; - m[1][2] *=z; - m[2][2] *=z; - } - - void Rotate(float ax, float ay, float az, float angle) - { - Matrix3x4 m1; - - FVector3 axis(ax, ay, az); - axis.MakeUnit(); - double c = cos(angle * M_PI/180.), s = sin(angle * M_PI/180.), t = 1 - c; - double sx = s*axis.X, sy = s*axis.Y, sz = s*axis.Z; - double tx, ty, txx, tyy, u, v; - - tx = t*axis.X; - m1.m[0][0] = float( (txx=tx*axis.X) + c ); - m1.m[0][1] = float( (u=tx*axis.Y) - sz); - m1.m[0][2] = float( (v=tx*axis.Z) + sy); - - ty = t*axis.Y; - m1.m[1][0] = float( u + sz); - m1.m[1][1] = float( (tyy=ty*axis.Y) + c ); - m1.m[1][2] = float( (u=ty*axis.Z) - sx); - - m1.m[2][0] = float( v - sy); - m1.m[2][1] = float( u + sx); - m1.m[2][2] = float( (t-txx-tyy) + c ); - - m1.m[0][3] = 0.f; - m1.m[1][3] = 0.f; - m1.m[2][3] = 0.f; - - *this = (*this) * m1; - } - - Matrix3x4 operator *(const Matrix3x4 &other) - { - Matrix3x4 result; - - result.m[0][0] = m[0][0]*other.m[0][0] + m[0][1]*other.m[1][0] + m[0][2]*other.m[2][0]; - result.m[0][1] = m[0][0]*other.m[0][1] + m[0][1]*other.m[1][1] + m[0][2]*other.m[2][1]; - result.m[0][2] = m[0][0]*other.m[0][2] + m[0][1]*other.m[1][2] + m[0][2]*other.m[2][2]; - result.m[0][3] = m[0][0]*other.m[0][3] + m[0][1]*other.m[1][3] + m[0][2]*other.m[2][3] + m[0][3]; - - result.m[1][0] = m[1][0]*other.m[0][0] + m[1][1]*other.m[1][0] + m[1][2]*other.m[2][0]; - result.m[1][1] = m[1][0]*other.m[0][1] + m[1][1]*other.m[1][1] + m[1][2]*other.m[2][1]; - result.m[1][2] = m[1][0]*other.m[0][2] + m[1][1]*other.m[1][2] + m[1][2]*other.m[2][2]; - result.m[1][3] = m[1][0]*other.m[0][3] + m[1][1]*other.m[1][3] + m[1][2]*other.m[2][3] + m[1][3]; - - result.m[2][0] = m[2][0]*other.m[0][0] + m[2][1]*other.m[1][0] + m[2][2]*other.m[2][0]; - result.m[2][1] = m[2][0]*other.m[0][1] + m[2][1]*other.m[1][1] + m[2][2]*other.m[2][1]; - result.m[2][2] = m[2][0]*other.m[0][2] + m[2][1]*other.m[1][2] + m[2][2]*other.m[2][2]; - result.m[2][3] = m[2][0]*other.m[0][3] + m[2][1]*other.m[1][3] + m[2][2]*other.m[2][3] + m[2][3]; - - return result; - } - - FVector3 operator *(const FVector3 &vec) - { - FVector3 result; - - result.X = vec.X*m[0][0] + vec.Y*m[0][1] + vec.Z*m[0][2] + m[0][3]; - result.Y = vec.X*m[1][0] + vec.Y*m[1][1] + vec.Z*m[1][2] + m[1][3]; - result.Z = vec.X*m[2][0] + vec.Y*m[2][1] + vec.Z*m[2][2] + m[2][3]; - return result; - } -}; - #endif diff --git a/src/polyrenderer/scene/poly_model.h b/src/polyrenderer/scene/poly_model.h index a3907edc0a..b4896b7d8d 100644 --- a/src/polyrenderer/scene/poly_model.h +++ b/src/polyrenderer/scene/poly_model.h @@ -23,7 +23,7 @@ #pragma once #include "polyrenderer/drawers/poly_triangle.h" -#include "gl/data/gl_matrix.h" +#include "r_data/matrix.h" #include "gl/models/gl_models.h" void PolyRenderModel(PolyRenderThread *thread, const TriMatrix &worldToClip, const PolyClipPlane &clipPlane, uint32_t stencilValue, float x, float y, float z, FSpriteModelFrame *smf, AActor *actor); diff --git a/src/gl/data/gl_matrix.cpp b/src/r_data/matrix.cpp similarity index 98% rename from src/gl/data/gl_matrix.cpp rename to src/r_data/matrix.cpp index 9063fc59be..edc26e833c 100644 --- a/src/gl/data/gl_matrix.cpp +++ b/src/r_data/matrix.cpp @@ -11,13 +11,16 @@ This is a simplified version of VSMatrix that has been adjusted for GZDoom's nee ----------------------------------------------------*/ #include -#include "gl/system/gl_system.h" #include #include #include #include #include "doomtype.h" -#include "gl/data/gl_matrix.h" +#include "matrix.h" + +#ifdef _MSC_VER +#pragma warning(disable : 4244) // truncate from double to float +#endif static inline FLOATTYPE DegToRad(FLOATTYPE degrees) @@ -310,22 +313,6 @@ VSMatrix::get(MatrixTypes aType) SEND MATRICES TO OPENGL ------------------------------------------------------*/ - - - -// universal -void -VSMatrix::matrixToGL(int loc) -{ -#ifdef USE_DOUBLE - float copyto[16]; - copy(copyto); - glUniformMatrix4fv(loc, 1, false, copyto); -#else - glUniformMatrix4fv(loc, 1, false, mMatrix); -#endif -} - // ----------------------------------------------------- // AUX functions // ----------------------------------------------------- diff --git a/src/gl/data/gl_matrix.h b/src/r_data/matrix.h similarity index 50% rename from src/gl/data/gl_matrix.h rename to src/r_data/matrix.h index 3ec1f5ff49..22774845aa 100644 --- a/src/gl/data/gl_matrix.h +++ b/src/r_data/matrix.h @@ -21,6 +21,8 @@ #define __VSMatrix__ #include +#include "vectors.h" +#include "doomtype.h" #ifdef USE_DOUBLE typedef double FLOATTYPE; @@ -82,7 +84,6 @@ class VSMatrix { return mMatrix; } - void matrixToGL(int location); void multMatrixPoint(const FLOATTYPE *point, FLOATTYPE *res); #ifdef USE_DOUBLE @@ -112,4 +113,103 @@ class VSMatrix { }; + +class Matrix3x4 // used like a 4x4 matrix with the last row always being (0,0,0,1) +{ + float m[3][4]; + +public: + + void MakeIdentity() + { + memset(m, 0, sizeof(m)); + m[0][0] = m[1][1] = m[2][2] = 1.f; + } + + void Translate(float x, float y, float z) + { + m[0][3] = m[0][0]*x + m[0][1]*y + m[0][2]*z + m[0][3]; + m[1][3] = m[1][0]*x + m[1][1]*y + m[1][2]*z + m[1][3]; + m[2][3] = m[2][0]*x + m[2][1]*y + m[2][2]*z + m[2][3]; + } + + void Scale(float x, float y, float z) + { + m[0][0] *=x; + m[1][0] *=x; + m[2][0] *=x; + + m[0][1] *=y; + m[1][1] *=y; + m[2][1] *=y; + + m[0][2] *=z; + m[1][2] *=z; + m[2][2] *=z; + } + + void Rotate(float ax, float ay, float az, float angle) + { + Matrix3x4 m1; + + FVector3 axis(ax, ay, az); + axis.MakeUnit(); + double c = cos(angle * M_PI/180.), s = sin(angle * M_PI/180.), t = 1 - c; + double sx = s*axis.X, sy = s*axis.Y, sz = s*axis.Z; + double tx, ty, txx, tyy, u, v; + + tx = t*axis.X; + m1.m[0][0] = float( (txx=tx*axis.X) + c ); + m1.m[0][1] = float( (u=tx*axis.Y) - sz); + m1.m[0][2] = float( (v=tx*axis.Z) + sy); + + ty = t*axis.Y; + m1.m[1][0] = float( u + sz); + m1.m[1][1] = float( (tyy=ty*axis.Y) + c ); + m1.m[1][2] = float( (u=ty*axis.Z) - sx); + + m1.m[2][0] = float( v - sy); + m1.m[2][1] = float( u + sx); + m1.m[2][2] = float( (t-txx-tyy) + c ); + + m1.m[0][3] = 0.f; + m1.m[1][3] = 0.f; + m1.m[2][3] = 0.f; + + *this = (*this) * m1; + } + + Matrix3x4 operator *(const Matrix3x4 &other) + { + Matrix3x4 result; + + result.m[0][0] = m[0][0]*other.m[0][0] + m[0][1]*other.m[1][0] + m[0][2]*other.m[2][0]; + result.m[0][1] = m[0][0]*other.m[0][1] + m[0][1]*other.m[1][1] + m[0][2]*other.m[2][1]; + result.m[0][2] = m[0][0]*other.m[0][2] + m[0][1]*other.m[1][2] + m[0][2]*other.m[2][2]; + result.m[0][3] = m[0][0]*other.m[0][3] + m[0][1]*other.m[1][3] + m[0][2]*other.m[2][3] + m[0][3]; + + result.m[1][0] = m[1][0]*other.m[0][0] + m[1][1]*other.m[1][0] + m[1][2]*other.m[2][0]; + result.m[1][1] = m[1][0]*other.m[0][1] + m[1][1]*other.m[1][1] + m[1][2]*other.m[2][1]; + result.m[1][2] = m[1][0]*other.m[0][2] + m[1][1]*other.m[1][2] + m[1][2]*other.m[2][2]; + result.m[1][3] = m[1][0]*other.m[0][3] + m[1][1]*other.m[1][3] + m[1][2]*other.m[2][3] + m[1][3]; + + result.m[2][0] = m[2][0]*other.m[0][0] + m[2][1]*other.m[1][0] + m[2][2]*other.m[2][0]; + result.m[2][1] = m[2][0]*other.m[0][1] + m[2][1]*other.m[1][1] + m[2][2]*other.m[2][1]; + result.m[2][2] = m[2][0]*other.m[0][2] + m[2][1]*other.m[1][2] + m[2][2]*other.m[2][2]; + result.m[2][3] = m[2][0]*other.m[0][3] + m[2][1]*other.m[1][3] + m[2][2]*other.m[2][3] + m[2][3]; + + return result; + } + + FVector3 operator *(const FVector3 &vec) + { + FVector3 result; + + result.X = vec.X*m[0][0] + vec.Y*m[0][1] + vec.Z*m[0][2] + m[0][3]; + result.Y = vec.X*m[1][0] + vec.Y*m[1][1] + vec.Z*m[1][2] + m[1][3]; + result.Z = vec.X*m[2][0] + vec.Y*m[2][1] + vec.Z*m[2][2] + m[2][3]; + return result; + } +}; + #endif \ No newline at end of file diff --git a/src/r_data/models/models.h b/src/r_data/models/models.h index 7e9dabf826..4a8678fa6f 100644 --- a/src/r_data/models/models.h +++ b/src/r_data/models/models.h @@ -24,8 +24,7 @@ #define __GL_MODELS_H_ #include "tarray.h" -#include "gl/utility/gl_geometric.h" -#include "gl/data/gl_matrix.h" +#include "r_data/matrix.h" #include "actor.h" #include "dobject.h" #include "p_pspr.h" diff --git a/src/r_data/models/models_voxel.cpp b/src/r_data/models/models_voxel.cpp index 6e96d7e7b6..848f5a54d3 100644 --- a/src/r_data/models/models_voxel.cpp +++ b/src/r_data/models/models_voxel.cpp @@ -38,6 +38,7 @@ #include "textures/bitmap.h" #include "g_levellocals.h" #include "models.h" +#include "v_palette.h" #ifdef _MSC_VER #pragma warning(disable:4244) // warning C4244: conversion from 'double' to 'float', possible loss of data