Skip to content

Commit

Permalink
Added awesome shield effect
Browse files Browse the repository at this point in the history
  • Loading branch information
Monsterovich committed Oct 27, 2024
1 parent 0a3801f commit bd7a485
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 13 deletions.
22 changes: 20 additions & 2 deletions data/base/shaders/tcmask_instanced.frag
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ uniform int tcmask; // whether a tcmask texture exists for the model
uniform int normalmap; // whether a normal map exists for the model
uniform int specularmap; // whether a specular map exists for the model
uniform int hasTangents; // whether tangents were calculated for model
uniform int shieldEffect;
uniform float graphicsCycle; // a periodically cycling value for special effects

uniform vec4 sceneColor; //emissive light
Expand Down Expand Up @@ -74,6 +75,10 @@ out vec4 FragColor;
#include "pointlights.frag"
#endif

float random(vec2 uv) {
return fract(sin(dot(uv.xy, vec2(12.9898, 78.233))) * 43758.5453123);
}

float getShadowMapDepthComp(vec2 base_uv, float u, float v, vec2 shadowMapSizeInv, int cascadeIndex, float z)
{
vec2 uv = base_uv + vec2(u, v) * shadowMapSizeInv;
Expand Down Expand Up @@ -418,8 +423,21 @@ void main()
}

#ifdef NEWGL
FragColor = fragColour;
if (shieldEffect == 1) {
float cycle = 0.66 + 0.66 * graphicsCycle;
vec3 col = vec3(random(vec2(fragColour.x * cycle, fragColour.y * cycle)));
col.b *= 1.5;
FragColor = vec4(col, fragColour.a / 6.0);
} else {
FragColor = fragColour;
}
#else
gl_FragColor = fragColour;
if (shieldEffect == 1) {
vec3 col = vec3(random(vec2(fragColour.x * cycle, fragColour.y * cycle)));
col.b *= 1.5;
gl_FragColor = vec4(col, fragColour.a / 6.0);
} else {
gl_FragColor = fragColour;
}
#endif
}
1 change: 1 addition & 0 deletions lib/ivis_opengl/gfx_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ namespace gfx_api
int normalMap;
int specularMap;
int hasTangents;
int shieldEffect;
};

