OpenGL ES 2.0动态纹理无法正常工作

时间:2014-07-20 16:41:12

标签: android opengl android-emulator shader render-to-texture

我对Android,Eclipse和OpenGL ES 2.0(以及一般的面向对象编程)都很陌生,但我得到了一个程序(基本上是基于我在互联网上找到的一些例子)来展示一个多维数据集几天前,每张脸都显示不同的512 x 512位图图像(在初始化阶段加载并转换一次PNG图像)。

我的系统是一台32位Vista PC,我一直在使用Bluestacks作为我的模拟器(版本0.8.11,发布于2014年6月22日,Android版本4.0.4)。我相信它的API级别是15.我目前没有物理Android设备来测试我的程序。

我现在想尝试一种使用动态纹理的技术。

我使用动态纹理(在Java中)查找完整的Android应用程序示例,但我发现只有一个,当我运行它时,它在我的Bluestacks上不起作用。

这是包含指向程序的完整项目链接的页面:

http://blog.shayanjaved.com/2011/05/13/android-opengl-es-2-0-render-to-texture/

当我在Bluestacks上运行时,我看到的只是一个黑屏。

我注意到的一件事是纹理尺寸很大。我将大小更改为512 x 512(我知道这可行,因为我的立方体程序使用512 x 512纹理),但我仍然只看到黑屏。

(如果有人可以尝试在他/她的物理Android设备和/或Bluestacks上运行此程序并发布结果,那将是很好的。)

以下是创建动态纹理并为其使用而准备的方法:

    /**
 * Sets up the framebuffer and renderbuffer to render to texture
 */
private void setupRenderToTexture() {
    fb = new int[1];
    depthRb = new int[1];
    renderTex = new int[1];

    // generate
    GLES20.glGenFramebuffers(1, fb, 0);
    GLES20.glGenRenderbuffers(1, depthRb, 0);
    GLES20.glGenTextures(1, renderTex, 0);

    // generate color texture
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, renderTex[0]);

    // parameters
    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);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,
            GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
            GLES20.GL_LINEAR);

    // create it 
    // create an empty intbuffer first?
    int[] buf = new int[texW * texH];
    texBuffer = ByteBuffer.allocateDirect(buf.length
            * FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asIntBuffer();;
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGB, texW, texH, 0, GLES20.GL_RGB, GLES20.GL_UNSIGNED_SHORT_5_6_5, texBuffer);

    // create render buffer and bind 16-bit depth buffer
    GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, depthRb[0]);
    GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, texW, texH);

    /*** PART BELOW SHOULD BE DONE IN onDrawFrame ***/

    // bind framebuffer
    /*GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fb[0]);

    // specify texture as color attachment
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, renderTex[0], 0);

    // attach render buffer as depth buffer
    GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, depthRb[0]);

    // check status
    int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);*/

}

上面的代码看起来是否合理?

哦,还有一件事:

我一直在使用以下作为我的顶点和片段着色器。他们一直很好地展示了一个带有静态纹理的立方体。 (正常的数据被读取但我现在没有使用任何灯光。)

     private final String vertexShaderCode =
        // This matrix member variable provides a hook to manipulate
        // the coordinates of the objects that use this vertex shader
        "uniform mat4 uMVPMatrix;" +
        "attribute vec4 a_Position;" +
        "attribute vec4 a_Color;" +
        "attribute vec3 a_Normal;" +
        "attribute vec2 a_TexCoordinate;" + // Per-vertex texture coordinate information we will pass in.
        "varying vec4 v_Color;" +
        "varying vec2 v_TexCoordinate;" +   // This will be passed into the fragment shader.
        "void main() {" +
        // The matrix must be included as a modifier of gl_Position.
        // Note that the uMVPMatrix factor *must be first* in order
        // for the matrix multiplication product to be correct.
        "  gl_Position = uMVPMatrix * a_Position;" +
        "  v_Color = a_Color;" +
        "  vec3 difuse = a_Normal;" +
        "  v_TexCoordinate = a_TexCoordinate;" +  // pass through
        "}";

private final String fragmentShaderCode =
        "precision mediump float;" +
        "uniform sampler2D u_Texture;" +    // The input texture.
        "varying vec4 v_Color;" +
        "varying vec2 v_TexCoordinate;" +   // Interpolated texture coordinate per fragment.
        "void main() {" +
        "  gl_FragColor = (v_Color * texture2D(u_Texture, v_TexCoordinate));" +
        "}";

当我渲染到纹理时它们应该工作吗?或者,当我渲染纹理时,是否必须有单独的顶点和片段着色器?

谢谢。

0 个答案:

没有答案