Skip to content

Instantly share code, notes, and snippets.

@billhsu
Created March 6, 2016 15:01
Show Gist options
  • Save billhsu/489aebe1e3a22b0118a4 to your computer and use it in GitHub Desktop.
Save billhsu/489aebe1e3a22b0118a4 to your computer and use it in GitHub Desktop.
HardwareBuffer
// Shipeng Xu
// [email protected]
/* Copyright [2016] [Shipeng Xu]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "HardwareBuffer.h"
#include <iostream>
#include "OpenGL/Util/Utility.h"
void HardwareBuffer::initBuffer(BufferInfo _bufferInfo) {
if (_bufferInfo.flags == 0) {
return;
}
checkGlErr(__FILE__, __LINE__);
glGenVertexArrays(1, &vertexArrayObj);
glBindVertexArray(vertexArrayObj);
if (_bufferInfo.flags & FLAG_ARRAY_BUFFER) {
glGenBuffers(1, &arrayBuffer);
glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer);
if (_bufferInfo.arrayBufferSize > 0) {
glBufferData(GL_ARRAY_BUFFER, _bufferInfo.arrayBufferSize,
_bufferInfo.arrayBufferData, _bufferInfo.usage);
}
}
if (_bufferInfo.flags & FLAG_ELEMENT_ARRAY_BUFFER) {
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
if (_bufferInfo.indexBufferSize > 0) {
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _bufferInfo.indexBufferSize,
_bufferInfo.indexBufferData, _bufferInfo.usage);
}
}
std::map<int, int>::iterator iter;
for (iter = _bufferInfo.attribPointerOffset.begin();
iter != _bufferInfo.attribPointerOffset.end(); ++iter) {
int channel = iter->first;
int attribSize = _bufferInfo.attribPointerSize[channel];
long attribOffset = _bufferInfo.attribPointerOffset[channel];
glEnableVertexAttribArray(channel);
glVertexAttribPointer(channel, attribSize, GL_FLOAT, GL_FALSE,
_bufferInfo.arrayBufferUnitSize,
(GLvoid*)attribOffset);
}
glBindVertexArray(0);
checkGlErr(__FILE__, __LINE__);
bufferInfo = _bufferInfo;
}
void HardwareBuffer::updateBuffer(BufferInfo _bufferInfo) {
checkGlErr(__FILE__, __LINE__);
if (_bufferInfo.flags & FLAG_ARRAY_BUFFER) {
glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer);
if (bufferInfo.arrayBufferSize != _bufferInfo.arrayBufferSize) {
glBufferData(GL_ARRAY_BUFFER, _bufferInfo.arrayBufferSize, 0,
_bufferInfo.usage);
}
float* dataBufVertices =
(float*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if (dataBufVertices == 0) {
std::cerr << "glMapBuffer failed: " << __FILE__ << "[" << __LINE__
<< "]" << std::endl;
} else {
memcpy(dataBufVertices, _bufferInfo.arrayBufferData,
_bufferInfo.arrayBufferSize * sizeof(float));
}
glUnmapBuffer(GL_ARRAY_BUFFER);
}
if (_bufferInfo.flags & FLAG_ELEMENT_ARRAY_BUFFER) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
if (bufferInfo.indexBufferSize != _bufferInfo.indexBufferSize) {
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _bufferInfo.indexBufferSize,
0, _bufferInfo.usage);
}
float* dataBufVertices =
(float*)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
if (dataBufVertices == 0) {
std::cerr << "glMapBuffer failed: " << __FILE__ << "[" << __LINE__
<< "]" << std::endl;
} else {
memcpy(dataBufVertices, _bufferInfo.indexBufferData,
_bufferInfo.indexBufferSize * sizeof(float));
}
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
}
checkGlErr(__FILE__, __LINE__);
bufferInfo = _bufferInfo;
}
void HardwareBuffer::render(GLenum mode) {
checkGlErr(__FILE__, __LINE__);
glBindVertexArray(vertexArrayObj);
if (bufferInfo.flags & FLAG_ELEMENT_ARRAY_BUFFER) {
glDrawElements(mode, bufferInfo.indexBufferSize, GL_UNSIGNED_INT, 0);
} else {
glDrawArrays(mode, 0, bufferInfo.arrayBufferSize /
bufferInfo.arrayBufferUnitSize);
}
glBindVertexArray(0);
checkGlErr(__FILE__, __LINE__);
}
// Shipeng Xu
// [email protected]
/* Copyright [2016] [Shipeng Xu]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <OpenGL/gl3.h>
#include <iostream>
#include <map>
class HardwareBuffer {
public:
HardwareBuffer() {
arrayBuffer = -1;
indexBuffer = -1;
vertexArrayObj = -1;
};
~HardwareBuffer() {
if (arrayBuffer != -1) {
glDeleteBuffers(1, &arrayBuffer);
}
if (indexBuffer != -1) {
glDeleteBuffers(1, &indexBuffer);
}
if (vertexArrayObj != -1) {
glDeleteVertexArrays(1, &vertexArrayObj);
}
if (bufferInfo.arrayBufferData != NULL) {
delete[] bufferInfo.arrayBufferData;
}
if (bufferInfo.indexBufferData != NULL) {
delete[] bufferInfo.indexBufferData;
}
}
struct BufferInfo {
BufferInfo() {
arrayBufferSize = 0;
arrayBufferData = 0;
indexBufferSize = 0;
indexBufferData = 0;
arrayBufferUnitSize = 0;
flags = 0;
usage = GL_STATIC_DRAW;
attribPointerOffset.clear();
attribPointerSize.clear();
}
int* indexBufferData;
int indexBufferSize;
float* arrayBufferData;
int arrayBufferSize;
int arrayBufferUnitSize;
unsigned int flags;
GLuint usage;
std::map<int, int> attribPointerOffset;
std::map<int, int> attribPointerSize;
};
void initBuffer(BufferInfo _bufferInfo);
void updateBuffer(BufferInfo _bufferInfo);
void render(GLenum mode = GL_TRIANGLES);
static const unsigned int FLAG_ARRAY_BUFFER = 0x0001;
static const unsigned int FLAG_ELEMENT_ARRAY_BUFFER = 0x0002;
private:
BufferInfo bufferInfo;
GLuint arrayBuffer;
GLuint indexBuffer;
GLuint vertexArrayObj;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment