mirror of
https://github.com/AquariaOSE/Aquaria.git
synced 2024-12-01 15:35:47 +00:00
Cleanups and fixes to AfterEffect
- fully uses RenderGrid now, no more immediate mode - remove RippleEffect because it was unused - Don't render the grid if no effects are active. Sames some GPU time. - Fix ShockEffect math, broken 2 commits ago
This commit is contained in:
parent
2f7dd87d8b
commit
cc78b300cc
4 changed files with 57 additions and 89 deletions
|
@ -35,7 +35,6 @@ AfterEffectManager::AfterEffectManager(int xDivs, int yDivs)
|
||||||
{
|
{
|
||||||
active = false;
|
active = false;
|
||||||
numEffects = 0;
|
numEffects = 0;
|
||||||
bRenderGridPoints = true;
|
|
||||||
shaderPipeline.resize(10, 0);
|
shaderPipeline.resize(10, 0);
|
||||||
|
|
||||||
this->xDivs = xDivs;
|
this->xDivs = xDivs;
|
||||||
|
@ -105,10 +104,15 @@ void AfterEffectManager::update(float dt)
|
||||||
{
|
{
|
||||||
if (core->particlesPaused) return;
|
if (core->particlesPaused) return;
|
||||||
|
|
||||||
|
const size_t N = effects.size();
|
||||||
|
|
||||||
|
if(!N && !active)
|
||||||
|
return;
|
||||||
|
|
||||||
resetGrid();
|
resetGrid();
|
||||||
|
|
||||||
bool isactive = false;
|
bool isactive = false;
|
||||||
for (size_t i = 0; i < effects.size(); i++)
|
for (size_t i = 0; i < N; i++)
|
||||||
{
|
{
|
||||||
Effect *e = effects[i];
|
Effect *e = effects[i];
|
||||||
if (e)
|
if (e)
|
||||||
|
@ -132,20 +136,7 @@ void AfterEffectManager::update(float dt)
|
||||||
|
|
||||||
void AfterEffectManager::resetGrid()
|
void AfterEffectManager::resetGrid()
|
||||||
{
|
{
|
||||||
Array2d<Vector>& a = grid.array2d();
|
grid.reset01();
|
||||||
const float mx = 1.0f / (float)(xDivs-1);
|
|
||||||
const float my = 1.0f / (float)(yDivs-1);
|
|
||||||
for (int y = 0; y < yDivs; y++)
|
|
||||||
{
|
|
||||||
Vector *row = a.row(y);
|
|
||||||
const float yy = y * my;
|
|
||||||
for (int x = 0; x < xDivs; x++)
|
|
||||||
{
|
|
||||||
row[x].x = x*mx;
|
|
||||||
row[x].y = yy;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
grid.needVBOUpdate = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AfterEffectManager::destroyEffect(int id)
|
void AfterEffectManager::destroyEffect(int id)
|
||||||
|
@ -212,7 +203,13 @@ void AfterEffectManager::renderGrid(const RenderState& rs) const
|
||||||
glTranslatef(offx, offy, 0);
|
glTranslatef(offx, offy, 0);
|
||||||
glScalef(vw, vh, 1);
|
glScalef(vw, vh, 1);
|
||||||
|
|
||||||
|
if(active)
|
||||||
|
{
|
||||||
grid.render(rs);
|
grid.render(rs);
|
||||||
|
//renderGridPoints(rs);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
blitQuad.render(rs);
|
||||||
|
|
||||||
if (activeShader)
|
if (activeShader)
|
||||||
activeShader->unbind();
|
activeShader->unbind();
|
||||||
|
@ -246,19 +243,7 @@ void AfterEffectManager::renderGrid(const RenderState& rs) const
|
||||||
activeShader->bind();
|
activeShader->bind();
|
||||||
activeShader->setInt("tex", 0);
|
activeShader->setInt("tex", 0);
|
||||||
|
|
||||||
//blitQuad.render(rs);
|
blitQuad.render(rs);
|
||||||
|
|
||||||
// note that offx, offy are negative here!
|
|
||||||
glBegin(GL_QUADS);
|
|
||||||
glTexCoord2f(0.0f, 0.0f);
|
|
||||||
glVertex3f(offx, vh+offy, 0.0f);
|
|
||||||
glTexCoord2f(percentX, 0.0f);
|
|
||||||
glVertex3f( vw+offx, vh+offy, 0.0f);
|
|
||||||
glTexCoord2f(percentX, percentY);
|
|
||||||
glVertex3f( vw+offx, offy, 0.0f);
|
|
||||||
glTexCoord2f(0.0f, percentY);
|
|
||||||
glVertex3f(offx, offy, 0.0f);
|
|
||||||
glEnd();
|
|
||||||
|
|
||||||
activeShader->unbind();
|
activeShader->unbind();
|
||||||
}
|
}
|
||||||
|
@ -266,9 +251,6 @@ void AfterEffectManager::renderGrid(const RenderState& rs) const
|
||||||
|
|
||||||
RenderObject::lastTextureApplied = 0;
|
RenderObject::lastTextureApplied = 0;
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
//if (bRenderGridPoints)
|
|
||||||
renderGridPoints(rs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AfterEffectManager::renderGridPoints(const RenderState& rs) const
|
void AfterEffectManager::renderGridPoints(const RenderState& rs) const
|
||||||
|
@ -280,6 +262,7 @@ void AfterEffectManager::unloadDevice()
|
||||||
{
|
{
|
||||||
backupBuffer.unloadDevice();
|
backupBuffer.unloadDevice();
|
||||||
grid.dropBuffers();
|
grid.dropBuffers();
|
||||||
|
blitQuad.dropBuffers();
|
||||||
unloadShaders();
|
unloadShaders();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -372,7 +355,7 @@ void ShockEffect::update(float dt, Array2d<Vector>& grid, int xDivs, int yDivs)
|
||||||
const float adjWaveLength = waveLength/distFromCamp;
|
const float adjWaveLength = waveLength/distFromCamp;
|
||||||
const float adjAmplitude = amplitude/distFromCamp;
|
const float adjAmplitude = amplitude/distFromCamp;
|
||||||
|
|
||||||
const float m = -1.0f / (adjWaveLength+currentDistance);
|
const float invAdjVaveLen = -1.0f / adjWaveLength;
|
||||||
const float dist = currentDistance*adjWaveLength;
|
const float dist = currentDistance*adjWaveLength;
|
||||||
|
|
||||||
if (amplitude < 0)
|
if (amplitude < 0)
|
||||||
|
@ -383,14 +366,14 @@ void ShockEffect::update(float dt, Array2d<Vector>& grid, int xDivs, int yDivs)
|
||||||
Vector *row = grid.row(y);
|
Vector *row = grid.row(y);
|
||||||
for (int x = 1; x < (xDivs-1); x++)
|
for (int x = 1; x < (xDivs-1); x++)
|
||||||
{
|
{
|
||||||
float xDist = (centerPoint.x - row[x].x)/.75f;
|
float xDist = (centerPoint.x - row[x].x)/.75f; // factor for 4:3 internal resolution
|
||||||
float yDist = centerPoint.y - row[x].y;
|
float yDist = centerPoint.y - row[x].y;
|
||||||
|
|
||||||
float tDist = sqrtf(xDist*xDist+yDist*yDist);
|
float tDist = sqrtf(xDist*xDist+yDist*yDist);
|
||||||
|
|
||||||
if (tDist < dist)
|
if (tDist < dist)
|
||||||
{
|
{
|
||||||
const float a = tDist * m;
|
const float a = tDist * invAdjVaveLen + currentDistance;
|
||||||
row[x].x += adjAmplitude*sinf(a)*.75f;
|
row[x].x += adjAmplitude*sinf(a)*.75f;
|
||||||
row[x].y += adjAmplitude*cosf(a);
|
row[x].y += adjAmplitude*cosf(a);
|
||||||
}
|
}
|
||||||
|
@ -398,35 +381,6 @@ void ShockEffect::update(float dt, Array2d<Vector>& grid, int xDivs, int yDivs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
RippleEffect::RippleEffect() : Effect()
|
|
||||||
{
|
|
||||||
time = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void RippleEffect::update(float dt, Array2d<Vector>& grid, int xDivs, int yDivs)
|
|
||||||
{
|
|
||||||
const float offx = (core->screenCenter.x/float(core->width)/2);
|
|
||||||
const float offy = (core->screenCenter.y/float(core->height)/2);
|
|
||||||
const float invx = 1.0f / float(xDivs);
|
|
||||||
time += dt*0.5f;
|
|
||||||
float amp = 0.002f;
|
|
||||||
|
|
||||||
for (int y = 0; y < (yDivs-1); y++)
|
|
||||||
{
|
|
||||||
float offbase = y*invx + offx + offy;
|
|
||||||
Vector *row = grid.row(y);
|
|
||||||
for (int x = 0; x < (xDivs-1); x++)
|
|
||||||
{
|
|
||||||
float offset = x*invx + offbase;
|
|
||||||
float a = (time+offset)*7.5f;
|
|
||||||
|
|
||||||
row[x].x += sinf(a)*(amp*0.5f);
|
|
||||||
row[x].y += cosf(a)*amp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int AfterEffectManager::loadShaderFile(const char *vert, const char *frag)
|
int AfterEffectManager::loadShaderFile(const char *vert, const char *frag)
|
||||||
{
|
{
|
||||||
Shader *sh = new Shader();
|
Shader *sh = new Shader();
|
||||||
|
@ -495,6 +449,8 @@ void AfterEffectManager::_initGrid()
|
||||||
grid.dropBuffers();
|
grid.dropBuffers();
|
||||||
|
|
||||||
blitQuad.init(2, 2);
|
blitQuad.init(2, 2);
|
||||||
|
blitQuad.reset01();
|
||||||
|
blitQuad.updateVBO(); // never changed afterwards
|
||||||
}
|
}
|
||||||
|
|
||||||
void AfterEffectManager::deleteShader(int handle)
|
void AfterEffectManager::deleteShader(int handle)
|
||||||
|
|
|
@ -66,14 +66,6 @@ public:
|
||||||
float currentDistance;
|
float currentDistance;
|
||||||
};
|
};
|
||||||
|
|
||||||
class RippleEffect : public Effect
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
RippleEffect();
|
|
||||||
void update(float dt, Array2d<Vector>& grid, int xDivs, int yDivs) OVERRIDE;
|
|
||||||
float time;
|
|
||||||
};
|
|
||||||
|
|
||||||
class AfterEffectManager
|
class AfterEffectManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -100,20 +92,6 @@ public:
|
||||||
void reloadDevice();
|
void reloadDevice();
|
||||||
void updateDevice();
|
void updateDevice();
|
||||||
|
|
||||||
std::vector<Effect*> effects;
|
|
||||||
std::vector<int> openSpots;
|
|
||||||
|
|
||||||
bool active;
|
|
||||||
|
|
||||||
bool bRenderGridPoints;
|
|
||||||
|
|
||||||
int numEffects;
|
|
||||||
int xDivs, yDivs;
|
|
||||||
int screenWidth, screenHeight;
|
|
||||||
int textureWidth, textureHeight;
|
|
||||||
|
|
||||||
RenderGrid grid, blitQuad;
|
|
||||||
|
|
||||||
// returns handle > 0 on success
|
// returns handle > 0 on success
|
||||||
int loadShaderFile(const char *vert, const char *frag);
|
int loadShaderFile(const char *vert, const char *frag);
|
||||||
int loadShaderSrc(const char *vert, const char *frag);
|
int loadShaderSrc(const char *vert, const char *frag);
|
||||||
|
@ -127,6 +105,14 @@ protected:
|
||||||
int _insertShader(Shader *sh);
|
int _insertShader(Shader *sh);
|
||||||
void _initGrid();
|
void _initGrid();
|
||||||
|
|
||||||
|
RenderGrid grid, blitQuad;
|
||||||
|
bool active;
|
||||||
|
int numEffects;
|
||||||
|
int xDivs, yDivs;
|
||||||
|
int screenWidth, screenHeight;
|
||||||
|
int textureWidth, textureHeight;
|
||||||
|
std::vector<Effect*> effects;
|
||||||
|
std::vector<int> openSpots;
|
||||||
std::vector<Shader*> shaderPipeline; // Shaders are applied in this order. Can contain the same pointer more than once.
|
std::vector<Shader*> shaderPipeline; // Shaders are applied in this order. Can contain the same pointer more than once.
|
||||||
std::vector<Shader*> loadedShaders;
|
std::vector<Shader*> loadedShaders;
|
||||||
FrameBuffer backupBuffer;
|
FrameBuffer backupBuffer;
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#include "RenderState.h"
|
#include "RenderState.h"
|
||||||
|
|
||||||
|
|
||||||
static void ResetGrid(Vector* dst, size_t w, size_t h)
|
static void ResetGridZeroCenter(Vector* dst, size_t w, size_t h)
|
||||||
{
|
{
|
||||||
assert(w > 1 && h > 1);
|
assert(w > 1 && h > 1);
|
||||||
const float xMulF = 1.0f / (float)(w-1);
|
const float xMulF = 1.0f / (float)(w-1);
|
||||||
|
@ -21,6 +21,25 @@ static void ResetGrid(Vector* dst, size_t w, size_t h)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void ResetGrid01(Vector* dst, size_t w, size_t h)
|
||||||
|
{
|
||||||
|
assert(w > 1 && h > 1);
|
||||||
|
const float xMulF = 1.0f / (float)(w-1);
|
||||||
|
const float yMulF = 1.0f / (float)(h-1);
|
||||||
|
|
||||||
|
for (size_t y = 0; y < h; y++)
|
||||||
|
{
|
||||||
|
const float yval = float(y)*yMulF;
|
||||||
|
for (size_t x = 0; x < w; x++)
|
||||||
|
{
|
||||||
|
dst->x = float(x)*xMulF;
|
||||||
|
dst->y = yval;
|
||||||
|
++dst;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void RenderGrid::ResetWithAlpha(Vector* dst, size_t w, size_t h, float alpha)
|
void RenderGrid::ResetWithAlpha(Vector* dst, size_t w, size_t h, float alpha)
|
||||||
{
|
{
|
||||||
assert(w > 1 && h > 1);
|
assert(w > 1 && h > 1);
|
||||||
|
@ -77,10 +96,16 @@ void RenderGrid::init(size_t w, size_t h, const TexCoordBox& tc)
|
||||||
this->init(w, h);
|
this->init(w, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RenderGrid::reset01()
|
||||||
|
{
|
||||||
|
ResetGrid01(grid.data(), grid.width(), grid.height());
|
||||||
|
needVBOUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void RenderGrid::reset()
|
void RenderGrid::reset()
|
||||||
{
|
{
|
||||||
ResetGrid(grid.data(), grid.width(), grid.height());
|
ResetGridZeroCenter(grid.data(), grid.width(), grid.height());
|
||||||
needVBOUpdate = true;
|
needVBOUpdate = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,7 @@ public:
|
||||||
|
|
||||||
void init(size_t w, size_t h);
|
void init(size_t w, size_t h);
|
||||||
void init(size_t w, size_t h, const TexCoordBox& tc);
|
void init(size_t w, size_t h, const TexCoordBox& tc);
|
||||||
|
void reset01();
|
||||||
void reset();
|
void reset();
|
||||||
void resetWithAlpha(float a);
|
void resetWithAlpha(float a);
|
||||||
void render(const RenderState& rs) const;
|
void render(const RenderState& rs) const;
|
||||||
|
|
Loading…
Reference in a new issue