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

Anakin's avatar
Anakin committed
7 8 9 10

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

11 12 13
GeometryEngine::GeometryEngine(QObject *parent)
	: QObject(parent)
	, m_indexBuf(QOpenGLBuffer::IndexBuffer)
14
{
Anakin's avatar
Anakin committed
15
	initializeOpenGLFunctions();
16 17 18 19
}

GeometryEngine::~GeometryEngine()
{
Anakin's avatar
Anakin committed
20
	clearData();
21 22
}

Anakin's avatar
Anakin committed
23 24 25 26

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

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

	//reset view
	emit requestResetView();
36
	emit sendMessage("loading file..", 0);
Anakin's avatar
Anakin committed
37

Anakin's avatar
Anakin committed
38 39
	try
	{
Anakin's avatar
Anakin committed
40
		//TODO normalize
Anakin's avatar
Anakin committed
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
48 49
		MshFile file(filePath, this);

Anakin's avatar
Anakin committed
50 51
		models = file.getModels();
		textureNames = file.getTextureNames();
Anakin's avatar
Anakin committed
52
		m_boundings = file.getBoundingBox();
Anakin's avatar
Anakin committed
53 54

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

Anakin's avatar
Anakin committed
69 70 71 72
				// 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
73 74 75 76 77 78 79

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

				// update offset
Anakin's avatar
Anakin committed
80 81
				indexOffset += new_info.size;
				vertexOffset += segmentIterator->vertices.size();
Anakin's avatar
Anakin committed
82 83 84 85 86 87 88 89 90 91 92
			}
		}

		// 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
93 94 95 96 97 98
		// get textures path
		std::string path = filePath;

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

99
		emit sendMessage("loading textures..", 0);
Anakin's avatar
Anakin committed
100 101 102
		// load the textures
		for(auto& it : *textureNames)
			loadTexture(std::string(path + it).c_str());
Anakin's avatar
Anakin committed
103

104 105
		emit requestUpdate();
		emit sendMessage("done..", 0);
Anakin's avatar
Anakin committed
106 107 108
	}
	catch (std::invalid_argument e)
	{
109
		emit sendMessage(QString(e.what()), 2);
Anakin's avatar
Anakin committed
110
	}
111 112
}

Anakin's avatar
Anakin committed
113
void GeometryEngine::loadTexture(const char* filePath)
114
{
Anakin's avatar
Anakin committed
115 116 117 118 119 120 121 122 123 124
	
	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
125 126

	// Set nearest filtering mode for texture minification
Anakin's avatar
Anakin committed
127
	new_texture->setMinificationFilter(QOpenGLTexture::Nearest);
Anakin's avatar
Anakin committed
128 129

	// Set bilinear filtering mode for texture magnification
Anakin's avatar
Anakin committed
130
	new_texture->setMagnificationFilter(QOpenGLTexture::Linear);
Anakin's avatar
Anakin committed
131 132 133

	// 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
134 135 136
	new_texture->setWrapMode(QOpenGLTexture::Repeat);

	m_textures.push_back(new_texture);
Anakin's avatar
Anakin committed
137 138
}

Anakin's avatar
Anakin committed
139 140 141 142 143 144 145 146 147 148 149 150 151
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
152 153 154 155

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

Anakin's avatar
Anakin committed
156 157
void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
{
Anakin's avatar
Anakin committed
158 159 160
	if (!m_arrayBuf.isCreated() || !m_indexBuf.isCreated())
		return;

Anakin's avatar
Anakin committed
161
// Setup
Anakin's avatar
Anakin committed
162
	// Tell OpenGL which VBOs to use
Anakin's avatar
Anakin committed
163 164
	m_arrayBuf.bind();
	m_indexBuf.bind();
Anakin's avatar
Anakin committed
165

Anakin's avatar
Anakin committed
166 167 168 169 170 171 172
	// 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
173
	// Allways use texture unit 0
Anakin's avatar
Anakin committed
174
	program->setUniformValue("texture", 0);
175

Anakin's avatar
Anakin committed
176 177
	// Offset for position
	quintptr offset = 0;
178

Anakin's avatar
Anakin committed
179 180 181 182
	// 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));
183

Anakin's avatar
Anakin committed
184 185
	// Offset for texture coordinate
	offset += sizeof(QVector3D);
186

Anakin's avatar
Anakin committed
187 188 189 190
	// 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));
191

Anakin's avatar
Anakin committed
192 193 194 195 196 197 198
// 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
199 200
		else
			m_textures.last()->bind();
Anakin's avatar
Anakin committed
201 202 203 204 205 206 207

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