146 lines
3.3 KiB
C++
146 lines
3.3 KiB
C++
#include "PlanetIndexBuffer.h"
|
|
|
|
PlanetIndexBuffer::PlanetIndexBuffer(unsigned int resolution) : m_resolution(resolution) {
|
|
createBuffer(PlanetBufferIndex::Base);
|
|
createBuffer(PlanetBufferIndex::FixR, false, true, false, false);
|
|
createBuffer(PlanetBufferIndex::FixL, false, false, true, false);
|
|
createBuffer(PlanetBufferIndex::FixB, false, false, false, true);
|
|
createBuffer(PlanetBufferIndex::FixT, true, false, false, false);
|
|
createBuffer(PlanetBufferIndex::FixTR, true, true, false, false);
|
|
createBuffer(PlanetBufferIndex::FixTL, true, false, true, false);
|
|
createBuffer(PlanetBufferIndex::FixBR, false, true, false, true);
|
|
createBuffer(PlanetBufferIndex::FixBL, false, false, true, true);
|
|
}
|
|
|
|
PlanetIndexBuffer::~PlanetIndexBuffer() {
|
|
for (int i = 0; i < 9; i++) {
|
|
if (m_buffers[i] == nullptr) continue;
|
|
glDeleteBuffers(1, &m_buffers[i]->ebo);
|
|
m_buffers[i]->indices.clear();
|
|
delete m_buffers[i];
|
|
}
|
|
}
|
|
|
|
PIB* PlanetIndexBuffer::getBuffer(PlanetBufferIndex idx) {
|
|
return m_buffers[idx];
|
|
}
|
|
|
|
void PlanetIndexBuffer::createBuffer(PlanetBufferIndex idx, bool fanTop, bool fanRight, bool fanLeft, bool fanBottom) {
|
|
PIB* buffer = new PIB();
|
|
// Create indices for mesh
|
|
for (unsigned int gz = 0; gz < m_resolution - 1; gz++)
|
|
{
|
|
bool slantLeft = gz % 2 == 0;
|
|
for (unsigned int gx = 0; gx < m_resolution - 1; gx++)
|
|
{
|
|
unsigned int topLeft = (gz * m_resolution) + gx;
|
|
unsigned int topRight = topLeft + 1;
|
|
unsigned int bottomLeft = ((gz + 1) * m_resolution) + gx;
|
|
unsigned int bottomRight = bottomLeft + 1;
|
|
|
|
bool uTri1 = true;
|
|
bool uTri2 = true;
|
|
unsigned int tri1[3];
|
|
unsigned int tri2[3];
|
|
|
|
if (slantLeft)
|
|
{
|
|
tri1[0] = topLeft;
|
|
tri1[1] = bottomLeft;
|
|
tri1[2] = bottomRight;
|
|
tri2[0] = topLeft;
|
|
tri2[1] = bottomRight;
|
|
tri2[2] = topRight;
|
|
}
|
|
else
|
|
{
|
|
tri1[0] = topLeft;
|
|
tri1[1] = bottomLeft;
|
|
tri1[2] = topRight;
|
|
tri2[0] = bottomLeft;
|
|
tri2[1] = bottomRight;
|
|
tri2[2] = topRight;
|
|
}
|
|
|
|
if (fanTop && gz == 0)
|
|
{
|
|
if (gx % 2 == 0)
|
|
{
|
|
tri2[0] = topLeft;
|
|
tri2[1] = bottomRight;
|
|
tri2[2] = topRight + 1;
|
|
}
|
|
else
|
|
{
|
|
uTri1 = false;
|
|
}
|
|
}
|
|
|
|
if (fanRight && gx == m_resolution - 2)
|
|
{
|
|
if (gz % 2 == 0)
|
|
{
|
|
tri2[0] = topRight;
|
|
tri2[1] = bottomLeft;
|
|
tri2[2] = bottomRight + m_resolution;
|
|
}
|
|
else
|
|
{
|
|
uTri2 = false;
|
|
}
|
|
}
|
|
|
|
if (fanBottom && gz == m_resolution - 2)
|
|
{
|
|
if (gx % 2 == 0)
|
|
{
|
|
tri2[0] = bottomLeft;
|
|
tri2[1] = bottomRight + 1;
|
|
tri2[2] = topRight;
|
|
}
|
|
else
|
|
{
|
|
uTri1 = false;
|
|
}
|
|
}
|
|
|
|
if (fanLeft && gx == 0)
|
|
{
|
|
if (gz % 2 == 0)
|
|
{
|
|
tri1[0] = topLeft;
|
|
tri1[1] = bottomLeft + m_resolution;
|
|
tri1[2] = bottomRight;
|
|
}
|
|
else
|
|
{
|
|
uTri1 = false;
|
|
}
|
|
}
|
|
|
|
if (uTri1)
|
|
{
|
|
buffer->indices.push_back(tri1[0]);
|
|
buffer->indices.push_back(tri1[1]);
|
|
buffer->indices.push_back(tri1[2]);
|
|
}
|
|
|
|
if (uTri2)
|
|
{
|
|
buffer->indices.push_back(tri2[0]);
|
|
buffer->indices.push_back(tri2[1]);
|
|
buffer->indices.push_back(tri2[2]);
|
|
}
|
|
|
|
slantLeft = !slantLeft;
|
|
}
|
|
}
|
|
|
|
glGenBuffers(1, &(buffer->ebo));
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer->ebo);
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * buffer->indices.size(), &(buffer->indices[0]), GL_STATIC_DRAW);
|
|
|
|
m_buffers[idx] = buffer;
|
|
}
|