先上效果
核心代码解析
//单张贴图时 只用绑定一次 glBindTexture(GL_TEXTURE_2D, texture0);
激活绑定第一张贴图
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture0);
激活绑定第二张贴图
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture1);
为两张贴图 传输纹理
glUniform1i(glGetUniformLocation(programId, "texture0"), 0);
glUniform1i(glGetUniformLocation(programId, "texture1"), 1);
vertexshader.vert
#version 430
uniform mat4 pMatrix;//, vMatrix, mMatrix;
in vec3 vPosition;
in vec4 vColor;
in vec2 vTexCoord0;
in vec2 vTexCoord1;
out vec4 fColor;
out vec2 TexCoord0;
out vec2 TexCoord1;
void main(void)
{
fColor = vColor;
gl_Position = pMatrix * vec4(vPosition, 1.0);
TexCoord0 = vTexCoord0;
TexCoord1 = vTexCoord1;
}
fragmentshader.frag
#version 430
in vec4 fColor;
in vec2 TexCoord0;
in vec2 TexCoord1;
uniform sampler2D texture0;
uniform sampler2D texture1;
void main(void)
{
gl_FragColor = texture2D(texture0, TexCoord0) * 0.4 + texture2D(texture1, TexCoord1)* 0.8 + fColor * 0.1;
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLTexture>
#include <QOpenGLVertexArrayObject>
#include <QImage>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QOpenGLWidget, public QOpenGLFunctions
{
Q_OBJECT
public:
Widget(QOpenGLWidget *parent = nullptr);
~Widget();
protected:
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
unsigned int setImage(const char *filename, const char* textureSample, unsigned int textureNum = GL_TEXTURE0);
QOpenGLTexture *textures;
QOpenGLShaderProgram program;
GLuint programId;
GLint mLocation;
GLint vLocation;
GLint pLocation;
GLint vertexLocation;
GLint colorLocation;
GLint uv0Location;
GLint uv1Location;
GLint textureLocation0;
GLint textureLocation1;
QVector3D cameraLocation;
GLuint vertexVbo;
GLuint indexEbo;
GLuint colorVbo;
GLuint uv0;
GLuint uv1;
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
QMatrix4x4 pMatrix;
unsigned int texture0;
unsigned int texture1;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
//注意 .pro文件要加入 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets opengl
#include <QGLWidget>
Widget::Widget(QOpenGLWidget *parent)
: QOpenGLWidget(parent)
{}
Widget::~Widget()
{}
void Widget::initializeGL()
{
initializeOpenGLFunctions();
if(!program.addShaderFromSourceFile(QOpenGLShader::Vertex, "E:\\wqs\\OpenglQt\\QtOpenglLesson02\\vertexshader.vert"))
{
return;
}
if(!program.addShaderFromSourceFile(QOpenGLShader::Fragment, "E:\\wqs\\OpenglQt\\QtOpenglLesson02\\fragmentshader.frag"))
{
return;
}
bool bl = program.link();
Q_ASSERT(bl);
bool bb = program.bind();
Q_ASSERT(bb);
programId = program.programId();
//mLocation = glGetUniformLocation(programId, "mMatrix");
//vLocation = glGetUniformLocation(programId, "vMatrix");
pLocation = glGetUniformLocation(programId, "pMatrix");
vertexLocation = glGetAttribLocation(programId, "vPosition");
colorLocation = glGetAttribLocation(programId, "vColor");
uv0Location = glGetAttribLocation(programId, "vTexCoord0");
uv1Location = glGetAttribLocation(programId, "vTexCoord1");
textureLocation0 = glGetUniformLocation(programId, "texture0");
textureLocation1 = glGetUniformLocation(programId, "texture1");
//Q_ASSERT(mLocation != -1);
// Q_ASSERT(vLocation != -1);
Q_ASSERT(pLocation != -1);
Q_ASSERT(vertexLocation != -1);
Q_ASSERT(colorLocation != -1);
Q_ASSERT(uv0Location != -1);
Q_ASSERT(textureLocation0 != -1);
//三角形顶点坐标
GLfloat vertex[] = {
-0.8f, -0.5f, 1.0f,
0.8f, -0.5f, 1.0f,
0.8f, 0.5f, 1.0f,
-0.8f, 0.5f, 1.0f
};
//三角形顶点索引
GLuint triIndexs[] = {0, 1, 2, 0, 2, 3};
//三角形顶点颜色
GLfloat colors[] = {0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
};
float _uv0Float[] = {
0.0f, 0.0f, // lower-left corner
2.0f, 0.0f, // lower-right corner
2.0f, 2.0f, // top-rigth corner
0.0f, 2.0f // top-rigth corner
};
float _uv1Float[] = {
0.0f, 0.0f, // lower-left corner
1.0f, 0.0f, // lower-right corner
1.0f, 1.0f, // top-rigth corner
0.0f, 1.0f // top-rigth corner
};
// 初始化索引buffer并装载数据到显存
glGenBuffers(1, &indexEbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexEbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(GLuint), triIndexs, GL_STATIC_DRAW);
//glBufferData glVertexAttribPointer 两个语句联合使用,不要分开为好
// 初始化颜色buffer并装载数据到显存
glGenBuffers(1, &colorVbo);
glBindBuffer(GL_ARRAY_BUFFER, colorVbo);
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(GLfloat), colors, GL_STATIC_DRAW);
// 将数据传输给shader
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4*(sizeof(float)), 0);
glGenBuffers(1, &vertexVbo);
glBindBuffer(GL_ARRAY_BUFFER, vertexVbo);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof (GLfloat), vertex, GL_STATIC_DRAW);
// 将数据传输给shader
glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glGenBuffers(1, &uv0);
glBindBuffer(GL_ARRAY_BUFFER, uv0);
//装载数据到显存
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof (GLfloat), _uv0Float, GL_STATIC_DRAW);
// 将数据传输给shader
glVertexAttribPointer(uv0Location, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)(0));
glGenBuffers(1, &uv1);
glBindBuffer(GL_ARRAY_BUFFER, uv1);
//装载数据到显存
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof (GLfloat), _uv1Float, GL_STATIC_DRAW);
// 将数据传输给shader
glVertexAttribPointer(uv1Location, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)(0));
//为shader 中的纹理传输数据做绑定。
glUniform1i(glGetUniformLocation(programId, "texture0"), 0);
glUniform1i(glGetUniformLocation(programId, "texture1"), 1);
texture0 = setImage("E:\\wqs\\OpenglQt\\QtOpenglLesson02\\n.jpg");
texture1 = setImage("E:\\wqs\\OpenglQt\\QtOpenglLesson02\\1.jpg");
}
unsigned int Widget:: 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();
//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 Widget::paintGL()
{
//清除之前图形并将背景设置为黑色(设置为黑色纯粹个人爱好!)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.2f, 0.2f, 1.0f);
//unsigned int texture = setImage("E:\\wqs\\OpenglQt\\QtOpenglLesson02\\n.jpg");
// shader传入模型视图矩阵 projection可以理解为建立了一个坐标系空间,可以再这个空间内设置图形
glUniformMatrix4fv(pLocation, 1, GL_FALSE, pMatrix.data());
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexEbo);
//启用颜色数组buffer
glEnableVertexAttribArray(colorLocation);
glEnableVertexAttribArray(vertexLocation);
glEnableVertexAttribArray(uv0Location);
glEnableVertexAttribArray(uv1Location);
//单张贴图时 只用绑定一次 glBindTexture(GL_TEXTURE_2D, texture0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture1);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0);
// 解绑buffer、关闭启用顶点、颜色数组
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(vertexLocation);
glDisableVertexAttribArray(colorLocation);
}
void Widget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
pMatrix.setToIdentity();
float aspect = float(w)/ float(h?h:1);
pMatrix.perspective(60.0f, aspect, 1.0f, 100.f);
//vMatrix.lookAt()
QVector3D cameraLocation;
cameraLocation.setX(0);
cameraLocation.setY(0);
cameraLocation.setZ(20);
vMatrix.lookAt(cameraLocation, QVector3D{0.0,0.0,0.0},QVector3D{0.0,1.0,0.0});
mMatrix.setToIdentity();
//mMatrix.translate(0, 0.0, 20.0f);
pMatrix.translate(0, 0.0, -2.0f);
}