0

我制作了这段代码,用于在时钟和逆时针方向上旋转一个正方形,实际上这是在 jayway 教程中旋转的示例代码。代码不起作用。提前谢谢。:) 英语也很抱歉..:)

public void onDrawFrame(final GL10 gl) {

Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try{

                gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
                gl.glLoadIdentity();
                gl.glTranslatef(0.0f, 4.9f,-9.3f);
                //gl.glTranslatef(2.0f, 0.0f,0.0f);
                gl.glPushMatrix();
                gl.glRotatef(-angle, 0, 0, 1);  

                gl.glScalef(1.0f, 1.0f, 1.0f);
                square.draw(gl);
                gl.glPopMatrix();
                angle++;
                  Thread.sleep(5000);

            }
            catch(InterruptedException e){
            } 
           // To do
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
            gl.glLoadIdentity();
            gl.glTranslatef(0.0f, 4.9f,-9.3f);
            //gl.glTranslatef(2.0f, 0.0f,0.0f);
            gl.glPushMatrix();
            gl.glRotatef(angle, 0, 0, 1);   

            //gl.glScalef(1.0f, 1.0f, 1.0f);
            square.draw(gl);    
            gl.glPopMatrix();
            angle++;
}   
});t.start(); 
4

1 回答 1

0

OpenGL 函数必须从 GLThread 调用。这意味着您的 opengl 函数必须由 3 种方法调用:

> onDrawFrame
> onSurfaceChanged 
> onSurfaceCreated

关于你的例子

您可以直接更新角度并绘制正方形而无需创建新线程,因为 onDrawFrame 将被 GLThread 调用重复。

public void onDrawFrame(final GL10 gl) {
    glClear(GL10.GL_COLOR_BUFFER_BIT);

    mAngle++; // update angle

    // draw with clockwise rotation
    glPushMatrix();
    {
        glTranslatef(100, 100, 0);
        glRotatef(+mAngle, 0, 0, 1);
        drawSquare();
    }
    glPopMatrix();

    // draw with anti clockwise rotation
    glPushMatrix();
    {
        glTranslatef(100, 200, 0);
        glRotatef(-mAngle, 0, 0, 1);
        drawSquare();
    }
    glPopMatrix();
}

如果您仍想创建一个新线程进行更新,请参见以下示例:

float mAngle = 0;
boolean mExit = false;

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    Thread updateThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (!mExit) {
                mAngle++;
                SystemClock.sleep(20);
            }
        }
    });
    updateThread.start();
}


public void onDrawFrame(final GL10 gl) {
    glClear(GL10.GL_COLOR_BUFFER_BIT);

    // clockwise rotation
    glPushMatrix();
    {
        glTranslatef(100, 100, 0);
        glRotatef(mAngle, 0, 0, 1);
        drawSquare();
    }
    glPopMatrix();

    // anti clockwise rotation
    glPushMatrix();
    {
        glTranslatef(100, 200, 0);
        glRotatef(-mAngle, 0, 0, 1);
        drawSquare();
    }
    glPopMatrix();
}
于 2013-03-08T22:02:17.780 回答