123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #include "Mesh.h"
- #include <cstring>
- #include <iostream>
- using namespace std;
- Mesh::Mesh()
- {
- }
- 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(const Mesh& orig)
- {
- }
- 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()
- {
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
-
- vertices = positionIndex;
-
- /*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()
- {
- glBindVertexArray(vba);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glDrawArrays(GL_TRIANGLES, 0, vertices);
- }
|