void FShaderManager::ApplyMatrices(VSMatrix *proj, VSMatrix *view) { if (gl.legacyMode) { glMatrixMode(GL_PROJECTION); glLoadMatrixf(proj->get()); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(view->get()); } else { VSMatrix norm; norm.computeNormalMatrix(*view); for (int i = 0; i < 4; i++) { mTextureEffects[i]->ApplyMatrices(proj, view, &norm); mTextureEffectsNAT[i]->ApplyMatrices(proj, view, &norm); } mTextureEffects[4]->ApplyMatrices(proj, view, &norm); if (gl_fuzztype != 0) { mTextureEffects[4 + gl_fuzztype]->ApplyMatrices(proj, view, &norm); } for (unsigned i = 12; i < mTextureEffects.Size(); i++) { mTextureEffects[i]->ApplyMatrices(proj, view, &norm); } for (int i = 0; i < MAX_EFFECTS; i++) { mEffectShaders[i]->ApplyMatrices(proj, view, &norm); } if (mActiveShader != NULL) mActiveShader->Bind(); } }
/* virtual */ VSMatrix EyePose::GetProjection(float fov, float aspectRatio, float fovRatio) const { VSMatrix result; // Lifted from gl_scene.cpp FGLRenderer::SetProjection() float fovy = (float)(2 * RAD2DEG(atan(tan(DEG2RAD(fov) / 2) / fovRatio))); result.perspective(fovy, aspectRatio, 5.f, 65536.f); return result; }
VSMatrix PolyModelRenderer::GetViewToWorldMatrix() { Mat4f swapYZ = Mat4f::Null(); swapYZ.Matrix[0 + 0 * 4] = 1.0f; swapYZ.Matrix[1 + 2 * 4] = 1.0f; swapYZ.Matrix[2 + 1 * 4] = 1.0f; swapYZ.Matrix[3 + 3 * 4] = 1.0f; VSMatrix worldToView; worldToView.loadMatrix((PolyRenderer::Instance()->Scene.CurrentViewpoint->WorldToView * swapYZ).Matrix); VSMatrix objectToWorld; worldToView.inverseMatrix(objectToWorld); return objectToWorld; }
void FShaderCollection::ApplyMatrices(VSMatrix *proj, VSMatrix *view) { VSMatrix norm; norm.computeNormalMatrix(*view); for (int i = 0; i < 4; i++) { mTextureEffects[i]->ApplyMatrices(proj, view, &norm); mTextureEffectsNAT[i]->ApplyMatrices(proj, view, &norm); } mTextureEffects[4]->ApplyMatrices(proj, view, &norm); if (gl_fuzztype != 0) { mTextureEffects[4 + gl_fuzztype]->ApplyMatrices(proj, view, &norm); } for (unsigned i = 12; i < mTextureEffects.Size(); i++) { mTextureEffects[i]->ApplyMatrices(proj, view, &norm); } for (int i = 0; i < MAX_EFFECTS; i++) { mEffectShaders[i]->ApplyMatrices(proj, view, &norm); } }
bool FRenderState::ApplyShader() { if (mSpecialEffect > EFF_NONE) { activeShader = GLRenderer->mShaderManager->BindEffect(mSpecialEffect); } else { activeShader = GLRenderer->mShaderManager->Get(mTextureEnabled ? mEffectState : 4, mAlphaThreshold >= 0.f); activeShader->Bind(); } int fogset = 0; if (mFogEnabled) { if ((mFogColor & 0xffffff) == 0) { fogset = gl_fogmode; } else { fogset = -gl_fogmode; } } glVertexAttrib4fv(VATTR_COLOR, mColor.vec); activeShader->muDesaturation.Set(mDesaturation / 255.f); activeShader->muFogEnabled.Set(fogset); activeShader->muTextureMode.Set(mTextureMode); activeShader->muCameraPos.Set(mCameraPos.vec); activeShader->muLightParms.Set(mLightParms); activeShader->muFogColor.Set(mFogColor); activeShader->muObjectColor.Set(mObjectColor); activeShader->muDynLightColor.Set(mDynColor.vec); activeShader->muInterpolationFactor.Set(mInterpolationFactor); activeShader->muClipHeightTop.Set(mClipHeightTop); activeShader->muClipHeightBottom.Set(mClipHeightBottom); activeShader->muTimer.Set(gl_frameMS * mShaderTimer / 1000.f); activeShader->muAlphaThreshold.Set(mAlphaThreshold); activeShader->muLightIndex.Set(mLightIndex); // will always be -1 for now activeShader->muClipSplit.Set(mClipSplit); if (mGlowEnabled) { activeShader->muGlowTopColor.Set(mGlowTop.vec); activeShader->muGlowBottomColor.Set(mGlowBottom.vec); activeShader->muGlowTopPlane.Set(mGlowTopPlane.vec); activeShader->muGlowBottomPlane.Set(mGlowBottomPlane.vec); activeShader->currentglowstate = 1; } else if (activeShader->currentglowstate) { // if glowing is on, disable it. static const float nulvec[] = { 0.f, 0.f, 0.f, 0.f }; activeShader->muGlowTopColor.Set(nulvec); activeShader->muGlowBottomColor.Set(nulvec); activeShader->muGlowTopPlane.Set(nulvec); activeShader->muGlowBottomPlane.Set(nulvec); activeShader->currentglowstate = 0; } if (mColormapState != activeShader->currentfixedcolormap) { float r, g, b; activeShader->currentfixedcolormap = mColormapState; if (mColormapState == CM_DEFAULT) { activeShader->muFixedColormap.Set(0); } else if (mColormapState < CM_MAXCOLORMAP) { FSpecialColormap *scm = &SpecialColormaps[gl_fixedcolormap - CM_FIRSTSPECIALCOLORMAP]; float m[] = { scm->ColorizeEnd[0] - scm->ColorizeStart[0], scm->ColorizeEnd[1] - scm->ColorizeStart[1], scm->ColorizeEnd[2] - scm->ColorizeStart[2], 0.f }; activeShader->muFixedColormap.Set(1); activeShader->muColormapStart.Set(scm->ColorizeStart[0], scm->ColorizeStart[1], scm->ColorizeStart[2], 0.f); activeShader->muColormapRange.Set(m); } else if (mColormapState == CM_FOGLAYER) { activeShader->muFixedColormap.Set(3); } else if (mColormapState == CM_LITE) { if (gl_enhanced_nightvision) { r = 0.375f, g = 1.0f, b = 0.375f; } else { r = g = b = 1.f; } activeShader->muFixedColormap.Set(2); activeShader->muColormapStart.Set(r, g, b, 1.f); } else if (mColormapState >= CM_TORCH) { int flicker = mColormapState - CM_TORCH; r = (0.8f + (7 - flicker) / 70.0f); if (r > 1.0f) r = 1.0f; b = g = r; if (gl_enhanced_nightvision) b = g * 0.75f; activeShader->muFixedColormap.Set(2); activeShader->muColormapStart.Set(r, g, b, 1.f); } } if (mTextureMatrixEnabled) { mTextureMatrix.matrixToGL(activeShader->texturematrix_index); activeShader->currentTextureMatrixState = true; } else if (activeShader->currentTextureMatrixState) { activeShader->currentTextureMatrixState = false; identityMatrix.matrixToGL(activeShader->texturematrix_index); } if (mModelMatrixEnabled) { mModelMatrix.matrixToGL(activeShader->modelmatrix_index); activeShader->currentModelMatrixState = true; } else if (activeShader->currentModelMatrixState) { activeShader->currentModelMatrixState = false; identityMatrix.matrixToGL(activeShader->modelmatrix_index); } return true; }
bool FRenderState::ApplyShader() { static uint64_t firstFrame = 0; // if firstFrame is not yet initialized, initialize it to current time // if we're going to overflow a float (after ~4.6 hours, or 24 bits), re-init to regain precision if ((firstFrame == 0) || (screen->FrameTime - firstFrame >= 1<<24) || level.ShaderStartTime >= firstFrame) firstFrame = screen->FrameTime; static const float nulvec[] = { 0.f, 0.f, 0.f, 0.f }; if (mSpecialEffect > EFF_NONE) { activeShader = GLRenderer->mShaderManager->BindEffect(mSpecialEffect, mPassType); } else { activeShader = GLRenderer->mShaderManager->Get(mTextureEnabled ? mEffectState : 4, mAlphaThreshold >= 0.f, mPassType); activeShader->Bind(); } int fogset = 0; if (mFogEnabled) { if ((mFogColor & 0xffffff) == 0) { fogset = gl_fogmode; } else { fogset = -gl_fogmode; } } glVertexAttrib4fv(VATTR_COLOR, mColor.vec); glVertexAttrib4fv(VATTR_NORMAL, mNormal.vec); //activeShader->muObjectColor2.Set(mObjectColor2); activeShader->muObjectColor2.Set(mObjectColor2); activeShader->muDesaturation.Set(mDesaturation / 255.f); activeShader->muFogEnabled.Set(fogset); activeShader->muPalLightLevels.Set(static_cast<int>(gl_bandedswlight) | (static_cast<int>(gl_fogmode) << 8)); activeShader->muGlobVis.Set(GLRenderer->mGlobVis / 32.0f); activeShader->muTextureMode.Set(mTextureMode); activeShader->muCameraPos.Set(mCameraPos.vec); activeShader->muLightParms.Set(mLightParms); activeShader->muFogColor.Set(mFogColor); activeShader->muObjectColor.Set(mObjectColor); activeShader->muDynLightColor.Set(mDynColor.vec); activeShader->muInterpolationFactor.Set(mInterpolationFactor); activeShader->muClipHeight.Set(mClipHeight); activeShader->muClipHeightDirection.Set(mClipHeightDirection); activeShader->muTimer.Set((double)(screen->FrameTime - firstFrame) * (double)mShaderTimer / 1000.); activeShader->muAlphaThreshold.Set(mAlphaThreshold); activeShader->muLightIndex.Set(mLightIndex); // will always be -1 for now activeShader->muClipSplit.Set(mClipSplit); if (mGlowEnabled) { activeShader->muGlowTopColor.Set(mGlowTop.vec); activeShader->muGlowBottomColor.Set(mGlowBottom.vec); activeShader->currentglowstate = 1; } else if (activeShader->currentglowstate) { // if glowing is on, disable it. activeShader->muGlowTopColor.Set(nulvec); activeShader->muGlowBottomColor.Set(nulvec); activeShader->currentglowstate = 0; } if (mGlowEnabled || mObjectColor2.a != 0) { activeShader->muGlowTopPlane.Set(mGlowTopPlane.vec); activeShader->muGlowBottomPlane.Set(mGlowBottomPlane.vec); } if (mSplitEnabled) { activeShader->muSplitTopPlane.Set(mSplitTopPlane.vec); activeShader->muSplitBottomPlane.Set(mSplitBottomPlane.vec); activeShader->currentsplitstate = 1; } else if (activeShader->currentsplitstate) { activeShader->muSplitTopPlane.Set(nulvec); activeShader->muSplitBottomPlane.Set(nulvec); activeShader->currentsplitstate = 0; } if (mClipLineEnabled) { activeShader->muClipLine.Set(mClipLine.vec); activeShader->currentcliplinestate = 1; } else if (activeShader->currentcliplinestate) { activeShader->muClipLine.Set(-10000000.0, 0, 0, 0); activeShader->currentcliplinestate = 0; } if (mColormapState != activeShader->currentfixedcolormap) { float r, g, b; activeShader->currentfixedcolormap = mColormapState; if (mColormapState == CM_DEFAULT) { activeShader->muFixedColormap.Set(0); } else if (mColormapState > CM_DEFAULT && mColormapState < CM_MAXCOLORMAP) { if (FGLRenderBuffers::IsEnabled()) { // When using postprocessing to apply the colormap, we must render the image fullbright here. activeShader->muFixedColormap.Set(2); activeShader->muColormapStart.Set(1, 1, 1, 1.f); } else { FSpecialColormap *scm = &SpecialColormaps[mColormapState - CM_FIRSTSPECIALCOLORMAP]; float m[] = { scm->ColorizeEnd[0] - scm->ColorizeStart[0], scm->ColorizeEnd[1] - scm->ColorizeStart[1], scm->ColorizeEnd[2] - scm->ColorizeStart[2], 0.f }; activeShader->muFixedColormap.Set(1); activeShader->muColormapStart.Set(scm->ColorizeStart[0], scm->ColorizeStart[1], scm->ColorizeStart[2], 0.f); activeShader->muColormapRange.Set(m); } } else if (mColormapState == CM_FOGLAYER) { activeShader->muFixedColormap.Set(3); } else if (mColormapState == CM_LITE) { if (gl_enhanced_nightvision) { r = 0.375f, g = 1.0f, b = 0.375f; } else { r = g = b = 1.f; } activeShader->muFixedColormap.Set(2); activeShader->muColormapStart.Set(r, g, b, 1.f); } else if (mColormapState >= CM_TORCH) { int flicker = mColormapState - CM_TORCH; r = (0.8f + (7 - flicker) / 70.0f); if (r > 1.0f) r = 1.0f; b = g = r; if (gl_enhanced_nightvision) b = g * 0.75f; activeShader->muFixedColormap.Set(2); activeShader->muColormapStart.Set(r, g, b, 1.f); } } if (mTextureMatrixEnabled) { matrixToGL(mTextureMatrix, activeShader->texturematrix_index); activeShader->currentTextureMatrixState = true; } else if (activeShader->currentTextureMatrixState) { activeShader->currentTextureMatrixState = false; matrixToGL(identityMatrix, activeShader->texturematrix_index); } if (mModelMatrixEnabled) { matrixToGL(mModelMatrix, activeShader->modelmatrix_index); VSMatrix norm; norm.computeNormalMatrix(mModelMatrix); matrixToGL(norm, activeShader->normalmodelmatrix_index); activeShader->currentModelMatrixState = true; } else if (activeShader->currentModelMatrixState) { activeShader->currentModelMatrixState = false; matrixToGL(identityMatrix, activeShader->modelmatrix_index); matrixToGL(identityMatrix, activeShader->normalmodelmatrix_index); } return true; }
void FSkyVertexBuffer::SetupMatrices(HWDrawInfo *di, 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 && (di->Level->flags & LEVEL_FORCETILEDSKY)) { modelMatrix.translate(0.f, (-40 + tex->tex->GetSkyOffset() + 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 = float(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->GetSkyOffset() + 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->GetSkyOffset() + 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); }