qmyopenglwidget.h
#ifndef QMYOPENGLWIDGET_H
#define QMYOPENGLWIDGET_H
#include <QWidget>
#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>
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, GLuint &ebo);
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 mixLocation;
GLint vertexLocation;
GLint colorLocation;
GLint uvLocation;
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
QMatrix4x4 pMatrix;
unsigned int texture0;
unsigned int texture1;
signals:
public slots:
};
#endif // QMYOPENGLWIDGET_H
qmyopenglwidget.cpp
#include "qmyopenglwidget.h"
#include <QGLWidget>
#include <QTime>
int count = 10000;
QMyOpenglWidget::QMyOpenglWidget(QWidget* parent):QOpenGLWidget(parent)
{
}
void QMyOpenglWidget::initializeGL()
{
initializeOpenGLFunctions();
cretaeShader();
//三角形顶点坐标
float s = 2.0f/2;
float s2 = 2.0f;
GLfloat *vertices = new float[count*3];
for(int i = 0; i < count * 3;)
{
vertices[i++] = rand()%1000 - 500;
vertices[i++] = rand()%1000 - 500;
vertices[i++] = rand()%1000 - 500;
}
initDrawData(vao1, vertices, count*3, ebo1);
glEnable(GL_CULL_FACE); //启动了背面裁剪
glFrontFace(GL_CCW); //设置逆时针为正面
glEnable(GL_BLEND); //启动混合
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); //混合模式选择
glEnable(GL_PROGRAM_POINT_SIZE); //开启shader 设置点大小
glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT); //设置点精灵中 贴图的原点 位置为 左下角
glEnable(GL_POINT_SPRITE_ARB); //启动点精灵贴图绘制
}
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(45.0f, aspect, 0.000f, 10000.f);
//pMatrix.ortho(float left, float bottom, float width, float height)
//pMatrix.ortho(0, 0 , w, h, 0, 100);
vMatrix.lookAt(QVector3D{0.0,0.0,-1000.0}, QVector3D{0.0,0.0,1.0},QVector3D{0.0,1.0,0.0});
mMatrix.setToIdentity();
mMatrix.translate(QVector3D{0.0,0.0,3.0});
mMatrix.rotate(-30, QVector3D{1.0,0.0,0.0});
update();
}
void QMyOpenglWidget::paintGL()
{
//qDebug()<<" paintGL";
glClearColor(0.2f, 0.2f, 0.1f, 0.1f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//混合计时
int nowMSec5 = QTime::currentTime().msec() + (QTime::currentTime().second()%5)*1000;
float mix = (abs((nowMSec5)%5000 - 2500))/2500.0f;
glUniform1f(mixLocation, mix);
//qDebug()<<mix;
glUniformMatrix4fv(mLocation, 1, GL_FALSE, mMatrix.data());
glUniformMatrix4fv(vLocation, 1, GL_FALSE, vMatrix.data());
glUniformMatrix4fv(pLocation, 1, GL_FALSE, pMatrix.data());
//多张纹理激活 绑定
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture1);
mMatrix.rotate(-1, QVector3D{1.0,0,0});
//glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, FALSE, 3*sizeof (GL_FLOAT), nullptr);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo1);
glBindVertexArray(vao1);
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
//点精灵 绘制时需要选择 GL_POINTS
glDrawArrays(GL_POINTS, 0, count);
update();
//glDisable(GL_BLEND);//关闭混合
//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");
mixLocation = glGetUniformLocation(programId, "mix");
vertexLocation = glGetAttribLocation(programId, "pos");
//colorLocation = glGetAttribLocation(programId, "color");
//uvLocation = glGetAttribLocation(programId, "uv0");
Q_ASSERT(mLocation != -1);
Q_ASSERT(vLocation != -1);
Q_ASSERT(pLocation != -1);
//Q_ASSERT(mixLocation != -1);
Q_ASSERT(vertexLocation != -1);
//Q_ASSERT(colorLocation != -1);
//Q_ASSERT(uvLocation != -1);
}
void QMyOpenglWidget::initDrawData(GLuint &vao, float *vertices, int vSize, GLuint &ebo)
{
//顶点点颜色
GLfloat colors[] = { 1.0f, 0.0f, 0.0f,1.0f , //红
0.0f, 1.0f, 0.0f ,1.0f , //绿
0.0f, 0.0f, 1.0f ,1.0f , //蓝
0.0f, 0.0f, 0.0f ,1.0f , //白
1.0f, 0.0f, 0.0f,1.0f , //红
0.0f, 1.0f, 0.0f ,1.0f , //绿
0.0f, 0.0f, 1.0f ,1.0f , //蓝
0.0f, 0.0f, 0.0f ,1.0f , //白
1.0f, 0.0f, 0.0f,1.0f , //红
0.0f, 1.0f, 0.0f ,1.0f , //绿
0.0f, 0.0f, 1.0f ,1.0f , //蓝
0.0f, 0.0f, 0.0f ,1.0f , //白
1.0f, 0.0f, 0.0f,1.0f , //红
0.0f, 1.0f, 0.0f ,1.0f , //绿
0.0f, 0.0f, 1.0f ,1.0f , //蓝
0.0f, 0.0f, 0.0f ,1.0f , //白
};
//**************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 color**************************/
//unsigned int vboColor;
//glGenBuffers(1, &vboColor);
//glBindBuffer(GL_ARRAY_BUFFER, vboColor);
//为当前绑定到target 的缓冲区对象创建一个新的数据存储 //
//glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
//告知显卡如何解析缓冲区里的属性值
//glVertexAttribPointer(colorLocation, 4, GL_FLOAT, FALSE, 4*sizeof (GL_FLOAT), nullptr);
//开启VAO管理的第一个属性
//glEnableVertexAttribArray(colorLocation);
//**************创建纹理**************///
glUniform1i(glGetUniformLocation(programId, "texture0"), 0);
texture0 = setImage(":/shui.png");
glUniform1i(glGetUniformLocation(programId, "texture1"), 1);
texture1 = setImage(":/xue.png");
}
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();
}
vertex.vert
#version 330 core
uniform mat4 pMatrix;
uniform mat4 vMatrix;
uniform mat4 mMatrix;
in vec3 pos;
//in vec4 color;
//in vec2 uv0;
//out vec4 fcolor;
out vec2 TexCoord0;
void main(void)
{
//fcolor = color;
gl_PointSize = 32;
gl_Position = pMatrix * vMatrix * mMatrix * vec4(pos, 1.0);
//TexCoord0 = uv0;
}
frag.frag
#version 330 core
//in vec4 fcolor;
out vec4 FragColor; //片段着色器输出
//in vec2 TexCoord0;
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform float mix;
void main(void)
{
//FragColor = fcolor*0.3 + (texture2D(texture0, TexCoord0) * mix) + ((1 - mix) * texture2D(texture1, TexCoord0));
//FragColor = fcolor*0.3 + (texture2D(texture0, gl_PointCoord) * mix);//+ ((1 - mix) * texture2D(texture1, gl_PointCoord));
FragColor = (texture2D(texture0, gl_PointCoord)* mix) + (1 - mix) *(texture2D(texture1, gl_PointCoord));//+ ((1 - mix) * texture2D(texture1, gl_PointCoord));
}