0

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

这是我的代码。它有 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
        }
4

1 回答 1

1

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

我不知道为什么,但它奏效了。

于 2012-10-08T13:03:10.683 回答