Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. /*
  2.  * File:   VertexBuffer.cpp
  3.  * Author: bombpersons
  4.  *
  5.  * Created on May 17, 2011, 7:40 PM
  6.  */
  7.  
  8. #include "VertexBuffer.h"
  9.  
  10. VertexBuffer::VertexBuffer() {
  11.     // Allocate a buffer
  12.     //glGenBuffers(1, &name);
  13. }
  14.  
  15. VertexBuffer::VertexBuffer(const VertexBuffer& orig) {
  16. }
  17.  
  18. VertexBuffer::~VertexBuffer() {
  19.     // We're finished with the buffers now
  20.     glDeleteBuffers(1, &name);
  21. }
  22.  
  23. /* ------------------------------- Generate a buffer name --------------------*/
  24. void VertexBuffer::GenBuffer() {
  25.     glGenBuffers(1, &name);
  26. }
  27.  
  28. /* -------------------------- Fill the buffer --------------------------------*/
  29. void VertexBuffer::Fill(void* _vertexData, GLsizeiptr _vertexSize, GLsizeiptr _vertexItemSize,
  30.                         void* _colorData, GLsizeiptr _colorSize, GLsizeiptr _colorItemSize,
  31.                         void* _texData, GLsizeiptr _texSize, GLsizeiptr _texItemSize,
  32.                         void* _normalData, GLsizeiptr _normalSize, GLsizeiptr _normalItemSize) {    
  33.     // Bind the buffer
  34.     glBindBuffer(GL_ARRAY_BUFFER, name);
  35.    
  36.     // Allocate memory in the buffer
  37.     glBufferData(GL_ARRAY_BUFFER, _vertexSize+_colorSize+_texSize+_normalSize, 0, GL_STATIC_DRAW);
  38.    
  39.     // Map the buffer to memory
  40.     uint8_t* buffer = (uint8_t*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
  41.     if (!buffer)
  42.         throw OutOfMemoryException();
  43.    
  44.     // Create more convenient pointers to the data
  45.     uint8_t* v = (uint8_t*)_vertexData;
  46.     uint8_t* c = (uint8_t*)_colorData;
  47.     uint8_t* t = (uint8_t*)_texData;
  48.     uint8_t* n = (uint8_t*)_normalData;
  49.    
  50.     // The length
  51.     unsigned int length = _vertexSize / _vertexItemSize;
  52.    
  53.     // The total size of one element
  54.     GLsizeiptr totalSize = _vertexItemSize+_colorItemSize+_texItemSize+_normalItemSize;
  55.    
  56.     // Loop through and write to the buffer
  57.     for (unsigned int i = 0; i < length; ++i) {
  58.         // Write vertex data
  59.         if (_vertexData) {
  60.             for (int j = 0; j < _vertexItemSize; ++j) {
  61.                 buffer[totalSize*i + j] = v[i*_vertexItemSize + j];
  62.             }
  63.         }
  64.        
  65.         // Write color data
  66.         if (_colorData) {
  67.             for (int j = 0; j < _colorItemSize; ++j) {
  68.                 buffer[totalSize*i + j + _vertexItemSize] = c[i*_colorItemSize + j];  
  69.             }
  70.         }
  71.        
  72.         // Write texture data
  73.         if (_texData) {
  74.             for (int j = 0; j < _texItemSize; ++j) {
  75.                 buffer[totalSize*i + j + _vertexItemSize + _colorItemSize] = t[i*_texItemSize + j];  
  76.             }
  77.         }
  78.        
  79.         // Write normal data
  80.         if (_normalData) {
  81.             for (int j = 0; j < _normalItemSize; ++j) {
  82.                 buffer[totalSize*i + j + _vertexItemSize + _colorItemSize + _texItemSize] = n[i*_normalItemSize + j];  
  83.             }
  84.         }        
  85.     }
  86.    
  87.     // Unmap the buffer
  88.     glUnmapBuffer(GL_ARRAY_BUFFER);
  89.    
  90.     // Unbind the buffer
  91.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  92.    
  93.     // Set the sizes of the buffers
  94.     vertexSize = _vertexItemSize;
  95.     colorSize = _colorItemSize;
  96.     texSize = _texItemSize;
  97.     normalSize = _normalItemSize;
  98.     itemSize = vertexSize + colorSize + texSize + normalSize;
  99.     bufferSize = _vertexSize+_colorSize+_texSize+_normalSize;
  100.     bufferLength = length;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement