From a62cd641386be53f08cfcd6daeff549bf5ddce36 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 27 Oct 2018 22:55:33 +0200 Subject: [PATCH] - removed all direct OpenGL dependencies from gl_models.cpp. --- src/gl/models/gl_models.cpp | 59 ++++++++++++--------------- src/gl/models/gl_models.h | 3 +- src/gl/renderer/gl_renderstate.cpp | 1 - src/gl/renderer/gl_renderstate.h | 12 ------ src/gl/scene/gl_drawinfo.cpp | 17 +++++++- src/gl/scene/gl_drawinfo.h | 3 +- src/gl/system/gl_framebuffer.cpp | 2 +- src/hwrenderer/scene/hw_drawinfo.h | 10 ++++- src/hwrenderer/scene/hw_renderstate.h | 12 ++++++ 9 files changed, 67 insertions(+), 52 deletions(-) diff --git a/src/gl/models/gl_models.cpp b/src/gl/models/gl_models.cpp index 2f72e485c..cb3230a5b 100644 --- a/src/gl/models/gl_models.cpp +++ b/src/gl/models/gl_models.cpp @@ -44,7 +44,6 @@ #include "gl/renderer/gl_renderer.h" #include "gl/scene/gl_drawinfo.h" #include "gl/models/gl_models.h" -#include "gl/renderer/gl_renderstate.h" #include "gl/shaders/gl_shader.h" CVAR(Bool, gl_light_models, true, CVAR_ARCHIVE) @@ -58,55 +57,52 @@ VSMatrix FGLModelRenderer::GetViewToWorldMatrix() void FGLModelRenderer::BeginDrawModel(AActor *actor, FSpriteModelFrame *smf, const VSMatrix &objectToWorldMatrix, bool mirrored) { - glDepthFunc(GL_LEQUAL); - gl_RenderState.EnableTexture(true); + di->SetDepthFunc(DF_LEqual); + state.EnableTexture(true); // [BB] In case the model should be rendered translucent, do back face culling. // This solves a few of the problems caused by the lack of depth sorting. // [Nash] Don't do back face culling if explicitly specified in MODELDEF // TO-DO: Implement proper depth sorting. - if (!(actor->RenderStyle == LegacyRenderStyles[STYLE_Normal]) && !(smf->flags & MDL_DONTCULLBACKFACES)) + if (!(actor->RenderStyle == DefaultRenderStyle()) && !(smf->flags & MDL_DONTCULLBACKFACES)) { - glEnable(GL_CULL_FACE); - glFrontFace((mirrored ^ screen->mPortalState->isMirrored()) ? GL_CCW : GL_CW); + di->SetCulling((mirrored ^ screen->mPortalState->isMirrored()) ? Cull_CCW : Cull_CW); } - gl_RenderState.mModelMatrix = objectToWorldMatrix; - gl_RenderState.EnableModelMatrix(true); + state.mModelMatrix = objectToWorldMatrix; + state.EnableModelMatrix(true); } void FGLModelRenderer::EndDrawModel(AActor *actor, FSpriteModelFrame *smf) { - gl_RenderState.EnableModelMatrix(false); - - glDepthFunc(GL_LESS); - if (!(actor->RenderStyle == LegacyRenderStyles[STYLE_Normal]) && !(smf->flags & MDL_DONTCULLBACKFACES)) - glDisable(GL_CULL_FACE); + state.EnableModelMatrix(false); + di->SetDepthFunc(DF_Less); + if (!(actor->RenderStyle == DefaultRenderStyle()) && !(smf->flags & MDL_DONTCULLBACKFACES)) + di->SetCulling(Cull_None); } void FGLModelRenderer::BeginDrawHUDModel(AActor *actor, const VSMatrix &objectToWorldMatrix, bool mirrored) { - glDepthFunc(GL_LEQUAL); + di->SetDepthFunc(DF_LEqual); // [BB] In case the model should be rendered translucent, do back face culling. // This solves a few of the problems caused by the lack of depth sorting. // TO-DO: Implement proper depth sorting. - if (!(actor->RenderStyle == LegacyRenderStyles[STYLE_Normal])) + if (!(actor->RenderStyle == DefaultRenderStyle())) { - glEnable(GL_CULL_FACE); - glFrontFace((mirrored ^ screen->mPortalState->isMirrored()) ? GL_CW : GL_CCW); + di->SetCulling((mirrored ^ screen->mPortalState->isMirrored()) ? Cull_CW : Cull_CCW); } - gl_RenderState.mModelMatrix = objectToWorldMatrix; - gl_RenderState.EnableModelMatrix(true); + state.mModelMatrix = objectToWorldMatrix; + state.EnableModelMatrix(true); } void FGLModelRenderer::EndDrawHUDModel(AActor *actor) { - gl_RenderState.EnableModelMatrix(false); + state.EnableModelMatrix(false); - glDepthFunc(GL_LESS); - if (!(actor->RenderStyle == LegacyRenderStyles[STYLE_Normal])) - glDisable(GL_CULL_FACE); + di->SetDepthFunc(DF_Less); + if (!(actor->RenderStyle == DefaultRenderStyle())) + di->SetCulling(Cull_None); } IModelVertexBuffer *FGLModelRenderer::CreateVertexBuffer(bool needindex, bool singleframe) @@ -116,35 +112,34 @@ IModelVertexBuffer *FGLModelRenderer::CreateVertexBuffer(bool needindex, bool si void FGLModelRenderer::SetVertexBuffer(IModelVertexBuffer *buffer) { - static_cast(buffer)->Bind(gl_RenderState); + static_cast(buffer)->Bind(state); } void FGLModelRenderer::ResetVertexBuffer() { - GLRenderer->mVBO->Bind(gl_RenderState); + GLRenderer->mVBO->Bind(state); } void FGLModelRenderer::SetInterpolation(double inter) { - gl_RenderState.SetInterpolationFactor((float)inter); + state.SetInterpolationFactor((float)inter); } void FGLModelRenderer::SetMaterial(FTexture *skin, bool clampNoFilter, int translation) { FMaterial * tex = FMaterial::ValidateTexture(skin, false); - gl_RenderState.ApplyMaterial(tex, clampNoFilter ? CLAMP_NOFILTER : CLAMP_NONE, translation, -1); - /*if (modellightindex != -1)*/ gl_RenderState.SetLightIndex(modellightindex); - gl_RenderState.Apply(); + state.SetMaterial(tex, clampNoFilter ? CLAMP_NOFILTER : CLAMP_NONE, translation, -1); + state.SetLightIndex(modellightindex); } void FGLModelRenderer::DrawArrays(int start, int count) { - glDrawArrays(GL_TRIANGLES, start, count); + di->Draw(DT_Triangles, state, start, count); } void FGLModelRenderer::DrawElements(int numIndices, size_t offset) { - glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, (void*)(intptr_t)offset); + di->DrawIndexed(DT_Triangles, state, offset / sizeof(unsigned int), numIndices); } //=========================================================================== @@ -162,7 +157,7 @@ FModelVertexBuffer::FModelVertexBuffer(bool needindex, bool singleframe) { 0, VATTR_VERTEX, VFmt_Float3, myoffsetof(FModelVertex, x) }, { 0, VATTR_TEXCOORD, VFmt_Float2, myoffsetof(FModelVertex, u) }, { 0, VATTR_NORMAL, VFmt_Packed_A2R10G10B10, myoffsetof(FModelVertex, packedNormal) }, - { 0, VATTR_VERTEX2, VFmt_Float3, myoffsetof(FModelVertex, x) } + { 1, VATTR_VERTEX2, VFmt_Float3, myoffsetof(FModelVertex, x) } }; mVertexBuffer->SetFormat(2, 4, sizeof(FModelVertex), format); } diff --git a/src/gl/models/gl_models.h b/src/gl/models/gl_models.h index 53ae922c7..a6b31660b 100644 --- a/src/gl/models/gl_models.h +++ b/src/gl/models/gl_models.h @@ -56,8 +56,9 @@ class FGLModelRenderer : public FModelRenderer { int modellightindex = -1; FDrawInfo *di; + FRenderState &state; public: - FGLModelRenderer(FDrawInfo *d, int mli) : modellightindex(mli), di(d) + FGLModelRenderer(FDrawInfo *d, FRenderState &st, int mli) : modellightindex(mli), di(d), state(st) {} ModelRendererType GetType() const override { return GLModelRendererType; } void BeginDrawModel(AActor *actor, FSpriteModelFrame *smf, const VSMatrix &objectToWorldMatrix, bool mirrored) override; diff --git a/src/gl/renderer/gl_renderstate.cpp b/src/gl/renderer/gl_renderstate.cpp index 1ca2579f1..f0074acab 100644 --- a/src/gl/renderer/gl_renderstate.cpp +++ b/src/gl/renderer/gl_renderstate.cpp @@ -70,7 +70,6 @@ void FGLRenderState::Reset() stBlendEquation = -1; stAlphaTest = 0; mLastDepthClamp = true; - mInterpolationFactor = 0.0f; mEffectState = 0; activeShader = nullptr; diff --git a/src/gl/renderer/gl_renderstate.h b/src/gl/renderer/gl_renderstate.h index 27f47ca0a..60bee5a58 100644 --- a/src/gl/renderer/gl_renderstate.h +++ b/src/gl/renderer/gl_renderstate.h @@ -53,8 +53,6 @@ class FGLRenderState : public FRenderState float mGlossiness, mSpecularLevel; float mShaderTimer; - float mInterpolationFactor; - int mEffectState; int mTempTM = TM_NORMAL; @@ -127,16 +125,6 @@ public: return res; } - void SetInterpolationFactor(float fac) - { - mInterpolationFactor = fac; - } - - float GetInterpolationFactor() - { - return mInterpolationFactor; - } - void SetPassType(EPassType passType) { mPassType = passType; diff --git a/src/gl/scene/gl_drawinfo.cpp b/src/gl/scene/gl_drawinfo.cpp index a588da1a9..d5551d13c 100644 --- a/src/gl/scene/gl_drawinfo.cpp +++ b/src/gl/scene/gl_drawinfo.cpp @@ -241,13 +241,13 @@ void FDrawInfo::DrawIndexed(EDrawType dt, FRenderState &state, int index, int co void FDrawInfo::DrawModel(GLSprite *spr, FRenderState &state) { - FGLModelRenderer renderer(this, spr->dynlightindex); + FGLModelRenderer renderer(this, state, spr->dynlightindex); renderer.RenderModel(spr->x, spr->y, spr->z, spr->modelframe, spr->actor, Viewpoint.TicFrac); } void FDrawInfo::DrawHUDModel(HUDSprite *huds, FRenderState &state) { - FGLModelRenderer renderer(this, huds->lightindex); + FGLModelRenderer renderer(this, state, huds->lightindex); renderer.RenderHUDModel(huds->weapon, huds->mx, huds->my); } @@ -305,6 +305,19 @@ void FDrawInfo::SetStencil(int offs, int op, int flags) glClear(GL_DEPTH_BUFFER_BIT); } +void FDrawInfo::SetCulling(int mode) +{ + if (mode != Cull_None) + { + glEnable(GL_CULL_FACE); + glFrontFace(mode == Cull_CCW ? GL_CCW : GL_CW); + } + else + { + glDisable(GL_CULL_FACE); + } +} + //========================================================================== // diff --git a/src/gl/scene/gl_drawinfo.h b/src/gl/scene/gl_drawinfo.h index d70f83dc2..d280cc39b 100644 --- a/src/gl/scene/gl_drawinfo.h +++ b/src/gl/scene/gl_drawinfo.h @@ -53,7 +53,8 @@ struct FDrawInfo : public HWDrawInfo void SetDepthRange(float min, float max) override; void EnableDrawBufferAttachments(bool on) override; void SetStencil(int offs, int op, int flags) override; - + void SetCulling(int mode) override; + void StartScene(); void DrawSorted(int listindex); diff --git a/src/gl/system/gl_framebuffer.cpp b/src/gl/system/gl_framebuffer.cpp index 0fd8afdfe..7fadb2c8e 100644 --- a/src/gl/system/gl_framebuffer.cpp +++ b/src/gl/system/gl_framebuffer.cpp @@ -350,7 +350,7 @@ void OpenGLFrameBuffer::PrecacheMaterial(FMaterial *mat, int translation) FModelRenderer *OpenGLFrameBuffer::CreateModelRenderer(int mli) { - return new FGLModelRenderer(nullptr, mli); + return new FGLModelRenderer(nullptr, gl_RenderState, mli); } IUniformBuffer *OpenGLFrameBuffer::CreateUniformBuffer(size_t size, bool staticuse) diff --git a/src/hwrenderer/scene/hw_drawinfo.h b/src/hwrenderer/scene/hw_drawinfo.h index 2c029cf36..2de7d19b7 100644 --- a/src/hwrenderer/scene/hw_drawinfo.h +++ b/src/hwrenderer/scene/hw_drawinfo.h @@ -49,7 +49,12 @@ enum EStencilOp SOP_Decrement = 2 }; - +enum ECull +{ + Cull_None, + Cull_CCW, + Cull_CW +}; struct FSectorPortalGroup; struct FLinePortalSpan; @@ -356,7 +361,8 @@ public: virtual void SetDepthRange(float min, float max) = 0; virtual void EnableDrawBufferAttachments(bool on) = 0; virtual void SetStencil(int offs, int op, int flags) = 0; - + virtual void SetCulling(int mode) = 0; + }; diff --git a/src/hwrenderer/scene/hw_renderstate.h b/src/hwrenderer/scene/hw_renderstate.h index 32488b562..7f39bf79b 100644 --- a/src/hwrenderer/scene/hw_renderstate.h +++ b/src/hwrenderer/scene/hw_renderstate.h @@ -93,6 +93,7 @@ protected: float mAlphaThreshold; float mClipSplit[2]; + float mInterpolationFactor; FStateVec4 mNormal; FStateVec4 mColor; @@ -138,6 +139,7 @@ public: mLightParms[3] = -1.f; mSpecialEffect = EFF_NONE; mLightIndex = -1; + mInterpolationFactor = 0; mRenderStyle = DefaultRenderStyle(); mMaterial.Reset(); mBias.Reset(); @@ -407,6 +409,16 @@ public: mIndexBuffer = ib; } + void SetInterpolationFactor(float fac) + { + mInterpolationFactor = fac; + } + + float GetInterpolationFactor() + { + return mInterpolationFactor; + } + void SetColor(int sectorlightlevel, int rellight, bool fullbright, const FColormap &cm, float alpha, bool weapon = false); void SetFog(int lightlevel, int rellight, bool fullbright, const FColormap *cmap, bool isadditive); };