mirror of
https://github.com/AquariaOSE/Aquaria.git
synced 2025-07-03 14:34:34 +00:00
rework Web to use VBO
This commit is contained in:
parent
b3d67d286a
commit
c993b49293
2 changed files with 46 additions and 12 deletions
|
@ -26,13 +26,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|||
|
||||
Web::Webs Web::webs;
|
||||
|
||||
Web::Web() : RenderObject()
|
||||
Web::Web()
|
||||
: RenderObject()
|
||||
, vbo(GPUBUF_VERTEXBUF | GPUBUF_DYNAMIC)
|
||||
{
|
||||
addType(SCO_WEB);
|
||||
webs.push_back(this);
|
||||
cull = false;
|
||||
parentEntity = 0;
|
||||
existence = 0;
|
||||
dirtyvbo = false;
|
||||
}
|
||||
|
||||
void Web::setParentEntity(Entity *e)
|
||||
|
@ -64,6 +67,7 @@ void Web::setExistence(float t)
|
|||
size_t Web::addPoint(const Vector &point)
|
||||
{
|
||||
points.push_back(point);
|
||||
dirtyvbo = true;
|
||||
return points.size()-1;
|
||||
}
|
||||
|
||||
|
@ -71,6 +75,7 @@ void Web::setPoint(size_t pt, const Vector &v)
|
|||
{
|
||||
if (pt >= points.size()) return;
|
||||
points[pt] = v;
|
||||
dirtyvbo = true;
|
||||
}
|
||||
|
||||
Vector Web::getPoint(size_t pt) const
|
||||
|
@ -90,6 +95,14 @@ void Web::onUpdate(float dt)
|
|||
{
|
||||
RenderObject::onUpdate(dt);
|
||||
|
||||
// Web is much more unpredictably updated than most other objects,
|
||||
// so it's guarded behind a dirty flag instead of doing it on every change.
|
||||
if(dirtyvbo)
|
||||
{
|
||||
dirtyvbo = false;
|
||||
updateVBO();
|
||||
}
|
||||
|
||||
if (!game->isPaused())
|
||||
{
|
||||
if (existence > 0)
|
||||
|
@ -135,21 +148,38 @@ void Web::onUpdate(float dt)
|
|||
}
|
||||
}
|
||||
|
||||
void Web::updateVBO()
|
||||
{
|
||||
const size_t N = points.size();
|
||||
if(!N)
|
||||
return;
|
||||
const size_t bytes = N * 2 * sizeof(float);
|
||||
|
||||
do
|
||||
{
|
||||
float *p = (float*)vbo.beginWrite(GPUBUFTYPE_VEC2, bytes, GPUACCESS_DEFAULT);
|
||||
|
||||
for (size_t i = 0; i < N; i++)
|
||||
{
|
||||
*p++ = points[i].x;
|
||||
*p++ = points[i].y;
|
||||
}
|
||||
}
|
||||
while(!vbo.commitWrite());
|
||||
}
|
||||
|
||||
void Web::onRender(const RenderState& rs) const
|
||||
{
|
||||
size_t N = points.size();
|
||||
if(!N)
|
||||
return;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
RenderObject::lastTextureApplied = 0;
|
||||
|
||||
glLineWidth(4);
|
||||
glColor4f(1, 1, 1, 0.5f*alpha.x);
|
||||
|
||||
glBegin(GL_LINES);
|
||||
if(points.size() > 0) {
|
||||
for (size_t i = 0; i < points.size()-1; i++) {
|
||||
glColor4f(1, 1, 1, 0.5f*alpha.x);
|
||||
glVertex3f(points[i].x, points[i].y, 0);
|
||||
glColor4f(1, 1, 1, 0.5f*alpha.x);
|
||||
glVertex3f(points[i+1].x, points[i+1].y, 0);
|
||||
}
|
||||
}
|
||||
glEnd();
|
||||
vbo.apply();
|
||||
glDrawArrays(GL_LINE_STRIP, 0, N);
|
||||
}
|
||||
|
|
|
@ -21,7 +21,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|||
#ifndef AQ_WEB_H
|
||||
#define AQ_WEB_H
|
||||
|
||||
#include "../BBGE/Quad.h"
|
||||
#include "RenderObject.h"
|
||||
#include "VertexBuffer.h"
|
||||
|
||||
class Entity;
|
||||
|
||||
|
@ -45,6 +46,9 @@ protected:
|
|||
std::vector<Vector> points;
|
||||
void onUpdate(float dt) OVERRIDE;
|
||||
void onRender(const RenderState& rs) const OVERRIDE;
|
||||
void updateVBO();
|
||||
bool dirtyvbo;
|
||||
DynamicGPUBuffer vbo;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue