0

私はいくつかのコードを持っています。ここにそのほとんどがあります:

class Cube {
    public Cube()
    {
    ...

        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        mFVertexBuffer = vbb.asFloatBuffer();
        mFVertexBuffer.put(vertices);
        mFVertexBuffer.position(0);

        mColorBuffer = ByteBuffer.allocateDirect(colors.length);
        mColorBuffer.put(colors);
        mColorBuffer.position(0);

        mTfan1 = ByteBuffer.allocateDirect(tfan1.length);
        mTfan1.put(tfan1);
        mTfan1.position(0);

        mTfan2 = ByteBuffer.allocateDirect(tfan2.length);
        mTfan2.put(tfan2);
        mTfan2.position(0);
    }

    public void draw(GL10 gl)
    {
        gl.glVertexPointer(3,  GL11.GL_FLOAT, 0, mFVertexBuffer);
        gl.glColorPointer(4,  GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);

        gl.glDrawElements(GL10.GL_TRIANGLE_FAN, 6 * 3, GL10.GL_UNSIGNED_BYTE, mTfan1);
        gl.glDrawElements(GL10.GL_TRIANGLE_FAN, 6 * 3, GL10.GL_UNSIGNED_BYTE, mTfan2);
    }

    private FloatBuffer mFVertexBuffer;
    private ByteBuffer mColorBuffer;
    private ByteBuffer mTfan1;
    private ByteBuffer mTfan2;

}

そしてレンダラー:

public class CubeRenderer implements GLSurfaceView.Renderer 
{
    public CubeRenderer(boolean useTranslucentBackground)
    {
        mTranslucentBackground = useTranslucentBackground;
        mCube = new Cube();
    }

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

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -7.0f);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

        mCube.draw(gl);

        mTransY += 0.075f;
    }

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

        float aspectRatio;
        float zNear = 0.1f;
        float zFar = 1000;
        float fieldOfView = 30.0f/57.3f;
        float size;

        gl.glEnable(GL10.GL_NORMALIZE);

        aspectRatio = (float)width/(float)height;

        gl.glMatrixMode(GL10.GL_PROJECTION);

        size = zNear * (float)(Math.tan((double)(fieldOfView/2.0f)));

        gl.glFrustumf(-size, size, -size / aspectRatio, 
                                    size / aspectRatio, zNear, zFar);


        gl.glMatrixMode(GL10.GL_MODELVIEW);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) 
    {
        gl.glDisable(GL10.GL_DITHER);

        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

        if (mTranslucentBackground)
        {
            gl.glClearColor(0, 0, 0, 0);
        } else {
            gl.glClearColor(1, 1, 1, 1);
        }
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glEnable(GL10.GL_DEPTH_TEST);
    }

    private boolean mTranslucentBackground;
    private Cube mCube;
    private float mTransY;
}

デバイスを横に傾けて画面を更新しない限り、3D キューブ オブジェクトは表示されません。その後、正常に動作します。

そのため、画面を一度更新する簡単な方法と、最初の更新をどこに置くかを探しています。

4

1 に答える 1

0

使い忘れた

SurfaceChanged 関数の gl.glLoadIdentity()

// In OnSurfaceChanged put this:
aspectRatio = (float)width/(float)height;
gl.glMatrixMode(GL10.GL_PROJECTION);

gl.glLoadIdentity();
于 2013-04-26T21:41:31.693 に答える