1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 11:22:45 +01:00
openrw/rwcore/gl/GeometryBuffer.hpp

87 lines
1.9 KiB
C++
Raw Normal View History

#ifndef _LIBRW_GEOMETRYBUFFER_HPP_
#define _LIBRW_GEOMETRYBUFFER_HPP_
#include <gl/gl_core_3_3.h>
2014-02-10 06:43:20 +01:00
#include <vector>
/**
* Enum used to determine which shader input an attribute maps to
*/
enum AttributeSemantic {
2016-09-09 22:13:21 +02:00
ATRS_Position,
ATRS_Normal,
ATRS_Colour,
ATRS_TexCoord
2014-02-10 06:43:20 +01:00
};
/**
* Stores Vertex Attribute data
*/
struct AttributeIndex {
2016-09-09 22:13:21 +02:00
AttributeSemantic sem;
GLsizei size;
GLsizei stride;
GLsizei offset;
GLenum type;
2016-09-09 22:13:21 +02:00
AttributeIndex(AttributeSemantic s, GLsizei sz, GLsizei strd, GLsizei offs,
GLenum type = GL_FLOAT)
: sem(s), size(sz), stride(strd), offset(offs), type(type) {
}
2014-02-10 06:43:20 +01:00
};
typedef std::vector<AttributeIndex> AttributeList;
2016-09-09 22:13:21 +02:00
/**
2014-02-13 11:55:11 +01:00
* GeometryBuffer stores a set of vertex attribute data
*/
2014-02-10 06:43:20 +01:00
class GeometryBuffer {
2016-09-09 22:13:21 +02:00
GLuint vbo;
GLsizei num;
AttributeList attributes;
2014-02-10 06:43:20 +01:00
public:
2016-09-09 22:13:21 +02:00
GeometryBuffer();
template <class T>
GeometryBuffer(const std::vector<T>& data) : vbo(0), num(0) {
uploadVertices(data);
}
~GeometryBuffer();
GLuint getVBOName() const {
return vbo;
}
GLsizei getCount() const {
return num;
}
2014-02-10 06:43:20 +01:00
2016-09-09 22:13:21 +02:00
/**
* Uploads Vertex Buffer data from an STL vector
*
* vertex_attributes() is assumed to exist so that vertex types
* can implicitly declare the strides and offsets for their data.
*/
template <class T>
void uploadVertices(const std::vector<T>& data) {
uploadVertices(data.size(), data.size() * sizeof(T), data.data());
// Assume T has a static method for attributes;
attributes = T::vertex_attributes();
}
2016-09-09 22:13:21 +02:00
/**
* Uploads raw memory into the buffer.
*/
void uploadVertices(GLsizei num, GLsizeiptr size, const GLvoid* mem);
2014-02-10 06:43:20 +01:00
2016-09-09 22:13:21 +02:00
const AttributeList& getDataAttributes() const {
return attributes;
}
AttributeList& getDataAttributes() {
return attributes;
}
2014-02-10 06:43:20 +01:00
};
#endif