OPENGL axis transformation

  • Axis
  • Translation
  • Rotation
  • Scaling
  • Reset axis
  • Matrix operations
  • Examples
1, the axes
 OpenGL used right-handed, from the front origin, counter-clockwise rotation is considered to be positive.
x-axis: from left to right
y-axis: from the bottom upwards
z-axis: from the screen back toward the front
 
2, translation
public abstract void glTranslatef(float x ,float y, float z)
Panning operation corresponding to the coordinate value of the subtraction
{2,1} in two dimensions starting point to go to {1,3} {3,2} we need to add
{1,1,0} three dimensions to move to {1,1, -3}, we need to add {0,0} -3 moved into the screen
gl.glTranslatef (0,0, -3);

 

3, rotation

public abstract void glRotatef(float angle,float x,float y,float z)
Operation of coordinate axis
x, y, z defined rotation vector, the rotation angle value is the degree,
 
Order to perform translation and rotation is important
A translation - rotation, first pan and rotate it on the grid, the pan over the grid coordinates of the current state, the rotational position of the new
First rotation - after translation, the first rotation, after moving to their rotating coordinate system
 
 
4, Zoom
public abstract void glScalef(float x,float y,float z)
Zoom corresponds to a coordinate value and the scale value is multiplied by all the points, the operation of the coordinate axis. gl.glScalef (2f, 2f, 2f) scale. It means that all vertices multiply and 2
Zoom and pan:
Zooming and panning of the order is important
2 units translation, scaling value 0.5
gl.glTranslatef(2,0,0);
gl.glScalef(0.5f,0.5f,0.5f);

First zoom, pan after

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

 

 

5, the reset axis
glLoadIdentity
public abstract void glLoadIdentity();
 
6, matrix operations
glPushMatrix
public abstract void glPushMatrix();

Copy the currently stored matrix operation on the stack.

glPopMatrix

public abstract void glPopMatrix();
Get a hold on the matrix from the stack,
 
实践案例:
   绘制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);
}

}

 

 

 

Guess you like

Origin www.cnblogs.com/changeMsBlog/p/11404306.html