# OPENGL 坐标轴转换

• 坐标轴
• 平移
• 旋转
• 缩放
• 重置坐标轴
• 矩阵操作
• 示例
1、坐标轴
OpenGL 使用的右手坐标系，从正面看原点，逆时针旋转被认为是正旋转。
x轴：从左到右
y轴：从底部向上
z轴：从屏幕背向朝向前方

2、平移
public abstract void glTranslatef(float x ,float y, float z)

gl.glTranslatef(0,0,-3);

3、旋转

public abstract void glRotatef(float angle,float x,float y,float z)

x,y,z 定义旋转的矢量，角度值是旋转的度数，

4、缩放
public abstract void glScalef(float x,float y,float z)

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

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

5、重置坐标轴
glLoadIdentity
public abstract void glLoadIdentity();

6、矩阵操作
glPushMatrix
public abstract void glPushMatrix();

glPopMatrix

public abstract void glPopMatrix();

绘制3个方格，A、B、C 。缩放B 50%，然后让A、C比B小50% ，然后让A逆时针旋转屏幕中心。B硬顺时针绕A旋转，最后C绕B顺时针旋转，逆时针绕其自身中心高速旋转。
public class GLES20Renderer3 implements GLSurfaceView.Renderer{
private Square square;
private float angle=0;
public GLES20Renderer3(){
square=new Square();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f,0.0f,0.0f,0.5f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_NICEST);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0,0,width,height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl,45.0f,(float)width/(float)height,0.1f,100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0,0,-10);
//A
gl.glPushMatrix();
gl.glRotatef(angle,0,0,1);
square.draw(gl);
gl.glPopMatrix();
//B
gl.glPushMatrix();
gl.glRotatef(-angle,0,0,1);
gl.glTranslatef(2,0,0);
gl.glScalef(.5f,.5f,.5f);
square.draw(gl);
//C
gl.glPushMatrix();
gl.glRotatef(-angle,0,0,1);
gl.glTranslatef(2,0,0);
gl.glScalef(.5f,.5f,.5f);
square.draw(gl);

gl.glPopMatrix();
gl.glPopMatrix();
angle++;

}

}
public class Square {
private float vertices[]={
-1.0f,1.0f,0.0f,
-1.0f,-1.0f,0.0f,
1.0f,-1.0f,0.0f,
1.0f,1.0f,0.0f,
};
private short[] indices={0,1,2,0,2,3};
private FloatBuffer vertexBuffer;
private ShortBuffer indexBuffer;
public Square(){
ByteBuffer vbb=ByteBuffer.allocateDirect(vertices.length*4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer=vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);

ByteBuffer ibb=ByteBuffer.allocateDirect(indices.length*2);
ibb.order(ByteOrder.nativeOrder());
indexBuffer=ibb.asShortBuffer();
indexBuffer.put(indices);
indexBuffer.position(0);

}
public void draw(GL10 gl){
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3,GL10.GL_FLOAT,0, vertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES,indices.length,GL10.GL_UNSIGNED_SHORT,indexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
}

}

10-23
05-30 1124

05-10 7455
06-24 1093
04-12
05-06 6422
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试