#include "mainwindow.h"
#include <QApplication>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QOpenGLWidget(parent),
m_texture(nullptr),
m_vertexBuffer(QOpenGLBuffer::VertexBuffer),
m_indexBuffer(QOpenGLBuffer::IndexBuffer){
}
MainWindow::~MainWindow()
{
}
void MainWindow::initializeGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
Scene1();
}
void MainWindow::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 ViewMatrix;
ViewMatrix.setToIdentity();
ViewMatrix.translate(0.0f, 0.0f, -5.0f);
QMatrix4x4 modelmatrix;
modelmatrix.setToIdentity();
m_texture->bind(0);
m_shaderProgramm.bind();
m_shaderProgramm.setUniformValue("projectionMatrix", m_projectionMatrix);
m_shaderProgramm.setUniformValue("viewMatrix", ViewMatrix);
m_shaderProgramm.setUniformValue("modelMatrix", modelmatrix);
m_shaderProgramm.setUniformValue("u_texture", 0);
// Bind buffers
m_vertexBuffer.bind();
m_indexBuffer.bind();
int offset = 0;
int location = m_shaderProgramm.attributeLocation("a_position");
m_shaderProgramm.enableAttributeArray(location);
m_shaderProgramm.setAttributeBuffer(location, GL_FLOAT, offset, 3, sizeof(VertexData));
offset += sizeof(QVector3D);
location = m_shaderProgramm.attributeLocation("vTexcoord");
m_shaderProgramm.enableAttributeArray(location);
m_shaderProgramm.setAttributeBuffer(location, GL_FLOAT, offset, 2, sizeof(VertexData));
glDrawElements(GL_TRIANGLES, m_indexBuffer.size(), GL_UNSIGNED_INT, 0);
}
void MainWindow::Scene1()
{
initShaders();
initCube();
}
void MainWindow::initShaders()
{
if (!m_shaderProgramm.addShaderFromSourceFile(QOpenGLShader::Vertex, "://vshader.vsh"))
close();
if (!m_shaderProgramm.addShaderFromSourceFile(QOpenGLShader::Fragment, "://fshader.fsh"))
close();
if (m_shaderProgramm.link())
close();
if (!m_shaderProgramm.bind())
close();
}
void MainWindow::initCube()
{
float width_div_2 =1;
QVector<VertexData> vertexes;
//front, right, top, back, left, down
тут геометрия куба
//front, right, top, back, left, down
QVector<GLuint> indexes;
for (int i = 0; i < 24; i += 4){
indexes.append(i + 0);
indexes.append(i + 1);
indexes.append(i + 2);
indexes.append(i + 2);
indexes.append(i + 1);
indexes.append(i + 3);
}
// Create and fill vertex buffer and then -> release
m_vertexBuffer.create();
m_vertexBuffer.bind();
m_vertexBuffer.allocate(vertexes.constData(), vertexes.size() * sizeof(VertexData));
m_vertexBuffer.release();
// Create and fill index buffer and then -> release
m_indexBuffer.create();
m_indexBuffer.bind();
m_indexBuffer.allocate(indexes.constData(), indexes.size() * sizeof(GLuint));
m_indexBuffer.release();
m_texture = new QOpenGLTexture(QImage("://cube.jpg").mirrored());
m_texture->setMinificationFilter(QOpenGLTexture::Nearest);
m_texture->setMagnificationFilter(QOpenGLTexture::Linear);
m_texture->setWrapMode(QOpenGLTexture::Repeat);
}