当前位置:网站首页>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;
}

边栏推荐
- FBI警告:有人利用AI换脸冒充他人身份进行远程面试
- Sentinel source code analysis part I sentinel overview
- Ego planner code parsing Bspline_ Optimizer section (3)
- P3402 persistent and searchable
- Help change the socket position of PCB part
- Integrated easy to pay secondary domain name distribution system
- Compared with 4G, what are the advantages of 5g to meet the technical requirements of industry 4.0
- Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]. StandardContext
- Zhang Fei hardware 90 day learning notes - personal record on day 6. Please see my personal profile / homepage for the complete record
- A green plug-in that allows you to stay focused, live and work hard
猜你喜欢

Record the errors reported when running fluent in the simulator

Yolov3 network model building

Using the visualization results, click to appear the corresponding sentence
![[free sharing] kotalog diary2022 plan electronic manual ledger](/img/ca/1ffbfcc16e3019261f70274a89c16f.jpg)
[free sharing] kotalog diary2022 plan electronic manual ledger

Flutter network and data storage framework construction-b1

SSM integration - joint debugging of front and rear protocols (list function, add function, add function status processing, modify function, delete function)

Pytorch introduction to deep learning practice notes 13- advanced chapter of cyclic neural network - Classification
![[optics] vortex generation based on MATLAB [including Matlab source code 1927]](/img/9b/b7f462e2ecbff0cee35e7de5c80cf7.jpg)
[optics] vortex generation based on MATLAB [including Matlab source code 1927]

Day_ 18 IO stream system

QT -- qfile file read / write operation
随机推荐
P1891 crazy LCM (Euler function)
How does if ($variable) work? [repeat] - how exactly does if ($variable) work? [duplicate]
Octopus online ecological chain tour Atocha protocol received near grant worth $50000
ActiveMQ的基础
How to build an efficient information warehouse
Simulation scheduling problem of SystemVerilog (1)
Webrtc[41] - Analysis of the establishment process of webrtc transmission channel
Does SQL always report foreign key errors when creating tables?
Zhang Fei hardware 90 day learning notes - personal record on day 5. Please see my personal profile / homepage for the complete record
Analyse du Code du planificateur ego bspline Section Optimizer (1)
记录在模拟器中运行flutter时报的错
【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁
Analysis of dart JSON encoder and decoder
KINGS
达梦数据库的物理备份和还原简解
flask 生成swagger文档
Simple solution of physical backup and restore of Damon database
235. Ancêtre public le plus proche de l'arbre de recherche binaire [modèle LCA + même chemin de recherche]
Succession of flutter
Smart wax therapy machine based on STM32 and smart cloud