// interleaved vertex data
Expand Down
5 changes: 3 additions & 2 deletions lib/ivis_opengl/gfx_api_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ static const std::map<SHADER_MODE, program_data> shader_to_file_table =
// per-frame global uniforms
"ProjectionMatrix", "ViewMatrix", "ModelUVLightmapMatrix", "ShadowMapMVPMatrix", "lightPosition", "sceneColor", "ambient", "diffuse", "specular", "fogColor", "ShadowMapCascadeSplits", "ShadowMapSize", "fogEnd", "fogStart", "graphicsCycle", "fogEnabled", "PointLightsPosition", "PointLightsColorAndEnergy", "bucketOffsetAndSize", "PointLightsIndex", "bucketDimensionUsed", "viewportWidth", "viewportHeight",
// per-mesh uniforms
"tcmask", "normalmap", "specularmap", "hasTangents"
"tcmask", "normalmap", "specularmap", "hasTangents", "shieldEffect",
},
{
{"shadowMap", 4},
Expand All @@ -860,7 +860,7 @@ static const std::map<SHADER_MODE, program_data> shader_to_file_table =
// per-frame global uniforms
"ProjectionMatrix", "ViewMatrix", "ModelUVLightmapMatrix", "ShadowMapMVPMatrix", "lightPosition", "sceneColor", "ambient", "diffuse", "specular", "fogColor", "ShadowMapCascadeSplits", "ShadowMapSize", "fogEnd", "fogStart", "graphicsCycle", "fogEnabled", "PointLightsPosition", "PointLightsColorAndEnergy", "bucketOffsetAndSize", "PointLightsIndex", "bucketDimensionUsed", "viewportWidth", "viewportHeight",
// per-mesh uniforms
"tcmask", "normalmap", "specularmap", "hasTangents",
"tcmask", "normalmap", "specularmap", "hasTangents", "shieldEffect",
},
{
{"shadowMap", 4}
Expand Down Expand Up @@ -2123,6 +2123,7 @@ void gl_pipeline_state_object::set_constants(const gfx_api::Draw3DShapeInstanced
setUniforms(24, cbuf.normalMap);
setUniforms(25, cbuf.specularMap);
setUniforms(26, cbuf.hasTangents);
setUniforms(27, cbuf.shieldEffect);
}

void gl_pipeline_state_object::set_constants(const gfx_api::Draw3DShapeInstancedDepthOnlyGlobalUniforms& cbuf)
Expand Down
24 changes: 15 additions & 9 deletions lib/ivis_opengl/piedraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,10 @@ bool InstancedMeshRenderer::Draw3DShape(iIMDShape *shape, int frame, PIELIGHT te
tshape.stretch = stretchDepth;
tshape.modelMatrix = modelMatrix;

if (pieFlag & pie_SHIELD) {
tshape.modelMatrix = glm::scale(tshape.modelMatrix, glm::vec3(pie_SHIELD_FACTOR, pie_SHIELD_FACTOR, pie_SHIELD_FACTOR));
}

if (pieFlag & pie_HEIGHT_SCALED) // construct
{
tshape.modelMatrix = glm::scale(tshape.modelMatrix, glm::vec3(1.0f, (float)pieFlagData / (float)pie_RAISE_SCALE, 1.0f));
Expand Down Expand Up @@ -1463,15 +1467,15 @@ bool InstancedMeshRenderer::DrawAll(uint64_t currentGameFrame, const glm::mat4&
}

template<SHADER_MODE shader, typename Draw3DInstancedPSO>
static void drawInstanced3dShapeTemplated_Inner(ShaderOnce& globalsOnce, const gfx_api::Draw3DShapeInstancedGlobalUniforms& globalUniforms, const iIMDShape * shape, gfx_api::buffer* instanceDataBuffer, size_t instanceBufferOffset, size_t instance_count, gfx_api::texture* lightmapTexture)
static void drawInstanced3dShapeTemplated_Inner(ShaderOnce& globalsOnce, const gfx_api::Draw3DShapeInstancedGlobalUniforms& globalUniforms, const iIMDShape * shape, gfx_api::buffer* instanceDataBuffer, size_t instanceBufferOffset, size_t instance_count, gfx_api::texture* lightmapTexture, bool shieldEffect)
{
const auto& textures = shape->getTextures();
auto* tcmask = textures.tcmaskpage != iV_TEX_INVALID ? &pie_Texture(textures.tcmaskpage) : nullptr;
auto* normalmap = textures.normalpage != iV_TEX_INVALID ? &pie_Texture(textures.normalpage) : nullptr;
auto* specularmap = textures.specularpage != iV_TEX_INVALID ? &pie_Texture(textures.specularpage) : nullptr;

gfx_api::Draw3DShapeInstancedPerMeshUniforms meshUniforms {
tcmask ? 1 : 0, normalmap != nullptr, specularmap != nullptr, shape->buffers[VBO_TANGENT] != nullptr
tcmask ? 1 : 0, normalmap != nullptr, specularmap != nullptr, shape->buffers[VBO_TANGENT] != nullptr, shieldEffect ? 1 : 0
};

gfx_api::buffer* pTangentBuffer = (shape->buffers[VBO_TANGENT] != nullptr) ? shape->buffers[VBO_TANGENT] : getZeroedVertexBuffer(shape->vertexCount * 4 * sizeof(gfx_api::gfxFloat));
Expand Down Expand Up @@ -1525,43 +1529,45 @@ static void drawInstanced3dShapeDepthOnly(ShaderOnce& globalsOnce, const gfx_api
template<SHADER_MODE shader, typename AdditivePSO, typename AdditiveNoDepthWRTPSO, typename AlphaPSO, typename AlphaNoDepthWRTPSO, typename PremultipliedPSO, typename PremultipliedNoDepthWRTPSO, typename OpaquePSO>
static void drawInstanced3dShapeTemplated(ShaderOnce& globalsOnce, const gfx_api::Draw3DShapeInstancedGlobalUniforms& globalUniforms, const iIMDShape * shape, int pieFlag, gfx_api::buffer* instanceDataBuffer, size_t instanceBufferOffset, size_t instance_count, gfx_api::texture* lightmapTexture)
{
bool shieldEffect = pieFlag & pie_SHIELD;

/* Set tranlucency */
if (pieFlag & pie_ADDITIVE)
{
if (!(pieFlag & pie_NODEPTHWRITE))
{
return drawInstanced3dShapeTemplated_Inner<shader, AdditivePSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture);
return drawInstanced3dShapeTemplated_Inner<shader, AdditivePSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture, shieldEffect);
}
else
{
return drawInstanced3dShapeTemplated_Inner<shader, AdditiveNoDepthWRTPSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture);
return drawInstanced3dShapeTemplated_Inner<shader, AdditiveNoDepthWRTPSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture, shieldEffect);
}
}
else if (pieFlag & pie_TRANSLUCENT)
{
if (!(pieFlag & pie_NODEPTHWRITE))
{
return drawInstanced3dShapeTemplated_Inner<shader, AlphaPSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture);
return drawInstanced3dShapeTemplated_Inner<shader, AlphaPSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture, shieldEffect);
}
else
{
return drawInstanced3dShapeTemplated_Inner<shader, AlphaNoDepthWRTPSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture);
return drawInstanced3dShapeTemplated_Inner<shader, AlphaNoDepthWRTPSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture, shieldEffect);
}
}
else if (pieFlag & pie_PREMULTIPLIED)
{
if (!(pieFlag & pie_NODEPTHWRITE))
{
return drawInstanced3dShapeTemplated_Inner<shader, PremultipliedPSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture);
return drawInstanced3dShapeTemplated_Inner<shader, PremultipliedPSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture, shieldEffect);
}
else
{
return drawInstanced3dShapeTemplated_Inner<shader, PremultipliedNoDepthWRTPSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture);
return drawInstanced3dShapeTemplated_Inner<shader, PremultipliedNoDepthWRTPSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture, shieldEffect);
}
}
else
{
return drawInstanced3dShapeTemplated_Inner<shader, OpaquePSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture);
return drawInstanced3dShapeTemplated_Inner<shader, OpaquePSO>(globalsOnce, globalUniforms, shape, instanceDataBuffer, instanceBufferOffset, instance_count, lightmapTexture, shieldEffect);
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/ivis_opengl/pietypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ using nonstd::nullopt;
#define pie_PREMULTIPLIED 0x200
#define pie_NODEPTHWRITE 0x400
#define pie_FORCELIGHT 0x800
#define pie_SHIELD 0x1000

#define pie_RAISE_SCALE 256
#define pie_SHIELD_FACTOR 1.125f

enum LIGHTING_TYPE
{
Expand Down
37 changes: 37 additions & 0 deletions src/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,18 @@ static bool displayCompObj(DROID *psDroid, bool bButton, const glm::mat4& modelM
SDWORD iConnector;
PROPULSION_STATS *psPropStats;
SDWORD pieFlag, iPieData;
SDWORD shieldPieFlag, iShieldPieData;
PIELIGHT brightness;
UDWORD colour;
size_t i = 0;
bool didDrawSomething = false;

if (!bButton && psDroid->shieldPoints > 0) {
double factor = static_cast<double>(psDroid->shieldPoints) / droidGetMaxShieldPoints(psDroid);
iShieldPieData = std::round(255.0f * factor);

Check warning on line 438 in src/component.cpp

View workflow job for this annotation

GitHub Actions / Fedora :LATEST [GCC]

conversion from 'double' to 'SDWORD' {aka 'int'} may change value [-Wfloat-conversion]

Check warning on line 438 in src/component.cpp

View workflow job for this annotation

GitHub Actions / Arch :LATEST [Clang]

implicit conversion turns floating-point number into integer: 'double' to 'SDWORD' (aka 'int') [-Wfloat-conversion]

Check warning on line 438 in src/component.cpp

View workflow job for this annotation

GitHub Actions / Arch :LATEST [GCC]

conversion from ‘double’ to ‘SDWORD’ {aka ‘int’} may change value [-Wfloat-conversion]

Check warning on line 438 in src/component.cpp

View workflow job for this annotation

GitHub Actions / Fedora :LATEST [GCC -m32]

conversion from 'double' to 'SDWORD' {aka 'int'} may change value [-Wfloat-conversion]

Check warning on line 438 in src/component.cpp

View workflow job for this annotation

GitHub Actions / Ubuntu 22.04 [Clang]

implicit conversion turns floating-point number into integer: 'double' to 'SDWORD' (aka 'int') [-Wfloat-conversion]

Check warning on line 438 in src/component.cpp

View workflow job for this annotation

GitHub Actions / Ubuntu 24.04 [Clang]

implicit conversion turns floating-point number into integer: 'double' to 'SDWORD' (aka 'int') [-Wfloat-conversion]
shieldPieFlag = pie_FORCELIGHT | pie_TRANSLUCENT | pie_SHIELD;
}

glm::mat4 modifiedModelMatrix = modelMatrix2;

if (psDroid->timeLastHit - graphicsTime < ELEC_DAMAGE_DURATION && psDroid->lastHitWeapon == WSC_ELECTRONIC && !gamePaused())
Expand Down Expand Up @@ -492,6 +499,12 @@ static bool displayCompObj(DROID *psDroid, bool bButton, const glm::mat4& modelM
{
didDrawSomething = true;
}
if (!bButton && psDroid->shieldPoints > 0) {
if (pie_Draw3DShape(psShapeProp->displayModel(), 0, colour, brightness, shieldPieFlag, iShieldPieData, modifiedModelMatrix, viewMatrix, -(psDroid->heightAboveMap)))

Check failure on line 503 in src/component.cpp

View workflow job for this annotation

GitHub Actions / Fedora :LATEST [GCC]

'iShieldPieData' may be used uninitialized [-Werror=maybe-uninitialized]

Check failure on line 503 in src/component.cpp

View workflow job for this annotation

GitHub Actions / Arch :LATEST [GCC]

‘iShieldPieData’ may be used uninitialized [-Werror=maybe-uninitialized]

Check failure on line 503 in src/component.cpp

View workflow job for this annotation

GitHub Actions / Fedora :LATEST [GCC -m32]

'iShieldPieData' may be used uninitialized [-Werror=maybe-uninitialized]

Check failure on line 503 in src/component.cpp

View workflow job for this annotation

GitHub Actions / Alpine :LATEST [GCC]

'iShieldPieData' may be used uninitialized [-Werror=maybe-uninitialized]
{
didDrawSomething = true;
}
}
}

/* set default components transparent */
Expand Down Expand Up @@ -526,6 +539,12 @@ static bool displayCompObj(DROID *psDroid, bool bButton, const glm::mat4& modelM
{
didDrawSomething = true;
}
if (!bButton && psDroid->shieldPoints > 0) {
if (drawShape(strImd, psDroid->timeAnimationStarted, colour, brightness, shieldPieFlag, iShieldPieData, modifiedModelMatrix, viewMatrix, -(psDroid->heightAboveMap)))

Check failure on line 543 in src/component.cpp

View workflow job for this annotation

GitHub Actions / Ubuntu 22.04 [GCC]

'iShieldPieData' may be used uninitialized in this function [-Werror=maybe-uninitialized]

Check failure on line 543 in src/component.cpp

View workflow job for this annotation

GitHub Actions / Ubuntu 20.04 [GCC]

'iShieldPieData' may be used uninitialized in this function [-Werror=maybe-uninitialized]
{
didDrawSomething = true;
}
}
strImd = strImd->next.get();
}
}
Expand Down Expand Up @@ -640,6 +659,12 @@ static bool displayCompObj(DROID *psDroid, bool bButton, const glm::mat4& modelM
{
didDrawSomething = true;
}
if (!bButton && psDroid->shieldPoints > 0) {
if (pie_Draw3DShape(psShape, 0, colour, brightness, shieldPieFlag, iShieldPieData, localModelMatrix, viewMatrix, -localHeightAboveTerrain))
{
didDrawSomething = true;
}
}
}
localModelMatrix *= glm::translate(glm::vec3(0, 0, recoilValue));

Expand Down Expand Up @@ -673,6 +698,12 @@ static bool displayCompObj(DROID *psDroid, bool bButton, const glm::mat4& modelM
{
didDrawSomething = true;
}
if (!bButton && psDroid->shieldPoints > 0) {
if (pie_Draw3DShape(psShape, 0, colour, brightness, shieldPieFlag, iShieldPieData, localModelMatrix, viewMatrix, -localHeightAboveTerrain))
{
didDrawSomething = true;
}
}
auto flashBaseModel = MUZZLE_FLASH_PIE(psDroid, i);
iIMDShape *pMuzzleFlash = (flashBaseModel) ? flashBaseModel->displayModel() : nullptr;
drawMuzzleFlash(psDroid->asWeaps[i], psShape, pMuzzleFlash, brightness, pieFlag, iPieData, localModelMatrix, viewMatrix, localHeightAboveTerrain);
Expand Down Expand Up @@ -827,6 +858,12 @@ static bool displayCompObj(DROID *psDroid, bool bButton, const glm::mat4& modelM
{
didDrawSomething = true;
}
if (!bButton && psDroid->shieldPoints > 0) {
if (pie_Draw3DShape(psShapeProp->displayModel(), 0, colour, brightness, shieldPieFlag, iShieldPieData, modifiedModelMatrix, viewMatrix, -(psDroid->heightAboveMap)))
{
didDrawSomething = true;
}
}
}

return didDrawSomething;
Expand Down

0 comments on commit bd7a485

Please sign in to comment.