IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Framebuffer Example

Render into a framebuffer and use it as a texture.

This documentation was introduced in QtCanvas3D 1.0.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Framebuffer Example▲

The Framebuffer Example shows how to render into a framebuffer, create a texture of it, and apply the texture to an object in normal on-screen rendering.

The example has a moving and rotating cube, which has another textured cube drawn into it via the framebuffer object. The cube in the framebuffer can be rotated using https://s from https://.

Image non disponible

Preparing the Framebuffer▲

We first define the variables we need for the render-to-texture framebuffer:

 
Sélectionnez
var rttFramebuffer;
var rttTexture;
var rttWidth = 512;
var rttHeight = 512;

Then, in the initializeGL function, we create the framebuffer object:

 
Sélectionnez
// Create the framebuffer object
rttFramebuffer = gl.createFramebuffer();
rttFramebuffer.name = "OffscreenRenderTarget";
gl.bindFramebuffer(gl.FRAMEBUFFER, rttFramebuffer);

Creating the Texture▲

After the creation of the framebuffer, we create the texture:

 
Sélectionnez
// Create the texture
rttTexture = gl.createTexture();
rttTexture.name = "OffscreenRenderTargetTexture";
gl.bindTexture(gl.TEXTURE_2D, rttTexture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
gl.texImage2D(gl.TEXTURE_2D, 0,
              gl.RGBA, rttWidth, rttHeight,
              0, gl.RGBA, gl.UNSIGNED_BYTE,
              null);
gl.generateMipmap(gl.TEXTURE_2D);

Then we need to bind the texture as a color attachment, create and bind a render buffer, and bind the depth attachment:

 
Sélectionnez
// Bind the texture as color attachment, create and bind a depth buffer
gl.framebufferTexture2D(gl.FRAMEBUFFER,
                        gl.COLOR_ATTACHMENT0,
                        gl.TEXTURE_2D, rttTexture, 0);
var renderbuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer);
gl.renderbufferStorage(gl.RENDERBUFFER,
                       gl.DEPTH_COMPONENT16,
                       rttWidth, rttHeight);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER,
                           gl.DEPTH_ATTACHMENT,
                           gl.RENDERBUFFER, renderbuffer);

Rendering into the Framebuffer▲

In paintGL function, we first need to draw the scene into the framebuffer. We start by binding the framebuffer object and setting a viewport:

 
Sélectionnez
// bind the FBO and setup viewport
gl.bindFramebuffer(gl.FRAMEBUFFER, rttFramebuffer);
gl.viewport(0, 0, rttWidth, rttHeight);

Then, we need to bind the loaded texture we want to use in rendering into the framebuffer object:

 
Sélectionnez
// Bind the loaded texture
gl.bindTexture(gl.TEXTURE_2D, cubeTexture);

And then we can draw the textured cube into the framebuffer:

 
Sélectionnez
// Draw the cube to the FBO
gl.drawElements(gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0);

Using the Framebuffer as a Texture▲

First, we bind the render-to-texture right after drawing, and generate mipmaps:

 
Sélectionnez
// Bind the render-to-texture and generate mipmaps
gl.bindTexture(gl.TEXTURE_2D, rttTexture);
gl.generateMipmap(gl.TEXTURE_2D);

Then we need to bind the default framebuffer (screen), and set up the viewport:

 
Sélectionnez
// Bind default framebuffer and setup viewport accordingly
gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.viewport(0, 0,
            canvas.width * canvas.devicePixelRatio,
            canvas.height * canvas.devicePixelRatio);

And finally, we draw the on-screen view:

 
Sélectionnez
// Draw the on-screen cube
gl.drawElements(gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0);

List of Files▲

Files:

List of Files▲

  • framebuffer/framebuffer.pro

  • framebuffer/framebuffer.qrc

  • framebuffer/main.cpp

  • framebuffer/qml/framebuffer/framebuffer.js

  • framebuffer/qml/framebuffer/main.qml

List of Files▲

Images:

List of Files▲
  • framebuffer/qml/framebuffer/qtlogo.png

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.