OpenGL ES 1.0 Android简单顶点绘图

时间:2012-10-07 12:56:34

标签: android opengl-es

我正在尝试绘制一些三角形但由于某种原因我只得到了背景。 我错过了什么?

这是我的代码。它有2个缓冲区,这是第一次调用onDrawFrame时创建的,它从我通过加载.OBJ文件创建的ArrayList或ArrayList加载顶点和三角形数据。

    public FloatBuffer fb = null; // To be loaded once in the first draw
    public ShortBuffer ib = null;

    @Override
    public void onDrawFrame(GL10 gl)
        {
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_LINE_SMOOTH);
        gl.glEnable(GL10.GL_POINT_SMOOTH);


        // Clear white
        gl.glClearColor(1,0,0,1);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);            

        // Look At
        gl.glPushMatrix();
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        GLU.gluLookAt(gl, 0, 0, -4, 0, 0, 0, 0,1.0f,0);
        try
            {
            gl.glColor4f(1.0f,1.0f,0,1.0f);
            int nv = SomeVertexArray.size();
            int nt = SomeTriangleArray.size();
        if (fb == null)
            {
            int TotalFBByteSize = 4*3*nv;
            fb = ByteBuffer.allocateDirect(TotalFBByteSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
            fb.clear();
            for(int i = 0 ; i < SomeVertexArray.size() ; i++)
                {
                VERTEX v = SomeVertexArray.get(i);
                fb.put(v.x);
                fb.put(v.y);
                fb.put(v.z);
                }
            }
        if (ib == null)
            {
            int TotalTRByteSize = 4*3*nt;
            ib = ByteBuffer.allocateDirect(TotalTRByteSize).order(ByteOrder.nativeOrder()).asShortBuffer();
            ib.clear();
            for(int i = 0 ; i < nt ; i++)
                {
                TRIANGLE v = SomeTriangleArray.get(i);
                ib.put((short)v.i1);
                ib.put((short)v.i2);
                ib.put((short)v.i3);
                }
            }


        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0,fb);
        gl.glDrawElements(GL10.GL_TRIANGLES, nt*3, GL10.GL_UNSIGNED_SHORT,ib);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
            }
        catch(Throwable ex)
            {
            ex.printStackTrace();
            }
        gl.glPopMatrix();
        gl.glFlush();

        }


    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height)
        {
        gl.glViewport(0, 0, width, height);
                gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity(); //reset projection matrix
                    GLU.gluPerspective(gl,54.0f, (float)width/(float)height, 1.0f, 1000.0f);
        gl.glMatrixMode(GL10.GL_MODELVIEW); //set modelview matrix
        gl.glLoadIdentity(); //reset modelview matrix
        }

1 个答案:

答案 0 :(得分:1)

通过使用fb.put(float [])而不是fb.put(float)来解决问题,即将整个顶点/三角形数组一次传递到直接缓冲区,而不是在循环内。

我不确定原因,但确实有效。