Android OpenGL ES 简明开发教程六: 真正的3D图形

http://www.imobilebbs.com/wordpress/?p=1554

Mesh

首先定义一个基类 Mesh,所有空间形体最基本的构成元素为Mesh(三角形网格) ,其基本定义如下:

public class Mesh {
	// Our vertex buffer. 
	private FloatBuffer verticesBuffer = null;  
	// Our index buffer. 
	private ShortBuffer indicesBuffer = null;  
	// The number of indices. 
	private int numOfIndices = -1;  
	// Flat Color 
	private float[] rgba = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
	// Smooth Colors 
	private FloatBuffer colorBuffer = null;  
	// Translate params. 
	public float x = 0;  
	public float y = 0;  
	public float z = 0;  
	// Rotate params. 
	public float rx = 0;  
	public float ry = 0;  
	public float rz = 0;  
	public void draw(GL10 gl) { 
		// Counter-clockwise winding. 
		gl.glFrontFace(GL10.GL_CCW); 
		// Enable face culling. 
		gl.glEnable(GL10.GL_CULL_FACE); 
		// What faces to remove with the face culling. 
		gl.glCullFace(GL10.GL_BACK); 
		// Enabled the vertices buffer for writing and 
		//to be used during 
		// rendering. 
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
		// Specifies the location and data format of an array of vertex coordinates to use when rendering. 
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer); 
		// Set flat color 
		gl.glColor4f(rgba[0], rgba[1], rgba[2], rgba[3]); 
		// Smooth color 
		if (colorBuffer != null) { 
			// Enable the color array buffer to be used during rendering. 
			gl.glEnableClientState(GL10.GL_COLOR_ARRAY); 
			gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer); 
		}  
		gl.glTranslatef(x, y, z); 
		gl.glRotatef(rx, 1, 0, 0); 
		gl.glRotatef(ry, 0, 1, 0); 
		gl.glRotatef(rz, 0, 0, 1);  
		// Point out the where the color buffer is. 
		gl.glDrawElements(GL10.GL_TRIANGLES, numOfIndices, GL10.GL_UNSIGNED_SHORT, indicesBuffer);
		// Disable the vertices buffer. 
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
		// Disable face culling. 
		gl.glDisable(GL10.GL_CULL_FACE); 
	}
	
	protected void setVertices(float[] vertices) { 
		// a float is 4 bytes, therefore we multiply the number if vertices with 4. 
		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); 
		vbb.order(ByteOrder.nativeOrder()); 
		verticesBuffer = vbb.asFloatBuffer(); 
		verticesBuffer.put(vertices); 
		verticesBuffer.position(0); 
	}  
	
	protected void setIndices(short[] indices) { 
		// short is 2 bytes, therefore we multiply the number if vertices with 2.
		ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2); 
		ibb.order(ByteOrder.nativeOrder()); 
		indicesBuffer = ibb.asShortBuffer(); 
		indicesBuffer.put(indices); 
		indicesBuffer.position(0); 
		numOfIndices = indices.length; 
	}  
	
	protected void setColor(float red, float green, float blue, float alpha) { 
		// Setting the flat color. 
		rgba[0] = red; 
		rgba[1] = green; 
		rgba[2] = blue; 
		rgba[3] = alpha; 
	} 
	
	protected void setColors(float[] colors) {
		// float has 4 bytes. 
		ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4); 
		cbb.order(ByteOrder.nativeOrder()); 
		colorBuffer = cbb.asFloatBuffer(); 
		colorBuffer.put(colors); 
		colorBuffer.position(0); 
	}
	
}


Plane类的定义如下:

public class Plane extends Mesh { 
	public Plane() { 
		this(1, 1, 1, 1); 
	}  
	public Plane(float width, float height) { 
		this(width, height, 1, 1); 
	}
	//widthSegments为x轴上分为几段,heightSegments为y轴上分为几段
	public Plane(float width, float height, int widthSegments, int heightSegments) {
		//总共有(widthSegments + 1) * (heightSegments + 1)个点,一个点由3个左边组成。
		float[] vertices = new float[(widthSegments + 1) * (heightSegments + 1) * 3];
		//一个正方形由两个三角形组成,一个三角形有3个点,因此一个平面有6个点,要乘以6
		short[] indices = new short[(widthSegments + 1) * (heightSegments + 1)* 6];  
		float xOffset = width / -2; 
		float yOffset = height / -2;
		//xWidth为x轴上一段的长度
		float xWidth = width / (widthSegments);
		//yWidth为y轴上一段的长度
		float yHeight = height / (heightSegments); 
		int currentVertex = 0; 
		int currentIndex = 0; 
		short w = (short) (widthSegments + 1);
		for (int y = 0; y < heightSegments + 1; y++) {
			for (int x = 0; x < widthSegments + 1; x++) {
				vertices[currentVertex] = xOffset + x * xWidth;
				vertices[currentVertex + 1] = yOffset + y * yHeight; 
				vertices[currentVertex + 2] = 0; 
				currentVertex += 3;  
				int n = y * (widthSegments + 1) + x;  
				if (y < heightSegments && x < widthSegments) { 
					// Face one 
					indices[currentIndex] = (short) n; 
					indices[currentIndex + 1] = (short) (n + 1); 
					indices[currentIndex + 2] = (short) (n + w); 
					// Face two 
					indices[currentIndex + 3] = (short) (n + 1); 
					indices[currentIndex + 4] = (short) (n + 1 + w); 
					indices[currentIndex + 5] = (short) (n + 1 + w - 1);  
					currentIndex += 6; 
				}
			}  
		}
		setIndices(indices); 
		setVertices(vertices);
	}
}

