Skip to content

Commit

Permalink
Refactor|Client: Relocated various functions from r_things.cpp to bet…
Browse files Browse the repository at this point in the history
…ter homes
  • Loading branch information
danij-deng committed Aug 31, 2013
1 parent 953fea4 commit d8e9b2a
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 207 deletions.
6 changes: 6 additions & 0 deletions doomsday/client/include/r_util.h
Expand Up @@ -25,6 +25,12 @@
#include <de/Matrix>
#include <de/Vector>

float R_MovementYaw(float const mom[2]);
float R_MovementXYYaw(float momx, float momy);

float R_MovementPitch(float const mom[3]);
float R_MovementXYZPitch(float momx, float momy, float momz);

/**
* Get a global angle from Cartesian coordinates in the map coordinate space
* relative to the viewer.
Expand Down
8 changes: 8 additions & 0 deletions doomsday/client/include/render/r_shadow.h
Expand Up @@ -91,4 +91,12 @@ int R_IterateShadowProjections(uint listIdx, int (*callback) (shadowprojection_t
*/
Plane *R_FindShadowPlane(struct mobj_s *mobj);

/**
* Calculate the strength of the shadow this mobj should cast.
*
* @note Implemented using a greatly simplified version of the lighting equation;
* no light diminishing or light range compression.
*/
float R_ShadowStrength(struct mobj_s *mo);

#endif // DENG_RENDER_SHADOW_H
27 changes: 1 addition & 26 deletions doomsday/client/include/render/r_things.h
Expand Up @@ -21,7 +21,7 @@
#ifndef DENG_RENDER_THINGS_H
#define DENG_RENDER_THINGS_H

#include "Materials"
#include <de/libdeng1.h>

DENG_EXTERN_C int levelFullBright;
DENG_EXTERN_C float pspOffset[2], pspLightLevelMultiplier;
Expand All @@ -38,31 +38,6 @@ DENG_EXTERN_C int psp3d;
extern "C" {
#endif

/// @return Radius of the mobj as it would visually appear to be.
coord_t R_VisualRadius(struct mobj_s *mo);

/**
* Calculate the strength of the shadow this mobj should cast.
*
* @note Implemented using a greatly simplified version of the lighting equation;
* no light diminishing or light range compression.
*/
float R_ShadowStrength(struct mobj_s *mo);

float R_Alpha(struct mobj_s *mo);

/**
* @return The current floatbob offset for the mobj, if the mobj is flagged
* for bobbing; otherwise @c 0.
*/
coord_t R_GetBobOffset(struct mobj_s *mo);

float R_MovementYaw(float const mom[2]);
float R_MovementXYYaw(float momx, float momy);

float R_MovementPitch(float const mom[3]);
float R_MovementXYZPitch(float momx, float momy, float momz);

/**
* Generates a vissprite for a mobj if it might be visible.
*/
Expand Down
14 changes: 12 additions & 2 deletions doomsday/client/include/world/p_object.h
Expand Up @@ -36,8 +36,7 @@ extern "C" {
typedef struct mobj_s
{
DD_BASE_MOBJ_ELEMENTS()
}
mobj_t;
} mobj_t;

#define MOBJ_SIZE gx.mobjSize

Expand Down Expand Up @@ -68,6 +67,17 @@ coord_t Mobj_ApproxPointDistance(mobj_t *start, coord_t const *point);

boolean Mobj_UnlinkFromSector(mobj_t *mobj);

/**
* @return The current floatbob offset for the mobj, if the mobj is flagged
* for bobbing; otherwise @c 0.
*/
coord_t Mobj_BobOffset(mobj_t *mobj);

float Mobj_Alpha(mobj_t *mo);

/// @return Radius of the mobj as it would visually appear to be.
coord_t Mobj_VisualRadius(mobj_t *mo);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
23 changes: 23 additions & 0 deletions doomsday/client/src/r_util.cpp
Expand Up @@ -34,6 +34,29 @@

using namespace de;

float R_MovementYaw(float const mom[])
{
// Multiply by 100 to get some artificial accuracy in bamsAtan2.
return BANG2DEG(bamsAtan2(-100 * mom[MY], 100 * mom[MX]));
}

float R_MovementXYYaw(float momx, float momy)
{
float mom[2] = { momx, momy };
return R_MovementYaw(mom);
}

float R_MovementPitch(float const mom[])
{
return BANG2DEG(bamsAtan2 (100 * mom[MZ], 100 * V2f_Length(mom)));
}

float R_MovementXYZPitch(float momx, float momy, float momz)
{
float mom[3] = { momx, momy, momz };
return R_MovementPitch(mom);
}

angle_t R_ViewPointToAngle(Vector2d point)
{
viewdata_t const *viewData = R_ViewData(viewPlayer - ddPlayers);
Expand Down
3 changes: 2 additions & 1 deletion doomsday/client/src/render/lumobj.cpp
Expand Up @@ -27,6 +27,7 @@
#include "de_console.h"
#include "de_render.h"
#include "de_graphics.h"
#include "de_resource.h"
#include "de_misc.h"
#include "de_play.h"
#include "de_defs.h"
Expand Down Expand Up @@ -801,7 +802,7 @@ static void addLuminous(mobj_t *mo)
}

Texture &tex = ms.texture(MTU_PRIMARY).generalCase();
float center = -tex.origin().y - mo->floorClip - R_GetBobOffset(mo) - yOffset;
float center = -tex.origin().y - mo->floorClip - Mobj_BobOffset(mo) - yOffset;

// Will the sprite be allowed to go inside the floor?
float mul = mo->origin[VZ] + -tex.origin().y - (float) ms.height() - mo->bspLeaf->visFloor().height();
Expand Down
79 changes: 77 additions & 2 deletions doomsday/client/src/render/r_shadow.cpp
Expand Up @@ -30,11 +30,14 @@
#include "de_console.h"
#include "de_render.h"
#include "de_play.h"
#include "de_resource.h"
#include "api_map.h"

#include "world/map.h"
#include "BspLeaf"

#include "gl/gl_tex.h"

#include "render/r_shadow.h"

using namespace de;
Expand Down Expand Up @@ -227,7 +230,7 @@ int RIT_ProjectShadowToSurfaceIterator(void *mobj, void *parameters)
return false;

// Calculate the radius of the shadow.
float shadowRadius = R_VisualRadius(mo);
float shadowRadius = Mobj_VisualRadius(mo);
if(shadowRadius <= 0)
return false;

Expand All @@ -236,7 +239,7 @@ int RIT_ProjectShadowToSurfaceIterator(void *mobj, void *parameters)

mobjOrigin[VZ] -= mo->floorClip;
if(mo->ddFlags & DDMF_BOB)
mobjOrigin[VZ] -= R_GetBobOffset(mo);
mobjOrigin[VZ] -= Mobj_BobOffset(mo);

coord_t mobjHeight = mo->height;
if(!mobjHeight) mobjHeight = 1;
Expand Down Expand Up @@ -381,3 +384,75 @@ Plane *R_FindShadowPlane(mobj_t *mo)
}
return 0;
}

