refactor: create GLMesh class
This commit is contained in:
@@ -26,6 +26,7 @@ qt_add_executable(${AppExec}
|
|||||||
src/gui/viewport/Viewport.cpp
|
src/gui/viewport/Viewport.cpp
|
||||||
src/gui/viewport/ViewportGLWidget.cpp
|
src/gui/viewport/ViewportGLWidget.cpp
|
||||||
src/gui/viewport/GLCamera.cpp
|
src/gui/viewport/GLCamera.cpp
|
||||||
|
src/gui/viewport/GLMesh.cpp
|
||||||
src/gui/network/NetworkGraphicsView.cpp
|
src/gui/network/NetworkGraphicsView.cpp
|
||||||
src/gui/network/NetworkGraphicsScene.cpp
|
src/gui/network/NetworkGraphicsScene.cpp
|
||||||
src/gui/network/Network.cpp
|
src/gui/network/Network.cpp
|
||||||
|
|||||||
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/ViewportGLWidget.h"
|
||||||
|
#include "gui/viewport/GLMesh.h"
|
||||||
#include <glm/mat4x4.hpp>
|
#include <glm/mat4x4.hpp>
|
||||||
#include <glm/ext/matrix_clip_space.hpp>
|
#include <glm/ext/matrix_clip_space.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
#include <memory>
|
||||||
#include <qtimer.h>
|
#include <qtimer.h>
|
||||||
|
|
||||||
void ViewportGLWidget::initializeGL()
|
void ViewportGLWidget::initializeGL()
|
||||||
{
|
{
|
||||||
initializeOpenGLFunctions();
|
initializeOpenGLFunctions();
|
||||||
|
|
||||||
|
triangleMesh_ = std::make_unique<GLMesh>();
|
||||||
|
|
||||||
QSurfaceFormat fmt = context()->format();
|
QSurfaceFormat fmt = context()->format();
|
||||||
std::cout << "format: " << (fmt.renderableType() == QSurfaceFormat::OpenGLES ? "GLES" : "Desktop") << "\n";
|
std::cout << "format: " << (fmt.renderableType() == QSurfaceFormat::OpenGLES ? "GLES" : "Desktop") << "\n";
|
||||||
std::cout << "format: " << (fmt.renderableType() == QSurfaceFormat::OpenGL ? "true" : "false") << "\n";
|
std::cout << "format: " << (fmt.renderableType() == QSurfaceFormat::OpenGL ? "true" : "false") << "\n";
|
||||||
@@ -23,25 +27,6 @@ void ViewportGLWidget::initializeGL()
|
|||||||
curCamera = GLCamera();
|
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
|
// vertex shader
|
||||||
const std::string vertexShaderSource = "#version 330 core\n"
|
const std::string vertexShaderSource = "#version 330 core\n"
|
||||||
"uniform mat4 uView;\n"
|
"uniform mat4 uView;\n"
|
||||||
@@ -101,11 +86,6 @@ void ViewportGLWidget::initializeGL()
|
|||||||
glDeleteShader(vertexShader);
|
glDeleteShader(vertexShader);
|
||||||
glDeleteShader(fragmentShader);
|
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);
|
glClearColor(0.16f, 0.16f, 0.16f, 1.0f);
|
||||||
@@ -140,7 +120,6 @@ void ViewportGLWidget::paintGL()
|
|||||||
curCamera.setUniform(viewMLoc);
|
curCamera.setUniform(viewMLoc);
|
||||||
|
|
||||||
|
|
||||||
|
triangleMesh_->draw();
|
||||||
|
|
||||||
glBindVertexArray(vao);
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,16 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <QOpenGLFunctions_3_2_Core>
|
#include <QOpenGLFunctions_3_2_Core>
|
||||||
#include "gui/viewport/GLCamera.h"
|
#include "gui/viewport/GLCamera.h"
|
||||||
|
#include "gui/viewport/GLMesh.h"
|
||||||
|
|
||||||
class ViewportGLWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_2_Core
|
class ViewportGLWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_2_Core
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ViewportGLWidget(QWidget *parent) : QOpenGLWidget(parent) { }
|
ViewportGLWidget(QWidget *parent) : QOpenGLWidget(parent) { }
|
||||||
QSize sizeHint() const override { return QSize(-1, -1); }
|
QSize sizeHint() const override { return QSize(-1, -1); }
|
||||||
GLuint vao;
|
|
||||||
GLuint shaderProgram;
|
GLuint shaderProgram;
|
||||||
GLCamera curCamera;
|
GLCamera curCamera;
|
||||||
|
std::unique_ptr<GLMesh> triangleMesh_ ;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void initializeGL() override;
|
void initializeGL() override;
|
||||||
|
|||||||
Reference in New Issue
Block a user