当前位置:网站首页>05 -- QT OpenGL draw cube uniform
05 -- QT OpenGL draw cube uniform
2022-07-03 19:19:00 【Qingsong 0527】


qmyopenglwidget.h
#ifndef QMYOPENGLWIDGET_H
#define QMYOPENGLWIDGET_H
#include <QWidget>
#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>
class QMyOpenglWidget : public QOpenGLWidget, QOpenGLFunctions_3_3_Core
{
public:
enum EType {
eNone,
eFull,
eLine,
ePoint,
};
Q_OBJECT
public:
explicit QMyOpenglWidget(QWidget* parent = nullptr);
protected:
virtual void initializeGL();
virtual void resizeGL(int w, int h);
virtual void paintGL();
public:
void cretaeShader();
void initDrawData(GLuint &vao, float *vertices, int vSize, GLuint &ebo);
void modeChange(EType type = eLine);
private:
unsigned int vao1;
unsigned int ebo1;
QOpenGLShaderProgram program;
GLuint programId;
GLint mLocation;
GLint vLocation;
GLint pLocation;
GLint vertexLocation;
GLint colorLocation;
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
QMatrix4x4 pMatrix;
signals:
public slots:
};
#endif // QMYOPENGLWIDGET_H
qmyopenglwidget.cpp
#include "qmyopenglwidget.h"
QMyOpenglWidget::QMyOpenglWidget(QWidget* parent):QOpenGLWidget(parent)
{
}
void QMyOpenglWidget::initializeGL()
{
initializeOpenGLFunctions();
cretaeShader();
// Triangle vertex coordinates
float s = 1.0f/2;
GLfloat vertices[] = {
-s, -s, -s,
s, -s, -s,
s, -s, s,
-s, -s, s,
-s, s, -s,
s, s, -s,
s, s, s,
-s, s, s,
};
initDrawData(vao1, vertices, sizeof(vertices), ebo1);
glEnable(GL_CULL_FACE); // Back clipping enabled
glFrontFace(GL_CCW); // Set counter clockwise to front
}
void QMyOpenglWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
pMatrix.setToIdentity();
float aspect = float(w*1.0)/ float(h);
aspect = int(aspect * 100) / 100.0f;
//aspect = 1.5f;
pMatrix.perspective(60.0f, aspect, 0.000f, 100.f);
vMatrix.lookAt(QVector3D{0.0,0.0,-1.0}, QVector3D{0.0,0.0,1.0},QVector3D{0.0,1.0,0.0});
mMatrix.setToIdentity();
mMatrix.translate(QVector3D{0.0,0.0,3.0});
//mMatrix.rotate(45, QVector3D{0.0,1.0,1.0});
//update();
}
void QMyOpenglWidget::paintGL()
{
glClearColor(0.2f, 0.2f, 0.1f, 0.1f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUniformMatrix4fv(mLocation, 1, GL_FALSE, mMatrix.data());
glUniformMatrix4fv(vLocation, 1, GL_FALSE, vMatrix.data());
glUniformMatrix4fv(pLocation, 1, GL_FALSE, pMatrix.data());
mMatrix.rotate(1, QVector3D{1.0,1.0,0.0});
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo1);
glBindVertexArray(vao1);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
//glDrawArrays(GL_TRIANGLES, 0, 36);
update();
//draw(eLine);
}
void QMyOpenglWidget::cretaeShader()
{
program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vertex.vert");
program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/frag.frag");
program.bind();
program.link();
programId = program.programId();
mLocation = glGetUniformLocation(programId, "mMatrix");
vLocation = glGetUniformLocation(programId, "vMatrix");
pLocation = glGetUniformLocation(programId, "pMatrix");
vertexLocation = glGetAttribLocation(programId, "pos");
colorLocation = glGetAttribLocation(programId, "color");
Q_ASSERT(mLocation != -1);
Q_ASSERT(vLocation != -1);
Q_ASSERT(pLocation != -1);
Q_ASSERT(vertexLocation != -1);
Q_ASSERT(colorLocation != -1);
}
void QMyOpenglWidget::initDrawData(GLuint &vao, float *vertices, int vSize, GLuint &ebo)
{
// Vertex Index
GLuint indexs[] = {
0, 1, 3, 1, 2, 3, // Next ABCD
1, 6, 2, 1, 5, 6, // Right BCGF
7, 5, 4, 7, 6, 5, // On EFGH
0, 3, 4, 3, 7, 4, // Left ADHE
0, 5, 1, 0, 4, 5, // front ABFE
2, 7, 3, 2, 6, 7 // after DCGH
};
// Vertex color
GLfloat colors[] = { 1.0f, 0.0f, 0.0f,1.0f , // red
0.0f, 1.0f, 0.0f ,1.0f , // green
0.0f, 0.0f, 1.0f ,1.0f , // blue
0.0f, 0.0f, 0.0f ,1.0f , // white
1.0f, 0.0f, 0.0f,1.0f , // red
0.0f, 1.0f, 0.0f ,1.0f , // green
0.0f, 0.0f, 1.0f ,1.0f , // blue
0.0f, 0.0f, 0.0f ,1.0f , // white
};
//**************vao******************************//
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//******************* Vbo vertices*******************//
unsigned int vbo1;
glGenBuffers(1, &vbo1);
glBindBuffer(GL_ARRAY_BUFFER, vbo1);
// Is currently bound to target Create a new data store for the buffer object //
glBufferData(GL_ARRAY_BUFFER, vSize, vertices, GL_STATIC_DRAW);
// Tell the graphics card how to parse the attribute value in the buffer
glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, FALSE, 3*sizeof (GL_FLOAT), nullptr);
// Turn on VAO The first attribute of Management
glEnableVertexAttribArray(vertexLocation);
//************** Vbo color**************************/
unsigned int vboColor;
glGenBuffers(1, &vboColor);
glBindBuffer(GL_ARRAY_BUFFER, vboColor);
// Is currently bound to target Create a new data store for the buffer object //
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
// Tell the graphics card how to parse the attribute value in the buffer
glVertexAttribPointer(colorLocation, 4, GL_FLOAT, FALSE, 4*sizeof (GL_FLOAT), nullptr);
// Turn on VAO The first attribute of Management
glEnableVertexAttribArray(colorLocation);
//***************EBO ******************************/
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof (indexs), indexs, GL_STATIC_DRAW);
}
void QMyOpenglWidget::modeChange(QMyOpenglWidget::EType type)
{
makeCurrent(); // Set up current opengl Rendering environment
switch(type)
{
case eLine: {glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); break;}
case eFull: {glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); break;}
case ePoint: {glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); break;}
default: {glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); break;}
}
//glBindVertexArray(vao1);
//glDrawArrays(GL_TRIANGLES, 0, 3);
update();
doneCurrent();
}
glEnalbe(GL_CULL_FACE) Turn on the culling effect
glDisable(GL_CULL_FACE) Turn off the culling effect
Culling operation
1.glCullFace() Parameters include GL_FRONT and GL_BACK. Indicates that lighting on the front or back of the polygon is disabled 、 Shadow and color calculation and operation , Eliminate unnecessary rendering calculations .
For example, no matter how the position of an object changes , When we can only see one side of the polygon that constitutes it , You can use this function .
2.glPolygonMode
brief introduction
glPolygonMode Function is used to control the display of polygons .
The prototype is :void glPolygonMode(GLenum face,GLenum mode);
Parameters
face This parameter determines which parts of the object the display mode will apply to , Controls the drawing mode of the front and back faces of polygons :
GL_FRONT Indicates that the display mode will apply to the forward face of the object ( That is, the surface that the object can see )
GL_BACK Indicates that the display mode will apply to the backward face of the object ( That is, the invisible surface on the object )
GL_FRONT_AND_BACK Indicates that the display mode will apply to all faces of the object
mode This parameter determines how the face of the selected object is displayed ( display mode ):
GL_POINT Indicates that only vertices are displayed , Polygons are displayed in dots
GL_LINE Indicates that the line segment is displayed , Polygons are shown in outline
GL_FILL Represents the display surface , Polygons are filled
example : glPolygonMode(GL_FRONT, GL_LINE); The forward face of the object is displayed with line segments
vertex.vert
#version 330 core
uniform mat4 pMatrix;
uniform mat4 vMatrix;
uniform mat4 mMatrix;
in vec3 pos;
in vec4 color;
out vec4 fcolor;
void main(void)
{
fcolor = color;
gl_Position = pMatrix * vMatrix * mMatrix * vec4(pos, 1.0);
}
frag.frag
#version 330 core
in vec4 fcolor;
out vec4 FragColor; // Clip shader output
void main(void)
{
FragColor = fcolor;
}

