坐標軸 平移 旋轉 縮放 重置坐標軸 矩陣操作 示例 1、坐標軸 OpenGL 使用的右手坐標系,從正面看原點,逆時針旋轉被認為是正旋轉。 x軸:從左到右 y軸:從底部向上 z軸:從屏幕背向朝向前方 2、平移 public abstract void glTranslatef(float x ,fl ...
- 坐標軸
- 平移
- 旋轉
- 縮放
- 重置坐標軸
- 矩陣操作
- 示例
public abstract void glTranslatef(float x ,float y, float z)平移操作相當於對坐標值進行加減法 二維中起點{-2,1} 要到{1,3} 我們需要添加{3,2} 三維中 {1,1,0} 要移動到{1,1,-3},我們需要添加{0,0,-3} 移動到屏幕中
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.glScalef(2f,2f,2f) 進行縮放。意味著所有頂點乘與2 縮放與平移: 縮放和平移的順序很重要 平移2個單位,縮放值0.5
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); } }