Android OPENGL没有将纹理绘制为四边形

时间:2011-12-30 21:40:04

标签: android opengl-es drawing textures opengl-es-2.0

我正在尝试在四边形上渲染一些纹理,但屏幕显示为空。 (Open GL 2.0) 我之前在四边形上使用了静态颜色着色器,四边形确实出现在屏幕上,所以定位很好。

更新:现在我只能看到黑色纹理......

这是我的代码:

顶点着色器:

uniform mat4 uTMatrix; 
uniform mat4 uMVPMatrix;
attribute vec4 vPosition;
attribute vec4 vTex;
attribute vec4 inputTextureCoordinate;
varying vec2 TexCoord0;
void main(){
    TexCoord0 = inputTextureCoordinate.xy;
    gl_Position = uMVPMatrix * uTMatrix * vPosition;
}

Frag Shader:

varying highp vec2 TexCoord0;
uniform sampler2D colorMap;
void main(){
    gl_FragColor = texture2D(colorMap, TexCoord0);
}

onDrawFrame():

            Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);

            GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);

            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

            GLES20.glUseProgram(mProgram);

        final float halfw = mWidth / 2f;
        final int rowHeight = mRowHeight;
        final float r = (float) rowHeight / halfw;

        int i = mFirstRow;

        final float initial = (float) ((i * rowHeight) - mScroll) / halfw;

        Matrix.setIdentityM(mTMatrix, 0);
        Matrix.translateM(mTMatrix, 0, 0, -initial, 0);

        GLES20.glFrontFace(GLES20.GL_CW);

        GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT,
                false, 0, mVertBuffer);
        GLES20.glEnableVertexAttribArray(maPositionHandle);

        GLES20.glVertexAttribPointer(maTexHandle, 3, GLES20.GL_FLOAT,
                false, 0, mTexBuffer);
        GLES20.glEnableVertexAttribArray(maTexHandle);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glEnable(GLES20.GL_TEXTURE_2D);

        final int l = mLastRow;
        for (; i <= l; i++) {
            if (\*A check to see if the bitmap is cached*\) {
                GLES20.glUniform1i(muTextureHandle, i);
                GLES20.glUniformMatrix4fv(muTMatrixHandle, 1, false,
                        mTMatrix, 0);
                GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
            }

            Matrix.translateM(mTMatrix, 0, 0, -r, 0);
        }

        GLES20.glDisableVertexAttribArray(maPositionHandle);
        GLES20.glDisableVertexAttribArray(maTexHandle);

loadTexture:

        public void loadTexture(GL10 gl, Context c) {
            int[] texture = new int[1];
            texture[0] = mRow;
            GLES20.glDeleteTextures(1, texture, 0);
            texture[0] = mRow;
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture[0]);

            // Create Nearest Filtered Texture
            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);

            // Different possible texture parameters, e.g.
            // GLES20.GL_CLAMP_TO_EDGE
            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
                    GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
                    GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);

            Bitmap bitmap = mBitmap;
            if (bitmap == null) {
                bitmap = mEmptyBitmap;
            }
            GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
        }

1 个答案:

答案 0 :(得分:0)

此外还有这一行,我不确定它应该做什么;

if (\*A check to see if the bitmap is cached*\) {
    GLES20.glUniform1i(muTextureHandle, i); // ??
    ...
}

我猜您的目标是不断更新GL_TEXTURE0,在这种情况下,您应该将常量值0放入muTextureHandle。或者将这一行全部删除,因为零是默认值。