-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometryengine.cpp
54 lines (45 loc) · 1.65 KB
/
geometryengine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "geometryengine.h"
GeometryEngine::GeometryEngine(Figure * __figure)
: indexBuf(QOpenGLBuffer::IndexBuffer), figure(__figure)
{
initializeOpenGLFunctions();
// Generate 2 VBOs
arrayBuf.create();
indexBuf.create();
// Initializes geometry and transfers it to VBOs
initGeometry();
}
GeometryEngine::~GeometryEngine()
{
arrayBuf.destroy();
indexBuf.destroy();
delete figure;
}
void GeometryEngine::initGeometry()
{
// Transfer vertex data to VBO 0
arrayBuf.bind();
arrayBuf.allocate(figure->verteces, figure->verticesNum * sizeof(VertexData));
// Transfer index data to VBO 1
indexBuf.bind();
indexBuf.allocate(figure->indices, figure->indicesNum * sizeof(unsigned short));
}
void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
{
// Tell OpenGL which VBOs to use
arrayBuf.bind();
indexBuf.bind();
// Otstup
quintptr offset = 0;
// Tell OpenGL programmable pipeline how to locate vertex position data
int vertexLocation = program->attributeLocation("a_position");
program->enableAttributeArray(vertexLocation);
program->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
offset += sizeof(QVector3D);
// Tell OpenGL programmable pipeline how to locate vertex texture coordinate data
int texcoordLocation = program->attributeLocation("a_texcoord");
program->enableAttributeArray(texcoordLocation);
program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData));
// Draw cube geometry using indices from VBO 1
glDrawElements(GL_TRIANGLE_STRIP, figure->indicesNum, GL_UNSIGNED_SHORT, 0);
}