2013-06-23 07:49:34 +00:00
|
|
|
/*
|
|
|
|
** gl_renderstate.cpp
|
|
|
|
** Render state maintenance
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2009 Christoph Oelckers
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
** 4. When not used as part of GZDoom or a GZDoom derivative, this code will be
|
|
|
|
** covered by the terms of the GNU Lesser General Public License as published
|
|
|
|
** by the Free Software Foundation; either version 2.1 of the License, or (at
|
|
|
|
** your option) any later version.
|
|
|
|
** 5. Full disclosure of the entire project's source code, except for third
|
|
|
|
** party libraries is mandatory. (NOTE: This clause is non-negotiable!)
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gl/system/gl_system.h"
|
2013-09-03 16:29:39 +00:00
|
|
|
#include "gl/system/gl_interface.h"
|
2013-06-23 07:49:34 +00:00
|
|
|
#include "gl/data/gl_data.h"
|
2014-05-10 19:47:07 +00:00
|
|
|
#include "gl/data/gl_vertexbuffer.h"
|
2013-06-23 07:49:34 +00:00
|
|
|
#include "gl/system/gl_cvars.h"
|
|
|
|
#include "gl/shaders/gl_shader.h"
|
|
|
|
#include "gl/renderer/gl_renderer.h"
|
|
|
|
#include "gl/renderer/gl_renderstate.h"
|
|
|
|
#include "gl/renderer/gl_colormap.h"
|
|
|
|
|
2013-08-18 13:41:52 +00:00
|
|
|
void gl_SetTextureMode(int type);
|
|
|
|
|
2013-06-23 07:49:34 +00:00
|
|
|
FRenderState gl_RenderState;
|
|
|
|
int FStateAttr::ChangeCounter;
|
|
|
|
|
|
|
|
CVAR(Bool, gl_direct_state_change, true, 0)
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FRenderState::Reset()
|
|
|
|
{
|
|
|
|
mTextureEnabled = true;
|
|
|
|
mBrightmapEnabled = mFogEnabled = mGlowEnabled = mLightEnabled = false;
|
|
|
|
ffTextureEnabled = ffFogEnabled = false;
|
|
|
|
mSpecialEffect = ffSpecialEffect = EFF_NONE;
|
|
|
|
mFogColor.d = ffFogColor.d = -1;
|
|
|
|
mFogDensity = ffFogDensity = 0;
|
|
|
|
mTextureMode = ffTextureMode = -1;
|
|
|
|
mSrcBlend = GL_SRC_ALPHA;
|
|
|
|
mDstBlend = GL_ONE_MINUS_SRC_ALPHA;
|
|
|
|
glSrcBlend = glDstBlend = -1;
|
|
|
|
glAlphaFunc = -1;
|
|
|
|
mAlphaFunc = GL_GEQUAL;
|
|
|
|
mAlphaThreshold = 0.5f;
|
|
|
|
mBlendEquation = GL_FUNC_ADD;
|
2014-05-11 14:06:25 +00:00
|
|
|
mObjectColor = 0xffffffff;
|
2013-06-23 07:49:34 +00:00
|
|
|
glBlendEquation = -1;
|
|
|
|
m2D = true;
|
2014-05-10 19:47:07 +00:00
|
|
|
mVertexBuffer = mCurrentVertexBuffer = NULL;
|
2014-05-11 19:47:54 +00:00
|
|
|
mColormapState = CM_DEFAULT;
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Set texture shader info
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2014-05-11 19:47:54 +00:00
|
|
|
int FRenderState::SetupShader(bool cameratexture, int &shaderindex, float warptime)
|
2013-06-23 07:49:34 +00:00
|
|
|
{
|
|
|
|
int softwarewarp = 0;
|
|
|
|
|
2014-05-11 12:46:37 +00:00
|
|
|
|
|
|
|
if (gl.hasGLSL())
|
2013-06-23 07:49:34 +00:00
|
|
|
{
|
2014-05-11 12:46:37 +00:00
|
|
|
mEffectState = shaderindex;
|
|
|
|
mWarpTime = warptime;
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
softwarewarp = shaderindex > 0 && shaderindex < 3? shaderindex : 0;
|
|
|
|
shaderindex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return softwarewarp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Apply shader settings
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool FRenderState::ApplyShader()
|
|
|
|
{
|
|
|
|
|
2014-05-11 19:47:54 +00:00
|
|
|
if (gl.hasGLSL())
|
2013-06-23 07:49:34 +00:00
|
|
|
{
|
2014-05-11 19:47:54 +00:00
|
|
|
FShader *activeShader;
|
|
|
|
if (mSpecialEffect > 0)
|
2014-05-11 11:27:51 +00:00
|
|
|
{
|
2014-05-11 19:47:54 +00:00
|
|
|
activeShader = GLRenderer->mShaderManager->BindEffect(mSpecialEffect);
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
2014-05-11 19:47:54 +00:00
|
|
|
FShaderContainer *shd = GLRenderer->mShaderManager->Get(mTextureEnabled ? mEffectState : 4);
|
2013-06-23 07:49:34 +00:00
|
|
|
|
2014-05-11 19:47:54 +00:00
|
|
|
if (shd != NULL)
|
|
|
|
{
|
|
|
|
activeShader = shd->Bind(mColormapState, mGlowEnabled, mWarpTime, mLightEnabled);
|
|
|
|
}
|
2014-05-11 12:46:37 +00:00
|
|
|
|
2013-06-23 07:49:34 +00:00
|
|
|
int fogset = 0;
|
2014-05-11 14:06:25 +00:00
|
|
|
//glColor4fv(mColor.vec);
|
2013-06-23 07:49:34 +00:00
|
|
|
if (mFogEnabled)
|
|
|
|
{
|
|
|
|
if ((mFogColor & 0xffffff) == 0)
|
|
|
|
{
|
|
|
|
fogset = gl_fogmode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fogset = -gl_fogmode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fogset != activeShader->currentfogenabled)
|
|
|
|
{
|
2013-09-03 16:29:39 +00:00
|
|
|
glUniform1i(activeShader->fogenabled_index, (activeShader->currentfogenabled = fogset));
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
|
|
|
if (mTextureMode != activeShader->currenttexturemode)
|
|
|
|
{
|
2013-09-03 16:29:39 +00:00
|
|
|
glUniform1i(activeShader->texturemode_index, (activeShader->currenttexturemode = mTextureMode));
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
|
|
|
if (activeShader->currentcamerapos.Update(&mCameraPos))
|
|
|
|
{
|
2013-09-03 16:29:39 +00:00
|
|
|
glUniform3fv(activeShader->camerapos_index, 1, mCameraPos.vec);
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
|
|
|
/*if (mLightParms[0] != activeShader->currentlightfactor ||
|
|
|
|
mLightParms[1] != activeShader->currentlightdist ||
|
|
|
|
mFogDensity != activeShader->currentfogdensity)*/
|
|
|
|
{
|
|
|
|
const float LOG2E = 1.442692f; // = 1/log(2)
|
|
|
|
//activeShader->currentlightdist = mLightParms[1];
|
|
|
|
//activeShader->currentlightfactor = mLightParms[0];
|
|
|
|
//activeShader->currentfogdensity = mFogDensity;
|
|
|
|
// premultiply the density with as much as possible here to reduce shader
|
|
|
|
// execution time.
|
2013-09-03 16:29:39 +00:00
|
|
|
glVertexAttrib4f(VATTR_FOGPARAMS, mLightParms[0], mLightParms[1], mFogDensity * (-LOG2E / 64000.f), 0);
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
|
|
|
if (mFogColor != activeShader->currentfogcolor)
|
|
|
|
{
|
|
|
|
activeShader->currentfogcolor = mFogColor;
|
|
|
|
|
2013-09-03 16:29:39 +00:00
|
|
|
glUniform4f (activeShader->fogcolor_index, mFogColor.r/255.f, mFogColor.g/255.f,
|
2013-06-23 07:49:34 +00:00
|
|
|
mFogColor.b/255.f, 0);
|
|
|
|
}
|
|
|
|
if (mGlowEnabled)
|
|
|
|
{
|
2013-09-03 16:29:39 +00:00
|
|
|
glUniform4fv(activeShader->glowtopcolor_index, 1, mGlowTop.vec);
|
|
|
|
glUniform4fv(activeShader->glowbottomcolor_index, 1, mGlowBottom.vec);
|
2014-05-10 15:09:43 +00:00
|
|
|
glUniform4fv(activeShader->glowtopplane_index, 1, mGlowTopPlane.vec);
|
|
|
|
glUniform4fv(activeShader->glowbottomplane_index, 1, mGlowBottomPlane.vec);
|
|
|
|
activeShader->currentglowstate = 1;
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
2014-05-10 15:09:43 +00:00
|
|
|
else if (activeShader->currentglowstate)
|
|
|
|
{
|
|
|
|
// if glowing is on, disable it.
|
|
|
|
glUniform4f(activeShader->glowtopcolor_index, 0.f, 0.f, 0.f, 0.f);
|
|
|
|
glUniform4f(activeShader->glowbottomcolor_index, 0.f, 0.f, 0.f, 0.f);
|
|
|
|
activeShader->currentglowstate = 0;
|
|
|
|
}
|
|
|
|
|
2013-06-23 07:49:34 +00:00
|
|
|
if (mLightEnabled)
|
|
|
|
{
|
2013-09-03 16:29:39 +00:00
|
|
|
glUniform3iv(activeShader->lightrange_index, 1, mNumLights);
|
|
|
|
glUniform4fv(activeShader->lights_index, mNumLights[2], mLightData);
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
2014-05-11 14:06:25 +00:00
|
|
|
if (mObjectColor != activeShader->currentobjectcolor)
|
|
|
|
{
|
|
|
|
activeShader->currentobjectcolor = mObjectColor;
|
|
|
|
glUniform4f(activeShader->objectcolor_index, mObjectColor.r / 255.f, mObjectColor.g / 255.f, mObjectColor.b / 255.f, mObjectColor.a / 255.f);
|
|
|
|
}
|
2014-05-11 14:49:17 +00:00
|
|
|
if (mDynColor != activeShader->currentdlightcolor)
|
|
|
|
{
|
|
|
|
activeShader->currentobjectcolor = mObjectColor;
|
|
|
|
glUniform4f(activeShader->dlightcolor_index, mDynColor.r / 255.f, mDynColor.g / 255.f, mDynColor.b / 255.f, 0);
|
|
|
|
}
|
2013-06-23 07:49:34 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Apply State
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FRenderState::Apply(bool forcenoshader)
|
|
|
|
{
|
|
|
|
if (!gl_direct_state_change)
|
|
|
|
{
|
|
|
|
if (mSrcBlend != glSrcBlend || mDstBlend != glDstBlend)
|
|
|
|
{
|
|
|
|
glSrcBlend = mSrcBlend;
|
|
|
|
glDstBlend = mDstBlend;
|
|
|
|
glBlendFunc(mSrcBlend, mDstBlend);
|
|
|
|
}
|
|
|
|
if (mAlphaFunc != glAlphaFunc || mAlphaThreshold != glAlphaThreshold)
|
|
|
|
{
|
|
|
|
glAlphaFunc = mAlphaFunc;
|
|
|
|
glAlphaThreshold = mAlphaThreshold;
|
2014-05-11 11:29:06 +00:00
|
|
|
::glAlphaFunc(mAlphaFunc, mAlphaThreshold);
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
|
|
|
if (mAlphaTest != glAlphaTest)
|
|
|
|
{
|
|
|
|
glAlphaTest = mAlphaTest;
|
|
|
|
if (mAlphaTest) glEnable(GL_ALPHA_TEST);
|
|
|
|
else glDisable(GL_ALPHA_TEST);
|
|
|
|
}
|
|
|
|
if (mBlendEquation != glBlendEquation)
|
|
|
|
{
|
|
|
|
glBlendEquation = mBlendEquation;
|
2013-09-03 16:29:39 +00:00
|
|
|
::glBlendEquation(mBlendEquation);
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 19:47:07 +00:00
|
|
|
if (mVertexBuffer != mCurrentVertexBuffer)
|
|
|
|
{
|
|
|
|
if (mVertexBuffer == NULL) glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
else mVertexBuffer->BindVBO();
|
|
|
|
mCurrentVertexBuffer = mVertexBuffer;
|
|
|
|
}
|
2013-06-23 07:49:34 +00:00
|
|
|
if (forcenoshader || !ApplyShader())
|
|
|
|
{
|
2014-05-11 14:06:25 +00:00
|
|
|
//if (mColor.vec[0] >= 0.f) glColor4fv(mColor.vec);
|
|
|
|
|
2013-06-23 07:49:34 +00:00
|
|
|
GLRenderer->mShaderManager->SetActiveShader(NULL);
|
|
|
|
if (mTextureMode != ffTextureMode)
|
|
|
|
{
|
2013-08-18 13:41:52 +00:00
|
|
|
gl_SetTextureMode((ffTextureMode = mTextureMode));
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
|
|
|
if (mTextureEnabled != ffTextureEnabled)
|
|
|
|
{
|
2013-09-03 12:05:41 +00:00
|
|
|
if ((ffTextureEnabled = mTextureEnabled)) glEnable(GL_TEXTURE_2D);
|
|
|
|
else glDisable(GL_TEXTURE_2D);
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
|
|
|
if (mFogEnabled != ffFogEnabled)
|
|
|
|
{
|
|
|
|
if ((ffFogEnabled = mFogEnabled))
|
|
|
|
{
|
2013-09-03 12:05:41 +00:00
|
|
|
glEnable(GL_FOG);
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
2013-09-03 12:05:41 +00:00
|
|
|
else glDisable(GL_FOG);
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
|
|
|
if (mFogEnabled)
|
|
|
|
{
|
|
|
|
if (ffFogColor != mFogColor)
|
|
|
|
{
|
|
|
|
ffFogColor = mFogColor;
|
|
|
|
GLfloat FogColor[4]={mFogColor.r/255.0f,mFogColor.g/255.0f,mFogColor.b/255.0f,0.0f};
|
2013-09-03 12:05:41 +00:00
|
|
|
glFogfv(GL_FOG_COLOR, FogColor);
|
2013-06-23 07:49:34 +00:00
|
|
|
}
|
|
|
|
if (ffFogDensity != mFogDensity)
|
|
|
|
{
|
2013-09-03 12:05:41 +00:00
|
|
|
glFogf(GL_FOG_DENSITY, mFogDensity/64000.f);
|
2013-06-23 07:49:34 +00:00
|
|
|
ffFogDensity=mFogDensity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mSpecialEffect != ffSpecialEffect)
|
|
|
|
{
|
|
|
|
switch (ffSpecialEffect)
|
|
|
|
{
|
|
|
|
case EFF_SPHEREMAP:
|
2013-09-03 12:05:41 +00:00
|
|
|
glDisable(GL_TEXTURE_GEN_T);
|
|
|
|
glDisable(GL_TEXTURE_GEN_S);
|
2013-06-23 07:49:34 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (mSpecialEffect)
|
|
|
|
{
|
|
|
|
case EFF_SPHEREMAP:
|
|
|
|
// Use sphere mapping for this
|
2013-09-03 12:05:41 +00:00
|
|
|
glEnable(GL_TEXTURE_GEN_T);
|
|
|
|
glEnable(GL_TEXTURE_GEN_S);
|
|
|
|
glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP);
|
|
|
|
glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP);
|
2013-06-23 07:49:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ffSpecialEffect = mSpecialEffect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|