[bugfix] motionstreak & DrawNode3D on GL (#19564)

This commit is contained in:
Arnold 2019-04-03 17:27:15 +08:00 committed by minggo
parent 4f81cd7356
commit 9a56e6923f
4 changed files with 25 additions and 16 deletions

View File

@ -133,11 +133,18 @@ bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Co
_pointState = (float *)malloc(sizeof(float) * _maxPoints);
_pointVertexes = (Vec2*)malloc(sizeof(Vec2) * _maxPoints);
const size_t VERTEX_SIZE = sizeof(Vec2) + sizeof(Tex2F) + sizeof(uint8_t) * 4;
_vertexCount = _maxPoints * 2;
_vertices = (Vec2*)malloc(sizeof(Vec2) * _vertexCount);
_texCoords = (Tex2F*)malloc(sizeof(Tex2F) * _vertexCount);
_colorPointer = (uint8_t*)malloc(sizeof(uint8_t) * 4 * _vertexCount);
_customCommand.createVertexBuffer(sizeof(Vec2) + sizeof(Tex2F) + sizeof(uint8_t) * 4, _vertexCount, CustomCommand::BufferUsage::DYNAMIC);
_customCommand.createVertexBuffer(VERTEX_SIZE, _vertexCount, CustomCommand::BufferUsage::DYNAMIC);
std::vector<uint8_t> zeros;
zeros.resize(VERTEX_SIZE * _vertexCount);
std::fill(zeros.begin(), zeros.end(), 0);
_customCommand.updateVertexBuffer(zeros.data(), zeros.size());
setTexture(texture);
setColor(color);

View File

@ -17,7 +17,7 @@ BufferGL::~BufferGL()
void BufferGL::updateData(void* data, unsigned int size)
{
assert(size);
assert(size && size <= _size);
if (_buffer)
{
@ -32,22 +32,16 @@ void BufferGL::updateData(void* data, unsigned int size)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
}
CHECK_GL_ERROR_DEBUG();
_bufferAllocated = true;
_bufferAllocated = size;
}
}
void BufferGL::updateSubData(void* data, unsigned int offset, unsigned int size)
{
assert(offset + size <= _size);
//invoke updateData if buffer is not allocated
if (!_bufferAllocated)
{
CCASSERT(offset == 0, "offset should be zero when allocate buffer");
updateData(data, size);
return;
}
CCASSERT(_bufferAllocated != 0, "updateData should be invoke before updateSubData");
CCASSERT(offset + size <= _bufferAllocated, "buffer size overflow");
if (_buffer)
{
CHECK_GL_ERROR_DEBUG();
@ -55,7 +49,6 @@ void BufferGL::updateSubData(void* data, unsigned int offset, unsigned int size)
{
glBindBuffer(GL_ARRAY_BUFFER, _buffer);
glBufferSubData(GL_ARRAY_BUFFER, offset, size, data);
}
else
{

View File

@ -19,7 +19,7 @@ public:
private:
GLuint _buffer = 0;
bool _bufferAllocated = false;
unsigned int _bufferAllocated = 0;
};
CC_BACKEND_END

View File

@ -24,7 +24,7 @@
****************************************************************************/
#include "DrawNode3D.h"
#include "renderer/backend/Buffer.h"
NS_CC_BEGIN
@ -58,7 +58,15 @@ void DrawNode3D::ensureCapacity(int count)
{
CCASSERT(count>=0, "capacity must be >= 0");
_bufferLines.reserve(_bufferLines.size() + count);
const int EXTENDED_SIZE = _bufferLines.size() + count;
_bufferLines.reserve(EXTENDED_SIZE);
if (!_customCommand.getVertexBuffer() || _customCommand.getVertexBuffer()->getSize() < (EXTENDED_SIZE * sizeof(_bufferLines[0])))
{
_customCommand.createVertexBuffer(sizeof(V3F_C4B), EXTENDED_SIZE + (EXTENDED_SIZE >> 1), CustomCommand::BufferUsage::DYNAMIC);
}
}
bool DrawNode3D::init()
@ -120,6 +128,7 @@ void DrawNode3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
if (_isDirty && !_bufferLines.empty())
{
_customCommand.updateVertexBuffer(_bufferLines.data(), (unsigned int)(_bufferLines.size() * sizeof(_bufferLines[0])));
_customCommand.setVertexDrawInfo(0, _bufferLines.size());
_isDirty = false;
}