Android OpenGL ES 简明开发教程四：3D 坐标变换

xiaoxiao2021-03-01  5

Coordinate System坐标系

OpenGL使用了右手坐标系统，右手坐标系判断方法：在空间直角坐标系中，让右手拇指指向x轴的正方向，食指指向y轴的正方向，如果中指能指向z轴的正方向，则称这个坐标系为右手直角坐标系。

Translate平移变换

Rotate旋转

gl.glRotatef(90f, 1.0f, 0.0f, 0.0f); gl.glRotatef(90f, 0.0f, 1.0f, 0.0f); gl.glRotatef(90f, 0.0f, 0.0f, 1.0f);

gl.glRotatef(90f, -1.0f, 0.0f, 0.0f); gl.glRotatef(90f, 0.0f, -1.0f, 0.0f); gl.glRotatef(90f, 0.0f, 0.0f, -1.0f);

gl.glRotatef(90f, 0.0f, 0.0f, -1.0f); gl.glRotatef(90f, 0.0f, -1.0f, 0.0f); gl.glRotatef(90f, -1.0f, 0.0f, 0.0f);

Translate & Rotate （平移和旋转组合变换）

Scale（缩放）

Translate & Scale（平移和缩放组合变换）

gl.glTranslatef(2, 0, 0); gl.glScalef(0.5f, 0.5f, 0.5f);

gl.glScalef(0.5f, 0.5f, 0.5f); gl.glTranslatef(2, 0, 0);

public abstract void glPushMatrix()

public abstract void glPopMatrix()。

public void onDrawFrame(GL10 gl) { // Clears the screen and depth buffer. gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // Replace the current matrix with the identity matrix gl.glLoadIdentity(); // Translates 10 units into the screen. gl.glTranslatef(0, 0, -10); // SQUARE A // Save the current matrix. gl.glPushMatrix(); // Rotate square A counter-clockwise. gl.glRotatef(angle, 0, 0, 1); // Draw square A. square.draw(gl); // Restore the last matrix. gl.glPopMatrix(); // SQUARE B // Save the current matrix gl.glPushMatrix(); // Rotate square B before moving it, //making it rotate around A. gl.glRotatef(-angle, 0, 0, 1); // Move square B. gl.glTranslatef(2, 0, 0); // Scale it to 50% of square A gl.glScalef(.5f, .5f, .5f); // Draw square B. square.draw(gl); // SQUARE C // Save the current matrix gl.glPushMatrix(); // Make the rotation around B gl.glRotatef(-angle, 0, 0, 1); gl.glTranslatef(2, 0, 0); // Scale it to 50% of square B gl.glScalef(.5f, .5f, .5f); // Rotate around it's own center. gl.glRotatef(angle*10, 0, 0, 1); // Draw square C. square.draw(gl); // Restore to the matrix as it was before C. gl.glPopMatrix(); // Restore to the matrix as it was before B. gl.glPopMatrix(); // Increse the angle. angle++; }