qt opengl 混合实现半透明
生活随笔
收集整理的這篇文章主要介紹了
qt opengl 混合实现半透明
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ?在二維繪圖里面的半透明很簡單,把alpha通道的值不設置為1或者255就能看見后面的物體;后面物體的清晰度根據設置值的大小來決定。而在opengl中要實現半透明效果也很簡單,但相比二維繪圖還是要麻煩一些了。
? ? ? 其方法為啟用GL_BLEND混合功能,設置混合因子;這樣opengl就會用混合因子將前、后兩個物體顏色進行混合;這里有很多種混合方式,可以只顯示前面物體,也可以只顯示后面物體,也可以像素疊加(分為rgb疊加,alpha疊加)等待。
? ? ?我實現的效果如下
? ? ? 實現相比以前簡單得多,就設置了下啟用GL_BLEND功能,和設置混合因子。其它的渲染器我是用的前面的現成的
這里我只介紹綠色圖片的混合實現,其渲染器很簡單就是一個普通的紋理2d圖片
#ifndef IMAGERENDER_H #define IMAGERENDER_H#include <QOpenGLShaderProgram> #include <QOpenGLBuffer> #include <QOpenGLTexture> #include <QOpenGLExtraFunctions> class ImageRender { public:ImageRender() = default;~ImageRender();void initsize(QString fileName);void render(QOpenGLExtraFunctions *f,QMatrix4x4 &pMatrix,QMatrix4x4 &vMatrix,QMatrix4x4 &mMatrix);private:QOpenGLShaderProgram program_;QOpenGLTexture *texture_{nullptr};QOpenGLBuffer vbo_; };#endif // IMAGERENDER_H #include "imagerender.h"ImageRender::~ImageRender() {texture_->destroy();delete texture_; }void ImageRender::initsize(QString fileName) {program_.addCacheableShaderFromSourceFile(QOpenGLShader::Vertex,"vsrc.vert");program_.addCacheableShaderFromSourceFile(QOpenGLShader::Fragment,"fsrc.frag");program_.link();texture_ = new QOpenGLTexture(QImage(fileName));texture_->setMinificationFilter(QOpenGLTexture::LinearMipMapNearest);texture_->setMagnificationFilter(QOpenGLTexture::LinearMipMapLinear);texture_->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::ClampToEdge);texture_->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::ClampToEdge);GLfloat points[4 * 3 + 8]{-1.0,-1.0,0.0,+1.0,-1.0,0.0,+1.0,+1.0,0.0,-1.0,+1.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0};vbo_.create();vbo_.bind();vbo_.allocate(points, sizeof points); }void ImageRender::render(QOpenGLExtraFunctions *f, QMatrix4x4 &pMatrix, QMatrix4x4 &vMatrix, QMatrix4x4 &mMatrix) {f->glEnable(GL_DEPTH_TEST);f->glEnable(GL_CULL_FACE);program_.bind();vbo_.bind();f->glActiveTexture(GL_TEXTURE0 + 0);program_.setUniformValue("sTexture",0);program_.setUniformValue("uPMatrix",pMatrix);program_.setUniformValue("uVMatrix",vMatrix);program_.setUniformValue("uMMatrix",mMatrix);program_.enableAttributeArray(0);program_.enableAttributeArray(1);program_.setAttributeBuffer(0,GL_FLOAT,0,3,3 * sizeof GLfloat);program_.setAttributeBuffer(1,GL_FLOAT,4 * 3 * sizeof GLfloat, 2, 2 * sizeof GLfloat);texture_->bind(0);f->glDrawArrays(GL_TRIANGLE_FAN,0,4);program_.disableAttributeArray(0);program_.disableAttributeArray(1);texture_->release();vbo_.release();program_.release();f->glDisable(GL_DEPTH_TEST);f->glDisable(GL_CULL_FACE); }其shader如下
#version 330 uniform mat4 uPMatrix,uVMatrix,uMMatrix; layout (location = 0) in vec3 aPosition; layout (location = 1) in vec2 aTexture; smooth out vec2 vTextureCood;void main(void) {gl_Position = uPMatrix * uVMatrix * uMMatrix * vec4(aPosition,1);vTextureCood = aTexture; } #version 330 uniform sampler2D sTexture; in vec2 vTextureCood; out vec4 fragColor;void main(void) {fragColor = texture2D(sTexture,vTextureCood); }? 其它的還用到了mipmap紋理渲染器,和一個obj模型渲染器,在前面有就不介紹了。在使用時根據需要打開混合功能。
mMatrix.setToIdentity();mMatrix.translate(3,1,10);imageRender_.render(f,pMatrix,vMatrix,mMatrix); //不啟用混合f->glEnable(GL_BLEND); //啟用混合mMatrix.setToIdentity();mMatrix.translate(-1.2,1.6,10);mMatrix.scale(1.2);f->glBlendFunc(GL_SRC_COLOR,GL_ONE_MINUS_SRC_COLOR); //用前物體因子用rgba、后物體因子為前物體rgba的1-前物體的rgba值(opengl中顏色值為0-1)。實現的效果類似于透過有色玻璃看物體imageRender_.render(f,pMatrix,vMatrix,mMatrix);mMatrix.setToIdentity();mMatrix.translate(1.0,2.6,10);f->glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); //以前物體的透明度為基準,前物體全透明,就只能看見后面的物體;前物體半透明,就能看見后面物體。imageRender1_.render(f,pMatrix,vMatrix,mMatrix);f->glDisable(GL_BLEND);//關閉混合? 源碼到此下載https://download.csdn.net/download/wanghualin033/10854601
總結
以上是生活随笔為你收集整理的qt opengl 混合实现半透明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 慎用yum update
- 下一篇: ROS高效入门第一章 -- ROS历史与