opengl es学习-画四边形

public class OpenGlRender implements GLSurfaceView.Renderer{
	
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glClearColor(0f, 0f, 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);
	}
	
	private float[] vertices={
			-1.0f,1.0f,0f, //0
			-1.0f,-1.0f,0f, //1
			1.0f,-1.0f,0f,
			1.0f,1.0f,0f,
	};
	private short[] indices={
			0,1,2,0,2,3
	};
	FloatBuffer vertBuffer;
	ShortBuffer indiBuffer;
	float rx=30f;
	void draw(GL10 gl){
		ByteBuffer vbb=ByteBuffer.allocateDirect(vertices.length*4);
		vbb.order(ByteOrder.nativeOrder());
		vertBuffer=vbb.asFloatBuffer();
		vertBuffer.put(vertices);
		vertBuffer.position(0);
		
		ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length*2);
		ibb.order(ByteOrder.nativeOrder());
		indiBuffer=ibb.asShortBuffer();
		indiBuffer.put(indices);
		indiBuffer.position(0);
		
		gl.glFrontFace(GL10.GL_CCW);
		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glCullFace(GL10.GL_BACK);
		
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glColor4f(1.0f, 0f, 0f, 1f);
//		gl.glTranslatef(0f, 0f, -1f);
		
		gl.glLoadIdentity();
		gl.glTranslatef(0, 0, -5);
//		gl.glRotatef(rx,1f, 0f, 0f);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuffer);
		
		gl.glDrawArrays(GL10.GL_TRIANGLES, 0,3);
//		gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indiBuffer);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisable(GL10.GL_CULL_FACE);
//		rx++;
	}
	
	public void onDrawFrame(GL10 gl) {
		gl.glClearColor(0f, 0f, 0f, 0.5f);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		draw(gl);
	}
	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.f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

UV Mapping

下一步要告知OpenGL库如何将Bitmap的像素映射到Mesh上。这可以分为两步来完成:

定义UV坐标

UV Mapping指将Bitmap的像素映射到Mesh上的顶点。UV坐标定义为左上角(0,0),右下角(1,1)(因为使用的2D Texture),下图坐标显示了UV坐标,右边为我们需要染色的平面的顶点顺序:

opengl es学习-画四边形_第1张图片为了能正确的匹配,需要把UV坐标中的(0,1)映射到顶点0,(1,1)映射到顶点2等等。

1 float textureCoordinates[] = {0.0f, 1.0f,
2  1.0f, 1.0f,
3  0.0f, 0.0f,
4  1.0f, 0.0f };

opengl es学习-画四边形_第2张图片






   

你可能感兴趣的:(Class,buffer,float)