OPENGL 坐标轴转换

  • 坐标轴
  • 平移
  • 旋转
  • 缩放
  • 重置坐标轴
  • 矩阵操作
  • 示例
1、坐标轴
 OpenGL 使用的右手坐标系,从正面看原点,逆时针旋转被认为是正旋转。
x轴:从左到右
y轴:从底部向上
z轴:从屏幕背向朝向前方
 
2、平移
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);
}

}

 

 

 
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页