Cube

下面来定义一个正方体(Cube),为简单起见,这个四面体只可以设置宽度,高度,和深度,没有和Plane一样提供Segments支持。

 

public class Cube extends Mesh { 
	public Cube(float width, float height, float depth) { 
		
		width  /= 2; 
		height /= 2; 
		depth  /= 2;  
		
		float vertices[] = { 
			-width, -height, -depth, // 0 
			 width, -height, -depth, // 1 
			 width,  height, -depth, // 2 
			-width,  height, -depth, // 3 
			-width, -height,  depth, // 4 
			 width, -height,  depth, // 5 
			 width,  height,  depth, // 6 
			-width,  height,  depth, // 7 
		};  
		short indices[] = { 
			0, 4, 5, 
			0, 5, 1, 
			1, 5, 6, 
			1, 6, 2, 
			2, 6, 7, 
			2, 7, 3, 
			3, 7, 4, 
			3, 4, 0, 
			4, 7, 6, 
			4, 6, 5, 
			3, 0, 1, 
			3, 1, 2, 
		};  
		
		setIndices(indices); setVertices(vertices); 
	}
}

Group

Group可以用来管理多个空间几何形体,如果把Mesh比作Android的View ,Group可以看作Android的ViewGroup,Android的View的设计也是采用的“Composite Pattern”。

Group的主要功能是把针对Group的操作(如draw)分发到Group中的每个成员对应的操作(如draw)。

Group定义如下:

 

public class Group extends Mesh { 
	private Vector<Mesh> children = new Vector<Mesh>();  
	@Override 
	public void draw(GL10 gl) { 
		int size = children.size(); 
		for( int i = 0; i < size; i++) 
			children.get(i).draw(gl); 
	}  
	
	/** 
	 *  @param location 
	 *  @param object 
	 *  @see java.util.Vector#add(int, java.lang.Object) 
	 **/ 
	public void add(int location, Mesh object) { 
		children.add(location, object); 
	}  
	
	/** 
	 * @param object
	 * @return
	 * @see java.util.Vector#add(java.lang.Object) 
	 **/ 
	public boolean add(Mesh object) { 
		return children.add(object); 
	}  
	
	/**
	 * @see java.util.Vector#clear() 
	 **/ 
	public void clear() { 
		children.clear(); 
	}  
	
	/**
	 * @param location
	 * @return
	 * @see java.util.Vector#get(int) 
	 **/ 
	public Mesh get(int location) { 
		return children.get(location); 
	}  
	
	/**
	 * @param location
	 * @return
	 * @see java.util.Vector#remove(int)
	 **/ 
	public Mesh remove(int location) { 
		return children.remove(location); 
	}  
	
	/**
	 * @param object
	 * @return
	 * @see java.util.Vector#remove(java.lang.Object) 
	 **/ 
	public boolean remove(Object object) { 
		return children.remove(object); 
	}  
	
	/**
	 * @return
	 * @see java.util.Vector#size() 
	 **/ 
	public int size() { 
		return children.size(); 
	} 
}


 Sphere球体

public class Sphere {
	public void draw(GL10 gl) {
		float    theta, pai;
		float    co, si;
		float    r1, r2;
		float    h1, h2;
		float    step = 2.0f;
		float[][] v = new float[32][3];
		ByteBuffer vbb;
		FloatBuffer vBuf;
	 
		vbb = ByteBuffer.allocateDirect(v.length * v[0].length * 4);
		vbb.order(ByteOrder.nativeOrder());
		vBuf = vbb.asFloatBuffer();
	 
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
		
		//关于下面顶点位置的计算,从Y轴上边往下看就是一个个圆叠加,每一个圆上面,y轴对应的值是不变的,
		//通过三角函数关系可以求出来x跟z的值
		for (pai = -90.0f; pai < 90.0f; pai += step) {	
			int    n = 0;
	 
			r1 = (float)Math.cos(pai * Math.PI / 180.0);
			r2 = (float)Math.cos((pai + step) * Math.PI / 180.0);
			h1 = (float)Math.sin(pai * Math.PI / 180.0);
			h2 = (float)Math.sin((pai + step) * Math.PI / 180.0);
	 
			for (theta = 0.0f; theta <= 360.0f; theta += step) {
				co = (float)Math.cos(theta * Math.PI / 180.0);
				si = -(float)Math.sin(theta * Math.PI / 180.0);
	 
				v[n][0] = (r2 * co);
				v[n][1] = (h2);
				v[n][2] = (r2 * si);
				v[n + 1][0] = (r1 * co);
				v[n + 1][1] = (h1);
				v[n + 1][2] = (r1 * si);
	 
				vBuf.put(v[n]);
				vBuf.put(v[n + 1]);
		 
				n += 2;
		 
				if(n>31){
					vBuf.position(0);
		 
					gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vBuf);
					gl.glNormalPointer(GL10.GL_FLOAT, 0, vBuf);
					gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, n);
		 
					n = 0;
					theta -= step;
				}
		 
			}
			vBuf.position(0);
		 
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vBuf);
			gl.glNormalPointer(GL10.GL_FLOAT, 0, vBuf);
			gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, n);
	 	}
	 
	 	gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	 	gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
	}
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值