📄 cg_view.c
字号:
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena 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 2 of the License,
or (at your option) any later version.
Quake III Arena 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 Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
//
// cg_view.c -- setup all the parameters (position, angle, etc)
// for a 3D rendering
#include "cg_local.h"
/*
=============================================================================
MODEL TESTING
The viewthing and gun positioning tools from Q2 have been integrated and
enhanced into a single model testing facility.
Model viewing can begin with either "testmodel <modelname>" or "testgun <modelname>".
The names must be the full pathname after the basedir, like
"models/weapons/v_launch/tris.md3" or "players/male/tris.md3"
Testmodel will create a fake entity 100 units in front of the current view
position, directly facing the viewer. It will remain immobile, so you can
move around it to view it from different angles.
Testgun will cause the model to follow the player around and supress the real
view weapon model. The default frame 0 of most guns is completely off screen,
so you will probably have to cycle a couple frames to see it.
"nextframe", "prevframe", "nextskin", and "prevskin" commands will change the
frame or skin of the testmodel. These are bound to F5, F6, F7, and F8 in
q3default.cfg.
If a gun is being tested, the "gun_x", "gun_y", and "gun_z" variables will let
you adjust the positioning.
Note that none of the model testing features update while the game is paused, so
it may be convenient to test with deathmatch set to 1 so that bringing down the
console doesn't pause the game.
=============================================================================
*/
/*
=================
CG_TestModel_f
Creates an entity in front of the current position, which
can then be moved around
=================
*/
void CG_TestModel_f (void) {
vec3_t angles;
memset( &cg.testModelEntity, 0, sizeof(cg.testModelEntity) );
if ( trap_Argc() < 2 ) {
return;
}
Q_strncpyz (cg.testModelName, CG_Argv( 1 ), MAX_QPATH );
cg.testModelEntity.hModel = trap_R_RegisterModel( cg.testModelName );
if ( trap_Argc() == 3 ) {
cg.testModelEntity.backlerp = atof( CG_Argv( 2 ) );
cg.testModelEntity.frame = 1;
cg.testModelEntity.oldframe = 0;
}
if (! cg.testModelEntity.hModel ) {
CG_Printf( "Can't register model\n" );
return;
}
VectorMA( cg.refdef.vieworg, 100, cg.refdef.viewaxis[0], cg.testModelEntity.origin );
angles[PITCH] = 0;
angles[YAW] = 180 + cg.refdefViewAngles[1];
angles[ROLL] = 0;
AnglesToAxis( angles, cg.testModelEntity.axis );
cg.testGun = qfalse;
}
/*
=================
CG_TestGun_f
Replaces the current view weapon with the given model
=================
*/
void CG_TestGun_f (void) {
CG_TestModel_f();
cg.testGun = qtrue;
cg.testModelEntity.renderfx = RF_MINLIGHT | RF_DEPTHHACK | RF_FIRST_PERSON;
}
void CG_TestModelNextFrame_f (void) {
cg.testModelEntity.frame++;
CG_Printf( "frame %i\n", cg.testModelEntity.frame );
}
void CG_TestModelPrevFrame_f (void) {
cg.testModelEntity.frame--;
if ( cg.testModelEntity.frame < 0 ) {
cg.testModelEntity.frame = 0;
}
CG_Printf( "frame %i\n", cg.testModelEntity.frame );
}
void CG_TestModelNextSkin_f (void) {
cg.testModelEntity.skinNum++;
CG_Printf( "skin %i\n", cg.testModelEntity.skinNum );
}
void CG_TestModelPrevSkin_f (void) {
cg.testModelEntity.skinNum--;
if ( cg.testModelEntity.skinNum < 0 ) {
cg.testModelEntity.skinNum = 0;
}
CG_Printf( "skin %i\n", cg.testModelEntity.skinNum );
}
static void CG_AddTestModel (void) {
int i;
// re-register the model, because the level may have changed
cg.testModelEntity.hModel = trap_R_RegisterModel( cg.testModelName );
if (! cg.testModelEntity.hModel ) {
CG_Printf ("Can't register model\n");
return;
}
// if testing a gun, set the origin reletive to the view origin
if ( cg.testGun ) {
VectorCopy( cg.refdef.vieworg, cg.testModelEntity.origin );
VectorCopy( cg.refdef.viewaxis[0], cg.testModelEntity.axis[0] );
VectorCopy( cg.refdef.viewaxis[1], cg.testModelEntity.axis[1] );
VectorCopy( cg.refdef.viewaxis[2], cg.testModelEntity.axis[2] );
// allow the position to be adjusted
for (i=0 ; i<3 ; i++) {
cg.testModelEntity.origin[i] += cg.refdef.viewaxis[0][i] * cg_gun_x.value;
cg.testModelEntity.origin[i] += cg.refdef.viewaxis[1][i] * cg_gun_y.value;
cg.testModelEntity.origin[i] += cg.refdef.viewaxis[2][i] * cg_gun_z.value;
}
}
trap_R_AddRefEntityToScene( &cg.testModelEntity );
}
//============================================================================
/*
=================
CG_CalcVrect
Sets the coordinates of the rendered window
=================
*/
static void CG_CalcVrect (void) {
int size;
// the intermission should allways be full screen
if ( cg.snap->ps.pm_type == PM_INTERMISSION ) {
size = 100;
} else {
// bound normal viewsize
if (cg_viewsize.integer < 30) {
trap_Cvar_Set ("cg_viewsize","30");
size = 30;
} else if (cg_viewsize.integer > 100) {
trap_Cvar_Set ("cg_viewsize","100");
size = 100;
} else {
size = cg_viewsize.integer;
}
}
cg.refdef.width = cgs.glconfig.vidWidth*size/100;
cg.refdef.width &= ~1;
cg.refdef.height = cgs.glconfig.vidHeight*size/100;
cg.refdef.height &= ~1;
cg.refdef.x = (cgs.glconfig.vidWidth - cg.refdef.width)/2;
cg.refdef.y = (cgs.glconfig.vidHeight - cg.refdef.height)/2;
}
//==============================================================================
/*
===============
CG_OffsetThirdPersonView
===============
*/
#define FOCUS_DISTANCE 512
static void CG_OffsetThirdPersonView( void ) {
vec3_t forward, right, up;
vec3_t view;
vec3_t focusAngles;
trace_t trace;
static vec3_t mins = { -4, -4, -4 };
static vec3_t maxs = { 4, 4, 4 };
vec3_t focusPoint;
float focusDist;
float forwardScale, sideScale;
cg.refdef.vieworg[2] += cg.predictedPlayerState.viewheight;
VectorCopy( cg.refdefViewAngles, focusAngles );
// if dead, look at killer
if ( cg.predictedPlayerState.stats[STAT_HEALTH] <= 0 ) {
focusAngles[YAW] = cg.predictedPlayerState.stats[STAT_DEAD_YAW];
cg.refdefViewAngles[YAW] = cg.predictedPlayerState.stats[STAT_DEAD_YAW];
}
if ( focusAngles[PITCH] > 45 ) {
focusAngles[PITCH] = 45; // don't go too far overhead
}
AngleVectors( focusAngles, forward, NULL, NULL );
VectorMA( cg.refdef.vieworg, FOCUS_DISTANCE, forward, focusPoint );
VectorCopy( cg.refdef.vieworg, view );
view[2] += 8;
cg.refdefViewAngles[PITCH] *= 0.5;
AngleVectors( cg.refdefViewAngles, forward, right, up );
forwardScale = cos( cg_thirdPersonAngle.value / 180 * M_PI );
sideScale = sin( cg_thirdPersonAngle.value / 180 * M_PI );
VectorMA( view, -cg_thirdPersonRange.value * forwardScale, forward, view );
VectorMA( view, -cg_thirdPersonRange.value * sideScale, right, view );
// trace a ray from the origin to the viewpoint to make sure the view isn't
// in a solid block. Use an 8 by 8 block to prevent the view from near clipping anything
if (!cg_cameraMode.integer) {
CG_Trace( &trace, cg.refdef.vieworg, mins, maxs, view, cg.predictedPlayerState.clientNum, MASK_SOLID );
if ( trace.fraction != 1.0 ) {
VectorCopy( trace.endpos, view );
view[2] += (1.0 - trace.fraction) * 32;
// try another trace to this position, because a tunnel may have the ceiling
// close enogh that this is poking out
CG_Trace( &trace, cg.refdef.vieworg, mins, maxs, view, cg.predictedPlayerState.clientNum, MASK_SOLID );
VectorCopy( trace.endpos, view );
}
}
VectorCopy( view, cg.refdef.vieworg );
// select pitch to look at focus point from vieword
VectorSubtract( focusPoint, cg.refdef.vieworg, focusPoint );
focusDist = sqrt( focusPoint[0] * focusPoint[0] + focusPoint[1] * focusPoint[1] );
if ( focusDist < 1 ) {
focusDist = 1; // should never happen
}
cg.refdefViewAngles[PITCH] = -180 / M_PI * atan2( focusPoint[2], focusDist );
cg.refdefViewAngles[YAW] -= cg_thirdPersonAngle.value;
}
// this causes a compiler bug on mac MrC compiler
static void CG_StepOffset( void ) {
int timeDelta;
// smooth out stair climbing
timeDelta = cg.time - cg.stepTime;
if ( timeDelta < STEP_TIME ) {
cg.refdef.vieworg[2] -= cg.stepChange
* (STEP_TIME - timeDelta) / STEP_TIME;
}
}
/*
===============
CG_OffsetFirstPersonView
===============
*/
static void CG_OffsetFirstPersonView( void ) {
float *origin;
float *angles;
float bob;
float ratio;
float delta;
float speed;
float f;
vec3_t predictedVelocity;
int timeDelta;
if ( cg.snap->ps.pm_type == PM_INTERMISSION ) {
return;
}
origin = cg.refdef.vieworg;
angles = cg.refdefViewAngles;
// if dead, fix the angle and don't add any kick
if ( cg.snap->ps.stats[STAT_HEALTH] <= 0 ) {
angles[ROLL] = 40;
angles[PITCH] = -15;
angles[YAW] = cg.snap->ps.stats[STAT_DEAD_YAW];
origin[2] += cg.predictedPlayerState.viewheight;
return;
}
// add angles based on weapon kick
VectorAdd (angles, cg.kick_angles, angles);
// add angles based on damage kick
if ( cg.damageTime ) {
ratio = cg.time - cg.damageTime;
if ( ratio < DAMAGE_DEFLECT_TIME ) {
ratio /= DAMAGE_DEFLECT_TIME;
angles[PITCH] += ratio * cg.v_dmg_pitch;
angles[ROLL] += ratio * cg.v_dmg_roll;
} else {
ratio = 1.0 - ( ratio - DAMAGE_DEFLECT_TIME ) / DAMAGE_RETURN_TIME;
if ( ratio > 0 ) {
angles[PITCH] += ratio * cg.v_dmg_pitch;
angles[ROLL] += ratio * cg.v_dmg_roll;
}
}
}
// add pitch based on fall kick
#if 0
ratio = ( cg.time - cg.landTime) / FALL_TIME;
if (ratio < 0)
ratio = 0;
angles[PITCH] += ratio * cg.fall_value;
#endif
// add angles based on velocity
VectorCopy( cg.predictedPlayerState.velocity, predictedVelocity );
delta = DotProduct ( predictedVelocity, cg.refdef.viewaxis[0]);
angles[PITCH] += delta * cg_runpitch.value;
delta = DotProduct ( predictedVelocity, cg.refdef.viewaxis[1]);
angles[ROLL] -= delta * cg_runroll.value;
// add angles based on bob
// make sure the bob is visible even at low speeds
speed = cg.xyspeed > 200 ? cg.xyspeed : 200;
delta = cg.bobfracsin * cg_bobpitch.value * speed;
if (cg.predictedPlayerState.pm_flags & PMF_DUCKED)
delta *= 3; // crouching
angles[PITCH] += delta;
delta = cg.bobfracsin * cg_bobroll.value * speed;
if (cg.predictedPlayerState.pm_flags & PMF_DUCKED)
delta *= 3; // crouching accentuates roll
if (cg.bobcycle & 1)
delta = -delta;
angles[ROLL] += delta;
//===================================
// add view height
origin[2] += cg.predictedPlayerState.viewheight;
// smooth out duck height changes
timeDelta = cg.time - cg.duckTime;
if ( timeDelta < DUCK_TIME) {
cg.refdef.vieworg[2] -= cg.duckChange
* (DUCK_TIME - timeDelta) / DUCK_TIME;
}
// add bob height
bob = cg.bobfracsin * cg.xyspeed * cg_bobup.value;
if (bob > 6) {
bob = 6;
}
origin[2] += bob;
// add fall height
delta = cg.time - cg.landTime;
if ( delta < LAND_DEFLECT_TIME ) {
f = delta / LAND_DEFLECT_TIME;
cg.refdef.vieworg[2] += cg.landChange * f;
} else if ( delta < LAND_DEFLECT_TIME + LAND_RETURN_TIME ) {
delta -= LAND_DEFLECT_TIME;
f = 1.0 - ( delta / LAND_RETURN_TIME );
cg.refdef.vieworg[2] += cg.landChange * f;
}
// add step offset
CG_StepOffset();
// add kick offset
VectorAdd (origin, cg.kick_origin, origin);
// pivot the eye based on a neck length
#if 0
{
#define NECK_LENGTH 8
vec3_t forward, up;
cg.refdef.vieworg[2] -= NECK_LENGTH;
AngleVectors( cg.refdefViewAngles, forward, NULL, up );
VectorMA( cg.refdef.vieworg, 3, forward, cg.refdef.vieworg );
VectorMA( cg.refdef.vieworg, NECK_LENGTH, up, cg.refdef.vieworg );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -