Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: VertexBuffer.cpp
- * Author: bombpersons
- *
- * Created on May 17, 2011, 7:40 PM
- */
- #include "VertexBuffer.h"
- VertexBuffer::VertexBuffer() {
- // Allocate a buffer
- //glGenBuffers(1, &name);
- }
- VertexBuffer::VertexBuffer(const VertexBuffer& orig) {
- }
- VertexBuffer::~VertexBuffer() {
- // We're finished with the buffers now
- glDeleteBuffers(1, &name);
- }
- /* ------------------------------- Generate a buffer name --------------------*/
- void VertexBuffer::GenBuffer() {
- glGenBuffers(1, &name);
- }
- /* -------------------------- Fill the buffer --------------------------------*/
- void VertexBuffer::Fill(void* _vertexData, GLsizeiptr _vertexSize, GLsizeiptr _vertexItemSize,
- void* _colorData, GLsizeiptr _colorSize, GLsizeiptr _colorItemSize,
- void* _texData, GLsizeiptr _texSize, GLsizeiptr _texItemSize,
- void* _normalData, GLsizeiptr _normalSize, GLsizeiptr _normalItemSize) {
- // Bind the buffer
- glBindBuffer(GL_ARRAY_BUFFER, name);
- // Allocate memory in the buffer
- glBufferData(GL_ARRAY_BUFFER, _vertexSize+_colorSize+_texSize+_normalSize, 0, GL_STATIC_DRAW);
- // Map the buffer to memory
- uint8_t* buffer = (uint8_t*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
- if (!buffer)
- throw OutOfMemoryException();
- // Create more convenient pointers to the data
- uint8_t* v = (uint8_t*)_vertexData;
- uint8_t* c = (uint8_t*)_colorData;
- uint8_t* t = (uint8_t*)_texData;
- uint8_t* n = (uint8_t*)_normalData;
- // The length
- unsigned int length = _vertexSize / _vertexItemSize;
- // The total size of one element
- GLsizeiptr totalSize = _vertexItemSize+_colorItemSize+_texItemSize+_normalItemSize;
- // Loop through and write to the buffer
- for (unsigned int i = 0; i < length; ++i) {
- // Write vertex data
- if (_vertexData) {
- for (int j = 0; j < _vertexItemSize; ++j) {
- buffer[totalSize*i + j] = v[i*_vertexItemSize + j];
- }
- }
- // Write color data
- if (_colorData) {
- for (int j = 0; j < _colorItemSize; ++j) {
- buffer[totalSize*i + j + _vertexItemSize] = c[i*_colorItemSize + j];
- }
- }
- // Write texture data
- if (_texData) {
- for (int j = 0; j < _texItemSize; ++j) {
- buffer[totalSize*i + j + _vertexItemSize + _colorItemSize] = t[i*_texItemSize + j];
- }
- }
- // Write normal data
- if (_normalData) {
- for (int j = 0; j < _normalItemSize; ++j) {
- buffer[totalSize*i + j + _vertexItemSize + _colorItemSize + _texItemSize] = n[i*_normalItemSize + j];
- }
- }
- }
- // Unmap the buffer
- glUnmapBuffer(GL_ARRAY_BUFFER);
- // Unbind the buffer
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- // Set the sizes of the buffers
- vertexSize = _vertexItemSize;
- colorSize = _colorItemSize;
- texSize = _texItemSize;
- normalSize = _normalItemSize;
- itemSize = vertexSize + colorSize + texSize + normalSize;
- bufferSize = _vertexSize+_colorSize+_texSize+_normalSize;
- bufferLength = length;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement