#include "Mesh.h" #include #include using namespace std; Mesh::Mesh(int mode) { // position positionStartIndex = vertexSize; vertexSize += 3; // color if(mode & COLOR) { colorStartIndex = vertexSize; vertexSize += 4; } // texture if(mode & TEXTURE) { textureStartIndex = vertexSize; vertexSize += 2; } // normal if(mode & NORMAL) { normalStartIndex = vertexSize; vertexSize += 3; } data = new float[dataSize * vertexSize]; glGenVertexArrays(1, &vba); glBindVertexArray(vba); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glVertexAttribPointer(0, 3, GL_FLOAT, 0, sizeof(float) * vertexSize, (GLvoid*) (sizeof(float) * positionStartIndex)); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 4, GL_FLOAT, 0, sizeof(float) * vertexSize, (GLvoid*) (sizeof(float) * colorStartIndex)); glEnableVertexAttribArray(1); glVertexAttribPointer(2, 2, GL_FLOAT, 0, sizeof(float) * vertexSize, (GLvoid*) (sizeof(float) * textureStartIndex)); glEnableVertexAttribArray(2); glVertexAttribPointer(3, 3, GL_FLOAT, 0, sizeof(float) * vertexSize, (GLvoid*) (sizeof(float) * normalStartIndex)); glEnableVertexAttribArray(3); } Mesh::~Mesh() { delete[] data; glDeleteVertexArrays(1, &vba); glDeleteBuffers(1, &vbo); } void Mesh::ensureCapacity(unsigned int index) { if(index >= dataSize) { float* newData = new float[dataSize * 2 * vertexSize]; memcpy(newData, data, sizeof(float) * dataSize * vertexSize); delete[] data; data = newData; dataSize *= 2; } } void Mesh::addPosition(float x, float y, float z) { ensureCapacity(positionIndex); unsigned int index = positionStartIndex + positionIndex * vertexSize; data[index] = x; data[index + 1] = y; data[index + 2] = z; positionIndex++; } void Mesh::addColor(float r, float g, float b, float a) { ensureCapacity(colorIndex); unsigned int index = colorStartIndex + colorIndex * vertexSize; data[index] = r; data[index + 1] = g; data[index + 2] = b; data[index + 3] = a; colorIndex++; } void Mesh::addTexture(float tx, float ty) { ensureCapacity(textureIndex); unsigned int index = textureStartIndex + textureIndex * vertexSize; data[index] = tx; data[index + 1] = ty; textureIndex++; } void Mesh::addNormal(float nx, float ny, float nz) { ensureCapacity(normalIndex); unsigned int index = normalStartIndex + normalIndex * vertexSize; data[index] = nx; data[index + 1] = ny; data[index + 2] = nz; normalIndex++; } void Mesh::build(bool print) { glBindBuffer(GL_ARRAY_BUFFER, vbo); vertices = positionIndex; if(print) { cout << "______________" << vertices << " " << vertexSize << endl; for(int i = 0; i < vertices; i++) { cout << "----------vert " << i << endl; int index = i * vertexSize; for(int j = 0; j < vertexSize; j++) { cout << data[index + j] << " " << endl; } } } glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * vertices, data, GL_STATIC_DRAW); positionIndex = 0; colorIndex = 0; textureIndex = 0; normalIndex = 0; } void Mesh::draw() { if(vertices == 0) { return; } glBindVertexArray(vba); glBindBuffer(GL_ARRAY_BUFFER, vbo); glDrawArrays(GL_TRIANGLES, 0, vertices); } void Mesh::draw(int start, int count) { if(vertices == 0 || start < 0 || start >= vertices || start + count > vertices) { return; } glBindVertexArray(vba); glBindBuffer(GL_ARRAY_BUFFER, vbo); glDrawArrays(GL_TRIANGLES, start, count); }