removed vertex class (i have my own data structure)
implemented the glInit and glPaint function
This commit is contained in:
parent
53ac8c3e5b
commit
1cc4f1ca90
|
@ -4,6 +4,8 @@
|
||||||
#include <QOpenGLBuffer>
|
#include <QOpenGLBuffer>
|
||||||
#include <QOPenGLVertexArrayObject>
|
#include <QOPenGLVertexArrayObject>
|
||||||
#include <QOpenGlShaderProgram>
|
#include <QOpenGlShaderProgram>
|
||||||
|
#include <QOpenGlTexture>
|
||||||
|
#include <QImage>
|
||||||
#include "..\Header\MshFile.h"
|
#include "..\Header\MshFile.h"
|
||||||
|
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
|
@ -11,13 +13,6 @@ struct Vertex {
|
||||||
GLfloat uv[2];
|
GLfloat uv[2];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct textureData {
|
|
||||||
bool alpha;
|
|
||||||
std::uint32_t width;
|
|
||||||
std::uint32_t height;
|
|
||||||
std::vector<std::uint8_t>* data;
|
|
||||||
};
|
|
||||||
|
|
||||||
class OpenGlViewer : public QOpenGLWidget, protected QOpenGLFunctions
|
class OpenGlViewer : public QOpenGLWidget, protected QOpenGLFunctions
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -28,13 +23,15 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// OpenGL ======================================
|
// OpenGL ======================================
|
||||||
|
int m_uniformMVP;
|
||||||
|
QOpenGLTexture* m_oglTexture;
|
||||||
QOpenGLBuffer m_vertexBuffer;
|
QOpenGLBuffer m_vertexBuffer;
|
||||||
QOpenGLVertexArrayObject m_vertexArray;
|
QOpenGLVertexArrayObject m_vertexArray;
|
||||||
QOpenGLShaderProgram* m_program = nullptr;
|
QOpenGLShaderProgram* m_program = nullptr;
|
||||||
|
|
||||||
// Data ========================================
|
// Data ========================================
|
||||||
std::vector<Model*>* m_vModels = nullptr;
|
std::vector<Model*>* m_vModels = nullptr;
|
||||||
std::vector<textureData*> m_vTextures;
|
std::vector<QImage*> m_vTextures;
|
||||||
BoundingBox m_sceneBoundings;
|
BoundingBox m_sceneBoundings;
|
||||||
|
|
||||||
// Transformation ==============================
|
// Transformation ==============================
|
||||||
|
@ -56,8 +53,9 @@ private:
|
||||||
virtual void paintGL() override final;
|
virtual void paintGL() override final;
|
||||||
|
|
||||||
void printContextInformation();
|
void printContextInformation();
|
||||||
|
QMatrix4x4 getMVPMatrix(unsigned int index) const;
|
||||||
void deleteData();
|
void deleteData();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void setData(std::vector<Model*>* models, std::vector<textureData*> textures);
|
void setData(std::vector<Model*>* models, std::vector<QImage*> textures);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,52 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <QVector3D>
|
|
||||||
|
|
||||||
class Vertex
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
// Constructors
|
|
||||||
Q_DECL_CONSTEXPR Vertex();
|
|
||||||
Q_DECL_CONSTEXPR explicit Vertex(const QVector3D &position);
|
|
||||||
Q_DECL_CONSTEXPR Vertex(const QVector3D &position, const QVector3D &color);
|
|
||||||
|
|
||||||
// Accessors / Mutators
|
|
||||||
Q_DECL_CONSTEXPR const QVector3D& position() const;
|
|
||||||
Q_DECL_CONSTEXPR const QVector3D& color() const;
|
|
||||||
void setPosition(const QVector3D& position);
|
|
||||||
void setColor(const QVector3D& color);
|
|
||||||
|
|
||||||
// OpenGL Helpers
|
|
||||||
static const int PositionTupleSize = 3;
|
|
||||||
static const int ColorTupleSize = 3;
|
|
||||||
static Q_DECL_CONSTEXPR int positionOffset();
|
|
||||||
static Q_DECL_CONSTEXPR int colorOffset();
|
|
||||||
static Q_DECL_CONSTEXPR int stride();
|
|
||||||
|
|
||||||
private:
|
|
||||||
QVector3D m_position;
|
|
||||||
QVector3D m_color;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*******************************************************************************
|
|
||||||
* Inline Implementation
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
// Note: Q_MOVABLE_TYPE means it can be memcpy'd.
|
|
||||||
Q_DECLARE_TYPEINFO(Vertex, Q_MOVABLE_TYPE);
|
|
||||||
|
|
||||||
// Constructors
|
|
||||||
Q_DECL_CONSTEXPR inline Vertex::Vertex() {}
|
|
||||||
Q_DECL_CONSTEXPR inline Vertex::Vertex(const QVector3D &position) : m_position(position) {}
|
|
||||||
Q_DECL_CONSTEXPR inline Vertex::Vertex(const QVector3D &position, const QVector3D &color) : m_position(position), m_color(color) {}
|
|
||||||
|
|
||||||
// Accessors / Mutators
|
|
||||||
Q_DECL_CONSTEXPR inline const QVector3D& Vertex::position() const { return m_position; }
|
|
||||||
Q_DECL_CONSTEXPR inline const QVector3D& Vertex::color() const { return m_color; }
|
|
||||||
void inline Vertex::setPosition(const QVector3D& position) { m_position = position; }
|
|
||||||
void inline Vertex::setColor(const QVector3D& color) { m_color = color; }
|
|
||||||
|
|
||||||
// OpenGL Helpers
|
|
||||||
Q_DECL_CONSTEXPR inline int Vertex::positionOffset() { return offsetof(Vertex, m_position); }
|
|
||||||
Q_DECL_CONSTEXPR inline int Vertex::colorOffset() { return offsetof(Vertex, m_color); }
|
|
||||||
Q_DECL_CONSTEXPR inline int Vertex::stride() { return sizeof(Vertex); }
|
|
|
@ -1,5 +1,4 @@
|
||||||
#include "..\Header\OpenGlViewer.h"
|
#include "..\Header\OpenGlViewer.h"
|
||||||
#include "..\Header\defines.h"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,14 +32,17 @@ OpenGlViewer::OpenGlViewer(QWidget *parent)
|
||||||
{
|
{
|
||||||
QSurfaceFormat format;
|
QSurfaceFormat format;
|
||||||
format.setRenderableType(QSurfaceFormat::OpenGL);
|
format.setRenderableType(QSurfaceFormat::OpenGL);
|
||||||
|
format.setSamples(4);
|
||||||
format.setProfile(QSurfaceFormat::CompatibilityProfile);
|
format.setProfile(QSurfaceFormat::CompatibilityProfile);
|
||||||
format.setVersion(DEFAULT_MAJOR_VERSION, DEFAULT_MINOR_VERSION);
|
format.setVersion(DEFAULT_MAJOR_VERSION, DEFAULT_MINOR_VERSION);
|
||||||
|
|
||||||
setFormat(format);
|
setFormat(format);
|
||||||
|
|
||||||
|
//TODO: mouse, move, key, drag/drop, scroll, resize
|
||||||
}
|
}
|
||||||
|
|
||||||
OpenGlViewer::~OpenGlViewer()
|
OpenGlViewer::~OpenGlViewer()
|
||||||
{
|
{
|
||||||
|
m_oglTexture->destroy();
|
||||||
m_vertexArray.destroy();
|
m_vertexArray.destroy();
|
||||||
m_vertexBuffer.destroy();
|
m_vertexBuffer.destroy();
|
||||||
delete m_program;
|
delete m_program;
|
||||||
|
@ -57,26 +59,45 @@ void OpenGlViewer::initializeGL()
|
||||||
initializeOpenGLFunctions();
|
initializeOpenGLFunctions();
|
||||||
printContextInformation();
|
printContextInformation();
|
||||||
|
|
||||||
//glEnable(GL_DEPTH_TEST);
|
// set Background
|
||||||
glClearColor(DEAFAULT_BACKGROUND);
|
glClearColor(DEAFAULT_BACKGROUND);
|
||||||
|
|
||||||
|
//TODO: z-order?
|
||||||
|
|
||||||
|
// draw vertices only from one side
|
||||||
|
glEnable(GL_CULL_FACE);
|
||||||
|
|
||||||
|
// Create texture
|
||||||
|
m_oglTexture = new QOpenGLTexture(QOpenGLTexture::Target2D);
|
||||||
|
m_oglTexture->setWrapMode(QOpenGLTexture::Repeat);
|
||||||
|
m_oglTexture->setMagnificationFilter(QOpenGLTexture::Linear);
|
||||||
|
m_oglTexture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
|
||||||
|
|
||||||
|
// Create Shader
|
||||||
m_program = new QOpenGLShaderProgram();
|
m_program = new QOpenGLShaderProgram();
|
||||||
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/simple.vert");
|
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/simple.vert");
|
||||||
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/simple.frag");
|
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/simple.frag");
|
||||||
m_program->link();
|
m_program->link();
|
||||||
m_program->bind();
|
m_program->bind();
|
||||||
|
|
||||||
|
// get Uniform location
|
||||||
|
//TODO: faster to give everything to shader and calculate there?
|
||||||
|
m_uniformMVP = m_program->uniformLocation("MVP");
|
||||||
|
|
||||||
|
// Create Vertex Buffer
|
||||||
m_vertexBuffer.create();
|
m_vertexBuffer.create();
|
||||||
m_vertexBuffer.bind();
|
m_vertexBuffer.bind();
|
||||||
m_vertexBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
|
m_vertexBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
|
||||||
|
|
||||||
|
// Create Vertex Array Object
|
||||||
m_vertexArray.create();
|
m_vertexArray.create();
|
||||||
m_vertexArray.bind();
|
m_vertexArray.bind();
|
||||||
m_program->enableAttributeArray(0);
|
m_program->enableAttributeArray(0);
|
||||||
m_program->enableAttributeArray(1);
|
m_program->enableAttributeArray(1);
|
||||||
//m_program->setAttributeBuffer(0, GL_FLOAT, Vertex::positionOffset(), Vertex::PositionTupleSize, Vertex::stride());
|
m_program->setAttributeBuffer(VERTEX_INDEX_XYZ, GL_FLOAT, VERTEX_OFFSET_XYZ, VERTEX_COMPONENTS_XYZ, sizeof(Vertex));
|
||||||
//m_program->setAttributeBuffer(1, GL_FLOAT, Vertex::colorOffset(), Vertex::ColorTupleSize, Vertex::stride());
|
m_program->setAttributeBuffer(VERTEX_INDEX_UV, GL_FLOAT, VERTEX_OFFSET_UV, VERTEX_COMPONENTS_UV, sizeof(Vertex));
|
||||||
|
|
||||||
|
// unbind everything
|
||||||
m_vertexArray.release();
|
m_vertexArray.release();
|
||||||
m_vertexBuffer.release();
|
m_vertexBuffer.release();
|
||||||
m_program->release();
|
m_program->release();
|
||||||
|
@ -95,7 +116,44 @@ void OpenGlViewer::paintGL()
|
||||||
|
|
||||||
m_program->bind();
|
m_program->bind();
|
||||||
m_vertexArray.bind();
|
m_vertexArray.bind();
|
||||||
|
m_oglTexture->bind();
|
||||||
|
|
||||||
|
if (m_vModels != nullptr)
|
||||||
|
{
|
||||||
|
int tmp_offset(0);
|
||||||
|
|
||||||
|
for (unsigned int modelIndex = 0; modelIndex < m_vModels->size(); modelIndex++)
|
||||||
|
{
|
||||||
|
// skip null, bones, shadowMesh, hidden things
|
||||||
|
if (m_vModels->at(modelIndex)->type == null ||
|
||||||
|
m_vModels->at(modelIndex)->type == bone ||
|
||||||
|
m_vModels->at(modelIndex)->type == shadowMesh ||
|
||||||
|
m_vModels->at(modelIndex)->renderFlags == 1)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (auto& segmentIterator : m_vModels->at(modelIndex)->segmList)
|
||||||
|
{
|
||||||
|
// set the texture
|
||||||
|
std::uint32_t tmp_textureIndex = segmentIterator->textureIndex >= m_vTextures.size() ? m_vTextures.size() - 1 : segmentIterator->textureIndex;
|
||||||
|
m_oglTexture->setData(*m_vTextures.at(tmp_textureIndex));
|
||||||
|
|
||||||
|
// give the MVP to the shader
|
||||||
|
m_program->setUniformValue(m_uniformMVP, getMVPMatrix(modelIndex));
|
||||||
|
|
||||||
|
// calculate the number of vertex
|
||||||
|
unsigned int tmp_vertexCount(0);
|
||||||
|
for (auto&it : segmentIterator->polyIndices)
|
||||||
|
tmp_vertexCount += (it.size() - 2) * 3;
|
||||||
|
|
||||||
|
glDrawArrays(GL_TRIANGLES, tmp_offset, tmp_vertexCount);
|
||||||
|
|
||||||
|
// increase the offset
|
||||||
|
tmp_offset += tmp_vertexCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
//glDrawArrays(GL_TRIANGLES, 0, sizeof(sg_vertexes) / sizeof(sg_vertexes[0]));
|
//glDrawArrays(GL_TRIANGLES, 0, sizeof(sg_vertexes) / sizeof(sg_vertexes[0]));
|
||||||
|
m_oglTexture->release();
|
||||||
m_vertexArray.release();
|
m_vertexArray.release();
|
||||||
m_program->release();
|
m_program->release();
|
||||||
}
|
}
|
||||||
|
@ -121,6 +179,11 @@ void OpenGlViewer::printContextInformation()
|
||||||
std::cout << glType.toStdString() << " - " << glVersion.toStdString() << " (" << glProfile.toStdString() << ")";
|
std::cout << glType.toStdString() << " - " << glVersion.toStdString() << " (" << glProfile.toStdString() << ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QMatrix4x4 OpenGlViewer::getMVPMatrix(unsigned int index) const
|
||||||
|
{
|
||||||
|
return QMatrix4x4();
|
||||||
|
}
|
||||||
|
|
||||||
void OpenGlViewer::deleteData()
|
void OpenGlViewer::deleteData()
|
||||||
{
|
{
|
||||||
if (m_vModels != NULL)
|
if (m_vModels != NULL)
|
||||||
|
@ -163,14 +226,11 @@ void OpenGlViewer::deleteData()
|
||||||
while (!m_vTextures.empty())
|
while (!m_vTextures.empty())
|
||||||
{
|
{
|
||||||
// remove the last texture
|
// remove the last texture
|
||||||
textureData* cursor = m_vTextures.back();
|
QImage* cursor = m_vTextures.back();
|
||||||
m_vTextures.pop_back();
|
m_vTextures.pop_back();
|
||||||
|
|
||||||
// delete the texture's data
|
//delete image
|
||||||
cursor->data->clear();
|
delete cursor;
|
||||||
|
|
||||||
//delete the texture's data vector
|
|
||||||
delete cursor->data;
|
|
||||||
|
|
||||||
//delete the texture
|
//delete the texture
|
||||||
delete cursor;
|
delete cursor;
|
||||||
|
@ -181,7 +241,7 @@ void OpenGlViewer::deleteData()
|
||||||
/////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////
|
||||||
// public functions
|
// public functions
|
||||||
|
|
||||||
void OpenGlViewer::setData(std::vector<Model*>* models, std::vector<textureData*> textures)
|
void OpenGlViewer::setData(std::vector<Model*>* models, std::vector<QImage*> textures)
|
||||||
{
|
{
|
||||||
m_vModels = models;
|
m_vModels = models;
|
||||||
m_vTextures = textures;
|
m_vTextures = textures;
|
||||||
|
|
Loading…
Reference in New Issue