GeometryEngine.cpp 5.19 KB
Newer Older
1
#include "..\Header\GeometryEngine.h"
Anakin's avatar
Anakin committed
2
#include "..\Header\MshFile.h"
Anakin's avatar
Anakin committed
3
#include <cmath>
4

Anakin's avatar
Anakin committed
5 6 7 8

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

9
GeometryEngine::GeometryEngine()
Anakin's avatar
Anakin committed
10
	: m_indexBuf(QOpenGLBuffer::IndexBuffer)
11
{
Anakin's avatar
Anakin committed
12
	initializeOpenGLFunctions();
13 14 15 16
}

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

Anakin's avatar
Anakin committed
20 21 22 23

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

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

	//reset view
	emit requestResetView();

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

		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
47
		m_boundings = file.getBoundingBox();
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

Anakin's avatar
Anakin committed
64 65 66 67
				// add offset to indices, no need to do it for the first one (maybe it's very big)
				if(vertexOffset != 0)
					for (auto& it : segmentIterator->indices)
						it += vertexOffset;
Anakin's avatar
Anakin committed
68 69 70 71 72 73 74

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

				// update offset
Anakin's avatar
Anakin committed
75 76
				indexOffset += new_info.size;
				vertexOffset += segmentIterator->vertices.size();
Anakin's avatar
Anakin committed
77 78 79 80 81 82 83 84 85 86 87
			}
		}

		// 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
88 89 90 91 92 93 94 95 96
		// 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
97

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

Anakin's avatar
Anakin committed
105
	
106 107
}

Anakin's avatar
Anakin committed
108
void GeometryEngine::loadTexture(const char* filePath)
109
{
Anakin's avatar
Anakin committed
110 111 112 113 114 115 116 117 118 119
	
	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
120 121

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

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

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

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

Anakin's avatar
Anakin committed
134 135 136 137 138 139 140 141 142 143 144 145 146
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
147 148 149 150

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

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

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

Anakin's avatar
Anakin committed
161 162 163 164 165 166 167
	// Allways normalize by this
	QMatrix4x4 normMatrix;
	float maxExtent = std::max(std::max(m_boundings.extents[0], m_boundings.extents[1]), m_boundings.extents[2]);
	normMatrix.scale(1 / maxExtent);
	normMatrix.translate(-m_boundings.center[0], -m_boundings.center[1], -m_boundings.center[2]);
	program->setUniformValue("norm_matrix", normMatrix);

Anakin's avatar
Anakin committed
168
	// Allways use texture unit 0
Anakin's avatar
Anakin committed
169
	program->setUniformValue("texture", 0);
170

Anakin's avatar
Anakin committed
171 172
	// Offset for position
	quintptr offset = 0;
173

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

Anakin's avatar
Anakin committed
179 180
	// Offset for texture coordinate
	offset += sizeof(QVector3D);
181

Anakin's avatar
Anakin committed
182 183 184 185
	// 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));
186

Anakin's avatar
Anakin committed
187 188 189 190 191 192 193
// 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
194 195
		else
			m_textures.last()->bind();
Anakin's avatar
Anakin committed
196 197 198 199 200 201 202

		// 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)));
	}
203
}