From 294304e91364479dd9084fdcf2d1386a52890a61 Mon Sep 17 00:00:00 2001 From: Karaulov Date: Sat, 26 Nov 2022 17:02:07 +0300 Subject: [PATCH] Revert back VertexBuffer code to original from bspguy --- src/gl/VertexBuffer.cpp | 145 +++++++++++++++++----------------------- src/gl/VertexBuffer.h | 43 +++++------- 2 files changed, 78 insertions(+), 110 deletions(-) diff --git a/src/gl/VertexBuffer.cpp b/src/gl/VertexBuffer.cpp index f0cf8a10..e1d3fe58 100644 --- a/src/gl/VertexBuffer.cpp +++ b/src/gl/VertexBuffer.cpp @@ -1,7 +1,6 @@ #include "VertexBuffer.h" #include "util.h" #include -#include "Renderer.h" VertexAttr commonAttr[VBUF_FLAGBITS] = { @@ -22,8 +21,8 @@ VertexAttr commonAttr[VBUF_FLAGBITS] = VertexAttr(3, GL_FLOAT, -1, GL_FALSE, ""), // POS_3F }; -VertexAttr::VertexAttr(int numValues, int valueType, int vhandle, int normalized, const char* varName) - : numValues(numValues), valueType(valueType), handle(vhandle), normalized(normalized), varName(varName) +VertexAttr::VertexAttr(int numValues, int valueType, int handle, int normalized, const char* varName) + : numValues(numValues), valueType(valueType), handle(handle), normalized(normalized), varName(varName) { switch (valueType) { @@ -47,30 +46,30 @@ VertexAttr::VertexAttr(int numValues, int valueType, int vhandle, int normalized } } -VertexBuffer::VertexBuffer(ShaderProgram* shaderProgram, int attFlags, const void* dat, GLsizei numVerts, int primitive) + + +VertexBuffer::VertexBuffer(ShaderProgram* shaderProgram, int attFlags, const void* dat, int numVerts, int primitive) { this->shaderProgram = shaderProgram; this->primitive = primitive; addAttributes(attFlags); - setData(dat, numVerts, primitive); - vboId = 0xFFFFFFFF; + setData(dat, numVerts); + vboId = (unsigned int)-1; } VertexBuffer::VertexBuffer(ShaderProgram* shaderProgram, int attFlags, int primitive) { numVerts = 0; data = NULL; - vboId = 0xFFFFFFFF; + vboId = (unsigned int)-1; this->shaderProgram = shaderProgram; this->primitive = primitive; addAttributes(attFlags); } -VertexBuffer::~VertexBuffer() -{ +VertexBuffer::~VertexBuffer() { deleteBuffer(); - if (ownData) - { + if (ownData) { delete[] data; } } @@ -86,8 +85,10 @@ void VertexBuffer::addAttributes(int attFlags) commonAttr[i].handle = shaderProgram->vposID; else if (i >= VBUF_COLOR_START) commonAttr[i].handle = shaderProgram->vcolorID; - else + else if (i >= VBUF_TEX_START) commonAttr[i].handle = shaderProgram->vtexID; + else + logf("Unused vertex buffer flag bit %d", i); attribs.push_back(commonAttr[i]); elementSize += commonAttr[i].size; @@ -95,16 +96,14 @@ void VertexBuffer::addAttributes(int attFlags) } } -void VertexBuffer::addAttribute(int numValues, int valueType, int normalized, const char* varName) -{ +void VertexBuffer::addAttribute(int numValues, int valueType, int normalized, const char* varName) { VertexAttr attribute(numValues, valueType, -1, normalized, varName); attribs.push_back(attribute); elementSize += attribute.size; } -void VertexBuffer::addAttribute(int type, const char* varName) -{ +void VertexBuffer::addAttribute(int type, const char* varName) { int idx = 0; while (type >>= 1) // unroll for more speed... @@ -112,8 +111,7 @@ void VertexBuffer::addAttribute(int type, const char* varName) idx++; } - if (idx >= VBUF_FLAGBITS) - { + if (idx >= VBUF_FLAGBITS) { logf("Invalid attribute type\n"); return; } @@ -126,28 +124,24 @@ void VertexBuffer::addAttribute(int type, const char* varName) elementSize += attribute.size; } -void VertexBuffer::setShader(ShaderProgram* program, bool hideErrors) -{ +void VertexBuffer::setShader(ShaderProgram* program, bool hideErrors) { shaderProgram = program; attributesBound = false; for (int i = 0; i < attribs.size(); i++) { - if (attribs[i].varName[0] != '\0') - { + if (strlen(attribs[i].varName) > 0) { attribs[i].handle = -1; } } bindAttributes(hideErrors); - if (vboId != 0xFFFFFFFF) - { + if (vboId != -1) { deleteBuffer(); upload(); } } -void VertexBuffer::bindAttributes(bool hideErrors) -{ +void VertexBuffer::bindAttributes(bool hideErrors) { if (attributesBound) return; @@ -165,21 +159,13 @@ void VertexBuffer::bindAttributes(bool hideErrors) attributesBound = true; } -void VertexBuffer::setData(const void* _data, GLsizei _numVerts) -{ - data = (unsigned char*)_data; - numVerts = _numVerts; -} - -void VertexBuffer::setData(const void* _data, GLsizei _numVerts, int _primitive) +void VertexBuffer::setData(const void* _data, int _numVerts) { data = (unsigned char*)_data; numVerts = _numVerts; - primitive = _primitive; } -void VertexBuffer::upload() -{ +void VertexBuffer::upload() { shaderProgram->bind(); bindAttributes(); @@ -187,14 +173,13 @@ void VertexBuffer::upload() glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, elementSize * numVerts, data, GL_STATIC_DRAW); - size_t offset = 0; + int offset = 0; for (int i = 0; i < attribs.size(); i++) { VertexAttr& a = attribs[i]; - void* ptr = (void*)offset; + void* ptr = ((char*)0) + offset; offset += a.size; - if (a.handle == -1) - { + if (a.handle == -1) { continue; } glBindBuffer(GL_ARRAY_BUFFER, vboId); @@ -205,37 +190,24 @@ void VertexBuffer::upload() glBindBuffer(GL_ARRAY_BUFFER, 0); } -void VertexBuffer::deleteBuffer() -{ - if (vboId != 0xFFFFFFFF) +void VertexBuffer::deleteBuffer() { + if (vboId != (unsigned int)-1) glDeleteBuffers(1, &vboId); - vboId = 0xFFFFFFFF; + vboId = (unsigned int)-1; } -void VertexBuffer::drawRange(GLint start, GLsizei end) +void VertexBuffer::drawRange(int _primitive, int start, int end) { - if (start < 0 || start > numVerts) - logf("Invalid start index: %d\n", start); - else if (end > numVerts || end < 0) - logf("Invalid end index: %d\n", end); - else if (end - start <= 0) - logf("Invalid draw range: %d -> %d\n", start, end); - else - { - char* offsetPtr = (char*)data; - - shaderProgram->bind(); - bindAttributes(); - - if (vboId != 0xFFFFFFFF) - { - glBindBuffer(GL_ARRAY_BUFFER, vboId); - offsetPtr = NULL; - } + shaderProgram->bind(); + bindAttributes(); + char* offsetPtr = (char*)data; + if (vboId != (unsigned int)-1) { + glBindBuffer(GL_ARRAY_BUFFER, vboId); + offsetPtr = NULL; + } + { int offset = 0; - - // Drop FPS below: FIXME for (int i = 0; i < attribs.size(); i++) { VertexAttr& a = attribs[i]; @@ -246,31 +218,36 @@ void VertexBuffer::drawRange(GLint start, GLsizei end) glEnableVertexAttribArray(a.handle); glVertexAttribPointer(a.handle, a.numValues, a.valueType, a.normalized != 0, elementSize, ptr); } - // Drop FPS above - - glDrawArrays(primitive, start, end - start); + } - for (int i = 0; i < attribs.size(); i++) - { - VertexAttr& a = attribs[i]; - if (a.handle == -1) - continue; - glDisableVertexAttribArray(a.handle); - } + if (start < 0 || start > numVerts) + logf("Invalid start index: %d\n", start); + else if (end > numVerts || end < 0) + logf("Invalid end index: %d\n", end); + else if (end - start <= 0) + logf("Invalid draw range: %d -> %d\n", start, end); + else + glDrawArrays(_primitive, start, end - start); + if (vboId != (unsigned int)-1) { glBindBuffer(GL_ARRAY_BUFFER, 0); + } - for (int i = 0; i < attribs.size(); i++) - { - VertexAttr& a = attribs[i]; - if (a.handle == -1) - continue; - glDisableVertexAttribArray(a.handle); - } + for (int i = 0; i < attribs.size(); i++) + { + VertexAttr& a = attribs[i]; + if (a.handle == -1) + continue; + glDisableVertexAttribArray(a.handle); } } -void VertexBuffer::drawFull() +void VertexBuffer::draw(int _primitive) { - return drawRange(0, numVerts); + drawRange(_primitive, 0, numVerts); } + +void VertexBuffer::drawFull() +{ + drawRange(primitive, 0, numVerts); +} \ No newline at end of file diff --git a/src/gl/VertexBuffer.h b/src/gl/VertexBuffer.h index 18afb13b..328dd751 100644 --- a/src/gl/VertexBuffer.h +++ b/src/gl/VertexBuffer.h @@ -1,21 +1,20 @@ #pragma once #include - #include #include "ShaderProgram.h" #include "util.h" // Combinable flags for setting common vertex attributes -#define TEX_2B (1 << 0) // 2D unsigned char texture coordinates +#define TEX_2B (1 << 0) // 2D byte texture coordinates #define TEX_2S (1 << 1) // 2D short texture coordinates #define TEX_2F (1 << 2) // 2D float texture coordinates -#define COLOR_3B (1 << 3) // RGB unsigned char color values +#define COLOR_3B (1 << 3) // RGB byte color values #define COLOR_3F (1 << 4) // RGB float color values -#define COLOR_4B (1 << 5) // RGBA unsigned char color values +#define COLOR_4B (1 << 5) // RGBA byte color values #define COLOR_4F (1 << 6) // RGBA float color values -#define NORM_3B (1 << 7) // 3D unsigned char normal coordinates +#define NORM_3B (1 << 7) // 3D byte normal coordinates #define NORM_3F (1 << 8) // 3D float normal coordinates -#define POS_2B (1 << 9) // 2D unsigned char position coordinates +#define POS_2B (1 << 9) // 2D byte position coordinates #define POS_2S (1 << 10) // 2D short position coordinates #define POS_2I (1 << 11) // 2D integer position coordinates #define POS_2F (1 << 12) // 2D float position coordinates @@ -38,13 +37,10 @@ struct VertexAttr int valueType; // Ex: GL_FLOAT int handle; // location in shader program (-1 indicates invalid attribute) int size; // size of the attribute in bytes - int normalized; // GL_TRUE/GL_FALSE Ex: unsigned char color values are normalized (0-255 = 0.0-1.0) + int normalized; // GL_TRUE/GL_FALSE Ex: byte color values are normalized (0-255 = 0.0-1.0) const char* varName; - VertexAttr() : handle(-1) - { - numValues = valueType = handle = size = normalized = 0; varName = ""; - } + VertexAttr() : handle(-1) {} VertexAttr(int numValues, int valueType, int handle, int normalized, const char* varName); }; @@ -55,25 +51,26 @@ class VertexBuffer unsigned char* data = NULL; std::vector attribs; int elementSize; - GLsizei numVerts; + int numVerts; int primitive; bool ownData = false; // set to true if buffer should delete data on destruction // Specify which common attributes to use. They will be located in the // shader program. If passing data, note that data is not copied, but referenced - VertexBuffer(ShaderProgram* shaderProgram, int attFlags, int primitive); - VertexBuffer(ShaderProgram* shaderProgram, int attFlags, const void* dat, GLsizei numVerts, int primitive); + VertexBuffer(ShaderProgram* shaderProgram, int attFlags, int primitive = 0); + VertexBuffer(ShaderProgram* shaderProgram, int attFlags, const void* dat, int numVerts, int primitive = 0); ~VertexBuffer(); // Note: Data is not copied into the class - don't delete your data. // Data will be deleted when the buffer is destroyed. - void setData(const void* data, GLsizei numVerts, int primitive); - void setData(const void* data, GLsizei numVerts); + void setData(const void* data, int numVerts); void upload(); void deleteBuffer(); void setShader(ShaderProgram* program, bool hideErrors = false); - void drawRange(GLint start, GLsizei end); + + void drawRange(int primitive, int start, int end); + void draw(int primitive); void drawFull(); void addAttribute(int numValues, int valueType, int normalized, const char* varName); @@ -81,16 +78,10 @@ class VertexBuffer void bindAttributes(bool hideErrors = false); // find handles for all vertex attributes (call from main thread only) private: - bool needDrawChecked = true; - bool needDraw = true; - vec3 camOrigin, camAngles; - double drawTime = 0.0; ShaderProgram* shaderProgram = NULL; // for getting handles to vertex attributes - unsigned int vboId = 0xFFFFFFFF; - + unsigned int vboId = (unsigned int)-1; bool attributesBound = false; - GLuint drawQuery = 0xFFFFFFFF; + // add attributes according to the attribute flags void addAttributes(int attFlags); -}; - +}; \ No newline at end of file