当前位置: 首页 > news >正文

QT + opengl 让2d贴图动起来

1 qt+opengl 实现纹理贴图,平移旋转,绘制三角形,方形-CSDN博客

在上篇文章里面我已经学会了给贴图,并且旋转,那我们如何动态的显示2D的图片呢,那我们在qt里面是如何实现呢,定时器连续更新。

上代码 在第一篇的代码上加上定时器,看看效果

static const char *vertexShaderSource ="#version 330\n""layout (location = 0) in vec3 aPos;\n"     // 位置变量的属性位置值为0"layout (location = 1) in vec3 aColor;\n"     // 颜色变量的属性位置值为1"layout (location = 2) in vec2 aTexCoord;\n"  //纹理变量的属性位置值为2"out vec3 ourColor;\n"                     // 为片段着色器指定一个颜色输出"out vec2 TexCoord;\n"                     // 为片段着色器指定一个纹理输出"uniform mat4 transform;\n""void main(){\n""gl_Position =  transform * vec4(aPos, 1.0);\n"    //顶点信息为4个值向量   // 注意我们如何把一个vec3作为vec4的构造器的参数"ourColor = aColor;\n"        // 输出颜色变量==输入颜色"TexCoord = aTexCoord;\n"    // 输出纹理变量==输入纹理"}\n";static const char *fragmentShaderSource ="#version 330\n""out vec4 FragColor;\n"     //输出颜色"in vec3 ourColor;\n"      //输入的颜色== vertexShaderSource(这里面的输入颜色)"in vec2 TexCoord;\n"      //输入的纹理== vertexShaderSource(这里面的输入纹理)"uniform sampler2D texture1;\n"  //得到输入的纹理"uniform sampler2D texture2;\n"  //得到输入的纹理"void main()""{\n""FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.7)* vec4(ourColor, 1.0);\n""}\n";myGlWidget::myGlWidget(QWidget *parent):QOpenGLWidget(parent), m_ebo(QOpenGLBuffer::IndexBuffer), vbo(QOpenGLBuffer::VertexBuffer)
{timer = new QTimer;timer->setInterval(50);connect(timer,&QTimer::timeout,this,[=]{m_angle +=30;m_x+=0.01;if(m_angle==360){m_angle = 0;}if(m_x>1){m_x=-0.6f;}update();qDebug()<<"1233"<<m_angle;});timer->start();
}myGlWidget::~myGlWidget()
{}
void myGlWidget::resizeGL(int w, int h)
{this->glViewport(0,0,w,h);                //定义视口区域
}
void myGlWidget::paintGL()
{this->glClearColor(0.1f,0.5f,0.7f,1.0f);  //设置清屏颜色this->glClear(GL_COLOR_BUFFER_BIT);QMatrix4x4 matrix;matrix.setToIdentity();matrix.translate(m_x,0.0,0.0);matrix.rotate(m_angle,0,0,1);matrix.scale(0.5);//    QMatrix4x4 matrix;
//    matrix.setToIdentity();
//    //matrix.translate(0, 0, 0);      // x往左移动0.5 y往上移动0.5(opengl中的y方向和屏幕方向是反的)
//    matrix.rotate(45, 0, 0, 1);
//    matrix.scale(0.5);// 渲染Shadervao.bind();//m_texture->bind();program->setUniformValue("texture1", 0);m_texture->bind(0);program->setUniformValue("texture2", 1);m_texture2->bind(1);program->setUniformValue("transform", matrix);//glDrawElements(GL_TRIANGLES, 4, GL_UNSIGNED_INT, 0);glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);//绘制纹理    //绘制3个定点,样式为三角形
}void myGlWidget::initializeGL()
{// 为当前环境初始化OpenGL函数initializeOpenGLFunctions();glClearColor(1.0f, 1.0f, 1.0f, 1.0f);    //设置背景色为白色//初始化纹理对象m_texture  = new QOpenGLTexture(QOpenGLTexture::Target2D);m_texture->setData(QImage(":/cube1.png").mirrored()); //加载砖块图片m_texture->setMinMagFilters(QOpenGLTexture::LinearMipMapLinear,QOpenGLTexture::Nearest);//设置缩小和放大的方式,缩小图片采用LinearMipMapLinear线性过滤,并使用多级渐远纹理邻近过滤,放大图片采用:Nearest邻近过滤m_texture->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::Repeat);m_texture->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::Repeat);//m_texture->allocateStorage();//   //初始化纹理对象m_texture2  = new QOpenGLTexture(QOpenGLTexture::Target2D);m_texture2->setData(QImage(":/0.png").mirrored()); //返回图片的镜像,设置为Y轴反向,因为在opengl的Y坐标中,0.0对应的是图片底部m_texture2->setMinMagFilters(QOpenGLTexture::LinearMipMapLinear,QOpenGLTexture::Nearest);//设置缩小和放大的方式,缩小图片采用LinearMipMapLinear线性过滤,并使用多级渐远纹理邻近过滤,放大图片采用:Nearest邻近过滤m_texture2->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::Repeat);m_texture2->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::Repeat);//m_texture2->allocateStorage();//创建着色器程序program = new QOpenGLShaderProgram;program->addShaderFromSourceCode(QOpenGLShader::Vertex,vertexShaderSource);program->addShaderFromSourceCode(QOpenGLShader::Fragment,fragmentShaderSource);program->link();program->bind();//激活Program对象//初始化VBO,将顶点数据存储到buffer中,等待VAO激活后才能释放
//   float vertices[] = {
//           // 位置              // 颜色             //纹理
//           // positions          // colors           // texture coords
//           0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // top right
//           0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // bottom right
//           -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // bottom left
//           -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f  // top left
//       };float vertices[] = {//     ---- 位置 ----       ---- 颜色 ----     - 纹理坐标 --1.0f,  -1.0f, 1.0f, 1.0f, 0.0f, 0.0f,   0.0f, 1.0f,   // 右上1.0f, -1.0f, 1.0f,   0.0f, 1.0f, 0.0f,   1.0f, 1.0f,   // 右下-1.0f, 1.0f, 1.0f,    0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // 左下1.0f,  1.0f, 1.0f,    1.0f, 1.0f, 0.0f,   1.0f, 0.0f    // 左上};vbo.create();vbo.bind();              //绑定到当前的OpenGL上下文,vbo.allocate(vertices, sizeof(vertices));vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);  //设置为一次修改,多次使用//初始化VAO,设置顶点数据状态(顶点,法线,纹理坐标等)vao.create();vao.bind();// void setAttributeBuffer(int location, GLenum type, int offset, int tupleSize, int stride = 0);program->setAttributeBuffer(0, GL_FLOAT, 0,                  3, 8 * sizeof(float));   //设置aPos顶点属性program->setAttributeBuffer(1, GL_FLOAT, 3 * sizeof(float),  3, 8 * sizeof(float));   //设置aColor顶点颜色program->setAttributeBuffer(2, GL_FLOAT, 6 * sizeof(float),  2, 8 * sizeof(float));   //设置aColor顶点颜色//offset:第一个数据的偏移量//tupleSize:一个数据有多少个元素,比如位置为xyz,颜色为rgb,所以是3//stride:步长,下个数据距离当前数据的之间距离,比如右下位置和左下位置之间间隔了:3个xyz值+3个rgb值,所以填入 6 * sizeof(float)program->enableAttributeArray(0); //使能aPos顶点属性program->enableAttributeArray(1); //使能aColor顶点颜色program->enableAttributeArray(2); //使能aColor顶点颜色//解绑所有对象vao.release();vbo.release();}

运行下看看是不是连续旋转起来了,还平移了呢。

http://www.lryc.cn/news/457934.html

相关文章:

  • 【selenium】webdriver测试脚本
  • 工业自动化中的关键信号:开关量、模拟量与脉冲量
  • VMware vCenter Server 8.0U3c 发布下载,修复 U3b 更新停止响应的问题
  • Java面试宝典-Java集合02
  • HJ212-2017协议详解:工业物联网环境监测标准简单了解
  • 【Golang】Go语言Seeker接口与文件断点续传实战
  • 【MySQL】基本查询(下):更新、删除
  • django urlconf路由分发
  • The 2024 ICPC Kunming Invitational Contest K. Permutation(交互 期望)
  • TensorFlow与Pytorch的转换——1简单线性回归
  • 短剧小程序短剧APP在线追剧APP网剧推广分销微短剧小剧场小程序集师知识付费集师短剧小程序集师小剧场小程序集师在线追剧小程序源码
  • AI与物理学的交汇:Hinton与Hopfield获诺贝尔物理学奖
  • 六西格玛设计DFSS方法论在消费级无人机设计中的应用——张驰咨询
  • 按分类调用标签 调用指定分类下的TAG
  • 报错 - llama-index pydantic error | arbitrary_types_allowed | PydanticUserError
  • PostgreSQL Docker Error – 5432: 地址已被占用
  • 【LeetCode】动态规划—646. 最长数对链(附完整Python/C++代码)
  • 数字媒体产业园区:创新资源集聚,助力企业成长
  • 【Linux】来查看当前系统的架构
  • QT中的信号槽
  • 域名怎么转让给别人?
  • 计算机网络思维导图
  • 07.useDefault
  • git更加详细和灵活的提交过程,附带如何配置. gitignore来忽略部分文件的提交。
  • 使用正则表达式删除文本的奇数行或者偶数行
  • YOLOv10改进策略【注意力机制篇】| CVPR2024 CAA上下文锚点注意力机制
  • Unity修改鼠标图片【超简单】
  • windows C++-创建数据流代理(三)
  • C语言学习-循环嵌套打印字母金字塔
  • 探索CI/CD:持续集成与持续部署的基本概念