- remove all uses of builtin matrix manipulation. Only glLoadMatrix for view and projection matrix are left.

This commit is contained in:
Christoph Oelckers 2014-07-13 22:37:34 +02:00
parent dbb05c5f33
commit 2214c0ac06
9 changed files with 78 additions and 121 deletions

View File

@ -253,20 +253,15 @@ VSMatrix::lookAt(FLOATTYPE xPos, FLOATTYPE yPos, FLOATTYPE zPos,
void void
VSMatrix::perspective(FLOATTYPE fov, FLOATTYPE ratio, FLOATTYPE nearp, FLOATTYPE farp) VSMatrix::perspective(FLOATTYPE fov, FLOATTYPE ratio, FLOATTYPE nearp, FLOATTYPE farp)
{ {
FLOATTYPE projMatrix[16];
FLOATTYPE f = 1.0f / tan (fov * (M_PI / 360.0f)); FLOATTYPE f = 1.0f / tan (fov * (M_PI / 360.0f));
setIdentityMatrix(projMatrix,4); loadIdentity();
mMatrix[0] = f / ratio;
projMatrix[0] = f / ratio; mMatrix[1 * 4 + 1] = f;
projMatrix[1 * 4 + 1] = f; mMatrix[2 * 4 + 2] = (farp + nearp) / (nearp - farp);
projMatrix[2 * 4 + 2] = (farp + nearp) / (nearp - farp); mMatrix[3 * 4 + 2] = (2.0f * farp * nearp) / (nearp - farp);
projMatrix[3 * 4 + 2] = (2.0f * farp * nearp) / (nearp - farp); mMatrix[2 * 4 + 3] = -1.0f;
projMatrix[2 * 4 + 3] = -1.0f; mMatrix[3 * 4 + 3] = 0.0f;
projMatrix[3 * 4 + 3] = 0.0f;
multMatrix(projMatrix);
} }
@ -276,18 +271,14 @@ VSMatrix::ortho(FLOATTYPE left, FLOATTYPE right,
FLOATTYPE bottom, FLOATTYPE top, FLOATTYPE bottom, FLOATTYPE top,
FLOATTYPE nearp, FLOATTYPE farp) FLOATTYPE nearp, FLOATTYPE farp)
{ {
FLOATTYPE m[16]; loadIdentity();
setIdentityMatrix(m,4); mMatrix[0 * 4 + 0] = 2 / (right - left);
mMatrix[1 * 4 + 1] = 2 / (top - bottom);
m[0 * 4 + 0] = 2 / (right - left); mMatrix[2 * 4 + 2] = -2 / (farp - nearp);
m[1 * 4 + 1] = 2 / (top - bottom); mMatrix[3 * 4 + 0] = -(right + left) / (right - left);
m[2 * 4 + 2] = -2 / (farp - nearp); mMatrix[3 * 4 + 1] = -(top + bottom) / (top - bottom);
m[3 * 4 + 0] = -(right + left) / (right - left); mMatrix[3 * 4 + 2] = -(farp + nearp) / (farp - nearp);
m[3 * 4 + 1] = -(top + bottom) / (top - bottom);
m[3 * 4 + 2] = -(farp + nearp) / (farp - nearp);
multMatrix(m);
} }

View File

