Render to Texture OpenGL ES 2.0

I am trying to use a hardware optimized 2D library to zoom (non-interpolated scaling) in and out of an image. Right now I am

  • Loading the original image
  • Making a copy of the original image
  • Using the 2D library to "zoom" into the copy
  • Generating textures using glTexImage2D from the images
  • Applying them to rectangles that I drew
  • I can't upload images (yet) but here is a link to a screenshot. http://img.photobucket.com/albums/v336/prankstar008/zoom.png

    I would like to zoom in and out of the image on the right by a certain amount every frame, and rather than kill my performance by using glTexImage2D every time, I would like to render to a texture. My questions are

  • Is this a valid application of rendering to a texture? For clarification, the 2D library takes a pointer to a buffer filled with raw RGB(A) data, and returns a pointer to the new data with the 2D operation applied.
  • I think most of my confusion has to do with how textures interact with shaders. Can someone explain the simplest way to apply a texture to a surface in GLES2? I obviously have something working, and I can post snippets of code if necessary.
  • Also for clarification, although I'm not sure it matters, this is being run on Android.
  • Thank you.


    1) No, it is not. If you just want to zoom in and out of the image (without using your zoom library), then rendering to another texture would be a waste of time. You can zoom directly in your view.

    2) Shaders are programs, that are capable of calculating and transforming coordinates (that is usually done in vertex shaders) and are able to use those coordinates to read from texture (that can only be done in fragment shader at the time).

    I believe your shaders could look like this:

    precision mediump float;
    
    uniform matrix4 modelviewProjectionMatrix; // input transformation matrix
    
    attribute vec3 position;
    attribute vec2 texcoord; // input coordinates of each vertex
    
    varying vec2 _texcoord; // output coordinate for fragment shader
    
    void main()
    {
        gl_Position = modelviewProjectionMatrix * vec4(position, 1.0); // transform position
        _texcoord = texcoord; // copy texcoord
    }
    

    That was vertex shader, and now fragment shader:

    precision mediump float;
    
    varying vec2 _texcoord; // input coordinate from vertex shader
    
    uniform sampler2D _sampler; // handle of a texturing unit (e.g. 0 for GL_TEXTURE0)
    
    void main()
    {
        gl_FragColor = glTexture2D(_sampler, _texcoord); // read texture
    }
    

    What you can do, is to include a zoom parameter (so called uniform) in vertex shader:

    precision mediump float;
    
    uniform float zoom; // zoom
    uniform matrix4 modelviewProjectionMatrix; // input transformation matrix
    
    attribute vec3 position;
    attribute vec2 texcoord; // input coordinates of each vertex
    
    varying vec2 _texcoord; // output coordinate for fragment shader
    
    void main()
    {
        gl_Position = modelviewProjectionMatrix * vec4(position, 1.0); // transform position
        _texcoord = (texcoord - .5) * zoom + .5; // zoom texcoord
    }
    

    There are no changes in fragment shader. This just calculates different coordinates for it, and that's how simple it is. To set zoom, you need to:

    int zoomLocation = glGetUniformLocation(yourProgram, "zoom");
    // this is done in init, where you call glGetUniformLocation(yourProgram, "modelviewProjectionMatrix")
    // also, zoomLocation is not local variable, it needs to be stored somewhere to be used later when drawing
    
    glUniform1f(zoomLocation, 1 + .5f * (float)Math.sin(System.nanotime() * 1e-8f));
    // this will set zoom that will animatically zoom in and out of the texture (must be called after glUseProgram(yourProgram))
    

    Now this will cause both textures to zoom in and out. To fix this (I assume you only want the right texture to be zooming), you need to:

    // draw the second quad
    
    glUniform1f(zoomLocation, 1);
    // this will set no zoom (1:1 scale)
    
    // draw the first quad
    

    I hope this helps ...

    链接地址: http://www.djcxy.com/p/34052.html

    上一篇: OpenGL渲染

    下一篇: 渲染到纹理OpenGL ES 2.0