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

 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 Left
			1.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 rotation
		gl.glFrontFace(GL10.GL_CW);

		// Point to our vertex buffer
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

		// Enable vertex buffer
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

		// Draw the vertices as triangle strip
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

		// Disable the client state before leaving
		gl.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 Shading
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
		gl.glClearDepthf(1.0f); // Depth Buffer Setup
		gl.glEnable(GL10.GL_DEPTH_TEST); // Enables Depth Testing
		gl.glDepthFunc(GL10.GL_LEQUAL); // The Type Of Depth Testing To Do

		// Really Nice Perspective Calculations
		gl.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 Buffer
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity(); // Reset The Current Modelview Matrix

		gl.glTranslatef(0.0f, -1.2f, -6.0f); // Move down 1.2 Unit And Into The
												// Screen 6.0
		polygon.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 By
			height = 1; // Making Height Equal One
		}

		gl.glViewport(0, 0, width, height); // Reset The Current Viewport
		gl.glMatrixMode(GL10.GL_PROJECTION); // Select The Projection Matrix
		gl.glLoadIdentity(); // Reset The Projection Matrix

		// Calculate The Aspect Ratio Of The Window
		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
				100.0f);

		gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix
		gl.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
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Create an Instance with this Activity
		glSurface = new GLSurfaceView(this);
		// Set our own Renderer
		glSurface.setRenderer(new Myrenderer());
		// Set the GLSurface as View to this Activity
		setContentView(glSurface);
	}

	/**
	 * Remember to resume the glSurface
	 */
	@Override
	protected void onResume() {
		super.onResume();
		glSurface.onResume();
	}

	/**
	 * Also pause the glSurface
	 */
	@Override
	protected void onPause() {
		super.onPause();
		glSurface.onPause();
	}

}


 

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值