@ -863,11 +863,6 @@ void gl_RenderHUDModel(pspdef_t *psp, fixed_t ofsx, fixed_t ofsy)
if ( smf == NULL ) if ( smf == NULL )
return; return;
// [BB] The model has to be drawn independtly from the position of the player,
// so we have to reset the GL_MODELVIEW matrix.
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
// [BB] In case the model should be rendered translucent, do back face culling. // [BB] In case the model should be rendered translucent, do back face culling.
@ -879,28 +874,31 @@ void gl_RenderHUDModel(pspdef_t *psp, fixed_t ofsx, fixed_t ofsy)
glFrontFace(GL_CCW); glFrontFace(GL_CCW);
} }
// [BB] The model has to be drawn independently from the position of the player,
// so we have to reset the view matrix.
gl_RenderState.mViewMatrix.loadIdentity();
// Scaling model (y scale for a sprite means height, i.e. z in the world!). // Scaling model (y scale for a sprite means height, i.e. z in the world!).
glScalef(smf->xscale, smf->zscale, smf->yscale); gl_RenderState.mViewMatrix.scale(smf->xscale, smf->zscale, smf->yscale);
// Aplying model offsets (model offsets do not depend on model scalings). // 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.mViewMatrix.translate(smf->xoffset / smf->xscale, smf->zoffset / smf->zscale, smf->yoffset / smf->yscale);
// [BB] Weapon bob, very similar to the normal Doom weapon bob. // [BB] Weapon bob, very similar to the normal Doom weapon bob.
glRotatef(FIXED2FLOAT(ofsx)/4, 0, 1, 0); gl_RenderState.mViewMatrix.rotate(FIXED2FLOAT(ofsx)/4, 0, 1, 0);
glRotatef(-FIXED2FLOAT(ofsy-WEAPONTOP)/4, 1, 0, 0); gl_RenderState.mViewMatrix.rotate(-FIXED2FLOAT(ofsy-WEAPONTOP)/4, 1, 0, 0);
// [BB] For some reason the jDoom models need to be rotated. // [BB] For some reason the jDoom models need to be rotated.
glRotatef(90., 0, 1, 0); gl_RenderState.mViewMatrix.rotate(90.f, 0, 1, 0);
// Applying angleoffset, pitchoffset, rolloffset. // Applying angleoffset, pitchoffset, rolloffset.
glRotatef(-ANGLE_TO_FLOAT(smf->angleoffset), 0, 1, 0); gl_RenderState.mViewMatrix.rotate(-ANGLE_TO_FLOAT(smf->angleoffset), 0, 1, 0);
glRotatef(smf->pitchoffset, 0, 0, 1); gl_RenderState.mViewMatrix.rotate(smf->pitchoffset, 0, 0, 1);
glRotatef(-smf->rolloffset, 1, 0, 0); gl_RenderState.mViewMatrix.rotate(-smf->rolloffset, 1, 0, 0);
gl_RenderState.ApplyMatrices();
gl_RenderFrameModels( smf, psp->state, psp->tics, playermo->player->ReadyWeapon->GetClass(), NULL, 0 ); gl_RenderFrameModels( smf, psp->state, psp->tics, playermo->player->ReadyWeapon->GetClass(), NULL, 0 );
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
if (!( playermo->RenderStyle == LegacyRenderStyles[STYLE_Normal] )) if (!( playermo->RenderStyle == LegacyRenderStyles[STYLE_Normal] ))
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);

View File

@ -267,14 +267,13 @@ void FGLRenderer::ClearBorders()
int borderHeight = (trueHeight - height) / 2; int borderHeight = (trueHeight - height) / 2;
glViewport(0, 0, width, trueHeight); glViewport(0, 0, width, trueHeight);
glMatrixMode(GL_PROJECTION); gl_RenderState.mProjectionMatrix.loadIdentity();
glLoadIdentity(); gl_RenderState.mProjectionMatrix.ortho(0.0f, width * 1.0f, 0.0f, trueHeight, -1.0f, 1.0f);
glOrtho(0.0, width * 1.0, 0.0, trueHeight, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
gl_RenderState.SetColor(0.f ,0.f ,0.f ,1.f); gl_RenderState.SetColor(0.f ,0.f ,0.f ,1.f);
gl_RenderState.Set2DMode(true); gl_RenderState.Set2DMode(true);
gl_RenderState.EnableTexture(false); gl_RenderState.EnableTexture(false);
gl_RenderState.Apply(); gl_RenderState.Apply();
gl_RenderState.ApplyMatrices();
FFlatVertex *ptr = GLRenderer->mVBO->GetBuffer(); FFlatVertex *ptr = GLRenderer->mVBO->GetBuffer();
ptr->Set(0, borderHeight, 0, 0, 0); ptr++; ptr->Set(0, borderHeight, 0, 0, 0); ptr++;

View File

@ -57,6 +57,7 @@ CVAR(Bool, gl_direct_state_change, true, 0)
static VSMatrix identityMatrix(1); static VSMatrix identityMatrix(1);
TArray<VSMatrix> gl_MatrixStack;
//========================================================================== //==========================================================================
// //
@ -296,3 +297,12 @@ void FRenderState::Apply()
ApplyShader(); ApplyShader();
} }
void FRenderState::ApplyMatrices()
{
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(mViewMatrix.get());
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(mProjectionMatrix.get());
}

View File

@ -9,6 +9,7 @@
#include "r_defs.h" #include "r_defs.h"
class FVertexBuffer; class FVertexBuffer;
extern TArray<VSMatrix> gl_MatrixStack;
EXTERN_CVAR(Bool, gl_direct_state_change) EXTERN_CVAR(Bool, gl_direct_state_change)

