.h
#pragma once
#include <QOpenGLWidget>
#include <qopenglfunctions_4_3_core.h>
class MyOpenglWegdit : public QOpenGLWidget, protected QOpenGLFunctions_4_3_Core
{
Q_OBJECT
public:
float cameraX, cameraY, cameraZ;
float cubeLocX, cubeLocY, cubeLocZ;
QOpenGLShaderProgram shaderProgram;
QOpenGLBuffer vbo,uvVbo;
QOpenGLTexture *mTexture;
GLuint mvLoc, projLoc, imgTexture;
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(60.0f, 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);
mTexture = new QOpenGLTexture(QImage(":/titleCoin/Resources/background.jpg"));
mTexture->setMinificationFilter(QOpenGLTexture::Nearest);
mTexture->setMagnificationFilter(QOpenGLTexture::Linear);
shaderProgram.setUniformValue("imgTexture", mTexture->textureId());
}
void MyOpenglWegdit::resizeGL()
{
glViewport(0, 0, width(), height());
}
void MyOpenglWegdit::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
mTexture->bind(mTexture->textureId());
//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glDrawArrays(GL_TRIANGLES,0,36);
mTexture->release();
}
void MyOpenglWegdit::setupVertices() {
GLfloat 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
};
vbo.create();
vbo.bind();
vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
vbo.allocate(vertexPositions, sizeof(vertexPositions));
GLuint vPosition = shaderProgram.attributeLocation("vPosition");
shaderProgram.setAttributeBuffer(vPosition, GL_FLOAT, 0, 3, 0);
glEnableVertexAttribArray(vPosition);
vbo.release();
GLfloat uv[72] = {
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f
};
uvVbo.create();
uvVbo.bind();
uvVbo.allocate(uv,sizeof(uv));
GLuint uvlo = shaderProgram.attributeLocation("inuv");
shaderProgram.setAttributeBuffer(uvlo, GL_FLOAT, 0, 2, 0);
glEnableVertexAttribArray(uvlo);
uvVbo.release();
}
.vs
#version 430
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
out vec2 uv;
in vec3 vPosition;
in vec2 inuv;
void main(void)
{
gl_Position=proj_matrix*mv_matrix*vec4(vPosition,1.0);
uv=inuv;
}
.fs
#version 430
out vec4 color;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
in vec2 uv;
uniform sampler2D imgTexture;
void main(void)
{
color =texture(imgTexture,uv) ;
}
目录
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校正