.h
#pragma once
#include
#include
class MyOpenglWegdit : public QOpenGLWidget, protected QOpenGLFunctions_4_3_Core
{
Q_OBJECT
public:
float cameraX, cameraY, cameraZ;
float cubeLocX, cubeLocY, cubeLocZ;
QOpenGLShaderProgram shaderProgram;
QOpenGLBuffer vao;
GLuint mvLoc, projLoc;
float aspect;
QMatrix4x4 pMat, vMat, mMat, mvMat;
void setupVertices();
MyOpenglWegdit(QWidget*parent);
~MyOpenglWegdit();
protected:
void initializeGL();
void resizeGL();
void paintGL();
};
.cpp
#include "stdafx.h"
#include "MyOpenglWegdit.h"
MyOpenglWegdit::MyOpenglWegdit(QWidget*parent)
: QOpenGLWidget(parent)
{
}
MyOpenglWegdit::~MyOpenglWegdit()
{
}
void MyOpenglWegdit::initializeGL()
{
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0);//设置深度缓存
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);//设置阴影平滑模式
glClearColor(0.98, 0.625, 0.12, 0.5);//改变窗口的背景颜色
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);//进行透视校正
if (!shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/QtGuiApplication1/Resources/config/shader.vs")) { //添加并编译顶点着色器
qDebug() << "编译ERROR:" << shaderProgram.log(); //如果编译出错,打印报错信息
}
if (!shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/QtGuiApplication1/Resources/config/shader.fs")) { //添加并编译片段着色器
qDebug() << "编译ERROR:" << shaderProgram.log(); //如果编译出错,打印报错信息
}
if (!shaderProgram.link()) { //链接着色器
qDebug() << "链接ERROR:" << shaderProgram.log(); //如果链接出错,打印报错信息
}
shaderProgram.bind();
cameraX = 0.0f; cameraY = 0.0f; cameraZ = 8.0f;
cubeLocX = 0.0f; cubeLocY = -2.0f; cubeLocZ = -0.0f;
setupVertices();
mvLoc = shaderProgram.uniformLocation("mv_matrix");
projLoc = shaderProgram.uniformLocation("proj_matrix");//获取程序的统一变量mv,投影矩阵
//构建透视矩阵
aspect = (float)width() / (float)height();
pMat.perspective(70.0472f, aspect, 0.1f, 1000.f);
//构建视图矩阵
vMat.translate(-cameraX, -cameraY, -cameraZ);
mMat.translate(cubeLocX, cubeLocY, cubeLocZ);
mvMat = vMat * mMat;
//将mv矩阵发送给对应的统一变量
shaderProgram.setUniformValue(mvLoc, mvMat);
shaderProgram.setUniformValue(projLoc, pMat);
}
void MyOpenglWegdit::resizeGL()
{
glViewport(0, 0, width(), height());
}
void MyOpenglWegdit::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLuint vPosition = shaderProgram.attributeLocation("vPosition");
shaderProgram.setAttributeBuffer(vPosition, GL_FLOAT,0,3,0);
glEnableVertexAttribArray(vPosition);
//调整OpenGL设置,绘制模型
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//线框模式
glDrawArrays(GL_TRIANGLES,0,36);
MyOpenglWegdit::update();
}
void MyOpenglWegdit::setupVertices() {
float vertexPositions[108] = {
-1.0f,1.0f,-1.0f,-1.0f,-1.0f,-1.0f,1.0f,-1.0f,-1.0f,
1.0f,-1.0f,-1.0f,1.0f,1.0f,-1.0f,-1.0f,1.0f,-1.0f,
1.0f,-1.0f,-1.0f,1.0f,-1.0f,1.0f,1.0f,1.0f,-1.0f,
1.0f,-1.0f,1.0f,1.0f, 1.0f, 1.0f,1.0f,1.0f,-1.0f,
1.0f,-1.0f,1.0f,-1.0f,-1.0f,1.0f,1.0f,1.0f,1.0f,
-1.0f,-1.0f,1.0f,-1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,
-1.0f,-1.0f,1.0f,-1.0f,-1.0f,-1.0f,-1.0f,1.0f,1.0f,
-1.0f,-1.0f,-1.0f,-1.0f,1.0f,-1.0f,-1.0f,1.0f,1.0f,
-1.0f,-1.0f,1.0f,1.0f,-1.0f,1.0f,1.0f,-1.0f,-1.0f,
1.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f,1.0f,
-1.0f,1.0f,-1.0f,1.0f,1.0f,-1.0f,1.0f,1.0f,1.0f,
1.0f,1.0f,1.0f,-1.0f,1.0f,1.0f,-1.0f,1.0f,-1.0f
};
vao.create();
vao.bind();
vao.setUsagePattern(QOpenGLBuffer::StaticDraw);
vao.allocate(vertexPositions, sizeof(vertexPositions));
}
shader.vs
#version 430
in vec3 vPosition;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
void main(void)
{gl_Position=proj_matrix*mv_matrix*vec4(vPosition,1.0);
}
shader.fs
#version 430
out vec4 color;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
void main(void)
{
color = vec4(1.0, 1.0, 1.0, 1.0);
}
image.png
着色器修改
.vs
#version 430
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
out vec4 varringColor;
in vec3 vPosition;
void main(void)
{
gl_Position=proj_matrix*mv_matrix*vec4(vPosition,1.0);
varringColor=vec4(vPosition,1.0)*0.5+vec4(0.5,0.5,0.5,0.5);
}
.fs
#version 430
in vec4 varringColor;
out vec4 color;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
void main(void)
{
color = varringColor;
}
image.png
目录
VSC++2019+QT+OpenGL
QT+OpenGL一之绘制立方体(三角形图元)
QT+OpenGL二之纹理贴图
QT+OpenGL三之矩阵简解
QT+OpenGL四之相机的移动和旋转
QT+OpenGL五之绘制不同的模型(vao,vbo机制)
QT+OpenGL六之天空盒
QT+OpenGL七之使用EBO
QT+OPenGL八之模型准备
QT+OPenGL九之模型解码
QT+OPenGL十之光照模型
QT+OPenGL十一之漫反射和镜面反射贴图
QT+OPenGL十二之定向光
QT+OPenGL十三之真正的点光源和聚光灯
QT+OPenGL十四之多光源混合的问题
QT+OPenGL十五之深度缓冲区
QT+OPenGL十六之模板缓冲区
QT+OPenGL十七帧缓冲区(离屏渲染)
QT+OPenGL十八抗锯齿
QT+OPenGL十九镜面反射效率调整
QT+OPenGL二十Gamma校正