GeometryEngine.cpp 4.79 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
	loadFile("..\\Release\\Msh\\5-Poly.msh");
14 15 16 17
}

GeometryEngine::~GeometryEngine()
{
Anakin's avatar
Anakin committed
18
	clearData();
19 20
}

Anakin's avatar
Anakin committed
21 22 23 24

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

Anakin's avatar
Anakin committed
25
void GeometryEngine::loadFile(const char* filePath)
26
{
Anakin's avatar
Anakin committed
27 28 29 30 31 32 33 34
	// cleanup old stuff and recreate buffers
	clearData();
	m_arrayBuf.create();
	m_indexBuf.create();

	//reset view
	emit requestResetView();

Anakin's avatar
Anakin committed
35 36
	try
	{
Anakin's avatar
Anakin committed
37
		//TODO normalize
Anakin's avatar
Anakin committed
38 39 40 41 42 43 44 45 46 47

		QVector<Model*>* models;
		QVector<std::string>* textureNames;
		QVector<VertexData> vertexData;
		QVector<GLuint> indexData;

		// open file and get the information
		MshFile file(filePath);
		models = file.getModels();
		textureNames = file.getTextureNames();
Anakin's avatar
Anakin committed
48 49

		// collect data
Anakin's avatar
Anakin committed
50 51
		unsigned int indexOffset(0);
		unsigned int vertexOffset(0);
Anakin's avatar
Anakin committed
52 53 54 55 56 57
		for (auto& modelIterator : *models)
		{
			for (auto& segmentIterator : modelIterator->segmList)
			{
				// get draw information
				DrawInformation new_info;
Anakin's avatar
Anakin committed
58
				new_info.offset = indexOffset;
Anakin's avatar
Anakin committed
59 60 61
				new_info.size = segmentIterator->indices.size();
				new_info.textureIndex = segmentIterator->textureIndex;
				new_info.modelMatrix = modelIterator->m4x4Translation;
Anakin's avatar
Anakin committed
62
				new_info.modelMatrix.rotate(modelIterator->quadRotation);
Anakin's avatar
Anakin committed
63 64 65

				// add offset to indices
				for (auto& it : segmentIterator->indices)
Anakin's avatar
Anakin committed
66
					it += vertexOffset;
Anakin's avatar
Anakin committed
67 68 69 70 71 72 73

				// save data
				vertexData += segmentIterator->vertices;
				indexData += segmentIterator->indices;
				m_drawList.push_back(new_info);

				// update offset
Anakin's avatar
Anakin committed
74 75
				indexOffset += new_info.size;
				vertexOffset += segmentIterator->vertices.size();
Anakin's avatar
Anakin committed
76 77 78
			}
		}

Anakin's avatar
Anakin committed
79 80
		//TODO: cleanup old stuff

Anakin's avatar
Anakin committed
81 82 83 84 85 86 87 88
		// Transfer vertex data to VBO 0
		m_arrayBuf.bind();
		m_arrayBuf.allocate(vertexData.data(), vertexData.size() * sizeof(VertexData));

		// Transfer index data to VBO 1
		m_indexBuf.bind();
		m_indexBuf.allocate(indexData.data(), indexData.size() * sizeof(GLuint));

Anakin's avatar
Anakin committed
89 90 91 92 93 94 95 96 97
		// get textures path
		std::string path = filePath;

		while (path.back() != '/' && path.back() != '\\')
			path.pop_back();

		// load the textures
		for(auto& it : *textureNames)
			loadTexture(std::string(path + it).c_str());
Anakin's avatar
Anakin committed
98

Anakin's avatar
Anakin committed
99 100 101 102 103 104
	}
	catch (std::invalid_argument e)
	{
		//TODO: make a cool message box
		auto msg = e.what();
	}
105

Anakin's avatar
Anakin committed
106
	
107 108
}

Anakin's avatar
Anakin committed
109
void GeometryEngine::loadTexture(const char* filePath)
110
{
Anakin's avatar
Anakin committed
111 112 113 114 115 116 117 118 119 120
	
	QImage img;
	if (!img.load(filePath))
	{
		img = QImage(1, 1, QImage::Format_RGB32);
		img.fill(Qt::red);
	}

	// Load image to OglTexture
	QOpenGLTexture* new_texture = new QOpenGLTexture(img.mirrored());
Anakin's avatar
Anakin committed
121 122

	// Set nearest filtering mode for texture minification
Anakin's avatar
Anakin committed
123
	new_texture->setMinificationFilter(QOpenGLTexture::Nearest);
Anakin's avatar
Anakin committed
124 125

	// Set bilinear filtering mode for texture magnification
Anakin's avatar
Anakin committed
126
	new_texture->setMagnificationFilter(QOpenGLTexture::Linear);
Anakin's avatar
Anakin committed
127 128 129

	// 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
130 131 132
	new_texture->setWrapMode(QOpenGLTexture::Repeat);

	m_textures.push_back(new_texture);
Anakin's avatar
Anakin committed
133 134
}

Anakin's avatar
Anakin committed
135 136 137 138 139 140 141 142 143 144 145 146 147
void GeometryEngine::clearData()
{
	if (m_arrayBuf.isCreated())
		m_arrayBuf.destroy();
	if (m_indexBuf.isCreated())
		m_indexBuf.destroy();

	for (auto it : m_textures)
		delete it;
	m_textures.clear();
	m_drawList.clear();
}

Anakin's avatar
Anakin committed
148 149 150 151

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

Anakin's avatar
Anakin committed
152 153
void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
{
Anakin's avatar
Anakin committed
154 155 156
	if (!m_arrayBuf.isCreated() || !m_indexBuf.isCreated())
		return;

Anakin's avatar
Anakin committed
157
// Setup
Anakin's avatar
Anakin committed
158
	// Tell OpenGL which VBOs to use
Anakin's avatar
Anakin committed
159 160
	m_arrayBuf.bind();
	m_indexBuf.bind();
Anakin's avatar
Anakin committed
161

Anakin's avatar
Anakin committed
162
	// Allways use texture unit 0
Anakin's avatar
Anakin committed
163
	program->setUniformValue("texture", 0);
164

Anakin's avatar
Anakin committed
165 166
	// Offset for position
	quintptr offset = 0;
167

Anakin's avatar
Anakin committed
168 169 170 171
	// 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));
172

Anakin's avatar
Anakin committed
173 174
	// Offset for texture coordinate
	offset += sizeof(QVector3D);
175

Anakin's avatar
Anakin committed
176 177 178 179
	// 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));
180

Anakin's avatar
Anakin committed
181 182 183 184 185 186 187
// Paint

	for (auto& it : m_drawList)
	{
		// bind the correct texture
		if (it.textureIndex < m_textures.size())
			m_textures.at(it.textureIndex)->bind();
Anakin's avatar
Anakin committed
188 189
		else
			m_textures.last()->bind();
Anakin's avatar
Anakin committed
190 191 192 193 194 195 196

		// Set model matrix
		program->setUniformValue("m_matrix", it.modelMatrix);

		// Draw cube geometry using indices from VBO 1
		glDrawElements(GL_TRIANGLES, it.size, GL_UNSIGNED_INT, (void*)(it.offset * sizeof(GLuint)));
	}
197
}