1。glBufferData glVertexAttribPointer 两个语句联合使用,不要分开为好
2 。如果是通过索引 绘图 一定不要忘了这句?。
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexEbo);
3 如果有 只有一张贴图的话 加上这句?
glBindTexture(GL_TEXTURE_2D, texture);
#include "widget.h"
#include "ui_widget.h"
#include <QGLWidget>
Widget::Widget(QOpenGLWidget *parent)
: QOpenGLWidget(parent)
{}
Widget::~Widget()
{}
void Widget::initializeGL()
{
initializeOpenGLFunctions();
if(!program.addShaderFromSourceFile(QOpenGLShader::Vertex, "E:\\wqs\\OpenglQt\\QtOpengLesson01\\vertexshader.vert"))
{
return;
}
if(!program.addShaderFromSourceFile(QOpenGLShader::Fragment, "E:\\wqs\\OpenglQt\\QtOpengLesson01\\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");
textureLocation = glGetAttribLocation(programId, "vTexCoord");
ourTextureLocation = glGetUniformLocation(programId, "ourTexture");
//Q_ASSERT(mLocation != -1);
// Q_ASSERT(vLocation != -1);
Q_ASSERT(pLocation != -1);
Q_ASSERT(vertexLocation != -1);
Q_ASSERT(colorLocation != -1);
Q_ASSERT(textureLocation != -1);
Q_ASSERT(ourTextureLocation != -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 texCoords[] = {
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, &uv);
glBindBuffer(GL_ARRAY_BUFFER, uv);
//装载数据到显存
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof (GLfloat), texCoords, GL_STATIC_DRAW);
// 将数据传输给shader
glVertexAttribPointer(textureLocation, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)(0));
setImage("E:\\wqs\\OpenglQt\\QtOpengLesson01\\n.jpg");
}
void Widget:: setImage(const char *filename)
{
//textures = new QOpenGLTexture(QImage(filename).mirrored());
QImage image = QImage(filename);
image = QGLWidget::convertToGLFormat(image); //格式转换
glGenTextures(1, &texture);
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);
}
}
void Widget::paintGL()
{
//清除之前图形并将背景设置为黑色(设置为黑色纯粹个人爱好!)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.2f, 0.2f, 1.0f);
// shader传入模型视图矩阵 projection可以理解为建立了一个坐标系空间,可以再这个空间内设置图形
glUniformMatrix4fv(pLocation, 1, GL_FALSE, pMatrix.data());
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexEbo);
//启用颜色数组buffer
glEnableVertexAttribArray(colorLocation);
glEnableVertexAttribArray(vertexLocation);
glEnableVertexAttribArray(textureLocation);
glBindTexture(GL_TEXTURE_2D, texture);
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);
}
vertexshader.vert
#version 430
uniform mat4 pMatrix;//, vMatrix, mMatrix;
in vec3 vPosition;
in vec4 vColor;
in vec2 vTexCoord;
out vec4 fColor;
out vec2 TexCoord;
void main(void)
{
fColor = vColor;
gl_Position = pMatrix * vec4(vPosition, 1.0);
TexCoord = vTexCoord;
}
fragmentshader.frag
#version 430
in vec4 fColor;
in vec2 TexCoord;
uniform sampler2D ourTexture;
//out vec4 gl_FragColor;
//gl_FragColor = fColor;
void main(void)
{
//gl_FragColor = texture(ourTexture, TexCoord);
gl_FragColor = texture2D(ourTexture, TexCoord) + fColor * 0.5;
}
|