First triangle drawn in Qt Project

This commit is contained in:
Anakin 2016-12-10 14:42:00 +01:00
parent 8cf86a41eb
commit 5faf584d84
9 changed files with 220 additions and 19 deletions

View File

@ -1,10 +1,8 @@
<UI version="4.0" >
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindowClass</class>
<widget class="QMainWindow" name="MainWindowClass" >
<property name="objectName" >
<string notr="true">MainWindowClass</string>
</property>
<property name="geometry" >
<widget class="QMainWindow" name="MainWindowClass">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
@ -12,18 +10,26 @@
<height>400</height>
</rect>
</property>
<property name="windowTitle" >
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QMenuBar" name="menuBar" />
<widget class="QToolBar" name="mainToolBar" />
<widget class="QWidget" name="centralWidget" />
<widget class="QStatusBar" name="statusBar" />
<widget class="QWidget" name="centralWidget"/>
<widget class="QToolBar" name="mainToolBar">
<property name="movable">
<bool>false</bool>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<layoutdefault spacing="6" margin="11"/>
<resources>
<include location="MainWindow.qrc"/>
<include location="../Resources/MainWindow.qrc"/>
</resources>
<connections/>
</UI>
</ui>

View File

@ -11,11 +11,21 @@ public:
MainWindow(QWidget *parent = Q_NULLPTR);
~MainWindow();
// Variables
private:
Ui::MainWindowClass* ui;
// Functions
private:
void setupWindow();
// Slots
private slots:
void openFile();
void aboutFile();
// Override Functions
protected:
virtual void keyPressEvent(QKeyEvent * keyEvent) override final;
};

View File

@ -2,6 +2,9 @@
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLBuffer>
#include <QOPenGLVertexArrayObject>
#include <QOpenGlShaderProgram>
class OpenGlViewer : public QOpenGLWidget, protected QOpenGLFunctions
{
@ -11,8 +14,18 @@ public:
OpenGlViewer(QWidget *parent);
~OpenGlViewer();
private:
QOpenGLBuffer mVertexBuffer;
QOpenGLVertexArrayObject mVertexArray;
QOpenGLShaderProgram* mProgram = nullptr;
private:
void printContextInformation();
protected:
virtual void initializeGL() override final;
virtual void resizeGL(int w, int h) override final;
virtual void paintGL() override final;
public:
};

View File

@ -0,0 +1,55 @@
#ifndef VERTEX_H
#define VERTEX_H
#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); }
#endif // VERTEX_H

View File

@ -2,4 +2,8 @@
<qresource prefix="/MainWindow">
<file>icon.ico</file>
</qresource>
<qresource prefix="/shaders">
<file>simple.frag</file>
<file>simple.vert</file>
</qresource>
</RCC>

View File

@ -0,0 +1,8 @@
#version 330
in vec4 vColor;
out vec4 fColor;
void main()
{
fColor = vColor;
}

View File

@ -0,0 +1,10 @@
#version 330
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
out vec4 vColor;
void main()
{
gl_Position = vec4(position, 1.0);
vColor = vec4(color, 1.0);
}

View File

