GeometryEngine.cpp 3.14 KB
Newer Older
1
#include "..\Header\GeometryEngine.h"
Anakin's avatar
Anakin committed
2
#include "..\Header\MshFile.h"
3

Anakin's avatar
Anakin committed
4 5 6 7

/////////////////////////////////////////////////////////////////////////
// public constructor/destructor

8
GeometryEngine::GeometryEngine()
Anakin's avatar
Anakin committed
9
	: m_indexBuf(QOpenGLBuffer::IndexBuffer)
10
{
Anakin's avatar
Anakin committed
11
	initializeOpenGLFunctions();
12

Anakin's avatar
Anakin committed
13
	// Generate 2 VBOs
Anakin's avatar
Anakin committed
14 15
	m_arrayBuf.create();
	m_indexBuf.create();
16

Anakin's avatar
Anakin committed
17 18
	// Initializes cube geometry and transfers it to VBOs
	initCubeGeometry();
19 20 21 22
}

GeometryEngine::~GeometryEngine()
{
Anakin's avatar
Anakin committed
23 24 25 26 27 28 29
	m_arrayBuf.destroy();
	m_indexBuf.destroy();

	for (auto it : m_textures)
		delete it;
	m_textures.clear();
	m_textures.squeeze();
30 31
}

Anakin's avatar
Anakin committed
32 33 34 35

/////////////////////////////////////////////////////////////////////////
// private functions

36 37
void GeometryEngine::initCubeGeometry()
{
Anakin's avatar
Anakin committed
38

Anakin's avatar
Anakin committed
39 40
	try
	{
Anakin's avatar
Anakin committed
41
		MshFile file("..\\Release\\Msh\\cubeTex.msh");
Anakin's avatar
Anakin committed
42 43 44 45 46 47 48 49 50 51
		m_models = file.getModels();
		//TODO use models local, apply MVP directly to the vertex, save size and tex index info

		//TODO: handle the textures
	}
	catch (std::invalid_argument e)
	{
		//TODO: make a cool message box
		auto msg = e.what();
	}
52

Anakin's avatar
Anakin committed
53 54
	// Transfer vertex data to VBO 0
	m_arrayBuf.bind();
Anakin's avatar
Anakin committed
55
	m_arrayBuf.allocate(m_models->first()->segmList.front()->vertices.data(), m_models->first()->segmList.front()->vertices.size() * sizeof(VertexData));
Anakin's avatar
Anakin committed
56 57 58

	// Transfer index data to VBO 1
	m_indexBuf.bind();
Anakin's avatar
Anakin committed
59
	m_indexBuf.allocate(m_models->first()->segmList.front()->indices.data(), m_models->first()->segmList.front()->indices.size() * sizeof(GLuint));
Anakin's avatar
Anakin committed
60

Anakin's avatar
Anakin committed
61
	// load the texture
Anakin's avatar
Anakin committed
62
	initTexture();
63 64
}

Anakin's avatar
Anakin committed
65
void GeometryEngine::initTexture()
66
{
Anakin's avatar
Anakin committed
67
	// Load cube.png image
Anakin's avatar
Anakin committed
68
	QOpenGLTexture* new_texture = new QOpenGLTexture(QImage(":images/cube.png").mirrored());
Anakin's avatar
Anakin committed
69 70

	// Set nearest filtering mode for texture minification
Anakin's avatar
Anakin committed
71
	new_texture->setMinificationFilter(QOpenGLTexture::Nearest);
Anakin's avatar
Anakin committed
72 73

	// Set bilinear filtering mode for texture magnification
Anakin's avatar
Anakin committed
74
	new_texture->setMagnificationFilter(QOpenGLTexture::Linear);
Anakin's avatar
Anakin committed
75 76 77

	// Wrap texture coordinates by repeating
	// f.ex. texture coordinate (1.1, 1.2) is same as (0.1, 0.2)
Anakin's avatar
Anakin committed
78 79 80
	new_texture->setWrapMode(QOpenGLTexture::Repeat);

	m_textures.push_back(new_texture);
Anakin's avatar
Anakin committed
81 82
}

Anakin's avatar
Anakin committed
83 84 85 86

/////////////////////////////////////////////////////////////////////////
// public functions

Anakin's avatar
Anakin committed
87 88 89
void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
{
	// Tell OpenGL which VBOs to use
Anakin's avatar
Anakin committed
90 91 92
	m_arrayBuf.bind();
	m_indexBuf.bind();
	m_textures.first()->bind();
Anakin's avatar
Anakin committed
93 94 95

	// Use texture unit 0 which contains cube.png
	program->setUniformValue("texture", 0);
96

Anakin's avatar
Anakin committed
97 98
	// Offset for position
	quintptr offset = 0;
99

Anakin's avatar
Anakin committed
100 101 102 103
	// 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));
104

Anakin's avatar
Anakin committed
105 106
	// Offset for texture coordinate
	offset += sizeof(QVector3D);
107

Anakin's avatar
Anakin committed
108 109 110 111
	// 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));
112

Anakin's avatar
Anakin committed
113
	// Draw cube geometry using indices from VBO 1
Anakin's avatar
Anakin committed
114
	glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
115
}