当前位置:网站首页>QT based OpenGL tutorial learning 13 - frame buffer

QT based OpenGL tutorial learning 13 - frame buffer

2021-01-23 16:24:47 Little Wang Ye_ two thousand and nineteen

course :

Frame buffer

This section introduces an important concept , Frame buffer , It can be used to achieve some cool effects ;

But I don't think it's better to read this tutorial alone , So I searched the Internet for other information myself , among :

Opengl Frame cache object This article will be more detailed , And also posted the original English article in the article , Interested partners can have a look ;

In the process of the subsequent implementation of the code encountered a small problem , If there are friends who read my previous articles, they may notice that , What I try to use is QT To implement these functions, you can encapsulate your own classes , But this time there was a small problem , Let's talk about it briefly ;

tutorial , application FBO The way to do this is to create a blank texture instance of your own attach To FBO For instance , Bind like this FBO And after executing the rendering command, the content will be rendered to us attach On this blank texture object , At this time, we will render the texture according to the normal path ; But if you use Qt Medium QOpenGLFrameBufferObject To achieve FBO Words , You'll find that you can't create a texture instance yourself attach To QOpenGLFrameBufferObject On an instance of . however QOpenGLFrameBufferObject Provides texture() Method to help you get the texture generated by rendering ;

So as long as we render the texture directly ; however texture() Back to a textureid Not at all QOpenGLTexture Example , So we can't just use it like before QT Encapsulated texture to paint , Only native functions and QT Encapsulate the object function mix to render ;

I went to look through this question QT The official forum of , The explanation given is ,QOpenGLTexture The ratio that appears QOpenGLFrameBufferObject On the evening of , So there's no way to get it directly QOpenGLTexture Types of textures , I do not know! QT6 Has this problem been fixed in ;

The following is a simple paste of the rendering part of the code , Other source code can go to git View in :

void MyGLWidget::paintGL()
{

    bool result = fbo->bind();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDisable( GL_CULL_FACE);
    switch (curdepthfunc) {
    case DEPTHFUNC::LESS:
    {
        glDepthFunc(GL_LESS);
        break;
    }
    case DEPTHFUNC::ALWAYS:
    {
        glDepthFunc(GL_ALWAYS);
        qDebug()<<"always";
        break;
    }
    case DEPTHFUNC::NEVER:
    {
        glDepthFunc(GL_NEVER);
        qDebug()<<"never";
        break;
    }
    case DEPTHFUNC::EQUAL:
    {
        glDepthFunc(GL_EQUAL);
        break;
    }
    case DEPTHFUNC::LEQUAL:
    {
        glDepthFunc(GL_LEQUAL);
        break;
    }
    case DEPTHFUNC::GREATER:
    {
        glDepthFunc(GL_GREATER);
        break;
    }
    case DEPTHFUNC::NOTEQUAL:
    {
        glDepthFunc(GL_NOTEQUAL);
        break;
    }
    case DEPTHFUNC::GEQUAL:
    {
        glDepthFunc(GEQUAL);
        break;
    }

    }
    plane->Render();
    glEnable( GL_CULL_FACE);
    QVector<Shape*>::iterator i;
    for(i=cubevec.begin();i!=cubevec.end();++i)
    {
        (*i)->Render();
    }

    // Sort 
    std::map<float,QVector3D> sorted;
    for(int j=0;j<windows.size();j++)
    {
        QVector3D disvec=m_camera->Position-windows[j];
        float distance=disvec.lengthSquared();
        sorted[distance] = windows[j];

    }
     glDisable(GL_CULL_FACE);
    int count=0;
    for(std::map<float,QVector3D>::reverse_iterator  it=sorted.rbegin();it!=sorted.rend();++it)
    {
        grassvec[count]->SetTranslateVec(it->second);
        grassvec[count]->Render();
        count++;
    }

    if(result)
    {
        GLuint textureid=fbo->texture();
        fbo->release();
      //   QImage img= fbo->toImage();
      //   img.save("rendertotexture.jpg");
        m_program->bind();
        {
         // bind textures on corresponding texture units
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, textureid);
        glUniform1i(m_program->uniformLocation("screenTexture"), 0);
        QMatrix4x4 view=m_camera->GetViewMatrix();
        m_program->setUniformValue("view", view);

        QMatrix4x4 projection;
        projection.perspective(m_camera->Zoom, 1.0f * this->width() /this->height(), 0.1f, 100.0f);
        m_program->setUniformValue("projection", projection);

        QOpenGLVertexArrayObject::Binder vaoBind(m_vao);
        glDrawArrays(GL_TRIANGLES, 0, 6);


        }
       m_program->release();

    }
    glEnable(GL_DEPTH_TEST);
}

In the above code , We can see , Bind first FBO, Then perform the rendering operations we wrote in the previous article , here , Rendering results are not rendered to the screen , It's rendering to what we created FBO in , If you use it at this time :

QImage img= fbo->toImage();
img.save("rendertotexture.jpg");

We can get a currently rendered image ;

And then we're going to FBO The rendering results stored in are rendered to our screen , This is the time , First of all, remember

  fbo->release();

So when we do the rendering again , To render to our screen ;

FBO Part of the basic usage is like this , The tutorial also provides us with some ways to implement post-processing ; That is, for the texture we get , We can implement some image processing operations on this texture in the shader ; This part of the content will not be pasted in detail , I think we should open a separate branch to introduce image processing using shaders ;

Source code :

Submission of information :FBO

Even though my reading of articles is very low , But I still want to continue , On the one hand, it's taking notes ,opengl It's been used off and on for a long time , But because it involves more concepts , Once there's a break in the middle , Pick up some concepts and recall them again , Notes I took elsewhere , After a long time, it became very messy , Now it's on the blog , It's a lot clearer , And I hope someone will have time to see the problems in my code , Can you tell me ;

To this FBO This part , I suddenly thought of another tutorial ,GLSL dependent , I'll write that down when I have time ; By the way, before the next update , Sort out the existing code , Now there are a lot of duplicate code in this experimental project , I can't watch it myself any more ;(*/ω\*)

版权声明
本文为[Little Wang Ye_ two thousand and nineteen]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210123162222127x.html