-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtriangle.cpp
72 lines (54 loc) · 1.52 KB
/
triangle.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "triangle.h"
#include <iostream>
struct VertexData
{
QVector3D position;
QVector2D texCoord;
};
Triangle::Triangle():
indexBuf(QOpenGLBuffer::IndexBuffer)
{
initializeOpenGLFunctions();
// Generate 2 VBOs
arrayBuf.create();
indexBuf.create();
// Initializes cube geometry and transfers it to VBOs
initTriangle();
color = QVector4D(1,0,0,1);
}
Triangle::~Triangle()
{
arrayBuf.destroy();
indexBuf.destroy();
}
void Triangle::initTriangle()
{
QVector3D vertices[] = {
// Vertex data for face 0
QVector3D(-1.0f, -1.0f, 1.0f),
QVector3D( 1.0f, -1.0f, 1.0f),
QVector3D(-1.0f, 1.0f, 1.0f)
};
GLushort indices[] = {
0, 1, 2,
};
arrayBuf.bind();
arrayBuf.allocate(vertices, 3 * sizeof(QVector3D));
indexBuf.bind();
indexBuf.allocate(indices, 3 * sizeof(GLushort));
}
void Triangle::drawTriangle(QOpenGLShaderProgram *program)
{
// Tell OpenGL which VBOs to use
arrayBuf.bind();
indexBuf.bind();
program->setUniformValue("objectColor", color);
// Offset for position
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(QVector3D));
// Draw cube geometry using indices from VBO 1
glDrawElements(GL_TRIANGLE_STRIP, 3, GL_UNSIGNED_SHORT, 0);
}