From dbb05c5f339d6e7ca40bfbfe19f96e4709c8b707 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 13 Jul 2014 20:41:20 +0200 Subject: [PATCH] - remove use of builtin texture matrices. - make matrix class single precision. --- src/gl/data/gl_matrix.cpp | 124 +++++++++++++----------- src/gl/data/gl_matrix.h | 63 +++++++----- src/gl/models/gl_models.cpp | 56 +++-------- src/gl/renderer/gl_renderstate.cpp | 23 +++++ src/gl/renderer/gl_renderstate.h | 20 ++++ src/gl/scene/gl_drawinfo.cpp | 9 +- src/gl/scene/gl_drawinfo.h | 2 +- src/gl/scene/gl_flats.cpp | 32 +++--- src/gl/scene/gl_portal.cpp | 9 +- src/gl/scene/gl_scene.cpp | 8 -- src/gl/scene/gl_skydome.cpp | 13 +-- src/gl/shaders/gl_shader.cpp | 2 + src/gl/shaders/gl_shader.h | 6 ++ wadsrc/static/shaders/glsl/shaderdefs.i | 6 +- 14 files changed, 198 insertions(+), 175 deletions(-) diff --git a/src/gl/data/gl_matrix.cpp b/src/gl/data/gl_matrix.cpp index 589cfb8ccf..c92f43c26f 100644 --- a/src/gl/data/gl_matrix.cpp +++ b/src/gl/data/gl_matrix.cpp @@ -18,16 +18,16 @@ This is a simplified version of VSMatrix that has been adjusted for GZDoom's nee #include "doomtype.h" #include "gl/data/gl_matrix.h" -static inline double -DegToRad(double degrees) +static inline FLOATTYPE +DegToRad(FLOATTYPE degrees) { - return (double)(degrees * (M_PI / 180.0f)); + return (FLOATTYPE)(degrees * (M_PI / 180.0f)); }; // sets the square matrix mat to the identity matrix, // size refers to the number of rows (or columns) void -VSMatrix::setIdentityMatrix( double *mat, int size) { +VSMatrix::setIdentityMatrix( FLOATTYPE *mat, int size) { // fill matrix with 0s for (int i = 0; i < size * size; ++i) @@ -56,10 +56,10 @@ VSMatrix::loadIdentity() // glMultMatrix implementation void -VSMatrix::multMatrix(const double *aMatrix) +VSMatrix::multMatrix(const FLOATTYPE *aMatrix) { - double res[16]; + FLOATTYPE res[16]; for (int i = 0; i < 4; ++i) { @@ -72,15 +72,16 @@ VSMatrix::multMatrix(const double *aMatrix) } } } - memcpy(mMatrix, res, 16 * sizeof(double)); + memcpy(mMatrix, res, 16 * sizeof(FLOATTYPE)); } +#ifdef USE_DOUBLE // glMultMatrix implementation void VSMatrix::multMatrix(const float *aMatrix) { - double res[16]; + FLOATTYPE res[16]; for (int i = 0; i < 4; ++i) { @@ -93,18 +94,20 @@ VSMatrix::multMatrix(const float *aMatrix) } } } - memcpy(mMatrix, res, 16 * sizeof(double)); + memcpy(mMatrix, res, 16 * sizeof(FLOATTYPE)); } +#endif // glLoadMatrix implementation void -VSMatrix::loadMatrix(const double *aMatrix) +VSMatrix::loadMatrix(const FLOATTYPE *aMatrix) { - memcpy(mMatrix, aMatrix, 16 * sizeof(double)); + memcpy(mMatrix, aMatrix, 16 * sizeof(FLOATTYPE)); } +#ifdef USE_DOUBLE // glLoadMatrix implementation void VSMatrix::loadMatrix(const float *aMatrix) @@ -114,13 +117,14 @@ VSMatrix::loadMatrix(const float *aMatrix) mMatrix[i] = aMatrix[i]; } } +#endif // gl Translate implementation with matrix selection void -VSMatrix::translate(double x, double y, double z) +VSMatrix::translate(FLOATTYPE x, FLOATTYPE y, FLOATTYPE z) { - double mat[16]; + FLOATTYPE mat[16]; setIdentityMatrix(mat); mat[12] = x; @@ -133,9 +137,9 @@ VSMatrix::translate(double x, double y, double z) // gl Scale implementation with matrix selection void -VSMatrix::scale(double x, double y, double z) +VSMatrix::scale(FLOATTYPE x, FLOATTYPE y, FLOATTYPE z) { - double mat[16]; + FLOATTYPE mat[16]; setIdentityMatrix(mat,4); mat[0] = x; @@ -148,22 +152,22 @@ VSMatrix::scale(double x, double y, double z) // gl Rotate implementation with matrix selection void -VSMatrix::rotate(double angle, double x, double y, double z) +VSMatrix::rotate(FLOATTYPE angle, FLOATTYPE x, FLOATTYPE y, FLOATTYPE z) { - double mat[16]; - double v[3]; + FLOATTYPE mat[16]; + FLOATTYPE v[3]; v[0] = x; v[1] = y; v[2] = z; - double radAngle = DegToRad(angle); - double co = cos(radAngle); - double si = sin(radAngle); + FLOATTYPE radAngle = DegToRad(angle); + FLOATTYPE co = cos(radAngle); + FLOATTYPE si = sin(radAngle); normalize(v); - double x2 = v[0]*v[0]; - double y2 = v[1]*v[1]; - double z2 = v[2]*v[2]; + FLOATTYPE x2 = v[0]*v[0]; + FLOATTYPE y2 = v[1]*v[1]; + FLOATTYPE z2 = v[2]*v[2]; // mat[0] = x2 + (y2 + z2) * co; mat[0] = co + x2 * (1 - co);// + (y2 + z2) * co; @@ -194,11 +198,11 @@ VSMatrix::rotate(double angle, double x, double y, double z) // gluLookAt implementation void -VSMatrix::lookAt(double xPos, double yPos, double zPos, - double xLook, double yLook, double zLook, - double xUp, double yUp, double zUp) +VSMatrix::lookAt(FLOATTYPE xPos, FLOATTYPE yPos, FLOATTYPE zPos, + FLOATTYPE xLook, FLOATTYPE yLook, FLOATTYPE zLook, + FLOATTYPE xUp, FLOATTYPE yUp, FLOATTYPE zUp) { - double dir[3], right[3], up[3]; + FLOATTYPE dir[3], right[3], up[3]; up[0] = xUp; up[1] = yUp; up[2] = zUp; @@ -213,7 +217,7 @@ VSMatrix::lookAt(double xPos, double yPos, double zPos, crossProduct(right,dir,up); normalize(up); - double m1[16],m2[16]; + FLOATTYPE m1[16],m2[16]; m1[0] = right[0]; m1[4] = right[1]; @@ -247,11 +251,11 @@ VSMatrix::lookAt(double xPos, double yPos, double zPos, // gluPerspective implementation void -VSMatrix::perspective(double fov, double ratio, double nearp, double farp) +VSMatrix::perspective(FLOATTYPE fov, FLOATTYPE ratio, FLOATTYPE nearp, FLOATTYPE farp) { - double projMatrix[16]; + FLOATTYPE projMatrix[16]; - double f = 1.0f / tan (fov * (M_PI / 360.0f)); + FLOATTYPE f = 1.0f / tan (fov * (M_PI / 360.0f)); setIdentityMatrix(projMatrix,4); @@ -268,11 +272,11 @@ VSMatrix::perspective(double fov, double ratio, double nearp, double farp) // glOrtho implementation void -VSMatrix::ortho(double left, double right, - double bottom, double top, - double nearp, double farp) +VSMatrix::ortho(FLOATTYPE left, FLOATTYPE right, + FLOATTYPE bottom, FLOATTYPE top, + FLOATTYPE nearp, FLOATTYPE farp) { - double m[16]; + FLOATTYPE m[16]; setIdentityMatrix(m,4); @@ -289,11 +293,11 @@ VSMatrix::ortho(double left, double right, // glFrustum implementation void -VSMatrix::frustum(double left, double right, - double bottom, double top, - double nearp, double farp) +VSMatrix::frustum(FLOATTYPE left, FLOATTYPE right, + FLOATTYPE bottom, FLOATTYPE top, + FLOATTYPE nearp, FLOATTYPE farp) { - double m[16]; + FLOATTYPE m[16]; setIdentityMatrix(m,4); @@ -312,7 +316,7 @@ VSMatrix::frustum(double left, double right, /* // returns a pointer to the requested matrix -double * +FLOATTYPE * VSMatrix::get(MatrixTypes aType) { return mMatrix[aType]; @@ -331,9 +335,13 @@ VSMatrix::get(MatrixTypes aType) 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 } // ----------------------------------------------------- @@ -343,7 +351,7 @@ VSMatrix::matrixToGL(int loc) // Compute res = M * point void -VSMatrix::multMatrixPoint(const double *point, double *res) +VSMatrix::multMatrixPoint(const FLOATTYPE *point, FLOATTYPE *res) { for (int i = 0; i < 4; ++i) @@ -360,7 +368,7 @@ VSMatrix::multMatrixPoint(const double *point, double *res) // res = a cross b; void -VSMatrix::crossProduct(const double *a, const double *b, double *res) { +VSMatrix::crossProduct(const FLOATTYPE *a, const FLOATTYPE *b, FLOATTYPE *res) { res[0] = a[1] * b[2] - b[1] * a[2]; res[1] = a[2] * b[0] - b[2] * a[0]; @@ -369,10 +377,10 @@ VSMatrix::crossProduct(const double *a, const double *b, double *res) { // returns a . b -double -VSMatrix::dotProduct(const double *a, const double *b) { +FLOATTYPE +VSMatrix::dotProduct(const FLOATTYPE *a, const FLOATTYPE *b) { - double res = a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; + FLOATTYPE res = a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; return res; } @@ -380,9 +388,9 @@ VSMatrix::dotProduct(const double *a, const double *b) { // Normalize a vec3 void -VSMatrix::normalize(double *a) { +VSMatrix::normalize(FLOATTYPE *a) { - double mag = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]); + FLOATTYPE mag = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]); a[0] /= mag; a[1] /= mag; @@ -392,7 +400,7 @@ VSMatrix::normalize(double *a) { // res = b - a void -VSMatrix::subtract(const double *a, const double *b, double *res) { +VSMatrix::subtract(const FLOATTYPE *a, const FLOATTYPE *b, FLOATTYPE *res) { res[0] = b[0] - a[0]; res[1] = b[1] - a[1]; @@ -402,7 +410,7 @@ VSMatrix::subtract(const double *a, const double *b, double *res) { // res = a + b void -VSMatrix::add(const double *a, const double *b, double *res) { +VSMatrix::add(const FLOATTYPE *a, const FLOATTYPE *b, FLOATTYPE *res) { res[0] = b[0] + a[0]; res[1] = b[1] + a[1]; @@ -411,8 +419,8 @@ VSMatrix::add(const double *a, const double *b, double *res) { // returns |a| -double -VSMatrix::length(const double *a) { +FLOATTYPE +VSMatrix::length(const FLOATTYPE *a) { return(sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2])); @@ -429,10 +437,10 @@ M3(int i, int j) // computes the derived normal matrix for the view matrix void -VSMatrix::computeNormalMatrix(const double *aMatrix) +VSMatrix::computeNormalMatrix(const FLOATTYPE *aMatrix) { - double mMat3x3[9]; + FLOATTYPE mMat3x3[9]; mMat3x3[0] = aMatrix[0]; mMat3x3[1] = aMatrix[1]; @@ -446,7 +454,7 @@ VSMatrix::computeNormalMatrix(const double *aMatrix) mMat3x3[7] = aMatrix[9]; mMat3x3[8] = aMatrix[10]; - double det, invDet; + FLOATTYPE det, invDet; det = mMat3x3[0] * (mMat3x3[4] * mMat3x3[8] - mMat3x3[5] * mMat3x3[7]) + mMat3x3[1] * (mMat3x3[5] * mMat3x3[6] - mMat3x3[8] * mMat3x3[3]) + @@ -476,10 +484,10 @@ VSMatrix::computeNormalMatrix(const double *aMatrix) // aux function resMat = resMat * aMatrix void -VSMatrix::multMatrix(double *resMat, const double *aMatrix) +VSMatrix::multMatrix(FLOATTYPE *resMat, const FLOATTYPE *aMatrix) { - double res[16]; + FLOATTYPE res[16]; for (int i = 0; i < 4; ++i) { @@ -492,5 +500,5 @@ VSMatrix::multMatrix(double *resMat, const double *aMatrix) } } } - memcpy(resMat, res, 16 * sizeof(double)); + memcpy(resMat, res, 16 * sizeof(FLOATTYPE)); } diff --git a/src/gl/data/gl_matrix.h b/src/gl/data/gl_matrix.h index 1ccbc5c378..de4b400da5 100644 --- a/src/gl/data/gl_matrix.h +++ b/src/gl/data/gl_matrix.h @@ -22,6 +22,12 @@ #include +#ifdef USE_DOUBLE +typedef double FLOATTYPE; +#else +typedef float FLOATTYPE; +#endif + class VSMatrix { public: @@ -35,27 +41,32 @@ class VSMatrix { loadIdentity(); } - void translate(double x, double y, double z); - void scale(double x, double y, double z); - void rotate(double angle, double x, double y, double z); + void translate(FLOATTYPE x, FLOATTYPE y, FLOATTYPE z); + void scale(FLOATTYPE x, FLOATTYPE y, FLOATTYPE z); + void rotate(FLOATTYPE angle, FLOATTYPE x, FLOATTYPE y, FLOATTYPE z); void loadIdentity(); +#ifdef USE_DOUBLE void multMatrix(const float *aMatrix); - void multMatrix(const double *aMatrix); +#endif + void multMatrix(const FLOATTYPE *aMatrix); void multMatrix(const VSMatrix &aMatrix) { multMatrix(aMatrix.mMatrix); } - void loadMatrix(const double *aMatrix); + void loadMatrix(const FLOATTYPE *aMatrix); +#ifdef USE_DOUBLE void loadMatrix(const float *aMatrix); - void lookAt(double xPos, double yPos, double zPos, double xLook, double yLook, double zLook, double xUp, double yUp, double zUp); - void perspective(double fov, double ratio, double nearp, double farp); - void ortho(double left, double right, double bottom, double top, double nearp=-1.0f, double farp=1.0f); - void frustum(double left, double right, double bottom, double top, double nearp, double farp); - void copy(double * pDest) +#endif + void lookAt(FLOATTYPE xPos, FLOATTYPE yPos, FLOATTYPE zPos, FLOATTYPE xLook, FLOATTYPE yLook, FLOATTYPE zLook, FLOATTYPE xUp, FLOATTYPE yUp, FLOATTYPE zUp); + void perspective(FLOATTYPE fov, FLOATTYPE ratio, FLOATTYPE nearp, FLOATTYPE farp); + void ortho(FLOATTYPE left, FLOATTYPE right, FLOATTYPE bottom, FLOATTYPE top, FLOATTYPE nearp=-1.0f, FLOATTYPE farp=1.0f); + void frustum(FLOATTYPE left, FLOATTYPE right, FLOATTYPE bottom, FLOATTYPE top, FLOATTYPE nearp, FLOATTYPE farp); + void copy(FLOATTYPE * pDest) { - memcpy(pDest, mMatrix, 16 * sizeof(double)); + memcpy(pDest, mMatrix, 16 * sizeof(FLOATTYPE)); } +#ifdef USE_DOUBLE void copy(float * pDest) { for (int i = 0; i < 16; i++) @@ -63,30 +74,38 @@ class VSMatrix { pDest[i] = (float)mMatrix[i]; } } +#endif + + const FLOATTYPE *get() const + { + return mMatrix; + } void matrixToGL(int location); - void multMatrixPoint(const double *point, double *res); + void multMatrixPoint(const FLOATTYPE *point, FLOATTYPE *res); +#ifdef USE_DOUBLE void computeNormalMatrix(const float *aMatrix); - void computeNormalMatrix(const double *aMatrix); +#endif + void computeNormalMatrix(const FLOATTYPE *aMatrix); void computeNormalMatrix(const VSMatrix &aMatrix) { computeNormalMatrix(aMatrix.mMatrix); } protected: - static void crossProduct(const double *a, const double *b, double *res); - static double dotProduct(const double *a, const double * b); - static void normalize(double *a); - static void subtract(const double *a, const double *b, double *res); - static void add(const double *a, const double *b, double *res); - static double length(const double *a); - static void multMatrix(double *resMatrix, const double *aMatrix); + static void crossProduct(const FLOATTYPE *a, const FLOATTYPE *b, FLOATTYPE *res); + static FLOATTYPE dotProduct(const FLOATTYPE *a, const FLOATTYPE * b); + static void normalize(FLOATTYPE *a); + static void subtract(const FLOATTYPE *a, const FLOATTYPE *b, FLOATTYPE *res); + static void add(const FLOATTYPE *a, const FLOATTYPE *b, FLOATTYPE *res); + static FLOATTYPE length(const FLOATTYPE *a); + static void multMatrix(FLOATTYPE *resMatrix, const FLOATTYPE *aMatrix); - static void setIdentityMatrix(double *mat, int size = 4); + static void setIdentityMatrix(FLOATTYPE *mat, int size = 4); /// The storage for matrices - double mMatrix[16]; + FLOATTYPE mMatrix[16]; }; diff --git a/src/gl/models/gl_models.cpp b/src/gl/models/gl_models.cpp index 67ec241e37..adec46570e 100644 --- a/src/gl/models/gl_models.cpp +++ b/src/gl/models/gl_models.cpp @@ -806,65 +806,41 @@ void gl_RenderModel(GLSprite * spr) // This is rather crappy way to transfer fixet_t type into angle in degrees, but its works! if(smf->flags & MDL_INHERITACTORPITCH) pitch += float(static_cast(spr->actor->pitch >> 16) / (1 << 13) * 45 + static_cast(spr->actor->pitch & 0x0000FFFF) / (1 << 29) * 45); if(smf->flags & MDL_INHERITACTORROLL) roll += float(static_cast(spr->actor->roll >> 16) / (1 << 13) * 45 + static_cast(spr->actor->roll & 0x0000FFFF) / (1 << 29) * 45); - - glActiveTexture(GL_TEXTURE7); // Hijack the otherwise unused seventh texture matrix for the model to world transformation. - glMatrixMode(GL_TEXTURE); - glLoadIdentity(); + + gl_RenderState.mModelMatrix.loadIdentity(); // Model space => World space - glTranslatef(spr->x, spr->z, spr->y ); + gl_RenderState.mModelMatrix.translate(spr->x, spr->z, spr->y ); // Applying model transformations: // 1) Applying actor angle, pitch and roll to the model - glRotatef(-angle, 0, 1, 0); - glRotatef(pitch, 0, 0, 1); - glRotatef(-roll, 1, 0, 0); + gl_RenderState.mModelMatrix.rotate(-angle, 0, 1, 0); + gl_RenderState.mModelMatrix.rotate(pitch, 0, 0, 1); + gl_RenderState.mModelMatrix.rotate(-roll, 1, 0, 0); // 2) Applying Doomsday like rotation of the weapon pickup models // The rotation angle is based on the elapsed time. if( smf->flags & MDL_ROTATING ) { - glTranslatef(smf->rotationCenterX, smf->rotationCenterY, smf->rotationCenterZ); - glRotatef(rotateOffset, smf->xrotate, smf->yrotate, smf->zrotate); - glTranslatef(-smf->rotationCenterX, -smf->rotationCenterY, -smf->rotationCenterZ); + gl_RenderState.mModelMatrix.translate(smf->rotationCenterX, smf->rotationCenterY, smf->rotationCenterZ); + gl_RenderState.mModelMatrix.rotate(rotateOffset, smf->xrotate, smf->yrotate, smf->zrotate); + gl_RenderState.mModelMatrix.translate(-smf->rotationCenterX, -smf->rotationCenterY, -smf->rotationCenterZ); } // 3) Scaling model. - glScalef(scaleFactorX, scaleFactorZ, scaleFactorY); + gl_RenderState.mModelMatrix.scale(scaleFactorX, scaleFactorZ, scaleFactorY); // 4) Aplying model offsets (model offsets do not depend on model scalings). - glTranslatef(smf->xoffset / smf->xscale, smf->zoffset / smf->zscale, smf->yoffset / smf->yscale); + gl_RenderState.mModelMatrix.translate(smf->xoffset / smf->xscale, smf->zoffset / smf->zscale, smf->yoffset / smf->yscale); // 5) Applying model rotations. - glRotatef(-ANGLE_TO_FLOAT(smf->angleoffset), 0, 1, 0); - glRotatef(smf->pitchoffset, 0, 0, 1); - glRotatef(-smf->rolloffset, 1, 0, 0); - - glActiveTexture(GL_TEXTURE0); - -#if 0 - if (gl_light_models) - { - // The normal transform matrix only contains the inverse rotations and scalings but not the translations - NormalTransform.MakeIdentity(); - - NormalTransform.Scale(1.f/scaleFactorX, 1.f/scaleFactorZ, 1.f/scaleFactorY); - if( smf->flags & MDL_ROTATING ) NormalTransform.Rotate(smf->xrotate, smf->yrotate, smf->zrotate, -rotateOffset); - if (pitch != 0) NormalTransform.Rotate(0,0,1,-pitch); - if (angle != 0) NormalTransform.Rotate(0,1,0, angle); - - gl_RenderFrameModels( smf, spr->actor->state, spr->actor->tics, RUNTIME_TYPE(spr->actor), &ModelToWorld, &NormalTransform, translation ); - } -#endif - + gl_RenderState.mModelMatrix.rotate(-ANGLE_TO_FLOAT(smf->angleoffset), 0, 1, 0); + gl_RenderState.mModelMatrix.rotate(smf->pitchoffset, 0, 0, 1); + gl_RenderState.mModelMatrix.rotate(-smf->rolloffset, 1, 0, 0); + gl_RenderState.EnableModelMatrix(true); gl_RenderFrameModels( smf, spr->actor->state, spr->actor->tics, RUNTIME_TYPE(spr->actor), NULL, translation ); - - glActiveTexture(GL_TEXTURE7); - glMatrixMode(GL_TEXTURE); - glLoadIdentity(); - glActiveTexture(GL_TEXTURE0); - glMatrixMode(GL_MODELVIEW); + gl_RenderState.EnableModelMatrix(false); glDepthFunc(GL_LESS); if (!( spr->actor->RenderStyle == LegacyRenderStyles[STYLE_Normal] )) diff --git a/src/gl/renderer/gl_renderstate.cpp b/src/gl/renderer/gl_renderstate.cpp index 6a510f85a6..6f7382c91a 100644 --- a/src/gl/renderer/gl_renderstate.cpp +++ b/src/gl/renderer/gl_renderstate.cpp @@ -56,6 +56,8 @@ FRenderState gl_RenderState; CVAR(Bool, gl_direct_state_change, true, 0) +static VSMatrix identityMatrix(1); + //========================================================================== // // @@ -225,6 +227,27 @@ bool FRenderState::ApplyShader() activeShader->muColormapStart.Set(r, g, b, 1.f); } } + if (mTextureMatrixEnabled) + { + mTextureMatrix.matrixToGL(activeShader->texturematrix_index); + activeShader->currentTextureMatrixState = true; + } + else if (activeShader->currentTextureMatrixState) + { + activeShader->currentTextureMatrixState = false; + identityMatrix.matrixToGL(activeShader->texturematrix_index); + } + + if (mModelMatrixEnabled) + { + mModelMatrix.matrixToGL(activeShader->modelmatrix_index); + activeShader->currentModelMatrixState = true; + } + else if (activeShader->currentModelMatrixState) + { + activeShader->currentModelMatrixState = false; + identityMatrix.matrixToGL(activeShader->modelmatrix_index); + } return true; } diff --git a/src/gl/renderer/gl_renderstate.h b/src/gl/renderer/gl_renderstate.h index 815974d974..6c2965e22a 100644 --- a/src/gl/renderer/gl_renderstate.h +++ b/src/gl/renderer/gl_renderstate.h @@ -4,6 +4,7 @@ #include #include "gl/system/gl_interface.h" #include "gl/data/gl_data.h" +#include "gl/data/gl_matrix.h" #include "c_cvars.h" #include "r_defs.h" @@ -58,6 +59,8 @@ class FRenderState bool m2D; float mInterpolationFactor; float mClipHeightTop, mClipHeightBottom; + bool mModelMatrixEnabled; + bool mTextureMatrixEnabled; FVertexBuffer *mVertexBuffer, *mCurrentVertexBuffer; FStateVec4 mColor; @@ -80,6 +83,12 @@ class FRenderState bool ApplyShader(); public: + + VSMatrix mProjectionMatrix; + VSMatrix mViewMatrix; + VSMatrix mModelMatrix; + VSMatrix mTextureMatrix; + FRenderState() { Reset(); @@ -89,6 +98,7 @@ public: void SetupShader(int &shaderindex, float warptime); void Apply(); + void ApplyMatrices(); void SetVertexBuffer(FVertexBuffer *vb) { @@ -180,6 +190,16 @@ public: mBrightmapEnabled = on; } + void EnableModelMatrix(bool on) + { + mModelMatrixEnabled = on; + } + + void EnableTextureMatrix(bool on) + { + mTextureMatrixEnabled = on; + } + void SetCameraPos(float x, float y, float z) { mCameraPos.Set(x, z, y, 0); diff --git a/src/gl/scene/gl_drawinfo.cpp b/src/gl/scene/gl_drawinfo.cpp index 7dcf6319ec..cac3bad6af 100644 --- a/src/gl/scene/gl_drawinfo.cpp +++ b/src/gl/scene/gl_drawinfo.cpp @@ -1083,10 +1083,9 @@ void FDrawInfo::DrawFloodedPlane(wallseg * ws, float planez, sector_t * sec, boo float fviewy = FIXED2FLOAT(viewy); float fviewz = FIXED2FLOAT(viewz); + gl_SetPlaneTextureRotation(&plane, gltexture); gl_RenderState.Apply(); - bool pushed = gl_SetPlaneTextureRotation(&plane, gltexture); - float prj_fac1 = (planez-fviewz)/(ws->z1-fviewz); float prj_fac2 = (planez-fviewz)/(ws->z2-fviewz); @@ -1113,11 +1112,7 @@ void FDrawInfo::DrawFloodedPlane(wallseg * ws, float planez, sector_t * sec, boo ptr++; GLRenderer->mVBO->RenderCurrent(ptr, GL_TRIANGLE_FAN); - if (pushed) - { - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - } + gl_RenderState.EnableTextureMatrix(false); } //========================================================================== diff --git a/src/gl/scene/gl_drawinfo.h b/src/gl/scene/gl_drawinfo.h index 07735eb3ff..01a72701b5 100644 --- a/src/gl/scene/gl_drawinfo.h +++ b/src/gl/scene/gl_drawinfo.h @@ -273,7 +273,7 @@ public: extern FDrawInfo * gl_drawinfo; -bool gl_SetPlaneTextureRotation(const GLSectorPlane * secplane, FMaterial * gltexture); +void gl_SetPlaneTextureRotation(const GLSectorPlane * secplane, FMaterial * gltexture); void gl_SetRenderStyle(FRenderStyle style, bool drawopaque, bool allowcolorblending); #endif \ No newline at end of file diff --git a/src/gl/scene/gl_flats.cpp b/src/gl/scene/gl_flats.cpp index 9f01953077..6b449ded30 100644 --- a/src/gl/scene/gl_flats.cpp +++ b/src/gl/scene/gl_flats.cpp @@ -70,7 +70,7 @@ // //========================================================================== -bool gl_SetPlaneTextureRotation(const GLSectorPlane * secplane, FMaterial * gltexture) +void gl_SetPlaneTextureRotation(const GLSectorPlane * secplane, FMaterial * gltexture) { // only manipulate the texture matrix if needed. if (secplane->xoffs != 0 || secplane->yoffs != 0 || @@ -93,15 +93,13 @@ bool gl_SetPlaneTextureRotation(const GLSectorPlane * secplane, FMaterial * glte float xscale2=64.f/gltexture->TextureWidth(GLUSE_TEXTURE); float yscale2=64.f/gltexture->TextureHeight(GLUSE_TEXTURE); - glMatrixMode(GL_TEXTURE); - glPushMatrix(); - glScalef(xscale1 ,yscale1,1.0f); - glTranslatef(uoffs,voffs,0.0f); - glScalef(xscale2 ,yscale2,1.0f); - glRotatef(angle,0.0f,0.0f,1.0f); - return true; + gl_RenderState.mTextureMatrix.loadIdentity(); + gl_RenderState.mTextureMatrix.scale(xscale1 ,yscale1,1.0f); + gl_RenderState.mTextureMatrix.translate(uoffs,voffs,0.0f); + gl_RenderState.mTextureMatrix.scale(xscale2 ,yscale2,1.0f); + gl_RenderState.mTextureMatrix.rotate(angle,0.0f,0.0f,1.0f); + gl_RenderState.EnableTextureMatrix(true); } - return false; } @@ -377,13 +375,9 @@ void GLFlat::Draw(int pass) case GLPASS_TEXTURE: { gltexture->Bind(); - bool pushed = gl_SetPlaneTextureRotation(&plane, gltexture); + gl_SetPlaneTextureRotation(&plane, gltexture); DrawSubsectors(pass, false); - if (pushed) - { - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - } + gl_RenderState.EnableTextureMatrix(false); break; } @@ -440,13 +434,9 @@ void GLFlat::Draw(int pass) else { gltexture->Bind(); - bool pushed = gl_SetPlaneTextureRotation(&plane, gltexture); + gl_SetPlaneTextureRotation(&plane, gltexture); DrawSubsectors(pass, true); - if (pushed) - { - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - } + gl_RenderState.EnableTextureMatrix(false); } if (renderstyle==STYLE_Add) gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break; diff --git a/src/gl/scene/gl_portal.cpp b/src/gl/scene/gl_portal.cpp index c0fee33859..557fbd106d 100644 --- a/src/gl/scene/gl_portal.cpp +++ b/src/gl/scene/gl_portal.cpp @@ -1000,7 +1000,7 @@ void GLHorizonPortal::DrawContents() gltexture->Bind(); - bool pushed = gl_SetPlaneTextureRotation(sp, gltexture); + gl_SetPlaneTextureRotation(sp, gltexture); gl_RenderState.EnableAlphaTest(false); gl_RenderState.BlendFunc(GL_ONE,GL_ZERO); gl_RenderState.Apply(); @@ -1059,12 +1059,7 @@ void GLHorizonPortal::DrawContents() ptr++; GLRenderer->mVBO->RenderCurrent(ptr, GL_TRIANGLE_STRIP); - if (pushed) - { - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - } - + gl_RenderState.EnableTextureMatrix(false); PortalAll.Unclock(); } diff --git a/src/gl/scene/gl_scene.cpp b/src/gl/scene/gl_scene.cpp index d013d7b30e..e93df197ce 100644 --- a/src/gl/scene/gl_scene.cpp +++ b/src/gl/scene/gl_scene.cpp @@ -286,14 +286,6 @@ void FGLRenderer::SetProjection(float fov, float ratio, float fovratio) void FGLRenderer::SetViewMatrix(bool mirror, bool planemirror) { - glActiveTexture(GL_TEXTURE7); - glMatrixMode(GL_TEXTURE); - glLoadIdentity(); - - glActiveTexture(GL_TEXTURE0); - glMatrixMode(GL_TEXTURE); - glLoadIdentity(); - glMatrixMode(GL_MODELVIEW); glLoadIdentity(); diff --git a/src/gl/scene/gl_skydome.cpp b/src/gl/scene/gl_skydome.cpp index 221f2203b6..04f77a80f7 100644 --- a/src/gl/scene/gl_skydome.cpp +++ b/src/gl/scene/gl_skydome.cpp @@ -293,20 +293,17 @@ void RenderDome(FMaterial * tex, float x_offset, float y_offset, bool mirror, in glScalef(1.f, 1.2f * 1.17f, 1.f); yscale = 240.f / texh; } - glMatrixMode(GL_TEXTURE); - glPushMatrix(); - glLoadIdentity(); - glScalef(mirror? -xscale : xscale, yscale, 1.f); - glTranslatef(1.f, y_offset / texh, 1.f); - glMatrixMode(GL_MODELVIEW); + gl_RenderState.EnableTextureMatrix(true); + gl_RenderState.mTextureMatrix.loadIdentity(); + gl_RenderState.mTextureMatrix.scale(mirror? -xscale : xscale, yscale, 1.f); + gl_RenderState.mTextureMatrix.translate(1.f, y_offset / texh, 1.f); } GLRenderer->mSkyVBO->RenderDome(tex, mode); + gl_RenderState.EnableTextureMatrix(false); if (tex) { - glMatrixMode(GL_TEXTURE); - glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); } diff --git a/src/gl/shaders/gl_shader.cpp b/src/gl/shaders/gl_shader.cpp index 50b67c4caf..79746e5c23 100644 --- a/src/gl/shaders/gl_shader.cpp +++ b/src/gl/shaders/gl_shader.cpp @@ -206,6 +206,8 @@ bool FShader::Load(const char * name, const char * vert_prog_lump, const char * timer_index = glGetUniformLocation(hShader, "timer"); lights_index = glGetUniformLocation(hShader, "lights"); fakevb_index = glGetUniformLocation(hShader, "fakeVB"); + modelmatrix_index = glGetUniformLocation(hShader, "ModelMatrix"); + texturematrix_index = glGetUniformLocation(hShader, "TextureMatrix"); glBindAttribLocation(hShader, VATTR_VERTEX2, "aVertex2"); diff --git a/src/gl/shaders/gl_shader.h b/src/gl/shaders/gl_shader.h index da3be18cba..a87b1e5766 100644 --- a/src/gl/shaders/gl_shader.h +++ b/src/gl/shaders/gl_shader.h @@ -198,11 +198,15 @@ class FShader int timer_index; int lights_index; + int modelmatrix_index; + int texturematrix_index; public: int fakevb_index; private: int currentglowstate; int currentfixedcolormap; + bool currentTextureMatrixState; + bool currentModelMatrixState; public: FShader(const char *name) @@ -211,6 +215,8 @@ public: hShader = hVertProg = hFragProg = 0; currentglowstate = 0; currentfixedcolormap = 0; + currentTextureMatrixState = true; // by setting the matrix state to 'true' it is guaranteed to be set the first time the render state gets applied. + currentModelMatrixState = true; } ~FShader(); diff --git a/wadsrc/static/shaders/glsl/shaderdefs.i b/wadsrc/static/shaders/glsl/shaderdefs.i index d281ee2311..ebe751c964 100644 --- a/wadsrc/static/shaders/glsl/shaderdefs.i +++ b/wadsrc/static/shaders/glsl/shaderdefs.i @@ -36,8 +36,8 @@ uniform ivec4 uLightRange; // redefine the matrix names to what they actually represent. -#define ModelMatrix gl_TextureMatrix[7] -#define ViewMatrix gl_ModelViewMatrix #define ProjectionMatrix gl_ProjectionMatrix -#define TextureMatrix gl_TextureMatrix[0] +#define ViewMatrix gl_ModelViewMatrix +uniform mat4 ModelMatrix; +uniform mat4 TextureMatrix;