jedioutcast/CODE-mp/renderer/tr_scene.cpp
2013-04-04 16:05:53 -05:00

517 lines
14 KiB
C++

#include "tr_local.h"
#if !defined(G2_H_INC)
#include "../ghoul2/G2.h"
#endif
#include "../ghoul2/G2_local.h"
#include "MatComp.h"
#pragma warning (disable: 4512) //default assignment operator could not be gened
#include "../qcommon/disablewarnings.h"
static int r_firstSceneDrawSurf;
static int r_numdlights;
static int r_firstSceneDlight;
static int r_numentities;
static int r_firstSceneEntity;
static int r_numminientities;
static int r_firstSceneMiniEntity;
static int refEntParent = -1;
static int r_numpolys;
static int r_firstScenePoly;
static int r_numpolyverts;
/*
====================
R_ToggleSmpFrame
====================
*/
void R_ToggleSmpFrame( void ) {
if ( r_smp->integer ) {
// use the other buffers next frame, because another CPU
// may still be rendering into the current ones
tr.smpFrame ^= 1;
} else {
tr.smpFrame = 0;
}
backEndData[tr.smpFrame]->commands.used = 0;
r_firstSceneDrawSurf = 0;
r_numdlights = 0;
r_firstSceneDlight = 0;
r_numentities = 0;
r_firstSceneEntity = 0;
refEntParent = -1;
r_numminientities = 0;
r_firstSceneMiniEntity = 0;
r_numpolys = 0;
r_firstScenePoly = 0;
r_numpolyverts = 0;
}
/*
====================
RE_ClearScene
====================
*/
void RE_ClearScene( void ) {
r_firstSceneDlight = r_numdlights;
r_firstSceneEntity = r_numentities;
r_firstScenePoly = r_numpolys;
refEntParent = -1;
r_firstSceneMiniEntity = r_numminientities;
}
/*
===========================================================================
DISCRETE POLYS
===========================================================================
*/
/*
=====================
R_AddPolygonSurfaces
Adds all the scene's polys into this view's drawsurf list
=====================
*/
void R_AddPolygonSurfaces( void ) {
int i;
shader_t *sh;
srfPoly_t *poly;
tr.currentEntityNum = ENTITYNUM_WORLD;
tr.shiftedEntityNum = tr.currentEntityNum << QSORT_ENTITYNUM_SHIFT;
for ( i = 0, poly = tr.refdef.polys; i < tr.refdef.numPolys ; i++, poly++ ) {
sh = R_GetShaderByHandle( poly->hShader );
R_AddDrawSurf( (surfaceType_t *)poly, sh, poly->fogIndex, qfalse );
}
}
/*
=====================
RE_AddPolyToScene
=====================
*/
void RE_AddPolyToScene( qhandle_t hShader, int numVerts, const polyVert_t *verts, int numPolys ) {
srfPoly_t *poly;
int i, j;
int fogIndex;
fog_t *fog;
vec3_t bounds[2];
if ( !tr.registered ) {
return;
}
if ( !hShader ) {
ri.Printf( PRINT_WARNING, "WARNING: RE_AddPolyToScene: NULL poly shader\n");
return;
}
for ( j = 0; j < numPolys; j++ ) {
if ( r_numpolyverts + numVerts > max_polyverts || r_numpolys >= max_polys ) {
ri.Printf( PRINT_WARNING, "WARNING: RE_AddPolyToScene: r_max_polys or r_max_polyverts reached\n");
return;
}
poly = &backEndData[tr.smpFrame]->polys[r_numpolys];
poly->surfaceType = SF_POLY;
poly->hShader = hShader;
poly->numVerts = numVerts;
poly->verts = &backEndData[tr.smpFrame]->polyVerts[r_numpolyverts];
Com_Memcpy( poly->verts, &verts[numVerts*j], numVerts * sizeof( *verts ) );
// done.
r_numpolys++;
r_numpolyverts += numVerts;
// if no world is loaded
if ( tr.world == NULL ) {
fogIndex = 0;
}
// see if it is in a fog volume
else if ( tr.world->numfogs == 1 ) {
fogIndex = 0;
} else {
// find which fog volume the poly is in
VectorCopy( poly->verts[0].xyz, bounds[0] );
VectorCopy( poly->verts[0].xyz, bounds[1] );
for ( i = 1 ; i < poly->numVerts ; i++ ) {
AddPointToBounds( poly->verts[i].xyz, bounds[0], bounds[1] );
}
for ( fogIndex = 1 ; fogIndex < tr.world->numfogs ; fogIndex++ ) {
fog = &tr.world->fogs[fogIndex];
if ( bounds[1][0] >= fog->bounds[0][0]
&& bounds[1][1] >= fog->bounds[0][1]
&& bounds[1][2] >= fog->bounds[0][2]
&& bounds[0][0] <= fog->bounds[1][0]
&& bounds[0][1] <= fog->bounds[1][1]
&& bounds[0][2] <= fog->bounds[1][2] ) {
break;
}
}
if ( fogIndex == tr.world->numfogs ) {
fogIndex = 0;
}
}
poly->fogIndex = fogIndex;
}
}
//=================================================================================
/*
=====================
RE_AddRefEntityToScene
=====================
*/
void RE_AddRefEntityToScene( const refEntity_t *ent ) {
if ( !tr.registered ) {
return;
}
if ( r_numentities >= MAX_ENTITIES ) {
return;
}
if ( ent->reType < 0 || ent->reType >= RT_MAX_REF_ENTITY_TYPE ) {
ri.Error( ERR_DROP, "RE_AddRefEntityToScene: bad reType %i", ent->reType );
}
backEndData[tr.smpFrame]->entities[r_numentities].e = *ent;
backEndData[tr.smpFrame]->entities[r_numentities].lightingCalculated = qfalse;
if (ent->ghoul2)
{
CGhoul2Info_v &ghoul2 = *((CGhoul2Info_v *)ent->ghoul2);
#ifndef __linux__
if (!ghoul2[0].mModel)
{
DebugBreak();
}
#endif
}
if (ent->reType == RT_ENT_CHAIN)
{
refEntParent = r_numentities;
backEndData[tr.smpFrame]->entities[r_numentities].e.uRefEnt.uMini.miniStart = r_numminientities - r_firstSceneMiniEntity;
backEndData[tr.smpFrame]->entities[r_numentities].e.uRefEnt.uMini.miniCount = 0;
}
else
{
refEntParent = -1;
}
r_numentities++;
}
/************************************************************************************************
* RE_AddMiniRefEntityToScene *
* Adds a mini ref ent to the scene. If the input parameter is null, it signifies the end *
* of the chain. Otherwise, if there is a valid chain parent, it will be added to that. *
* If there is no parent, it will be added as a regular ref ent. *
* *
* Input *
* ent: the mini ref ent to be added *
* *
* Output / Return *
* none *
* *
************************************************************************************************/
void RE_AddMiniRefEntityToScene( const miniRefEntity_t *ent )
{
refEntity_t *parent;
if ( !tr.registered )
{
return;
}
if (!ent)
{
refEntParent = -1;
return;
}
if ( r_numminientities >= MAX_MINI_ENTITIES)
{
ri.Printf( PRINT_WARNING, S_COLOR_YELLOW "WARNING: Attempting to add too many miniRefEntity_t\n");
return;
}
if ( ent->reType < 0 || ent->reType >= RT_MAX_REF_ENTITY_TYPE )
{
ri.Error( ERR_DROP, "RE_AddMiniRefEntityToScene: bad reType %i", ent->reType );
}
if (!r_numentities || refEntParent == -1)
{
// ri.Error( ERR_DROP, "RE_AddMiniRefEntityToScene: mini without parent ref ent");
refEntity_t tempEnt;
memcpy(&tempEnt, ent, sizeof(*ent));
memset(((char *)&tempEnt)+sizeof(*ent), 0, sizeof(tempEnt) - sizeof(*ent));
RE_AddRefEntityToScene(&tempEnt);
return;
}
parent = &backEndData[tr.smpFrame]->entities[refEntParent].e;
parent->uRefEnt.uMini.miniCount++;
backEndData[tr.smpFrame]->miniEntities[r_numminientities].e = *ent;
r_numminientities++;
}
/*
=====================
RE_AddDynamicLightToScene
=====================
*/
void RE_AddDynamicLightToScene( const vec3_t org, float intensity, float r, float g, float b, int additive ) {
dlight_t *dl;
if ( !tr.registered ) {
return;
}
if ( r_numdlights >= MAX_DLIGHTS ) {
return;
}
if ( intensity <= 0 ) {
return;
}
dl = &backEndData[tr.smpFrame]->dlights[r_numdlights++];
VectorCopy (org, dl->origin);
dl->radius = intensity;
dl->color[0] = r;
dl->color[1] = g;
dl->color[2] = b;
dl->additive = additive;
}
/*
=====================
RE_AddLightToScene
=====================
*/
void RE_AddLightToScene( const vec3_t org, float intensity, float r, float g, float b ) {
dlight_t *dl;
if (r_newDLights->integer)
{
if ( !tr.registered ) {
return;
}
if ( r_numdlights >= MAX_DLIGHTS ) {
return;
}
//dl = &backEnd.refdef.dlights[r_numdlights++];
dl = &backEndData[tr.smpFrame]->dlights[r_numdlights++];
dl->mType=DLIGHT_VERTICAL;
VectorCopy (org, dl->origin);
dl->color[0] = r;
dl->color[1] = g;
dl->color[2] = b;
if ( intensity <= 0 )
{
// projected from viewer
VectorCopy (tr.viewParms.ori.axis[0], dl->mDirection);
VectorCopy (tr.viewParms.ori.axis[1], dl->mBasis2);
VectorCopy (tr.viewParms.ori.axis[2], dl->mBasis3);
dl->mType=DLIGHT_PROJECTED;
dl->radius = -intensity;
}
else
{
dl->radius = intensity;
}
// dl->additive = qtrue;
}
else
{
RE_AddDynamicLightToScene( org, intensity, r, g, b, qfalse );
}
}
/*
=====================
RE_AddAdditiveLightToScene
=====================
*/
void RE_AddAdditiveLightToScene( const vec3_t org, float intensity, float r, float g, float b ) {
RE_AddDynamicLightToScene( org, intensity, r, g, b, qtrue );
}
/*
@@@@@@@@@@@@@@@@@@@@@
RE_RenderScene
Draw a 3D view into a part of the window, then return
to 2D drawing.
Rendering a scene may require multiple views to be rendered
to handle mirrors,
@@@@@@@@@@@@@@@@@@@@@
*/
void RE_RenderScene( const refdef_t *fd ) {
viewParms_t parms;
int startTime;
static int lastTime = 0;
if ( !tr.registered ) {
return;
}
GLimp_LogComment( "====== RE_RenderScene =====\n" );
if ( r_norefresh->integer ) {
return;
}
startTime = ri.Milliseconds();
if (!tr.world && !( fd->rdflags & RDF_NOWORLDMODEL ) ) {
ri.Error (ERR_DROP, "R_RenderScene: NULL worldmodel");
}
Com_Memcpy( tr.refdef.text, fd->text, sizeof( tr.refdef.text ) );
tr.refdef.x = fd->x;
tr.refdef.y = fd->y;
tr.refdef.width = fd->width;
tr.refdef.height = fd->height;
tr.refdef.fov_x = fd->fov_x;
tr.refdef.fov_y = fd->fov_y;
VectorCopy( fd->vieworg, tr.refdef.vieworg );
VectorCopy( fd->viewaxis[0], tr.refdef.viewaxis[0] );
VectorCopy( fd->viewaxis[1], tr.refdef.viewaxis[1] );
VectorCopy( fd->viewaxis[2], tr.refdef.viewaxis[2] );
tr.refdef.time = fd->time;
tr.refdef.frametime = fd->time - lastTime;
lastTime = fd->time;
if (tr.refdef.frametime > 500)
{
tr.refdef.frametime = 500;
}
else if (tr.refdef.frametime < 0)
{
tr.refdef.frametime = 0;
}
tr.refdef.rdflags = fd->rdflags;
// copy the areamask data over and note if it has changed, which
// will force a reset of the visible leafs even if the view hasn't moved
tr.refdef.areamaskModified = qfalse;
if ( ! (tr.refdef.rdflags & RDF_NOWORLDMODEL) ) {
int areaDiff;
int i;
// compare the area bits
areaDiff = 0;
for (i = 0 ; i < MAX_MAP_AREA_BYTES/4 ; i++) {
areaDiff |= ((int *)tr.refdef.areamask)[i] ^ ((int *)fd->areamask)[i];
((int *)tr.refdef.areamask)[i] = ((int *)fd->areamask)[i];
}
if ( areaDiff ) {
// a door just opened or something
tr.refdef.areamaskModified = qtrue;
}
}
// derived info
tr.refdef.floatTime = tr.refdef.time * 0.001f;
tr.refdef.numDrawSurfs = r_firstSceneDrawSurf;
tr.refdef.drawSurfs = backEndData[tr.smpFrame]->drawSurfs;
tr.refdef.num_entities = r_numentities - r_firstSceneEntity;
tr.refdef.entities = &backEndData[tr.smpFrame]->entities[r_firstSceneEntity];
tr.refdef.miniEntities = &backEndData[tr.smpFrame]->miniEntities[r_firstSceneMiniEntity];
tr.refdef.num_dlights = r_numdlights - r_firstSceneDlight;
tr.refdef.dlights = &backEndData[tr.smpFrame]->dlights[r_firstSceneDlight];
tr.refdef.numPolys = r_numpolys - r_firstScenePoly;
tr.refdef.polys = &backEndData[tr.smpFrame]->polys[r_firstScenePoly];
// turn off dynamic lighting globally by clearing all the
// dlights if it needs to be disabled or if vertex lighting is enabled
if ( r_dynamiclight->integer == 0 ||
r_vertexLight->integer == 1 ) {
tr.refdef.num_dlights = 0;
}
// a single frame may have multiple scenes draw inside it --
// a 3D game view, 3D status bar renderings, 3D menus, etc.
// They need to be distinguished by the light flare code, because
// the visibility state for a given surface may be different in
// each scene / view.
tr.frameSceneNum++;
tr.sceneCount++;
// setup view parms for the initial view
//
// set up viewport
// The refdef takes 0-at-the-top y coordinates, so
// convert to GL's 0-at-the-bottom space
//
Com_Memset( &parms, 0, sizeof( parms ) );
parms.viewportX = tr.refdef.x;
parms.viewportY = glConfig.vidHeight - ( tr.refdef.y + tr.refdef.height );
parms.viewportWidth = tr.refdef.width;
parms.viewportHeight = tr.refdef.height;
parms.isPortal = qfalse;
parms.fovX = tr.refdef.fov_x;
parms.fovY = tr.refdef.fov_y;
VectorCopy( fd->vieworg, parms.ori.origin );
VectorCopy( fd->viewaxis[0], parms.ori.axis[0] );
VectorCopy( fd->viewaxis[1], parms.ori.axis[1] );
VectorCopy( fd->viewaxis[2], parms.ori.axis[2] );
VectorCopy( fd->vieworg, parms.pvsOrigin );
R_RenderView( &parms );
// the next scene rendered in this frame will tack on after this one
r_firstSceneDrawSurf = tr.refdef.numDrawSurfs;
r_firstSceneEntity = r_numentities;
r_firstSceneMiniEntity = r_numminientities;
r_firstSceneDlight = r_numdlights;
r_firstScenePoly = r_numpolys;
refEntParent = -1;
tr.frontEndMsec += ri.Milliseconds() - startTime;
}