Android OpenGL ES没有绘制纹理

时间:2010-12-22 18:27:47

标签: android opengl-es

我现在有一个非常基本的活动。它创建一个GLSurfaceView并设置渲染器。问题是我看到的只是红色,来自glClearColor,没有纹理。甚至不是白色区域。 glGetError()也没有报告任何内容。

这是渲染器:

public class MyRenderer implements Renderer {

    public MyRenderer(Context context)
    {
        mContext = context;
    }

    public void onDrawFrame(GL10 gl) {
        gl.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);

        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertex);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoords);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);

        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrthof(-160.0f, 160.0f, -240.0f, 240.0f, 0.1f, 1.0f);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        float vertexBuffer[] = {
                -160.0f, -240.0f,
                -160.0f, 240.0f,
                160.0f, -240.0f,
                160.0f, 240.0f
        };

        vertex = ByteBuffer.allocateDirect(8 * 4).asFloatBuffer().put(vertexBuffer);

        float texCoordsBuffer[] = {
                0.0f, 0.0f,
                0.0f, 480.0f/512.0f,
                320.0f/512.0f, 0.0f,
                320.0f/512.0f, 480.0f/512.0f
        };

        texCoords = ByteBuffer.allocateDirect(8 * 4).asFloatBuffer().put(texCoordsBuffer);

            BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDensity = 240; // needed so that the image will be 512x512
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.image, options);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        Log.i(TAG, "Bitmap:{w:" + width + " h:" + height + "}");

        gl.glEnable(GL10.GL_TEXTURE_2D);

        texture = new int[1];

        gl.glGenTextures(1, texture, 0);

        gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);

        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

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

        bitmap.recycle();

        int error = gl.glGetError();
        if (error != GL10.GL_NO_ERROR)
        { 
            Log.e(TAG, "GL Texture Load Error: " + error);

        }


    }

    private Context mContext;
    private int texture[];
    private FloatBuffer vertex;
    private FloatBuffer texCoords;
}

2 个答案:

答案 0 :(得分:1)

您的代码存在一些问题:

您需要将缓冲区的字节顺序设置为native:

vertex.order(ByteOrder.nativeOrder())

将数据复制到缓冲区后,将位置重置为0:

vertex.position(0)

(同样适用于texCoord)。

将你的近剪裁平面放在-1.0而不是.1(在glOrthof中)也可能有帮助。

答案 1 :(得分:0)

  

//需要因为图像不是512x512

要让OpenGL渲染纹理,纹理大小需要为2的幂,即64x64,128x32或256x1024