边栏推荐
- Ctrip will implement a 3+2 work system in March, with 3 days on duty and 2 days at home every week
- Next spread
- 东数西算拉动千亿产业,敢啃“硬骨头”的存储厂商才更有机会
- [leetcode] [SQL] notes
- [optics] vortex generation based on MATLAB [including Matlab source code 1927]
- 我们做了一个智能零售结算平台
- Summary of composition materials for 2020 high-frequency examination center of educational resources
- [proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD
- Summary of learning materials and notes of Zhang Fei's actual combat electronics 1-31
- Driveseg: dynamic driving scene segmentation data set
猜你喜欢

Basic principle of LSM tree

Flutter网络和数据存储框架搭建 -b1

【水质预测】基于matlab模糊神经网络水质预测【含Matlab源码 1923期】

Summary of composition materials for 2020 high-frequency examination center of educational resources

What does a really excellent CTO look like in my eyes

Integrated easy to pay secondary domain name distribution system

Flutter network and data storage framework construction-b1

Ego planner code parsing Bspline_ Optimizer section (2)

EGO Planner代码解析bspline_optimizer部分(3)

Help change the socket position of PCB part
随机推荐
[leetcode] [SQL] notes
The necessity of lean production and management in sheet metal industry
Pytorch introduction to deep learning practice notes 13- advanced chapter of cyclic neural network - Classification
Smart wax therapy machine based on STM32 and smart cloud
We have built an intelligent retail settlement platform
OSPF - detailed explanation of stub area and full stub area
High concurrency Architecture - separate databases and tables
Summary of learning materials and notes of Zhang Fei's actual combat electronics 1-31
math_ Taylor formula
EGO Planner代码解析bspline_optimizer部分(2)
Simulation scheduling problem of SystemVerilog (1)
Zhang Fei hardware 90 day learning notes - personal record of day 3, please see my personal profile / homepage for the complete
达梦数据库的物理备份和还原简解
Pecan — @expose()
Comments on flowable source code (37) asynchronous job processor
2020 intermediate financial management (escort class)
The most valuable thing
The more you talk, the more your stupidity will be exposed.
Using the visualization results, click to appear the corresponding sentence
Streaming media server (16) -- figure out the difference between live broadcast and on-demand