@ -2,8 +2,12 @@
#include "OpenGlViewer.h"
#include "defines.h"
#include <QKeyEvent>
#include <QMessageBox>
/////////////////////////////////////////////////////////////////////////
// constructor/destructor
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui (new Ui::MainWindowClass)
@ -16,6 +20,10 @@ MainWindow::~MainWindow()
delete ui;
}
/////////////////////////////////////////////////////////////////////////
// private functions
void MainWindow::setupWindow()
{
ui->setupUi(this);
@ -26,11 +34,32 @@ void MainWindow::setupWindow()
this->setCentralWidget(new OpenGlViewer(this));
ui->mainToolBar->addAction("File Info", this, &MainWindow::aboutFile);
ui->statusBar->showMessage(DEFAULT_STATUS_MESSAGE);
}
/////////////////////////////////////////////////////////////////////////
// private slots
void MainWindow::openFile()
{
//TODO: Open file
}
void MainWindow::aboutFile()
{
//TODO: Open Window with file information
QMessageBox* dialog = new QMessageBox(QMessageBox::Information, WINDOW_NAME, "File Info", QMessageBox::StandardButton::Close, this, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
dialog->setDetailedText("This is the cool detailed text\n");
dialog->exec();
}
/////////////////////////////////////////////////////////////////////////
// events
void MainWindow::keyPressEvent(QKeyEvent * keyEvent)
{
switch (keyEvent->key())

View File

@ -1,25 +1,84 @@
#include "OpenGlViewer.h"
#include "defines.h"
#include "Vertex.h"
#include <iostream>
static const Vertex sg_vertexes[] = {
Vertex(QVector3D(0.00f, 0.75f, 1.0f), QVector3D(1.0f, 0.0f, 0.0f)),
Vertex(QVector3D(0.75f, -0.75f, 1.0f), QVector3D(0.0f, 1.0f, 0.0f)),
Vertex(QVector3D(-0.75f, -0.75f, 1.0f), QVector3D(0.0f, 0.0f, 1.0f))
};
OpenGlViewer::OpenGlViewer(QWidget *parent)
: QOpenGLWidget(parent)
{
QSurfaceFormat format;
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setProfile(QSurfaceFormat::CompatibilityProfile);
format.setVersion(DEFAULT_MAJOR_VERSION, DEFAULT_MINOR_VERSION);
format.setProfile(QSurfaceFormat::CoreProfile);
this->setFormat(format);
setFormat(format);
}
OpenGlViewer::~OpenGlViewer()
{
mVertexArray.destroy();
mVertexBuffer.destroy();
delete mProgram;
}
void OpenGlViewer::printContextInformation()
{
QString glType;
QString glVersion;
QString glProfile;
glType = (context()->isOpenGLES()) ? "OpenGL ES" : "OpenGL";
glVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
#define CASE(c) case QSurfaceFormat::c: glProfile = #c; break
switch (format().profile())
{
CASE(NoProfile);
CASE(CoreProfile);
CASE(CompatibilityProfile);
}
#undef CASE
std::cout << glType.toStdString() << " - " << glVersion.toStdString() << " (" << glProfile.toStdString() << ")";
}
void OpenGlViewer::initializeGL()
{
initializeOpenGLFunctions();
printContextInformation();
//glEnable(GL_DEPTH_TEST);
glClearColor(DEAFAULT_BACKGROUND);
mProgram = new QOpenGLShaderProgram();
mProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/simple.vert");
mProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/simple.frag");
mProgram->link();
mProgram->bind();
mVertexBuffer.create();
mVertexBuffer.bind();
mVertexBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
mVertexBuffer.allocate(sg_vertexes, sizeof(sg_vertexes));
mVertexArray.create();
mVertexArray.bind();
mProgram->enableAttributeArray(0);
mProgram->enableAttributeArray(1);
mProgram->setAttributeBuffer(0, GL_FLOAT, Vertex::positionOffset(), Vertex::PositionTupleSize, Vertex::stride());
mProgram->setAttributeBuffer(1, GL_FLOAT, Vertex::colorOffset(), Vertex::ColorTupleSize, Vertex::stride());
mVertexArray.release();
mVertexBuffer.release();
mProgram->release();
}
void OpenGlViewer::resizeGL(int w, int h)
@ -30,5 +89,12 @@ void OpenGlViewer::resizeGL(int w, int h)
void OpenGlViewer::paintGL()
{
//TODO: paint here
glClear(GL_COLOR_BUFFER_BIT);
mProgram->bind();
mVertexArray.bind();
glDrawArrays(GL_TRIANGLES, 0, sizeof(sg_vertexes) / sizeof(sg_vertexes[0]));
mVertexArray.release();
mProgram->release();
}