#include "geometryengine.h"
#include <QVector2D>
#include <QVector3D>
struct VertexData
{
QVector3D position;
QVector2D texCoord;
};
GeometryEngine::GeometryEngine()
: indexBuf(QOpenGLBuffer::IndexBuffer)
{
initializeOpenGLFunctions();
arrayBuf.create();
indexBuf.create();
initCubeGeometry();
}
GeometryEngine::~GeometryEngine()
{
arrayBuf.destroy();
indexBuf.destroy();
}
void GeometryEngine::initCubeGeometry()
{
VertexData vertices[] = {
{QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(0.0f, 0.0f)},
{QVector3D( 1.0f, -1.0f, 1.0f), QVector2D(0.33f, 0.0f)},
{QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(0.0f, 0.5f)},
{QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.33f, 0.5f)},
{QVector3D( 1.0f, -1.0f, 1.0f), QVector2D( 0.0f, 0.5f)},
{QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.33f, 0.5f)},
{QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.0f, 1.0f)},
{QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.33f, 1.0f)},
{QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.5f)},
{QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(1.0f, 0.5f)},
{QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.66f, 1.0f)},
{QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(1.0f, 1.0f)},
{QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.0f)},
{QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(1.0f, 0.0f)},
{QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(0.66f, 0.5f)},
{QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(1.0f, 0.5f)},
{QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(0.33f, 0.0f)},
{QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.0f)},
{QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(0.33f, 0.5f)},
{QVector3D( 1.0f, -1.0f, 1.0f), QVector2D(0.66f, 0.5f)},
{QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(0.33f, 0.5f)},
{QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.66f, 0.5f)},
{QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(0.33f, 1.0f)},
{QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.66f, 1.0f)}
};
GLushort indices[] = {
0, 1, 2, 3, 3,
4, 4, 5, 6, 7, 7,
8, 8, 9, 10, 11, 11,
12, 12, 13, 14, 15, 15,
16, 16, 17, 18, 19, 19,
20, 20, 21, 22, 23
};
arrayBuf.bind();
arrayBuf.allocate(vertices, 24 * sizeof(VertexData));
indexBuf.bind();
indexBuf.allocate(indices, 34 * sizeof(GLushort));
}
void GeometryEngine::drawCubeGeometry(QOpenGLShaderProgram *program)
{
arrayBuf.bind();
indexBuf.bind();
quintptr offset = 0;
int vertexLocation = program->attributeLocation("a_position");
program->enableAttributeArray(vertexLocation);
program->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
offset += sizeof(QVector3D);
int texcoordLocation = program->attributeLocation("a_texcoord");
program->enableAttributeArray(texcoordLocation);
program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData));
glDrawElements(GL_TRIANGLE_STRIP, 34, GL_UNSIGNED_SHORT, 0);
}