View File

@ -117,17 +117,14 @@ void GLPortal::BeginScene()
void GLPortal::ClearScreen() void GLPortal::ClearScreen()
{ {
bool multi = !!glIsEnabled(GL_MULTISAMPLE); bool multi = !!glIsEnabled(GL_MULTISAMPLE);
glMatrixMode(GL_MODELVIEW); gl_MatrixStack.Push(gl_RenderState.mViewMatrix);
glPushMatrix(); gl_MatrixStack.Push(gl_RenderState.mProjectionMatrix);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
screen->Begin2D(false); screen->Begin2D(false);
screen->Dim(0, 1.f, 0, 0, SCREENWIDTH, SCREENHEIGHT); screen->Dim(0, 1.f, 0, 0, SCREENWIDTH, SCREENHEIGHT);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION); gl_MatrixStack.Pop(gl_RenderState.mProjectionMatrix);
glPopMatrix(); gl_MatrixStack.Pop(gl_RenderState.mViewMatrix);
glMatrixMode(GL_MODELVIEW); gl_RenderState.ApplyMatrices();
glPopMatrix();
if (multi) glEnable(GL_MULTISAMPLE); if (multi) glEnable(GL_MULTISAMPLE);
gl_RenderState.Set2DMode(false); gl_RenderState.Set2DMode(false);
} }

View File

