refactor: create GLMesh class
This commit is contained in:
57
src/gui/viewport/GLMesh.cpp
Normal file
57
src/gui/viewport/GLMesh.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "gui/viewport/GLMesh.h"
|
||||
#include <iostream>
|
||||
|
||||
GLMesh::GLMesh()
|
||||
{
|
||||
initializeOpenGLFunctions();
|
||||
init();
|
||||
}
|
||||
|
||||
void GLMesh::init()
|
||||
{
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
initBuffers();
|
||||
|
||||
// unbind vertex array
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void GLMesh::initBuffers()
|
||||
{
|
||||
|
||||
float vertices[] = {
|
||||
-0.5f, -0.5f, 0.0f,
|
||||
0.5f, -0.5f, 0.0f,
|
||||
0.0f, 0.5f, 0.0f
|
||||
};
|
||||
|
||||
// create buffer of vertices
|
||||
glGenBuffers(1, &vbo);
|
||||
// set purpose
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
// store data in the buffer
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
// gives the shader a way to read
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), (void*)0);
|
||||
// disable vertex attrib array
|
||||
glEnableVertexAttribArray(0);
|
||||
}
|
||||
|
||||
void GLMesh::bind()
|
||||
{
|
||||
glBindVertexArray(vao);
|
||||
}
|
||||
|
||||
void GLMesh::unbind()
|
||||
{
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void GLMesh::draw()
|
||||
{
|
||||
bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
}
|
||||
17
src/gui/viewport/GLMesh.h
Normal file
17
src/gui/viewport/GLMesh.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include <QOpenGLFunctions_3_2_Core>
|
||||
|
||||
class GLMesh
|
||||
: protected QOpenGLFunctions_3_2_Core
|
||||
{
|
||||
public:
|
||||
GLMesh();
|
||||
GLuint vao;
|
||||
GLuint vbo;
|
||||
|
||||
void init();
|
||||
void initBuffers();
|
||||
void bind();
|
||||
void unbind();
|
||||
void draw();
|
||||
};
|
||||
@@ -1,14 +1,18 @@
|
||||
#include "gui/viewport/ViewportGLWidget.h"
|
||||
#include "gui/viewport/GLMesh.h"
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <memory>
|
||||
#include <qtimer.h>
|
||||
|
||||
void ViewportGLWidget::initializeGL()
|
||||
{
|
||||
initializeOpenGLFunctions();
|
||||
|
||||
triangleMesh_ = std::make_unique<GLMesh>();
|
||||
|
||||
QSurfaceFormat fmt = context()->format();
|
||||
std::cout << "format: " << (fmt.renderableType() == QSurfaceFormat::OpenGLES ? "GLES" : "Desktop") << "\n";
|
||||
std::cout << "format: " << (fmt.renderableType() == QSurfaceFormat::OpenGL ? "true" : "false") << "\n";
|
||||
@@ -23,25 +27,6 @@ void ViewportGLWidget::initializeGL()
|
||||
curCamera = GLCamera();
|
||||
|
||||
|
||||
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
float vertices[] = {
|
||||
-0.5f, -0.5f, 0.0f,
|
||||
0.5f, -0.5f, 0.0f,
|
||||
0.0f, 0.5f, 0.0f
|
||||
};
|
||||
|
||||
GLuint vbo;
|
||||
// create buffer of vertices
|
||||
glGenBuffers(1, &vbo);
|
||||
// set purpose
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
// store data in the buffer
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
|
||||
// vertex shader
|
||||
const std::string vertexShaderSource = "#version 330 core\n"
|
||||
"uniform mat4 uView;\n"
|
||||
@@ -101,11 +86,6 @@ void ViewportGLWidget::initializeGL()
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// unbind vertex array
|
||||
glBindVertexArray(0);
|
||||
|
||||
|
||||
glClearColor(0.16f, 0.16f, 0.16f, 1.0f);
|
||||
@@ -140,7 +120,6 @@ void ViewportGLWidget::paintGL()
|
||||
curCamera.setUniform(viewMLoc);
|
||||
|
||||
|
||||
triangleMesh_->draw();
|
||||
|
||||
glBindVertexArray(vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
}
|
||||
|
||||
@@ -4,15 +4,16 @@
|
||||
#include <iostream>
|
||||
#include <QOpenGLFunctions_3_2_Core>
|
||||
#include "gui/viewport/GLCamera.h"
|
||||
#include "gui/viewport/GLMesh.h"
|
||||
|
||||
class ViewportGLWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_2_Core
|
||||
{
|
||||
public:
|
||||
ViewportGLWidget(QWidget *parent) : QOpenGLWidget(parent) { }
|
||||
QSize sizeHint() const override { return QSize(-1, -1); }
|
||||
GLuint vao;
|
||||
GLuint shaderProgram;
|
||||
GLCamera curCamera;
|
||||
std::unique_ptr<GLMesh> triangleMesh_ ;
|
||||
|
||||
protected:
|
||||
void initializeGL() override;
|
||||
|
||||
Reference in New Issue
Block a user