doom3-bfg/neo/renderer/AutoRender.cpp

303 lines
7.9 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "precompiled.h"
2012-11-26 18:58:24 +00:00
#include "tr_local.h"
const int AUTO_RENDER_STACK_SIZE = 256 * 1024;
idAutoRender rAutoRender;
/*
============================
idAutoRender::idAutoRender
============================
*/
idAutoRender::idAutoRender()
{
2012-11-26 18:58:24 +00:00
nextRotateTime = 0.0f;
currentRotation = 0.0f;
autoRenderIcon = AUTORENDER_DEFAULTICON;
}
/*
============================
idAutoRender::Run
============================
*/
int idAutoRender::Run()
{
while( !IsTerminating() )
{
2012-11-26 18:58:24 +00:00
RenderFrame();
}
2012-11-26 18:58:24 +00:00
return 0;
}
/*
============================
idAutoRender::StartBackgroundAutoSwaps
============================
*/
void idAutoRender::StartBackgroundAutoSwaps( autoRenderIconType_t iconType )
{
2012-11-26 18:58:24 +00:00
if( IsRunning() )
{
2012-11-26 18:58:24 +00:00
EndBackgroundAutoSwaps();
}
2012-11-26 18:58:24 +00:00
autoRenderIcon = iconType;
idLib::Printf( "Starting Background AutoSwaps\n" );
2012-11-26 18:58:24 +00:00
const bool captureToImage = true;
common->UpdateScreen( captureToImage );
// unbind any shaders prior to entering the background autoswaps so we don't run
2012-11-26 18:58:24 +00:00
// into any problems with cached vertex shader indices from the main thread
renderProgManager.Unbind();
2012-11-26 18:58:24 +00:00
// unbind all texture units so we don't run into a race condition where the device is owned
// by the autorender thread but an image is trying to be unset from the main thread because
// it is getting purged before our our first frame has been rendered.
globalImages->UnbindAll();
StartThread( "BackgroundAutoSwaps", CORE_0B, THREAD_NORMAL, AUTO_RENDER_STACK_SIZE );
2012-11-26 18:58:24 +00:00
}
/*
============================
idAutoRender::EndBackgroundAutoSwaps
============================
*/
void idAutoRender::EndBackgroundAutoSwaps()
{
idLib::Printf( "End Background AutoSwaps\n" );
2012-11-26 18:58:24 +00:00
StopThread();
2012-11-26 18:58:24 +00:00
}
/*
============================
idAutoRender::RenderFrame
============================
*/
void idAutoRender::RenderFrame()
{
2012-11-26 18:58:24 +00:00
// values are 0 to 1
float loadingIconPosX = 0.5f;
float loadingIconPosY = 0.6f;
float loadingIconScale = 0.025f;
float loadingIconSpeed = 0.095f;
if( autoRenderIcon == AUTORENDER_HELLICON )
{
2012-11-26 18:58:24 +00:00
loadingIconPosX = 0.85f;
loadingIconPosY = 0.85f;
loadingIconScale = 0.1f;
loadingIconSpeed = 0.095f;
}
else if( autoRenderIcon == AUTORENDER_DIALOGICON )
{
2012-11-26 18:58:24 +00:00
loadingIconPosY = 0.73f;
}
2012-11-26 18:58:24 +00:00
GL_SetDefaultState();
GL_Cull( CT_TWO_SIDED );
const bool stereoRender = false;
2012-11-26 18:58:24 +00:00
const int width = renderSystem->GetWidth();
const int height = renderSystem->GetHeight();
const int guardBand = height / 24;
if( stereoRender )
{
for( int viewNum = 0 ; viewNum < 2; viewNum++ )
{
2012-11-26 18:58:24 +00:00
GL_ViewportAndScissor( 0, viewNum * ( height + guardBand ), width, height );
RenderBackground();
RenderLoadingIcon( loadingIconPosX, loadingIconPosY, loadingIconScale, loadingIconSpeed );
}
}
else
{
2012-11-26 18:58:24 +00:00
GL_ViewportAndScissor( 0, 0, width, height );
RenderBackground();
RenderLoadingIcon( loadingIconPosX, loadingIconPosY, loadingIconScale, loadingIconSpeed );
}
2012-11-26 18:58:24 +00:00
}
/*
============================
idAutoRender::RenderBackground
============================
*/
void idAutoRender::RenderBackground()
{
2012-11-26 18:58:24 +00:00
GL_SelectTexture( 0 );
2012-11-26 18:58:24 +00:00
globalImages->currentRenderImage->Bind();
2012-11-26 18:58:24 +00:00
GL_State( GLS_DEPTHFUNC_ALWAYS | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ZERO );
2012-11-26 18:58:24 +00:00
float mvpMatrix[16] = { 0 };
mvpMatrix[0] = 1;
mvpMatrix[5] = 1;
mvpMatrix[10] = 1;
mvpMatrix[15] = 1;
2012-11-26 18:58:24 +00:00
// Set Parms
float texS[4] = { 1.0f, 0.0f, 0.0f, 0.0f };
float texT[4] = { 0.0f, 1.0f, 0.0f, 0.0f };
renderProgManager.SetRenderParm( RENDERPARM_TEXTUREMATRIX_S, texS );
renderProgManager.SetRenderParm( RENDERPARM_TEXTUREMATRIX_T, texT );
2012-11-26 18:58:24 +00:00
// disable texgen
float texGenEnabled[4] = { 0, 0, 0, 0 };
renderProgManager.SetRenderParm( RENDERPARM_TEXGEN_0_ENABLED, texGenEnabled );
2012-11-26 18:58:24 +00:00
// set matrix
renderProgManager.SetRenderParms( RENDERPARM_MVPMATRIX_X, mvpMatrix, 4 );
2012-11-26 18:58:24 +00:00
renderProgManager.BindShader_TextureVertexColor();
2012-11-26 18:58:24 +00:00
RB_DrawElementsWithCounters( &backEnd.unitSquareSurface );
}
/*
============================
idAutoRender::RenderLoadingIcon
============================
*/
void idAutoRender::RenderLoadingIcon( float fracX, float fracY, float size, float speed )
{
2012-11-26 18:58:24 +00:00
float s = 0.0f;
2012-11-26 18:58:24 +00:00
float c = 1.0f;
if( autoRenderIcon != AUTORENDER_HELLICON )
{
if( Sys_Milliseconds() >= nextRotateTime )
{
2012-11-26 18:58:24 +00:00
nextRotateTime = Sys_Milliseconds() + 100;
currentRotation -= 90.0f;
}
float angle = DEG2RAD( currentRotation );
idMath::SinCos( angle, s, c );
}
2012-11-26 18:58:24 +00:00
const float pixelAspect = renderSystem->GetPixelAspect();
const float screenWidth = renderSystem->GetWidth();
const float screenHeight = renderSystem->GetHeight();
2012-11-26 18:58:24 +00:00
const float minSize = Min( screenWidth, screenHeight );
if( minSize <= 0.0f )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
float scaleX = size * minSize / screenWidth;
float scaleY = size * minSize / screenHeight;
2012-11-26 18:58:24 +00:00
float scale[16] = { 0 };
scale[0] = c * scaleX / pixelAspect;
scale[1] = -s * scaleY;
scale[4] = s * scaleX / pixelAspect;
scale[5] = c * scaleY;
scale[10] = 1.0f;
scale[15] = 1.0f;
2012-11-26 18:58:24 +00:00
scale[12] = fracX;
scale[13] = fracY;
2012-11-26 18:58:24 +00:00
float ortho[16] = { 0 };
ortho[0] = 2.0f;
ortho[5] = -2.0f;
ortho[10] = -2.0f;
ortho[12] = -1.0f;
ortho[13] = 1.0f;
ortho[14] = -1.0f;
ortho[15] = 1.0f;
2012-11-26 18:58:24 +00:00
float finalOrtho[16];
R_MatrixMultiply( scale, ortho, finalOrtho );
2012-11-26 18:58:24 +00:00
float projMatrixTranspose[16];
R_MatrixTranspose( finalOrtho, projMatrixTranspose );
renderProgManager.SetRenderParms( RENDERPARM_MVPMATRIX_X, projMatrixTranspose, 4 );
2012-11-26 18:58:24 +00:00
float a = 1.0f;
if( autoRenderIcon == AUTORENDER_HELLICON )
{
2012-11-26 18:58:24 +00:00
float alpha = DEG2RAD( Sys_Milliseconds() * speed );
a = idMath::Sin( alpha );
a = 0.35f + ( 0.65f * idMath::Fabs( a ) );
}
2012-11-26 18:58:24 +00:00
GL_SelectTexture( 0 );
if( autoRenderIcon == AUTORENDER_HELLICON )
{
2012-11-26 18:58:24 +00:00
globalImages->hellLoadingIconImage->Bind();
}
else
{
2012-11-26 18:58:24 +00:00
globalImages->loadingIconImage->Bind();
}
2012-11-26 18:58:24 +00:00
GL_State( GLS_DEPTHFUNC_ALWAYS | GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA );
2012-11-26 18:58:24 +00:00
// Set Parms
float texS[4] = { 1.0f, 0.0f, 0.0f, 0.0f };
float texT[4] = { 0.0f, 1.0f, 0.0f, 0.0f };
renderProgManager.SetRenderParm( RENDERPARM_TEXTUREMATRIX_S, texS );
renderProgManager.SetRenderParm( RENDERPARM_TEXTUREMATRIX_T, texT );
if( autoRenderIcon == AUTORENDER_HELLICON )
{
2012-11-26 18:58:24 +00:00
GL_Color( 1.0f, 1.0f, 1.0f, a );
}
2012-11-26 18:58:24 +00:00
// disable texgen
float texGenEnabled[4] = { 0, 0, 0, 0 };
renderProgManager.SetRenderParm( RENDERPARM_TEXGEN_0_ENABLED, texGenEnabled );
2012-11-26 18:58:24 +00:00
renderProgManager.BindShader_TextureVertexColor();
2012-11-26 18:58:24 +00:00
RB_DrawElementsWithCounters( &backEnd.unitSquareSurface );
}