@ -245,36 +245,11 @@ void FGLRenderer::SetCameraPos(fixed_t viewx, fixed_t viewy, fixed_t viewz, angl
// //
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void setPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
GLdouble m[4][4];
double sine, cotangent, deltaZ;
double radians = fovy / 2 * M_PI / 180;
deltaZ = zFar - zNear;
sine = sin(radians);
if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) {
return;
}
cotangent = cos(radians) / sine;
memset(m, 0, sizeof(m));
m[0][0] = cotangent / aspect;
m[1][1] = cotangent;
m[2][2] = -(zFar + zNear) / deltaZ;
m[2][3] = -1;
m[3][2] = -2 * zNear * zFar / deltaZ;
m[3][3] = 0;
glLoadMatrixd(&m[0][0]);
}
void FGLRenderer::SetProjection(float fov, float ratio, float fovratio) void FGLRenderer::SetProjection(float fov, float ratio, float fovratio)
{ {
glMatrixMode(GL_PROJECTION);
float fovy = 2 * RAD2DEG(atan(tan(DEG2RAD(fov) / 2) / fovratio)); float fovy = 2 * RAD2DEG(atan(tan(DEG2RAD(fov) / 2) / fovratio));
setPerspective(fovy, ratio, 5.f, 65536.f); gl_RenderState.mProjectionMatrix.perspective(fovy, ratio, 5.f, 65536.f);
gl_RenderState.Set2DMode(false); gl_RenderState.Set2DMode(false);
} }
@ -286,17 +261,15 @@ void FGLRenderer::SetProjection(float fov, float ratio, float fovratio)
void FGLRenderer::SetViewMatrix(bool mirror, bool planemirror) void FGLRenderer::SetViewMatrix(bool mirror, bool planemirror)
{ {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float mult = mirror? -1:1; float mult = mirror? -1:1;
float planemult = planemirror? -1:1; float planemult = planemirror? -1:1;
glRotatef(GLRenderer->mAngles.Roll, 0.0f, 0.0f, 1.0f); gl_RenderState.mViewMatrix.loadIdentity();
glRotatef(GLRenderer->mAngles.Pitch, 1.0f, 0.0f, 0.0f); gl_RenderState.mViewMatrix.rotate(GLRenderer->mAngles.Roll, 0.0f, 0.0f, 1.0f);
glRotatef(GLRenderer->mAngles.Yaw, 0.0f, mult, 0.0f); gl_RenderState.mViewMatrix.rotate(GLRenderer->mAngles.Pitch, 1.0f, 0.0f, 0.0f);
glTranslatef( GLRenderer->mCameraPos.X * mult, -GLRenderer->mCameraPos.Z*planemult, -GLRenderer->mCameraPos.Y); gl_RenderState.mViewMatrix.rotate(GLRenderer->mAngles.Yaw, 0.0f, mult, 0.0f);
glScalef(-mult, planemult, 1); gl_RenderState.mViewMatrix.translate( GLRenderer->mCameraPos.X * mult, -GLRenderer->mCameraPos.Z*planemult, -GLRenderer->mCameraPos.Y);
gl_RenderState.mViewMatrix.scale(-mult, planemult, 1);
} }
@ -310,6 +283,7 @@ void FGLRenderer::SetupView(fixed_t viewx, fixed_t viewy, fixed_t viewz, angle_t
{ {
SetCameraPos(viewx, viewy, viewz, viewangle); SetCameraPos(viewx, viewy, viewz, viewangle);
SetViewMatrix(mirror, planemirror); SetViewMatrix(mirror, planemirror);
gl_RenderState.ApplyMatrices();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -887,6 +861,7 @@ sector_t * FGLRenderer::RenderViewpoint (AActor * camera, GL_IRECT * bounds, flo
SetProjection(fov, ratio, fovratio); // switch to perspective mode and set up clipper SetProjection(fov, ratio, fovratio); // switch to perspective mode and set up clipper
SetCameraPos(viewx, viewy, viewz, viewangle); SetCameraPos(viewx, viewy, viewz, viewangle);
SetViewMatrix(false, false); SetViewMatrix(false, false);
gl_RenderState.ApplyMatrices();
clipper.Clear(); clipper.Clear();
angle_t a1 = FrustumAngle(); angle_t a1 = FrustumAngle();

View File

@ -268,29 +268,30 @@ void RenderDome(FMaterial * tex, float x_offset, float y_offset, bool mirror, in
if (tex) if (tex)
{ {
glPushMatrix();
tex->Bind(0, 0); tex->Bind(0, 0);
texw = tex->TextureWidth(GLUSE_TEXTURE); texw = tex->TextureWidth(GLUSE_TEXTURE);
texh = tex->TextureHeight(GLUSE_TEXTURE); texh = tex->TextureHeight(GLUSE_TEXTURE);
gl_RenderState.EnableModelMatrix(true);
glRotatef(-180.0f+x_offset, 0.f, 1.f, 0.f); gl_RenderState.mModelMatrix.loadIdentity();
gl_RenderState.mModelMatrix.rotate(-180.0f+x_offset, 0.f, 1.f, 0.f);
float xscale = 1024.f / float(texw); float xscale = 1024.f / float(texw);
float yscale = 1.f; float yscale = 1.f;
if (texh < 200) if (texh < 200)
{ {
glTranslatef(0.f, -1250.f, 0.f); gl_RenderState.mModelMatrix.translate(0.f, -1250.f, 0.f);
glScalef(1.f, texh/230.f, 1.f); gl_RenderState.mModelMatrix.scale(1.f, texh/230.f, 1.f);
} }
else if (texh <= 240) else if (texh <= 240)
{ {
glTranslatef(0.f, (200 - texh + tex->tex->SkyOffset + skyoffset)*skyoffsetfactor, 0.f); gl_RenderState.mModelMatrix.translate(0.f, (200 - texh + tex->tex->SkyOffset + skyoffset)*skyoffsetfactor, 0.f);
glScalef(1.f, 1.f + ((texh-200.f)/200.f) * 1.17f, 1.f); gl_RenderState.mModelMatrix.scale(1.f, 1.f + ((texh-200.f)/200.f) * 1.17f, 1.f);
} }
else else
{ {
glTranslatef(0.f, (-40 + tex->tex->SkyOffset + skyoffset)*skyoffsetfactor, 0.f); gl_RenderState.mModelMatrix.translate(0.f, (-40 + tex->tex->SkyOffset + skyoffset)*skyoffsetfactor, 0.f);
glScalef(1.f, 1.2f * 1.17f, 1.f); gl_RenderState.mModelMatrix.scale(1.f, 1.2f * 1.17f, 1.f);
yscale = 240.f / texh; yscale = 240.f / texh;
} }
gl_RenderState.EnableTextureMatrix(true); gl_RenderState.EnableTextureMatrix(true);
@ -301,13 +302,7 @@ void RenderDome(FMaterial * tex, float x_offset, float y_offset, bool mirror, in
GLRenderer->mSkyVBO->RenderDome(tex, mode); GLRenderer->mSkyVBO->RenderDome(tex, mode);
gl_RenderState.EnableTextureMatrix(false); gl_RenderState.EnableTextureMatrix(false);
gl_RenderState.EnableModelMatrix(false);
if (tex)
{
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
} }
@ -323,10 +318,12 @@ static void RenderBox(FTextureID texno, FMaterial * gltex, float x_offset, bool
int faces; int faces;
FMaterial * tex; FMaterial * tex;
gl_RenderState.EnableModelMatrix(true);
if (!sky2) if (!sky2)
glRotatef(-180.0f+x_offset, glset.skyrotatevector.X, glset.skyrotatevector.Z, glset.skyrotatevector.Y); gl_RenderState.mModelMatrix.rotate(-180.0f+x_offset, glset.skyrotatevector.X, glset.skyrotatevector.Z, glset.skyrotatevector.Y);
else else
glRotatef(-180.0f+x_offset, glset.skyrotatevector2.X, glset.skyrotatevector2.Z, glset.skyrotatevector2.Y); gl_RenderState.mModelMatrix.rotate(-180.0f+x_offset, glset.skyrotatevector2.X, glset.skyrotatevector2.Z, glset.skyrotatevector2.Y);
FFlatVertex *ptr; FFlatVertex *ptr;
if (sb->faces[5]) if (sb->faces[5])
@ -456,6 +453,7 @@ static void RenderBox(FTextureID texno, FMaterial * gltex, float x_offset, bool
ptr->Set(-128.f, -128.f, 128.f, 1, 1); ptr->Set(-128.f, -128.f, 128.f, 1, 1);
ptr++; ptr++;
GLRenderer->mVBO->RenderCurrent(ptr, GL_TRIANGLE_STRIP); GLRenderer->mVBO->RenderCurrent(ptr, GL_TRIANGLE_STRIP);
gl_RenderState.EnableModelMatrix(false);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -477,8 +475,7 @@ void GLSkyPortal::DrawContents()
gl_RenderState.EnableAlphaTest(false); gl_RenderState.EnableAlphaTest(false);
gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_MODELVIEW); gl_MatrixStack.Push(gl_RenderState.mViewMatrix);
glPushMatrix();
GLRenderer->SetupView(0, 0, 0, viewangle, !!(MirrorFlag&1), !!(PlaneMirrorFlag&1)); GLRenderer->SetupView(0, 0, 0, viewangle, !!(MirrorFlag&1), !!(PlaneMirrorFlag&1));
if (origin->texture[0] && origin->texture[0]->tex->gl_info.bSkybox) if (origin->texture[0] && origin->texture[0]->tex->gl_info.bSkybox)
@ -519,7 +516,8 @@ void GLSkyPortal::DrawContents()
} }
gl_RenderState.SetVertexBuffer(GLRenderer->mVBO); gl_RenderState.SetVertexBuffer(GLRenderer->mVBO);
} }
glPopMatrix(); gl_MatrixStack.Pop(gl_RenderState.mViewMatrix);
gl_RenderState.ApplyMatrices();
glset.lightmode = oldlightmode; glset.lightmode = oldlightmode;
} }

View File

@ -54,6 +54,7 @@
#include "gl/system/gl_interface.h" #include "gl/system/gl_interface.h"
#include "gl/system/gl_framebuffer.h" #include "gl/system/gl_framebuffer.h"
#include "gl/renderer/gl_renderer.h" #include "gl/renderer/gl_renderer.h"
#include "gl/renderer/gl_renderstate.h"
#include "gl/renderer/gl_lightdata.h" #include "gl/renderer/gl_lightdata.h"
#include "gl/data/gl_data.h" #include "gl/data/gl_data.h"
#include "gl/textures/gl_hwtexture.h" #include "gl/textures/gl_hwtexture.h"
@ -386,23 +387,10 @@ FNativePalette *OpenGLFrameBuffer::CreatePalette(FRemapTable *remap)
//========================================================================== //==========================================================================
bool OpenGLFrameBuffer::Begin2D(bool) bool OpenGLFrameBuffer::Begin2D(bool)
{ {
glActiveTexture(GL_TEXTURE7); gl_RenderState.mViewMatrix.loadIdentity();
glMatrixMode(GL_TEXTURE); gl_RenderState.mProjectionMatrix.ortho(0, GetWidth(), GetHeight(), 0, -1.0f, 1.0f);
glLoadIdentity(); gl_RenderState.ApplyMatrices();
glActiveTexture(GL_TEXTURE0);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(
(GLdouble) 0,
(GLdouble) GetWidth(),
(GLdouble) GetHeight(),
(GLdouble) 0,
(GLdouble) -1.0,
(GLdouble) 1.0
);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
// Korshun: ENABLE AUTOMAP ANTIALIASING!!! // Korshun: ENABLE AUTOMAP ANTIALIASING!!!