- Place the HUD model correctly in the world so that shader light calculations work

This commit is contained in:
Magnus Norddahl 2017-11-05 15:35:03 +01:00
parent 23e5d81746
commit d4ebe51e83
7 changed files with 23 additions and 48 deletions

View file

@ -59,7 +59,7 @@ struct FDynLightData
bool gl_GetLight(int group, Plane & p, ADynamicLight * light, bool checkside, FDynLightData &data); bool gl_GetLight(int group, Plane & p, ADynamicLight * light, bool checkside, FDynLightData &data);
void gl_AddLightToList(int group, ADynamicLight * light, FDynLightData &ldata, bool hudmodel); void gl_AddLightToList(int group, ADynamicLight * light, FDynLightData &ldata);
void gl_UploadLights(FDynLightData &data); void gl_UploadLights(FDynLightData &data);

View file

@ -77,7 +77,7 @@ bool gl_GetLight(int group, Plane & p, ADynamicLight * light, bool checkside, FD
return false; return false;
} }
gl_AddLightToList(group, light, ldata, false); gl_AddLightToList(group, light, ldata);
return true; return true;
} }
@ -86,38 +86,13 @@ bool gl_GetLight(int group, Plane & p, ADynamicLight * light, bool checkside, FD
// Add one dynamic light to the light data list // Add one dynamic light to the light data list
// //
//========================================================================== //==========================================================================
void gl_AddLightToList(int group, ADynamicLight * light, FDynLightData &ldata, bool hudmodel) void gl_AddLightToList(int group, ADynamicLight * light, FDynLightData &ldata)
{ {
int i = 0; int i = 0;
DVector3 pos = light->PosRelative(group); DVector3 pos = light->PosRelative(group);
float radius = light->GetRadius(); float radius = light->GetRadius();
if (hudmodel)
{
// HUD model is already translated and rotated. We must rotate the lights into that view space.
DVector3 rotation;
DVector3 localpos = pos - r_viewpoint.Pos;
rotation.X = localpos.X * r_viewpoint.Angles.Yaw.Sin() - localpos.Y * r_viewpoint.Angles.Yaw.Cos();
rotation.Y = localpos.X * r_viewpoint.Angles.Yaw.Cos() + localpos.Y * r_viewpoint.Angles.Yaw.Sin();
rotation.Z = localpos.Z;
localpos = rotation;
rotation.X = localpos.X;
rotation.Y = localpos.Y * r_viewpoint.Angles.Pitch.Sin() - localpos.Z * r_viewpoint.Angles.Pitch.Cos();
rotation.Z = localpos.Y * r_viewpoint.Angles.Pitch.Cos() + localpos.Z * r_viewpoint.Angles.Pitch.Sin();
localpos = rotation;
rotation.Y = localpos.Y;
rotation.Z = localpos.Z * r_viewpoint.Angles.Roll.Sin() - localpos.X * r_viewpoint.Angles.Roll.Cos();
rotation.X = localpos.Z * r_viewpoint.Angles.Roll.Cos() + localpos.X * r_viewpoint.Angles.Roll.Sin();
localpos = rotation;
pos = localpos;
}
float cs; float cs;
if (light->IsAdditive()) if (light->IsAdditive())
{ {

View file

@ -1095,33 +1095,33 @@ void gl_RenderHUDModel(DPSprite *psp, float ofsX, float ofsY)
glFrontFace(GL_CCW); glFrontFace(GL_CCW);
} }
// [BB] The model has to be drawn independently from the position of the player, // The model position and orientation has to be drawn independently from the position of the player,
// so we have to reset the view matrix. // but we need to position it correctly in the world for light to work properly.
gl_RenderState.mViewMatrix.loadIdentity(); VSMatrix objectToWorldMatrix;
gl_RenderState.mViewMatrix.inverseMatrix(objectToWorldMatrix);
// Need to reset the normal matrix too
gl_RenderState.mNormalViewMatrix.loadIdentity();
// Scaling model (y scale for a sprite means height, i.e. z in the world!). // Scaling model (y scale for a sprite means height, i.e. z in the world!).
gl_RenderState.mViewMatrix.scale(smf->xscale, smf->zscale, smf->yscale); objectToWorldMatrix.scale(smf->xscale, smf->zscale, smf->yscale);
// Aplying model offsets (model offsets do not depend on model scalings). // Aplying model offsets (model offsets do not depend on model scalings).
gl_RenderState.mViewMatrix.translate(smf->xoffset / smf->xscale, smf->zoffset / smf->zscale, smf->yoffset / smf->yscale); objectToWorldMatrix.translate(smf->xoffset / smf->xscale, smf->zoffset / smf->zscale, smf->yoffset / smf->yscale);
// [BB] Weapon bob, very similar to the normal Doom weapon bob. // [BB] Weapon bob, very similar to the normal Doom weapon bob.
gl_RenderState.mViewMatrix.rotate(ofsX/4, 0, 1, 0); objectToWorldMatrix.rotate(ofsX/4, 0, 1, 0);
gl_RenderState.mViewMatrix.rotate((ofsY-WEAPONTOP)/-4., 1, 0, 0); objectToWorldMatrix.rotate((ofsY-WEAPONTOP)/-4., 1, 0, 0);
// [BB] For some reason the jDoom models need to be rotated. // [BB] For some reason the jDoom models need to be rotated.
gl_RenderState.mViewMatrix.rotate(90.f, 0, 1, 0); objectToWorldMatrix.rotate(90.f, 0, 1, 0);
// Applying angleoffset, pitchoffset, rolloffset. // Applying angleoffset, pitchoffset, rolloffset.
gl_RenderState.mViewMatrix.rotate(-smf->angleoffset, 0, 1, 0); objectToWorldMatrix.rotate(-smf->angleoffset, 0, 1, 0);
gl_RenderState.mViewMatrix.rotate(smf->pitchoffset, 0, 0, 1); objectToWorldMatrix.rotate(smf->pitchoffset, 0, 0, 1);
gl_RenderState.mViewMatrix.rotate(-smf->rolloffset, 1, 0, 0); objectToWorldMatrix.rotate(-smf->rolloffset, 1, 0, 0);
gl_RenderState.ApplyMatrices();
gl_RenderState.mModelMatrix = objectToWorldMatrix;
gl_RenderState.EnableModelMatrix(true);
gl_RenderFrameModels( smf, psp->GetState(), psp->GetTics(), playermo->player->ReadyWeapon->GetClass(), nullptr, 0 ); gl_RenderFrameModels( smf, psp->GetState(), psp->GetTics(), playermo->player->ReadyWeapon->GetClass(), nullptr, 0 );
gl_RenderState.EnableModelMatrix(false);
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
if (!( playermo->RenderStyle == LegacyRenderStyles[STYLE_Normal] )) if (!( playermo->RenderStyle == LegacyRenderStyles[STYLE_Normal] ))

View file

@ -335,7 +335,7 @@ void GLSprite::Draw(int pass)
if (gl_lights && GLRenderer->mLightCount && mDrawer->FixedColormap == CM_DEFAULT && !fullbright) if (gl_lights && GLRenderer->mLightCount && mDrawer->FixedColormap == CM_DEFAULT && !fullbright)
{ {
if (modelframe && !particle) if (modelframe && !particle)
gl_SetDynModelLight(gl_light_sprites ? actor : NULL, false); gl_SetDynModelLight(gl_light_sprites ? actor : NULL);
else else
gl_SetDynSpriteLight(gl_light_sprites ? actor : NULL, gl_light_particles ? particle : NULL); gl_SetDynSpriteLight(gl_light_sprites ? actor : NULL, gl_light_particles ? particle : NULL);
} }

View file

@ -177,7 +177,7 @@ void BSPWalkCircle(float x, float y, float radiusSquared, const Callback &callba
BSPNodeWalkCircle(level.HeadNode(), x, y, radiusSquared, callback); BSPNodeWalkCircle(level.HeadNode(), x, y, radiusSquared, callback);
} }
void gl_SetDynModelLight(AActor *self, bool hudmodel) void gl_SetDynModelLight(AActor *self)
{ {
// Legacy and deferred render paths gets the old flat model light // Legacy and deferred render paths gets the old flat model light
if (gl.lightmethod != LM_DIRECT) if (gl.lightmethod != LM_DIRECT)
@ -218,7 +218,7 @@ void gl_SetDynModelLight(AActor *self, bool hudmodel)
{ {
if (std::find(addedLights.begin(), addedLights.end(), light) == addedLights.end()) // Check if we already added this light from a different subsector if (std::find(addedLights.begin(), addedLights.end(), light) == addedLights.end()) // Check if we already added this light from a different subsector
{ {
gl_AddLightToList(group, light, modellightdata, hudmodel); gl_AddLightToList(group, light, modellightdata);
addedLights.push_back(light); addedLights.push_back(light);
} }
} }

View file

@ -422,6 +422,6 @@ inline float Dist2(float x1,float y1,float x2,float y2)
void gl_SetDynSpriteLight(AActor *self, float x, float y, float z, subsector_t *subsec); void gl_SetDynSpriteLight(AActor *self, float x, float y, float z, subsector_t *subsec);
void gl_SetDynSpriteLight(AActor *actor, particle_t *particle); void gl_SetDynSpriteLight(AActor *actor, particle_t *particle);
void gl_SetDynModelLight(AActor *self, bool hudmodel); void gl_SetDynModelLight(AActor *self);
#endif #endif

View file

@ -422,7 +422,7 @@ void GLSceneDrawer::DrawPlayerSprites(sector_t * viewsector, bool hudModelStep)
{ {
FSpriteModelFrame *smf = playermo->player->ReadyWeapon ? gl_FindModelFrame(playermo->player->ReadyWeapon->GetClass(), psp->GetState()->sprite, psp->GetState()->GetFrame(), false) : nullptr; FSpriteModelFrame *smf = playermo->player->ReadyWeapon ? gl_FindModelFrame(playermo->player->ReadyWeapon->GetClass(), psp->GetState()->sprite, psp->GetState()->GetFrame(), false) : nullptr;
if (smf) if (smf)
gl_SetDynModelLight(playermo, true); gl_SetDynModelLight(playermo);
else else
gl_SetDynSpriteLight(playermo, NULL); gl_SetDynSpriteLight(playermo, NULL);
} }