当前位置:网站首页>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;
}
边栏推荐
- 【光学】基于matlab介电常数计算【含Matlab源码 1926期】
- Common text labels
- Succession of flutter
- Zhang Fei hardware 90 day learning notes - personal records on day 4, please see my personal profile / homepage for the complete
- 记录在模拟器中运行flutter时报的错
- 2020 intermediate financial management (escort class)
- Ego planner code parsing Bspline_ Optimizer section (1)
- The most valuable thing
- Web Security (VII) specific process of authentication with session cookie scheme
- Counting from the East and counting from the West will stimulate 100 billion industries. Only storage manufacturers who dare to bite the "hard bone" will have more opportunities
猜你喜欢
Basic principle of LSM tree
application
东数西算拉动千亿产业,敢啃“硬骨头”的存储厂商才更有机会
[optics] vortex generation based on MATLAB [including Matlab source code 1927]
This Chinese numpy quick look-up table is too easy!
[free sharing] kotalog diary2022 plan electronic manual ledger
第一章:求n的阶乘n!
记录在模拟器中运行flutter时报的错
Merge K ascending linked lists
Record: solve the problem that MySQL is not an internal or external command environment variable
随机推荐
论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
Think of new ways
Dynamic planning -- expansion topics
How to design a high concurrency system
The necessity of lean production and management in sheet metal industry
Scrapy爬虫框架
Sentinel source code analysis part II - sentinel dashboard console startup and configuration
C enum contains value - C enum contains value
Yolov3 network model building
math_泰勒公式
EGO Planner代码解析bspline_optimizer部分(2)
UE source code analysis: uccharactermovementcomponent - rootmotion
High concurrency Architecture - separate databases and tables
Record: pymysql is used in pycharm to connect to the database
These problems should be paid attention to in the production of enterprise promotional videos
Verilog HDL continuous assignment statement, process assignment statement, process continuous assignment statement
Day18 - basis of interface testing
Why should we do feature normalization / standardization?
第二十章:y= sin(x)/x,漫步坐标系计算,y= sin(x)/x 带廓幅图形,奥运五环,小球滚动与弹跳,流水显示,矩形优化裁剪,r个皇后全控nxn棋盘
Sentinel source code analysis part I sentinel overview