android OpenGLES开发第三课 绘制一个可以翻转的Polygon

来源:互联网 发布:unity 象棋源码 编辑:程序博客网 时间:2024/06/04 19:01

这一课和前面的唯一不同在于MyRenderer类中的onDrawFrame(GL10 gl)方法,在这个方法里我们添加了对Polygon翻转的操作: 

public class Myrenderer implements Renderer {Polygon polygon;// 定义一个控制翻转角度的变量private float rquad;public Myrenderer() {polygon = new Polygon();}/** * The Surface is created/init() *  * 这个方法是当surface创建时调用的方法,主要是设置一些属性 */public void onSurfaceCreated(GL10 gl, EGLConfig config) {gl.glShadeModel(GL10.GL_SMOOTH); // Enable Smooth Shadinggl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Backgroundgl.glClearDepthf(1.0f); // Depth Buffer Setupgl.glEnable(GL10.GL_DEPTH_TEST); // Enables Depth Testinggl.glDepthFunc(GL10.GL_LEQUAL); // The Type Of Depth Testing To Do// Really Nice Perspective Calculationsgl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);}/** * Here we do our drawing */public void onDrawFrame(GL10 gl) {// Clear Screen And Depth Buffergl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);gl.glLoadIdentity(); // Reset The Current Modelview Matrix// Drawinggl.glTranslatef(0.0f, -1.2f, -6.0f); // Move down 1.0 Unit And Into The// Screen 6.0gl.glRotatef(rquad, 1.0f, 0.0f, 0.0f); // Rotate ThePolygonOn The X axis ( NEW ) 沿x轴翻转polygon.draw(gl); // Draw the Polygonrquad -= 0.15f; // Decrease The Rotation Variable For ThePolygon( NEW ) 改变翻转角度}/** * If the surface changes, reset the view */public void onSurfaceChanged(GL10 gl, int width, int height) {// 这个方法是当surface改变时调用的方法,也是设置一些gl的属性,// 大体的设置没有太大变化,所以这基本上是一个通用的写法if (height == 0) { // Prevent A Divide By Zero Byheight = 1; // Making Height Equal One}gl.glViewport(0, 0, width, height); // Reset The Current Viewportgl.glMatrixMode(GL10.GL_PROJECTION); // Select The Projection Matrixgl.glLoadIdentity(); // Reset The Projection Matrix// Calculate The Aspect Ratio Of The WindowGLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,100.0f);gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrixgl.glLoadIdentity(); // Reset The Modelview Matrix}}