static modeldef_t *currentModelDefForMobj(mobj_t *mo)
{
// If models are being used, use the model's radius.
if(useModels)
{
modeldef_t *mf = 0, *nextmf = 0;
Models_ModelForMobj(mo, &mf, &nextmf);
return mf;
}
return 0;
}

float R_ShadowStrength(mobj_t *mo)
{
DENG_ASSERT(mo);

float const minSpriteAlphaLimit = .1f;
float ambientLightLevel, strength = .65f; ///< Default strength factor.

// Is this mobj in a valid state for shadow casting?
if(!mo->state || !mo->bspLeaf) return 0;

// Should this mobj even have a shadow?
if((mo->state->flags & STF_FULLBRIGHT) ||
(mo->ddFlags & DDMF_DONTDRAW) || (mo->ddFlags & DDMF_ALWAYSLIT))
return 0;

// Sample the ambient light level at the mobj's position.
if(useBias && App_World().map().hasLightGrid())
{
// Evaluate in the light grid.
ambientLightLevel = App_World().map().lightGrid().evaluateLightLevel(mo->origin);
}
else
{
ambientLightLevel = mo->bspLeaf->sector().lightLevel();
Rend_ApplyLightAdaptation(ambientLightLevel);
}

// Sprites have their own shadow strength factor.
if(!currentModelDefForMobj(mo))
{
Material *mat = R_MaterialForSprite(mo->sprite, mo->frame);
if(mat)
{
// Ensure we've prepared this.
MaterialSnapshot const &ms = mat->prepare(Rend_SpriteMaterialSpec());

averagealpha_analysis_t const *aa = (averagealpha_analysis_t const *)
ms.texture(MTU_PRIMARY).generalCase().analysisDataPointer(Texture::AverageAlphaAnalysis);
DENG_ASSERT(aa != 0);

// We use an average which factors in the coverage ratio
// of alpha:non-alpha pixels.
/// @todo Constant weights could stand some tweaking...
float weightedSpriteAlpha = aa->alpha * (0.4f + (1 - aa->coverage) * 0.6f);

// Almost entirely translucent sprite? => no shadow.
if(weightedSpriteAlpha < minSpriteAlphaLimit) return 0;

// Apply this factor.
strength *= MIN_OF(1, 0.2f + weightedSpriteAlpha);
}
}

// Factor in Mobj alpha.
strength *= Mobj_Alpha(mo);

/// @note This equation is the same as that used for fakeradio.
return (0.6f - ambientLightLevel * 0.4f) * strength;
}

0 comments on commit d8e9b2a

Please sign in to comment.