0

我在 SO 上看到了一些这样的问题,但到目前为止没有任何帮助。我制作了 GLTriangle 和 GLRectangle 类。我可以用颜色画出这两个形状没问题。我正在尝试在其中实现纹理,但无法使其正常工作(和调试)。

我将尝试发布我目前所拥有的相关代码,如果您需要查看更多内容,请告诉我。

编辑:好的,所以在用 texcoord 处理程序修复错误之后,我仍然得到一个黑色方块。glGetError 唯一一次报告任何内容是在调用 GLES20.glEnable(GLES20.GL_TEXTURE_2D); 之后。

GLRectangle相关代码:

@Override
public void onSurfaceCreated() {
    ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
    vertexByteBuffer.order(ByteOrder.nativeOrder());
    vertexBuffer = vertexByteBuffer.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    mProgram = createAndLinkProgram(VertexShaderHandle(), FragmentShaderHandle(), new String[] { "uMVPMatrix", "vPosition", "a_TexCoordinate" });
    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    maPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
    loadTexture(context, imageId);
    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
indexBuffer = ibb.asShortBuffer();
indexBuffer.put(indices);
indexBuffer.position(0);

ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
tbb.order(ByteOrder.nativeOrder());
textureCoordBuffer = tbb.asFloatBuffer();
textureCoordBuffer.put(texCoords).position(0);
}

在 onDrawFrame 中:

GLES20.glUseProgram(mProgram);
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 12, vertexBuffer);
GLES20.glEnableVertexAttribArray(maPositionHandle);
Matrix.multiplyMM(mMVPMatrix, 0, projMatrix, 0, mvMatrix, 0);
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
mTextureUniformHandle = GLES20.glGetUniformLocation(mProgram, "u_Texture");
        mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgram, "a_TexCoordinate");

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle);
GLES20.glUniform1i(mTextureUniformHandle, 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length, GLES20.GL_UNSIGNED_SHORT, indexBuffer);

我知道,或者至少我相当确定正在加载位图,因为我在加载它时返回了 int 句柄(如果 int 为 0,它将导致运行时错误)。

我猜它与纹理坐标有关。

这是我的 tex 坐标:

texCoords = new float[] { 
    0, 0,
    0, 1,
    1, 1,
    1, 0
};  

以防万一它与纹理加载器有关,这里是:

int[] textureHandle  = new int[1];

            GLES20.glGenTextures(1, textureHandle, 0);

            if (textureHandle[0] != 0) {
                final BitmapFactory.Options options = new BitmapFactory.Options();
                options.inScaled = false;

                final Bitmap bitmap = BitmapFactory.decodeResource(view.getContext().getResources(), resourceId, options);



                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);                   

                GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
                GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);


                GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);


                bitmap.recycle();

            }

            if (textureHandle[0] == 0) {
                throw new RuntimeException("Unable to load texture!");
            }
            mTextureDataHandle = textureHandle[0];

编辑:忘记添加我的着色器,我现在只是通过代码设置字符串:

setVertexShader( 
                "uniform mat4 uMVPMatrix;   \n" +
                "uniform mat4 MVMatrix;     \n" +
                "attribute vec4 vPosition;  \n" +
                "attribute vec2 a_TexCoordinate;    \n" +                   
                "varying vec2 v_TexCoordinate;  \n" +

                "void main() {      \n" +

                "v_TexCoordinate = a_TexCoordinate; \n" +
                "gl_Position = uMVPMatrix * vPosition;  \n" +
                "}  \n");                   

        setFragmentShader("precision mediump float;  \n" +
                "uniform sampler2D u_Texture;   \n" +                   
                "varying vec2 v_TexCoordinate;  \n" +
                "void main() {              \n" +
                "gl_FragColor = texture2D(u_Texture, v_TexCoordinate); \n" +
                "}                         \n");
4

3 回答 3

3
mTextureCoordinateHandle = GLES20.glGetUniformLocation(mProgram, "u_Texture");
mTextureUniformHandle = GLES20.glGetAttribLocation(mProgram, "a_TexCoordinate");

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle);

这都是落后的。您正在获取 texcoord 位置并将其设置为制服,并获取制服位置并将其设置为 texcoord。另外,您甚至没有对 texcoord 属性做任何事情。

于 2012-07-15T05:30:11.887 回答
1

您可能不会首先获得图像数据。检查您正在使用 png/jpeg 文件填充纹理的代码。

于 2012-09-04T18:24:26.593 回答
0

用于激活透明度添加

onSurfaceCreated(GL10 unused, EGLConfig config)
    {
...        // Set alpha blend on
        GLES20.glEnable(GLES20.GL_BLEND);
        GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);
...
于 2019-09-27T17:30:10.740 回答