openGL es2.0 创建无盖颜色圆柱

一、Java代码:

package com.gzdxid.utils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import android.opengl.GLES20;

public class DrawCylinderSideColor {

	int mProgram;
	int muMVPMatrixHandle;
	int maPositionHandle;
	
	int muColorRHandle;
	int muColorGHandle;
	int muColorBHandle;
	int muColorAHandle;
	
	FloatBuffer mVertexBuffer;
	int vCount = 0;
	
	final float UNIT_SIZE=0.5f;
	final int n=24;
	
	public DrawCylinderSideColor(float r,float length,int mProgram) {
		// TODO Auto-generated constructor stub
		initVertex(r,length);
		initShader(mProgram);
	}

	private void initVertex(float r, float h) {
		// TODO Auto-generated method stub
		vCount=6*n;
		r=r*UNIT_SIZE;
		h=h*UNIT_SIZE;
		int count=0;
		float angleSpan=360f/n;
		float[] vertices=new float[vCount*3];
		for(float angle=0;Math.ceil(angle)<360;angle+=angleSpan){
			double a0=Math.toRadians(angle);
			double a1=Math.toRadians(angle+angleSpan);
			vertices[count++]=(float) (-r*Math.sin(a0));
			vertices[count++]=0;
			vertices[count++]=(float) (-r*Math.cos(a0));
			
			vertices[count++]=(float) (-r*Math.sin(a1));
			vertices[count++]=h;
			vertices[count++]=(float) (-r*Math.cos(a1));
			
			vertices[count++]=(float) (-r*Math.sin(a0));
			vertices[count++]=h;
			vertices[count++]=(float) (-r*Math.cos(a0));
			
			vertices[count++]=(float) (-r*Math.sin(a0));
			vertices[count++]=0;
			vertices[count++]=(float) (-r*Math.cos(a0));
			
			vertices[count++]=(float) (-r*Math.sin(a1));
			vertices[count++]=0;
			vertices[count++]=(float) (-r*Math.cos(a1));
			
			vertices[count++]=(float) (-r*Math.sin(a1));
			vertices[count++]=h;
			vertices[count++]=(float) (-r*Math.cos(a1));
		}
		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(vertices);
        mVertexBuffer.position(0);
        
	}

	private void initShader(int mProgram) {
		// TODO Auto-generated method stub
		this.mProgram=mProgram;
		muMVPMatrixHandle=GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
		maPositionHandle=GLES20.glGetAttribLocation(mProgram, "aPosition");
		muColorRHandle=GLES20.glGetUniformLocation(mProgram, "uColorR");
		muColorGHandle=GLES20.glGetUniformLocation(mProgram, "uColorG");
		muColorBHandle=GLES20.glGetUniformLocation(mProgram, "uColorB");
		muColorAHandle=GLES20.glGetUniformLocation(mProgram, "uColorA");
	}
	
	public void drawSelf(float r,float g,float b,float a){
		GLES20.glUseProgram(mProgram);
		GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
		GLES20.glUniform1f(muColorRHandle, r);
		GLES20.glUniform1f(muColorGHandle, g);
		GLES20.glUniform1f(muColorBHandle, b);
		GLES20.glUniform1f(muColorAHandle, a);
		GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3*4, mVertexBuffer);
		GLES20.glEnableVertexAttribArray(maPositionHandle);
		GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
	}
}
二、定点着色器:

uniform mat4 uMVPMatrix;
attribute vec3 aPosition;

void main(){
    gl_Position=uMVPMatrix*vec4(aPosition,1); 
}
三、片源着色器:

precision mediump float;

uniform float uColorR;
uniform float uColorG;
uniform float uColorB;
uniform float uColorA;

void main(){
    vec4 finalColor;
    finalColor=vec4(uColorR,uColorG,uColorB,uColorA);
    gl_FragColor=finalColor;
}




你可能感兴趣的:(Android)