???qmyopenglwidget.h
#ifndef QMYOPENGLWIDGET_H
#define QMYOPENGLWIDGET_H
#include <QWidget>
#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>
#include <QMouseEvent>
#include <QtMath>
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();
virtual void mousePressEvent(QMouseEvent *event);
virtual void mouseReleaseEvent(QMouseEvent *event);
virtual void mouseDoubleClickEvent(QMouseEvent *event);
virtual void mouseMoveEvent(QMouseEvent *event);
public:
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
public:
void cretaeShader();
void initDrawData(GLuint &vao, float *vertices, int vSize, float *uv, int uSize, float *normals, int nSize);
void modeChange(EType type = eLine);
unsigned int setImage(const char *filename);
private:
unsigned int vao1;
unsigned int vao2;
unsigned int vao3;
unsigned int vao4;
unsigned int vao5;
unsigned int ebo1;
QOpenGLShaderProgram program;
GLuint programId;
GLint mLocation;
GLint vLocation;
GLint pLocation;
GLint lightPosLocation;
GLint viewPosLocation;
GLint vertexLocation;
GLint normalLocation;
GLint colorLocation;
GLint uvLocation;
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
QMatrix4x4 pMatrix;
unsigned int texture0;
unsigned int texture1;
bool isLeftPress;
QPoint curPos;
float yaw;
float pitch;
QVector3D cameraFront;
QVector3D cameraPos;
QVector3D cameraUp;
signals:
public slots:
};
#endif // QMYOPENGLWIDGET_H
qmyopenglwidget.cpp
#include "qmyopenglwidget.h"
#include <QGLWidget>
#include <QApplication>
QMyOpenglWidget::QMyOpenglWidget(QWidget* parent):QOpenGLWidget(parent)
{
isLeftPress = false;
yaw = -90;
pitch = 0;
cameraFront = QVector3D(0, 0, -1);
cameraPos = QVector3D(0, 0, 10);
cameraUp = QVector3D(0, 1, 0);
}
void QMyOpenglWidget::initializeGL()
{
initializeOpenGLFunctions();
glEnable(GL_CULL_FACE); //启动了背面裁剪
glFrontFace(GL_CCW); //设置逆时针为正面
//glEnable(GL_DEPTH_TEST);
//glDepthFunc(GL_LESS);
//glClearDepth (1.0f);
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,
};
GLfloat uv[] = {0, 0,
1, 0,
1, 1,
0, 1,
0, 1,
1, 1,
1, 0,
0, 0,
0, 0,
1, 0,
1, 1,
0, 1,
0, 1,
1, 1,
1, 0,
0, 0,
0, 0,
1, 0,
1, 1,
0, 1,
0, 1,
1, 1,
1, 0,
0, 0,
};
GLfloat normals[]{
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1,
-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0,
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
};
initDrawData(vao1, vertices, sizeof(vertices), uv, sizeof(uv), normals, sizeof(normals));
glUniform1i(glGetUniformLocation(programId, "texture0"), 0);
texture0 = setImage(":/container.png");
glUniform1i(glGetUniformLocation(programId, "specular"), 1);
texture1 = setImage(":/specular.png");
}
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 = "<<aspect<<" resizeGL";
//aspect = int(aspect * 100) / 100.0f;
//aspect = 1.42857f;
pMatrix.perspective(60.0f, aspect, 0.000f, 100.f);
//pMatrix.ortho(0, 0 , w, h, 0, 100);
vMatrix.setToIdentity();
vMatrix.lookAt(cameraPos, cameraPos+ cameraFront , cameraUp);
mMatrix.setToIdentity();
//mMatrix.translate(QVector3D{0.0,1.0,1.0});
mMatrix.rotate(00, QVector3D{1.0,0.0,0.0});
}
void QMyOpenglWidget::paintGL()
{
//qDebug()<<" paintGL";
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int tranlate;
static int xrotate = 1;
xrotate +=1;
glUniformMatrix4fv(mLocation, 1, GL_FALSE, mMatrix.data());
glUniformMatrix4fv(vLocation, 1, GL_FALSE, vMatrix.data());
glUniformMatrix4fv(pLocation, 1, GL_FALSE, pMatrix.data());
GLfloat x=0, y=0, z=3.0f;
glUniform3f(lightPosLocation, x, y, z);
glUniform3f(viewPosLocation, cameraPos[0], cameraPos[1], cameraPos[2]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture1);
//mMatrix.rotate(1, QVector3D{1.0,0.5,0.0});
//glActiveTexture(GL_TEXTURE0);
int num = 5;
//for(int i = 0; i < num; i++)
{
mMatrix.setToIdentity();
mMatrix.translate(-3, 3, 0);
glUniformMatrix4fv(mLocation, 1, GL_FALSE, mMatrix.data());
glBindVertexArray(vao1);
glDrawArrays(GL_QUADS, 0, 64);
mMatrix.setToIdentity();
mMatrix.translate(0, 0, 0);
glUniformMatrix4fv(mLocation, 1, GL_FALSE, mMatrix.data());
glBindVertexArray(vao1);
glDrawArrays(GL_QUADS, 0, 64);
mMatrix.setToIdentity();
mMatrix.translate(3, 3, 0);
glUniformMatrix4fv(mLocation, 1, GL_FALSE, mMatrix.data());
glBindVertexArray(vao1);
glDrawArrays(GL_QUADS, 0, 64);
}
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");
lightPosLocation = glGetUniformLocation(programId, "lightPos");
viewPosLocation = glGetUniformLocation(programId, "viewPos");
vertexLocation = glGetAttribLocation(programId, "pos");
normalLocation = glGetAttribLocation(programId, "normal");
uvLocation = glGetAttribLocation(programId, "uv0");
Q_ASSERT(mLocation != -1);
Q_ASSERT(vLocation != -1);
Q_ASSERT(pLocation != -1);
Q_ASSERT(vertexLocation != -1);
Q_ASSERT(normalLocation != -1);
Q_ASSERT(lightPosLocation != -1);
Q_ASSERT(viewPosLocation != -1);
Q_ASSERT(uvLocation != -1);
}
void QMyOpenglWidget::initDrawData(GLuint &vao, float *vertices, int vSize, float *uv, int uSize, float *normals, int nSize)
{
//**************vao******************************//
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//******************* Vbo vertices*******************//
unsigned int vbo1;
glGenBuffers(1, &vbo1);
glBindBuffer(GL_ARRAY_BUFFER, vbo1);
//为当前绑定到target 的缓冲区对象创建一个新的数据存储 //
glBufferData(GL_ARRAY_BUFFER, vSize, vertices, GL_STATIC_DRAW);
//告知显卡如何解析缓冲区里的属性值
glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, FALSE, 3*sizeof (GL_FLOAT), nullptr);
//开启VAO管理的第一个属性
glEnableVertexAttribArray(vertexLocation);
//******************* Vbo Normals*******************//
unsigned int vbo2;
glGenBuffers(1, &vbo2);
glBindBuffer(GL_ARRAY_BUFFER, vbo2);
//为当前绑定到target 的缓冲区对象创建一个新的数据存储 //
glBufferData(GL_ARRAY_BUFFER, nSize, normals, GL_STATIC_DRAW);
//告知显卡如何解析缓冲区里的属性值
glVertexAttribPointer(normalLocation, 3, GL_FLOAT, FALSE, 3*sizeof (GL_FLOAT), nullptr);
//开启VAO管理的第一个属性
glEnableVertexAttribArray(normalLocation);
//************** Vbo UV**************************/
unsigned int vboUv;
glGenBuffers(1, &vboUv);
glBindBuffer(GL_ARRAY_BUFFER, vboUv);
//为当前绑定到target 的缓冲区对象创建一个新的数据存储 //
glBufferData(GL_ARRAY_BUFFER, uSize, uv, GL_STATIC_DRAW);
//告知显卡如何解析缓冲区里的属性值
glVertexAttribPointer(uvLocation, 2, GL_FLOAT, FALSE, 2*sizeof (GL_FLOAT), nullptr);
//开启VAO管理的第一个属性
glEnableVertexAttribArray(uvLocation);
}
unsigned int QMyOpenglWidget:: setImage(const char *filename)
{
QImage image = QImage(filename);
image = QGLWidget::convertToGLFormat(image); //格式转换
unsigned int texture;
glGenTextures(1, &texture);
//glActiveTexture(textureNum);
glBindTexture(GL_TEXTURE_2D, texture);
// set the texture wrapping/filtering options (on the currently bound texture object)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load and generate the texture
int width, height, nrChannels;
width = image.width();
height = image.height();
nrChannels = image.depth();
Q_ASSERT(width>0 && height > 0);
//unsigned char *data = 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();
}
void QMyOpenglWidget::mousePressEvent(QMouseEvent *event)
{
isLeftPress = true;
curPos = event->pos();
}
void QMyOpenglWidget::mouseReleaseEvent(QMouseEvent *event)
{
isLeftPress = false;
}
void QMyOpenglWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
}
void QMyOpenglWidget::mouseMoveEvent(QMouseEvent *event)
{
if(!isLeftPress) return;
QPoint newPos = event->pos();
QPoint xy = newPos - curPos;
curPos = newPos;
//vMatrix.rotate(xy.x(), QVector3D(0, 1, 0));
//vMatrix.rotate(xy.y(), QVector3D(1, 0, 0));
yaw += xy.x()*0.1;
pitch += xy.y()*0.1;
#if 0
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
#endif
QVector3D front;
front[0] = qCos(qDegreesToRadians(yaw)) * qCos(qDegreesToRadians(pitch));
front[1] = qSin(qDegreesToRadians(pitch));
front[2] = qSin(qDegreesToRadians(yaw)) * qCos(qDegreesToRadians(pitch));
cameraFront = front.normalized();
vMatrix.setToIdentity();
vMatrix.lookAt(cameraPos, cameraPos+ cameraFront , cameraUp);
}
void QMyOpenglWidget::keyPressEvent(QKeyEvent *event)
{
//qDebug()<<"keyPress";
}
void QMyOpenglWidget::keyReleaseEvent(QKeyEvent *event)
{
//qDebug()<<"keyRelease";
int keyCode = event->key();
if(keyCode == Qt::Key_Space)
{
//mMatrix.setToIdentity();
//vMatrix.setToIdentity();
//vMatrix.lookAt(QVector3D{0.0,0.0,10.0}, QVector3D{0.0,0.0,0.0},QVector3D{0.0,1.0,0.0});
yaw = -90;
pitch = 0;
QVector3D front;
front[0] = qCos(qDegreesToRadians(yaw)) * qCos(qDegreesToRadians(pitch));
front[1] = qSin(qDegreesToRadians(pitch));
front[2] = qSin(qDegreesToRadians(yaw)) * qCos(qDegreesToRadians(pitch));
cameraFront = front.normalized();
vMatrix.setToIdentity();
cameraFront = QVector3D(0, 0, -1);
cameraPos = QVector3D(0, 0, 10);
cameraUp = QVector3D(0, 1, 0);
vMatrix.lookAt(cameraPos, cameraPos+ cameraFront , cameraUp);
}
switch(keyCode)
{
case Qt::Key_W:
{
cameraPos += cameraFront;
}
break;
case Qt::Key_S:
{
cameraPos -= cameraFront;
}
break;
case Qt::Key_A:
{
cameraPos -= QVector3D::crossProduct(cameraFront, cameraUp).normalized() * 0.1;
}
break;
case Qt::Key_D:
{
cameraPos += QVector3D::crossProduct(cameraFront, cameraUp).normalized() * 0.1;
}
break;
case Qt::Key_Q:
{
cameraPos -= QVector3D(cameraFront[1], cameraFront[2], cameraFront[0]);
}
break;
case Qt::Key_E:
{
cameraPos += QVector3D(cameraFront[1], cameraFront[2], cameraFront[0]);
}
break;
case Qt::Key_Escape:
{
QApplication::quit();
}
break;
}
vMatrix.setToIdentity();
vMatrix.lookAt(cameraPos, cameraPos+ cameraFront , cameraUp);
}
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;
out vec3 fragPos;
void main(void)
{
//法线方向随着模型 矩阵变动而变动
//fnormal = vec3(mMatrix * vec4(normal, 1.0));
//fnormal = mat3(transpose(inverse(mMatrix))) * normal;
fnormal = vec3(transpose(inverse(mMatrix)) * vec4(normal, 1.0f));
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;
in vec3 fragPos;
out vec4 FragColor; //片段着色器输出
in vec2 TexCoord0;
uniform sampler2D texture0;
uniform sampler2D specular;
//uniform vec3 lightDir;
uniform vec3 lightPos;
uniform vec3 viewPos;
void main(void)
{
vec3 lightColor = vec3(1.0, 1.0, 1.0);
//距离
float distance = length(lightPos - fragPos);
//点光源强度
float pointPow = max(dot(normalize(fnormal), normalize(lightPos - fragPos)), 0.2) ;
//贴图状态
vec3 ambient = texture2D(texture0, TexCoord0).rgb * 0.2;
//点光源漫反射
vec3 lightAmbient = texture2D(texture0, TexCoord0).rgb * pointPow * 3 * 1/ distance;
//视觉方向
vec3 viewDir = normalize(viewPos - fragPos);
//光源方向
vec3 lightDir = normalize(lightPos - fragPos);
//反射方向
vec3 reflectDir = reflect(-lightDir, normalize(fnormal));
//镜面发射强度计算
float specPow = pow(max(dot(viewDir, reflectDir), 0.0), 32) * 3;
//镜面反射贴图叠加 反射强度
vec3 speColor = specPow * texture2D(specular, TexCoord0).rgb * lightColor;
FragColor = vec4(ambient +lightAmbient + speColor, 1.0);
}
?
?
?
?
?
?
|