为什么这个OpenGL ES代码不会创建纹理?

时间:2011-12-26 10:10:12

标签: java android opengl-es render

我在Android平台上工作。我刚刚开始使用OpenGL,并试图将图像放在矩形上。这是我的代码:

public class IntroActivity extends Activity {


private GLSurfaceView glsv = null;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        glsv = new GLSurfaceView(this);
        glsv.setRenderer(new IntroRenderer());
        setContentView(glsv);



 }

 @Override
    protected void onResume() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onResume();
       glsv.onResume();
    }

    @Override
    protected void onPause() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onPause();
        glsv.onPause();
    }

这里的IntroRenderer是一个内部类。

 class IntroRenderer implements GLSurfaceView.Renderer{

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClearColor(0,1.0f,1.0f,1.0f);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        float positions[] = { 
                0.0f,1.0f,0.0f,
                0.0f, 0.0f, 0.0f,
                1.0f,1.0f,0.0f,

                0.0f,0.0f,0.0f,
                1.0f,1.0f, 0.0f,
                1.0f,0.0f,0.0f
        };

        ByteBuffer bb = ByteBuffer.allocateDirect(positions.length*4);
        bb.order(ByteOrder.nativeOrder());

        FloatBuffer fb = bb.asFloatBuffer();
        fb.put(positions);
        fb.position(0);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3,GL10.GL_FLOAT, 0, fb);
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3*2);

        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.rect2985);
        Bitmap bmp256 = Bitmap.createScaledBitmap(bmp, 256,256,false);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        int[] buffers = new int[1];
        gl.glGenTextures(1,buffers,0);

        int texture = buffers[0];

        gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D,0,bmp256,0);

        //gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST);
        //gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST);

        bmp.recycle();

    }

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


    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // TODO Auto-generated method stub

    }



 }

}

在运行此活动时,会渲染背景矩形,但不会显示图像。我做错了什么?

2 个答案:

答案 0 :(得分:2)

我稍微修改了你的渲染器:

class IntroRenderer implements GLSurfaceView.Renderer {

    private boolean textureReady = false;

    public void onDrawFrame(GL10 gl) {

        if (!this.textureReady) {
            this.prepareTexture(gl); // only initialize once
        }           

        gl.glClearColor(0, 0.4f, 0.4f, 1.0f);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        float positions[] = { 
                      0.0f, 1.0f, 0.0f, 
                      0.0f, 0.0f, 0.0f, 
                      1.0f, 1.0f, 0.0f,
          0.0f, 0.0f, 0.0f, 
                      1.0f, 1.0f, 0.0f, 
                      1.0f, 0.0f, 0.0f };

        float texCoords[] = {  // define texture coordinates
                      0.0f, 1.0f, 
                      0.0f, 0.0f, 
                      1.0f, 1.0f, 
                      0.0f, 0.0f, 
                      1.0f, 1.0f, 
                      1.0f, 0.0f };

        ByteBuffer bb = ByteBuffer.allocateDirect(positions.length * 4);
        bb.order(ByteOrder.nativeOrder());

        FloatBuffer fb = bb.asFloatBuffer();
        fb.put(positions);
        fb.position(0);

        // create buffer for texture coordinates
        ByteBuffer texCoordByteBuffer = ByteBuffer.allocateDirect(texCoords.length * 4);
        texCoordByteBuffer.order(ByteOrder.nativeOrder());

        FloatBuffer texCoordBuffer = texCoordByteBuffer.asFloatBuffer();
        texCoordBuffer.put(texCoords);
        texCoordBuffer.position(0);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, fb);

        // enable texture buffer
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoordBuffer);

        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3 * 2);


    }

    private void prepareTexture(GL10 gl) {
        int[] buffers = new int[1];
        gl.glGenTextures(1, buffers, 0);
        int texture = buffers[0];

        // enable texturing and bind texture
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);

        Bitmap bmp = BitmapFactory.decodeResource(IntroActivity.this.getResources(), R.drawable.rect2985);
        Bitmap bmp256 = Bitmap.createScaledBitmap(bmp, 256, 256, false);

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

        // setup filters
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);

        bmp.recycle();
        this.textureReady = true;
    }

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

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // TODO Auto-generated method stub
    }

}

我在修改过的部分添加了评论:

  • 您必须定义纹理坐标并创建缓冲区(就像您对顶点位置所做的那样)
  • 使用gl.glEnable(GL10.GL_TEXTURE_2D);
  • 启用纹理
  • 您需要min / mag过滤器

正如奥伦建议你应该只进行一次初始化。

纹理坐标定义纹理如何映射到三角形。您可以想到在x和y轴上从0.0到1.0的坐标系,其中角(0,0; ​​1,0; 1,1; 0,1)定义纹理的角。您必须为您定义的每个顶点指定纹理坐标。

也许这可以帮助您了解纹理坐标的工作原理:http://www.opengl.org/resources/code/samples/sig99/advanced99/notes/node52.html

答案 1 :(得分:0)

我不确定为什么不呈现,但你的代码效率低下。 您正在加载位图并将其绑定在每个帧上。 你应该只做一次。

此外,此链接可以帮助您使用Android的Nehe端口提供很好的示例: Nehe Port for Android

祝你好运:)