Drawing multiple cubes at random positions in opengl es android

笑着哭i 提交于 2019-12-08 05:15:29

You must use glPushMatrix and glPopMatrix to return to previus possition after translate,rotate...

@Override
public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub


    gl.glDisable(GL10.GL_DITHER);
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);   

     gl.glMatrixMode(GL10.GL_MODELVIEW);
     gl.glLoadIdentity();
     gl.glClientActiveTexture(DRAWING_CACHE_QUALITY_HIGH);

     //Draw cube 1
     gl.glPushMatrix();
     gl.glTranslatef(-0.5f, 0, -3.0f);
     gl.glRotatef(mAngleX, 0, 1, 0);
     gl.glRotatef(mAngleY, 1, 0, 0);
     mCube.draw(gl);
     gl.glPopMatrix();

     //Draw cube 2
     gl.glPushMatrix();
     gl.glTranslatef(0, 0, -3.0f); 
     gl.glRotatef(mAngleX, 0, 1, 0);
     gl.glRotatef(mAngleY, 1, 0, 0);
     mCube.draw(gl);
     gl.glPopMatrix();

     //Draw cube 3
     gl.glPushMatrix();
     gl.glTranslatef(0.5f, 0, -3.0f); 
     gl.glRotatef(mAngleX, 0, 1, 0);
     gl.glRotatef(mAngleY, 1, 0, 0);
     mCube.draw(gl);
     gl.glPopMatrix();

}

The code you have that actually draws the cube are these three lines:

gl.glTranslatef(0, 0, -3.0f);
gl.glRotatef(mAngleX, 0, 1, 0);
gl.glRotatef(mAngleY, 1, 0, 0);
mCube.draw(gl);

It moved the coordinate system, then rotates it and then draws the cube. What you want to do it move it somewhere else and draw the cube there. Like this:

gl.glTranslatef(1.0, 0, 0f);
mCube.draw(gl);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!