mirror of
https://github.com/AquariaOSE/Aquaria.git
synced 2025-05-11 19:43:50 +00:00
rework Strand to use VBOs
This commit is contained in:
parent
f167ce7167
commit
9cc323b501
4 changed files with 92 additions and 42 deletions
|
@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|||
#define SEGMENTED_H
|
||||
|
||||
#include "../BBGE/Quad.h"
|
||||
#include "VertexBuffer.h"
|
||||
|
||||
class Segmented
|
||||
{
|
||||
|
@ -51,6 +52,8 @@ public:
|
|||
protected:
|
||||
void onUpdate(float dt) OVERRIDE;
|
||||
void onRender(const RenderState& rs) const OVERRIDE;
|
||||
|
||||
DynamicGPUBuffer gpubuf;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -21,12 +21,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|||
#include "Segmented.h"
|
||||
#include "RenderBase.h"
|
||||
|
||||
Strand::Strand(const Vector &position, size_t segs, size_t dist) : RenderObject(), Segmented(dist, dist)
|
||||
Strand::Strand(const Vector &position, size_t segs, size_t dist)
|
||||
: RenderObject(), Segmented(dist, dist)
|
||||
, gpubuf(GPUBUF_DYNAMIC | GPUBUF_VERTEXBUF)
|
||||
{
|
||||
assert(segs);
|
||||
cull = false;
|
||||
segments.resize(segs);
|
||||
for (size_t i = 0; i < segments.size(); i++)
|
||||
{
|
||||
// FIXME: This is super costly to waste an entire RenderObject just to store a position.
|
||||
segments[i] = new RenderObject;
|
||||
}
|
||||
initSegments(position);
|
||||
|
@ -47,46 +51,69 @@ void Strand::onUpdate(float dt)
|
|||
{
|
||||
RenderObject::onUpdate(dt);
|
||||
updateSegments(position);
|
||||
|
||||
const size_t numSegments = segments.size();
|
||||
|
||||
do
|
||||
{
|
||||
const size_t bytes = (numSegments+1) * 6 * sizeof(float);
|
||||
float *p = (float*)gpubuf.beginWrite(GPUBUFTYPE_VEC2_RGBA, bytes, GPUACCESS_DEFAULT);
|
||||
|
||||
// Note: We're rewriting all of the vertex data here.
|
||||
// Ideally we'd only rewrite position data since vertex colors never change,
|
||||
// but currently this is to keep buffer layouts simple.
|
||||
|
||||
const float factor = 1.0f / 50.0f;
|
||||
const size_t colorLimit = numSegments<50 ? numSegments : 50;
|
||||
const Vector falloff = color * factor;
|
||||
const float falloffAlpha = 1.0f / float(numSegments);
|
||||
float a = 1.0f;
|
||||
Vector c = color;
|
||||
|
||||
// initial vertex (this is the +1)
|
||||
*p++ = position.x;
|
||||
*p++ = position.y;
|
||||
|
||||
*p++ = c.x;
|
||||
*p++ = c.y;
|
||||
*p++ = c.z;
|
||||
*p++ = a;
|
||||
|
||||
size_t i;
|
||||
for(i = 0; i < colorLimit; ++i)
|
||||
{
|
||||
*p++ = segments[i]->position.x;
|
||||
*p++ = segments[i]->position.y;
|
||||
|
||||
*p++ = c.x;
|
||||
*p++ = c.y;
|
||||
*p++ = c.z;
|
||||
*p++ = a;
|
||||
|
||||
c -= falloff;
|
||||
a -= falloffAlpha;
|
||||
}
|
||||
for( ; i < numSegments; ++i)
|
||||
{
|
||||
*p++ = segments[i]->position.x;
|
||||
*p++ = segments[i]->position.y;
|
||||
|
||||
*p++ = 0.0f;
|
||||
*p++ = 0.0f;
|
||||
*p++ = 0.0f;
|
||||
*p++ = a;
|
||||
|
||||
a -= falloffAlpha;
|
||||
}
|
||||
}
|
||||
while(!gpubuf.commitWrite());
|
||||
}
|
||||
|
||||
void Strand::onRender(const RenderState& rs) const
|
||||
{
|
||||
const size_t numSegments = segments.size();
|
||||
if (numSegments == 0) return;
|
||||
|
||||
glTranslatef(-position.x, -position.y, 0);
|
||||
glLineWidth(1);
|
||||
|
||||
glBegin(GL_LINE_STRIP);
|
||||
|
||||
|
||||
unsigned int r = (unsigned int)(color.x * (255<<8));
|
||||
unsigned int g = (unsigned int)(color.y * (255<<8));
|
||||
unsigned int b = (unsigned int)(color.z * (255<<8));
|
||||
unsigned int a = (255<<8);
|
||||
unsigned int dr = r/50;
|
||||
unsigned int dg = g/50;
|
||||
unsigned int db = b/50;
|
||||
unsigned int da = a/numSegments;
|
||||
glColor4ub(r>>8, g>>8, b>>8, a>>8);
|
||||
glVertex2f(position.x, position.y);
|
||||
glVertex2f(segments[0]->position.x, segments[0]->position.y);
|
||||
const size_t colorLimit = numSegments<50 ? numSegments : 50;
|
||||
size_t i;
|
||||
for (i = 1; i < colorLimit; i++)
|
||||
{
|
||||
r -= dr;
|
||||
g -= dg;
|
||||
b -= db;
|
||||
a -= da;
|
||||
glColor4ub(r>>8, g>>8, b>>8, a>>8);
|
||||
glVertex2f(segments[i]->position.x, segments[i]->position.y);
|
||||
}
|
||||
for (; i < numSegments; i++)
|
||||
{
|
||||
a -= da;
|
||||
glColor4ub(0, 0, 0, a>>8);
|
||||
glVertex2f(segments[i]->position.x, segments[i]->position.y);
|
||||
}
|
||||
glEnd();
|
||||
gpubuf.apply();
|
||||
glDrawArrays(GL_LINE_STRIP, 0, GLsizei(segments.size() + 1));
|
||||
}
|
||||
|
|
|
@ -13,7 +13,8 @@ static unsigned s_lastState = 0; // StateBits
|
|||
|
||||
enum StateBits
|
||||
{
|
||||
SB_COLOR_FROM_BUFFER = 0x01
|
||||
SB_TC_FROM_BUFFER = 0x01,
|
||||
SB_COLOR_FROM_BUFFER = 0x02
|
||||
};
|
||||
|
||||
static unsigned toGlUsage(unsigned usage)
|
||||
|
@ -115,6 +116,14 @@ bool DynamicGPUBuffer::commitWrite(size_t used)
|
|||
return _commitWrite(used);
|
||||
}
|
||||
|
||||
bool DynamicGPUBuffer::commitWriteExact(const void * p)
|
||||
{
|
||||
const void *origin = _d_map ? _d_map : _h_data;
|
||||
ptrdiff_t d = (const char*)p - (const char*)origin;
|
||||
assert(d == _size);
|
||||
return commitWrite();
|
||||
}
|
||||
|
||||
bool DynamicGPUBuffer::_commitWrite(size_t used)
|
||||
{
|
||||
if(_HasARB)
|
||||
|
@ -210,12 +219,15 @@ void DynamicGPUBuffer::apply(BufDataType usetype) const
|
|||
const unsigned tcoffset = (u >> 16u) & 0xff;
|
||||
const unsigned coloroffset = u >> 24u;
|
||||
|
||||
// vertex and texcoords are always enabled
|
||||
// vertices are always enabled
|
||||
glVertexPointer(scalars, gltype, stride, p);
|
||||
if(tcoffset)
|
||||
glTexCoordPointer(2, gltype, stride, (void*)((uintptr_t)p + tcoffset));
|
||||
|
||||
unsigned wantedstate = 0;
|
||||
if(tcoffset)
|
||||
{
|
||||
wantedstate |= SB_TC_FROM_BUFFER;
|
||||
glTexCoordPointer(2, gltype, stride, (void*)((uintptr_t)p + tcoffset));
|
||||
}
|
||||
if(coloroffset)
|
||||
{
|
||||
wantedstate |= SB_COLOR_FROM_BUFFER;
|
||||
|
@ -225,6 +237,13 @@ void DynamicGPUBuffer::apply(BufDataType usetype) const
|
|||
unsigned wrongbits = wantedstate ^ s_lastState;
|
||||
if(wrongbits)
|
||||
{
|
||||
if(wrongbits & SB_TC_FROM_BUFFER)
|
||||
{
|
||||
if(wantedstate & SB_TC_FROM_BUFFER)
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
else
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
}
|
||||
if(wrongbits & SB_COLOR_FROM_BUFFER)
|
||||
{
|
||||
if(wantedstate & SB_COLOR_FROM_BUFFER)
|
||||
|
|
|
@ -27,9 +27,9 @@ enum BufDataType
|
|||
^^-- offset of colors, if present */
|
||||
GPUBUFTYPE_U16 = 0x00000010, // densely packed u16, for indexing
|
||||
GPUBUFTYPE_VEC2_TC = 0x00081021, // xyuv xyuv xyuv
|
||||
GPUBUFTYPE_VEC2_TC_RGBA = 0x10082021, // xyuvrgba xyuvrgba xyuvrgba
|
||||
GPUBUFTYPE_VEC2_TC_RGBA = 0x10082021, // xyuvrgba xyuvrgba xyuvrgba
|
||||
GPUBUFTYPE_VEC2_RGBA = 0x08001821, // xyrgba xyrgba xyrgba
|
||||
// ccoossnt
|
||||
GPUBUFTYPE_VEC2_TC_RGBA_BUT_NO_COLOR = GPUBUFTYPE_VEC2_TC_RGBA & 0xffffff,
|
||||
GPUBUFTYPE_UVEC2 = 0x00000420
|
||||
};
|
||||
|
||||
|
@ -59,6 +59,7 @@ public:
|
|||
void *beginWrite(BufDataType type, size_t newsize, unsigned access); // AccessFlags
|
||||
bool commitWrite(); // used same size as passed to beginWrite()
|
||||
bool commitWrite(size_t used); // explicitly specify used size (may be less than initially requested)
|
||||
bool commitWriteExact(const void *p); // asserts that as many bytes as allocated were written
|
||||
|
||||
void upload(BufDataType type, const void *data, size_t size);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue