android OpenGLES开发第一课 绘制简单的Polygon

来源:互联网 发布:unity 象棋源码 编辑:程序博客网 时间:2024/06/05 09:09
 1)首先你要定义自己的Polygon类,当然你一可以直接在Renderer的子类中直接绘制,为了更加符合面向对象,还是自定义一个Polygon类更好,这样代码更加清晰,Polygon类中主要有Polygon的顶点坐标,和绘制Polygon对象的draw方法,例如下面的代码:

public class Polygon {/** * The buffer holding the Polygon‘s vertices *  * 保存Polygon对象顶点坐标的FloatBuffer */private FloatBuffer vertexBuffer;/** * The initial vertex definition *  * 保存Polygon对象顶点坐标的的float数组 */private float vertices[] = { 0.0f, 1.0f, 0.0f, // Top-1.0f, -1.0f, 0.0f, // Bottom Left1.0f, -1.0f, 0.0f // Bottom Right};/** * The Triangle constructor. *  * Initiate the buffers. */public Polygon() {// this is the common method to initiate the FloatBuffer// 下面是一种常用的初始化FloatBuffer的方法,本人还见到过一种方法,如下:// vertexBuffer = FloatBuffer.wrap(vertices)// 但是如果用这种方法在运行的时候会报错,指出你的FloatBuffer没有序列化,// 不明白原因,如有明白的高手帮解释一下,不胜感激ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);byteBuf.order(ByteOrder.nativeOrder());vertexBuffer = byteBuf.asFloatBuffer();vertexBuffer.put(vertices);vertexBuffer.position(0);}/** * The object own drawing function. Called from the renderer to redraw this * instance with possible changes in values. *  * @param gl *            - The GL context */public void draw(GL10 gl) {// 这就是Polygon被绘制的方法,下面都是一些在draw方法中经常用到的简单的设置,// 在此不一一解释了// Set the face rotationgl.glFrontFace(GL10.GL_CW);// Point to our vertex buffergl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);// Enable vertex buffergl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// Draw the vertices as triangle stripgl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);// Disable the client state before leavinggl.glDisableClientState(GL10.GL_VERTEX_ARRAY);}}


 

2)下面就是Renderer子类,它就是一个OpenGL渲染器,就是真正绘制3D图形的地方,主要是重写三个方法(三个方法将在下面一一标出),设置一些属性,来完成我们想要达到的效果,代码如下:

public class Myrenderer implements Renderer {Polygon polygon;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) {// 这个方法就是真正绘制3D图形的方法,系统会根据机器的性能在固定的时间间隔自动调用这个方法// 这里通过设置gl的属性,和我们定义的Polygon类的顶点坐标来绘制我们想要达到的效果// Clear Screen And Depth Buffergl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);gl.glLoadIdentity(); // Reset The Current Modelview Matrixgl.glTranslatef(0.0f, -1.2f, -6.0f); // Move down 1.2 Unit And Into The// Screen 6.0polygon.draw(gl); // Draw the triangle}/** * 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}}


 

3)最后就是把我们的GLSurfaceView通过activity的setContentView()方法加载到屏幕上,代码如下:

public class TestActivity extends Activity {/** The OpenGL View */private GLSurfaceView glSurface;/** * Initiate the OpenGL View and set our own Renderer */@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// Create an Instance with this ActivityglSurface = new GLSurfaceView(this);// Set our own RendererglSurface.setRenderer(new Myrenderer());// Set the GLSurface as View to this ActivitysetContentView(glSurface);}/** * Remember to resume the glSurface */@Overrideprotected void onResume() {super.onResume();glSurface.onResume();}/** * Also pause the glSurface */@Overrideprotected void onPause() {super.onPause();glSurface.onPause();}}


 

希望给刚刚入门的同学一些帮助,也希望和高手们交流一下经验。

原创粉丝点击