qmyopenglwidget.h
#ifndef QMYOPENGLWIDGET_H
#define QMYOPENGLWIDGET_H#include
#include
#include
#include 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);
void modeChange(EType type = eLine);
unsigned int setImage(const char *filename);
private:
unsigned int vao1;
unsigned int ebo1;
QOpenGLShaderProgram program;
GLuint programId;
GLint mLocation;
GLint vLocation;
GLint pLocation;
GLint lightDirLocation;
GLint vertexLocation;
GLint normalLocation;
GLint colorLocation;
GLint uvLocation;
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
QMatrix4x4 pMatrix;
float *lightDirVec3;
unsigned int texture0;
signals:public slots:};
#endif // QMYOPENGLWIDGET_H
qmyopenglwidget.cpp
#include "qmyopenglwidget.h"
#include QMyOpenglWidget::QMyOpenglWidget(QWidget* parent):QOpenGLWidget(parent)
{}void QMyOpenglWidget::initializeGL()
{
initializeOpenGLFunctions();
cretaeShader();
//三角形顶点坐标
float s = 2.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,//前
-s, -s,s,
s, -s,s,
s,s,s,
-s,s,s,
//后
s, -s, -s,
-s, -s, -s,
-s,s, -s,
s,s, -s,
//left
-s, -s, -s,
-s, -s,s,
-s,s,s,
-s,s, -s,
//right
s, -s,s,
s, -s, -s,
s,s, -s,
s,s,s,
};
initDrawData(vao1, vertices, sizeof(vertices));
glEnable(GL_CULL_FACE);
//启动了背面裁剪
glFrontFace(GL_CCW);
//设置逆时针为正面
//glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_GEQUAL);
}void QMyOpenglWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
pMatrix.setToIdentity();
float aspect = float(w*1.0)/ float(h);
//printf("aspect = %f \n", aspect);
qDebug()<<"aspect = "<0 && height > 0);
//unsigned char *data = https://www.it610.com/article/stbi_load("container.jpg", &width, &height, &nrChannels, 0);
if (image.bits())
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.bits());
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
//std::cout << "Failed to load texture" << std::endl;
Q_ASSERT(0);
}return texture;
}void QMyOpenglWidget::modeChange(QMyOpenglWidget::EType type)
{
makeCurrent();
//设置当前opengl 渲染环境
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();
}
vertex.vert
#version 330 core
uniform mat4 pMatrix;
uniform mat4 vMatrix;
uniform mat4 mMatrix;
in vec3 pos;
in vec3 normal;
in vec2 uv0;
out vec2 TexCoord0;
out vec3 fnormal;
void main(void)
{
//法线方向随着模型 矩阵变动而变动
fnormal = vec3(mMatrix * vec4(normal, 1.0));
;
gl_Position = pMatrix* vMatrix * mMatrix * vec4(pos, 1.0);
//fragPos = vec3(mMatrix * vec4(pos, 1.0));
TexCoord0 = uv0;
}
frag.frag
#version 330 core
in vec3 fnormal;
out vec4 FragColor;
//片段着色器输出
in vec2 TexCoord0;
uniform sampler2D texture0;
uniform vec3 lightDir;
void main(void)
{
vec4 lightColor = vec4(3.0, 1.5, 0, 1);
float fcolor = max(dot(normalize(fnormal), normalize(lightDir)), 0.0);
FragColor = texture2D(texture0, TexCoord0) * fcolor * fcolor * fcolor * lightColor;
}
文章图片
【opengl|09——qt opengl 方向光源 shader】
文章图片
文章图片
推荐阅读
- Qt|Qt常用UI控件读取、写入方法
- Qt|Qt For Android 之启动页
- QT|QT面试
- QT安装
- qt|qt for android实现app内通过按键后台运行程序
- qt|qt for android程序保活 程序后台一直运行不被清理掉(未完)
- QWidget实现手机ipad屏幕滑屏效果
- Qt|Qt官方示例-QML Axes
- 在QT/QML中使用FontAwesome做图标显示