123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- #include <cstring>
- #include <iostream>
- #include "client/engine/Mesh.h"
- 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(unsigned int i = 0; i < vertices; i++)
- {
- cout << "----------vert " << i << endl;
- unsigned int index = i * vertexSize;
- for(unsigned 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(unsigned int start, unsigned 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);
- }
- NormalTextureMesh::NormalTextureMesh() : Mesh(Mesh::NORMAL | Mesh::TEXTURE)
- {
- }
- void NormalTextureMesh::addTriangle(
- float p1x, float p1y, float p1z, float p1nx, float p1ny, float p1nz, float p1texX, float p1texY,
- float p2x, float p2y, float p2z, float p2nx, float p2ny, float p2nz, float p2texX, float p2texY,
- float p3x, float p3y, float p3z, float p3nx, float p3ny, float p3nz, float p3texX, float p3texY)
- {
- addPosition(p1x, p1y, p1z);
- addPosition(p2x, p2y, p2z);
- addPosition(p3x, p3y, p3z);
-
- addNormal(p1nx, p1ny, p1nz);
- addNormal(p2nx, p2ny, p2nz);
- addNormal(p3nx, p3ny, p3nz);
-
- addTexture(p1texX, p1texY);
- addTexture(p2texX, p2texY);
- addTexture(p3texX, p3texY);
- }
- void NormalTextureMesh::addCuboid(float sx, float sy, float sz, float ex, float ey, float ez,
- float topTexStartX, float topTexStartY, float topTexEndX, float topTexEndY,
- float bottomTexStartX, float bottomTexStartY, float bottomTexEndX, float bottomTexEndY,
- float northTexStartX, float northTexStartY, float northTexEndX, float northTexEndY,
- float southTexStartX, float southTexStartY, float southTexEndX, float southTexEndY,
- float eastTexStartX, float eastTexStartY, float eastTexEndX, float eastTexEndY,
- float westTexStartX, float westTexStartY, float westTexEndX, float westTexEndY)
- {
- // bottom side
- addTriangle(
- sx, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexStartY,
- ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
- sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY);
- addTriangle(
- ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
- ex, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexEndY,
- sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY);
-
- // top side
- addTriangle(
- sx, ey, sz, 0.0f, 1.0f, 0.0f, topTexStartX, topTexStartY,
- sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
- ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY);
- addTriangle(
- ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY,
- sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
- ex, ey, ez, 0.0f, 1.0f, 0.0f, topTexEndX, topTexEndY);
-
- // north side
- addTriangle(
- ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
- ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY,
- ex, sy, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexEndY);
- addTriangle(
- ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
- ex, ey, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexStartY,
- ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY);
-
- // south side
- addTriangle(
- sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
- sx, sy, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexEndY,
- sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY);
- addTriangle(
- sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
- sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY,
- sx, ey, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexStartY);
-
- // east side
- addTriangle(
- sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
- ex, sy, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexEndY,
- ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY);
- addTriangle(
- sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
- ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY,
- sx, ey, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexStartY);
-
- // west side
- addTriangle(
- sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
- ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY,
- ex, sy, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexEndY);
- addTriangle(
- sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
- sx, ey, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexStartY,
- ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY);
- }
|