From 34ee04f2ce94d41b61192d84a7d01c6a14597193 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Mon, 18 Jun 2018 18:18:28 +0200 Subject: [PATCH] - moved the matrix math for the sky dome into backend independent code. --- src/gl/scene/gl_skydome.cpp | 49 ++------------------------- src/hwrenderer/scene/hw_skydome.cpp | 52 +++++++++++++++++++++++++++++ src/hwrenderer/scene/hw_skydome.h | 5 ++- src/polyrenderer/scene/poly_sky.cpp | 3 -- src/r_sky.h | 7 ++++ 5 files changed, 65 insertions(+), 51 deletions(-) diff --git a/src/gl/scene/gl_skydome.cpp b/src/gl/scene/gl_skydome.cpp index 4077387545..30ff36e9ed 100644 --- a/src/gl/scene/gl_skydome.cpp +++ b/src/gl/scene/gl_skydome.cpp @@ -38,7 +38,6 @@ #include "gl/shaders/gl_shader.h" -EXTERN_CVAR(Float, skyoffset) //----------------------------------------------------------------------------- // // @@ -118,57 +117,13 @@ void FSkyVertexBuffer::RenderDome(FMaterial *tex, int mode) void RenderDome(FMaterial * tex, float x_offset, float y_offset, bool mirror, int mode) { - int texh = 0; - int texw = 0; - - // 57 world units roughly represent one sky texel for the glTranslate call. - const float skyoffsetfactor = 57; - if (tex) { gl_RenderState.SetMaterial(tex, CLAMP_NONE, 0, -1, false); - texw = tex->TextureWidth(); - texh = tex->TextureHeight(); gl_RenderState.EnableModelMatrix(true); - - gl_RenderState.mModelMatrix.loadIdentity(); - gl_RenderState.mModelMatrix.rotate(-180.0f + x_offset, 0.f, 1.f, 0.f); - - float xscale = texw < 1024.f ? floor(1024.f / float(texw)) : 1.f; - float yscale = 1.f; - if (texh <= 128 && (level.flags & LEVEL_FORCETILEDSKY)) - { - gl_RenderState.mModelMatrix.translate(0.f, (-40 + tex->tex->SkyOffset + skyoffset)*skyoffsetfactor, 0.f); - gl_RenderState.mModelMatrix.scale(1.f, 1.2f * 1.17f, 1.f); - yscale = 240.f / texh; - } - else if (texh < 128) - { - // smaller sky textures must be tiled. We restrict it to 128 sky pixels, though - gl_RenderState.mModelMatrix.translate(0.f, -1250.f, 0.f); - gl_RenderState.mModelMatrix.scale(1.f, 128 / 230.f, 1.f); - yscale = 128 / texh; // intentionally left as integer. - } - else if (texh < 200) - { - gl_RenderState.mModelMatrix.translate(0.f, -1250.f, 0.f); - gl_RenderState.mModelMatrix.scale(1.f, texh / 230.f, 1.f); - } - else if (texh <= 240) - { - gl_RenderState.mModelMatrix.translate(0.f, (200 - texh + tex->tex->SkyOffset + skyoffset)*skyoffsetfactor, 0.f); - gl_RenderState.mModelMatrix.scale(1.f, 1.f + ((texh - 200.f) / 200.f) * 1.17f, 1.f); - } - else - { - gl_RenderState.mModelMatrix.translate(0.f, (-40 + tex->tex->SkyOffset + skyoffset)*skyoffsetfactor, 0.f); - gl_RenderState.mModelMatrix.scale(1.f, 1.2f * 1.17f, 1.f); - yscale = 240.f / texh; - } 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->SetupMatrices(tex, x_offset, y_offset, mirror, mode, gl_RenderState.mModelMatrix, gl_RenderState.mTextureMatrix); } GLRenderer->mSkyVBO->RenderDome(tex, mode); diff --git a/src/hwrenderer/scene/hw_skydome.cpp b/src/hwrenderer/scene/hw_skydome.cpp index 78bde1d63d..1de0999f6c 100644 --- a/src/hwrenderer/scene/hw_skydome.cpp +++ b/src/hwrenderer/scene/hw_skydome.cpp @@ -59,7 +59,10 @@ #include "r_state.h" #include "r_utility.h" #include "g_levellocals.h" +#include "r_sky.h" + #include "textures/skyboxtexture.h" +#include "hwrenderer/textures/hw_material.h" #include "hw_skydome.h" //----------------------------------------------------------------------------- @@ -68,6 +71,7 @@ // also shamelessly lifted from ZDoomGL! ;) // //----------------------------------------------------------------------------- +EXTERN_CVAR(Float, skyoffset) //----------------------------------------------------------------------------- // @@ -258,4 +262,52 @@ void FSkyDomeCreator::CreateDome() ptr[37].SetXYZ(-128.f, 128.f, 128.f, 1, 0); } +//----------------------------------------------------------------------------- +// +// +// +//----------------------------------------------------------------------------- +void FSkyDomeCreator::SetupMatrices(FMaterial *tex, float x_offset, float y_offset, bool mirror, int mode, VSMatrix &modelMatrix, VSMatrix &textureMatrix) +{ + int texw = tex->TextureWidth(); + int texh = tex->TextureHeight(); + + modelMatrix.loadIdentity(); + modelMatrix.rotate(-180.0f + x_offset, 0.f, 1.f, 0.f); + + float xscale = texw < 1024.f ? floor(1024.f / float(texw)) : 1.f; + float yscale = 1.f; + if (texh <= 128 && (level.flags & LEVEL_FORCETILEDSKY)) + { + modelMatrix.translate(0.f, (-40 + tex->tex->SkyOffset + skyoffset)*skyoffsetfactor, 0.f); + modelMatrix.scale(1.f, 1.2f * 1.17f, 1.f); + yscale = 240.f / texh; + } + else if (texh < 128) + { + // smaller sky textures must be tiled. We restrict it to 128 sky pixels, though + modelMatrix.translate(0.f, -1250.f, 0.f); + modelMatrix.scale(1.f, 128 / 230.f, 1.f); + yscale = 128 / texh; // intentionally left as integer. + } + else if (texh < 200) + { + modelMatrix.translate(0.f, -1250.f, 0.f); + modelMatrix.scale(1.f, texh / 230.f, 1.f); + } + else if (texh <= 240) + { + modelMatrix.translate(0.f, (200 - texh + tex->tex->SkyOffset + skyoffset)*skyoffsetfactor, 0.f); + modelMatrix.scale(1.f, 1.f + ((texh - 200.f) / 200.f) * 1.17f, 1.f); + } + else + { + modelMatrix.translate(0.f, (-40 + tex->tex->SkyOffset + skyoffset)*skyoffsetfactor, 0.f); + modelMatrix.scale(1.f, 1.2f * 1.17f, 1.f); + yscale = 240.f / texh; + } + textureMatrix.loadIdentity(); + textureMatrix.scale(mirror ? -xscale : xscale, yscale, 1.f); + textureMatrix.translate(1.f, y_offset / texh, 1.f); +} \ No newline at end of file diff --git a/src/hwrenderer/scene/hw_skydome.h b/src/hwrenderer/scene/hw_skydome.h index a0f9a2b46f..9fcaf29eff 100644 --- a/src/hwrenderer/scene/hw_skydome.h +++ b/src/hwrenderer/scene/hw_skydome.h @@ -1,6 +1,9 @@ #pragma once #include "v_palette.h" +#include "r_data/matrix.h" + +class FMaterial; struct FSkyVertex { @@ -60,7 +63,7 @@ public: FSkyDomeCreator(); virtual ~FSkyDomeCreator(); - void RenderDome(FMaterial *tex, int mode); + void SetupMatrices(FMaterial *tex, float x_offset, float y_offset, bool mirror, int mode, VSMatrix &modelmatrix, VSMatrix &textureMatrix); int FaceStart(int i) { diff --git a/src/polyrenderer/scene/poly_sky.cpp b/src/polyrenderer/scene/poly_sky.cpp index 6f7294b9e4..59157bf7b7 100644 --- a/src/polyrenderer/scene/poly_sky.cpp +++ b/src/polyrenderer/scene/poly_sky.cpp @@ -219,9 +219,6 @@ Mat4f PolySkyDome::GLSkyMath() int texh = 0; int texw = 0; - // 57 world units roughly represent one sky texel for the glTranslate call. - const float skyoffsetfactor = 57; - Mat4f modelMatrix = Mat4f::Identity(); if (tex) { diff --git a/src/r_sky.h b/src/r_sky.h index a6e4cae9f4..d9963d96c4 100644 --- a/src/r_sky.h +++ b/src/r_sky.h @@ -47,4 +47,11 @@ extern int freelookviewheight; void R_InitSkyMap (); void R_UpdateSky (uint64_t mstime); +// 57 world units roughly represent one sky texel for the glTranslate call. +enum +{ + skyoffsetfactor = 57 +}; + + #endif //__R_SKY_H__