ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • OpenGL 끄적임 - FrameBuffer
    Graphics/OpenGL ES 2.0 2019. 7. 7. 19:20
    반응형

    Graphics Fundamantals

    • Modeling → Rendering 

    Modeling

    What to Draw

    modeling is the process of developing a mathematical representation an inanimate or living object.

    ex) where is the position of the triangle. 

    Rendering 

    How to Draw

    rendering is about taking the actual box and displaying it realistically or placing it into a realistic setting.

    ex) where is the light, where is the position of the camera, what is the material of the surface.

     

    OpenGL Pipeline

    OpenGL

    Rendering machine of the triangle.

    OpenGL Pipeline

    The graphics pipeline covers all of the steps that follow each other up on processing the input data to get to the final output image.

    https://open.gl/drawing

    vertices, vertex shader

    It all begins with the vertices, these are the points from which shapes like triangles will later be constructed.

    The vertex shader is a small program running on your graphics card that processes every one of these input vertices individually. This is where the perspective transformation takes place, which projects vertices with a 3D world position onto your 2D screen! It also passes important attributes like color and texture coordinates further down the pipeline.

     

    primitives

    After the input vertices have been transformed, the graphics card will form triangles, lines or points out of them. These shapes are called primitives because they form the basis of more complex shapes.

     

    rasterize

    After the final list of shapes is composed and converted to screen coordinates, the rasterizer turns the visible parts of the shapes into pixel-sized fragments. The vertex attributes coming from the vertex shader or geometry shader are interpolated and passed as input to the fragment shader for each fragment. As you can see in the image, the colors are smoothly interpolated over the fragments that make up the triangle, even though only 3 points were specified.

     

    fragment shader

    The fragment shader processes each individual fragment along with its interpolated attributes and should output the final color. This is usually done by sampling from a texture using the interpolated texture coordinate vertex attributes or simply outputting a color. In more advanced scenarios, there could also be calculations related to lighting and shadowing and special effects in this program. The shader also has the ability to discard certain fragments, which means that a shape will be see-through there.

     

    on the screen

    Finally, the end result is composed from all these shape fragments by blending them together and performing depth and stencil testing. All you need to know about these last two right now, is that they allow you to use additional rules to throw away certain fragments and let others pass. For example, if one triangle is obscured by another triangle, the fragment of the closer triangle should end up on the screen.

     

    EGL Context

    OpenGL ES defines an API for rendering graphics. It does not define a windowing system. To allow GLES to work on a variety of platforms, it is designed to be combined with a library that knows how to create and access windows through the operating system. The library used for Android is called EGL. If you want to draw textured polygons, you use GLES calls; if you want to put your rendering on the screen, you use EGL calls.

    https://source.android.com/devices/graphics/arch-egl-opengl

     

    FrameBuffer

    The final rendering destination of the OpenGL pipeline is called framebuffer. Framebuffer is a collection of 2D arrays or storages utilized by OpenGL; colour buffers, depth buffer, stencil buffer and accumulation buffer. By default, OpenGL uses the framebuffer as a rendering destination that is created and managed entirely by the window system. This default framebuffer is called window-system-provided framebuffer.

    By using framebuffer object (FBO), an OpenGL application can redirect the rendering output to the application-created framebuffer object (FBO) other than the traditional window-system-provided framebuffer. And, it is fully controlled by OpenGL.

    The below picture means to use default framebuffer provided by window-system, and to create the application-created framebuffer object.

     

     

    Render to Texture

    Render To Texture (RTT) technique is one of the most important things to know in modern computer graphics programming. It’s heavily used in rendering complex scenes because it lets you create a bunch of effects and it open doors for other techniques like shadow mapping, deferred lighting or post-processing

    The concept behind this method is super simple, instead of rendering directly into the back buffer (main buffer) you will render the objects from your scene into a intermediate buffer which creates a texture or multiple texture (in a complex scenario). This texture is used to generate some cool effects and finally render it to the back buffer (main buffer).

    How to

    In OpenGL we can implement this technique by using Framebuffer objects (FBOs). These FBOs are not actually buffers, they just hold attachements (actual buffers) to read or write them during rendering time. The Framebuffer is llke a manager for these attachements which in the end they are textures or Renderbuffers (not covered here).

    Example

    To create a class with a few methods that will generate a FBO and attach a texture to it.

    Generate Texture

    
    // Create a texture object and bind it.  This will be the color buffer.
    GLES20.glGenTextures(1, values, 0);
    int mOffscreenTexture = values[0];   // expected > 0
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mOffscreenTexture);
    
    // Create texture storage.
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0,
            GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    
    // Set parameters.  We're probably using non-power-of-two dimensions, so
    // some values may not be available for use.
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
            GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,
            GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
            GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
            GLES20.GL_CLAMP_TO_EDGE);
    

    Generate FBO

    
    //Generate FBO and two empty textures
    void GenerateFBO(unsigned int width, unsigned int height)
    {
      //Generate a framebuffer object(FBO)
      int[] ids = new int[1];
      GLES20.glGenFramebuffers(1, ids, 0);
      this.frameBufferId = ids[0];
      // and bind it to the pipeline
      GLES20.glBindFramebuffer(GL_FRAMEBUFFER, this.frameBufferId);
      GLES20.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mOffscreenTexture, 0);
    
      int status = GLES20.glCheckFramebufferStatus(36160);
      GLES20.glBindFramebuffer(GL_FRAMEBUFFER, 0);    //unbind framebuffer
    }
    

     

    Render to Texture

    
    //main drawing method
    void notifyDisplayFrame(){
        
        // draw into FBO
        GLES20.glBindFramebuffer(GL_FRAMEBUFFER, this.frameBufferId);
        {
            //draw rim lighting scene like you normally do
        }
        GLES20.glBindFramebuffer(GL_FRAMEBUFFER, 0);
        
        // daw to default FBO(backbuffer)
        {
            GLES20.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
           	GLES20.glUseProgram(rtt_program_shader);
        	GLES20.glActiveTexture(GL_TEXTURE0 + 1);
            //use texture from our FBO generated in PASS 1     
        	GLES20.glBindTexture(GL_TEXTURE_2D, fbo.getColorTexture());
        	GLES20.glUniform1i(glGetUniformLocation(rtt_program_shader, "texture_color"), 1);
        	
            GLES20.glBindVertexArray(rtt_vao);
        	GLES20.glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        }    
    }
    

     

    http://in2gpu.com/2014/09/24/render-to-texture-in-opengl/

     

     

    반응형

    'Graphics > OpenGL ES 2.0' 카테고리의 다른 글

    Blur 알고리즘  (0) 2021.07.06
    OpenGL 끄적임 - Image Crop  (0) 2019.05.17
    OpenGL 끄적임 - 4. Adding color and Shader  (0) 2018.06.17
    OpenGL 끄적임 - 5. Screen Aspect Ratio  (0) 2018.05.31
    OpenGL 끄적임 - 2 Vertices, Shaders  (0) 2018.04.08
Designed by Tistory.