- removed all direct OpenGL dependencies from gl_models.cpp.

This commit is contained in:
Christoph Oelckers 2018-10-27 22:55:33 +02:00
parent ac37ff422a
commit a62cd64138
9 changed files with 67 additions and 52 deletions

View file

@ -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<FModelVertexBuffer*>(buffer)->Bind(gl_RenderState);
static_cast<FModelVertexBuffer*>(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);
}

View file

@ -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;

View file

@ -70,7 +70,6 @@ void FGLRenderState::Reset()
stBlendEquation = -1;
stAlphaTest = 0;
mLastDepthClamp = true;
mInterpolationFactor = 0.0f;
mEffectState = 0;
activeShader = nullptr;

View file

@ -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;

View file

@ -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);
}
}
//==========================================================================
//

View file

@ -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);

View file

@ -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)

View file

@ -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;
};

View file

@ -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);
};