1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-07-03 22:44:32 +00:00

rework Web to use VBO

This commit is contained in:
fgenesis 2025-03-05 03:31:46 +01:00
parent b3d67d286a
commit c993b49293
2 changed files with 46 additions and 12 deletions

View file

@ -26,13 +26,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Web::Webs Web::webs; Web::Webs Web::webs;
Web::Web() : RenderObject() Web::Web()
: RenderObject()
, vbo(GPUBUF_VERTEXBUF | GPUBUF_DYNAMIC)
{ {
addType(SCO_WEB); addType(SCO_WEB);
webs.push_back(this); webs.push_back(this);
cull = false; cull = false;
parentEntity = 0; parentEntity = 0;
existence = 0; existence = 0;
dirtyvbo = false;
} }
void Web::setParentEntity(Entity *e) void Web::setParentEntity(Entity *e)
@ -64,6 +67,7 @@ void Web::setExistence(float t)
size_t Web::addPoint(const Vector &point) size_t Web::addPoint(const Vector &point)
{ {
points.push_back(point); points.push_back(point);
dirtyvbo = true;
return points.size()-1; return points.size()-1;
} }
@ -71,6 +75,7 @@ void Web::setPoint(size_t pt, const Vector &v)
{ {
if (pt >= points.size()) return; if (pt >= points.size()) return;
points[pt] = v; points[pt] = v;
dirtyvbo = true;
} }
Vector Web::getPoint(size_t pt) const Vector Web::getPoint(size_t pt) const
@ -90,6 +95,14 @@ void Web::onUpdate(float dt)
{ {
RenderObject::onUpdate(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 (!game->isPaused())
{ {
if (existence > 0) 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 void Web::onRender(const RenderState& rs) const
{ {
size_t N = points.size();
if(!N)
return;
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
RenderObject::lastTextureApplied = 0; RenderObject::lastTextureApplied = 0;
glLineWidth(4); glLineWidth(4);
glColor4f(1, 1, 1, 0.5f*alpha.x);
glBegin(GL_LINES); vbo.apply();
if(points.size() > 0) { glDrawArrays(GL_LINE_STRIP, 0, N);
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();
} }

View file

@ -21,7 +21,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef AQ_WEB_H #ifndef AQ_WEB_H
#define AQ_WEB_H #define AQ_WEB_H
#include "../BBGE/Quad.h" #include "RenderObject.h"
#include "VertexBuffer.h"
class Entity; class Entity;
@ -45,6 +46,9 @@ protected:
std::vector<Vector> points; std::vector<Vector> points;
void onUpdate(float dt) OVERRIDE; void onUpdate(float dt) OVERRIDE;
void onRender(const RenderState& rs) const OVERRIDE; void onRender(const RenderState& rs) const OVERRIDE;
void updateVBO();
bool dirtyvbo;
DynamicGPUBuffer vbo;
}; };
#endif #endif