openGL es2.0 创建颜色球

创建带有颜色的球体:

一、Java代码:

package com.gzdxid.utils;

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

import android.opengl.GLES20;


public class DrawBallColor {

	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 float angleSpan=10f;
	float R=0;
	
	public DrawBallColor(float r,int mProgram) {
		// TODO Auto-generated constructor stub
		initVertex(r);
		initShader(mProgram);
	}

	private void initVertex(float r) {
		// TODO Auto-generated method stub
		R=r;
		ArrayList<Float>alVertix=new ArrayList<Float>();
		for(float vAngle=90;vAngle>-90;vAngle-=angleSpan){
			for(float hAngle=360;hAngle>0;hAngle-=angleSpan){
				float x1=getCoor(0, vAngle, hAngle);
				float y1=getCoor(1, vAngle, hAngle);
				float z1=getCoor(2, vAngle, hAngle);
				
				float x2=getCoor(0, vAngle-angleSpan, hAngle);
				float y2=getCoor(1, vAngle-angleSpan, hAngle);
				float z2=getCoor(2, vAngle-angleSpan, hAngle);
				
				float x3=getCoor(0, vAngle-angleSpan, hAngle-angleSpan);
				float y3=getCoor(1, vAngle-angleSpan, hAngle-angleSpan);
				float z3=getCoor(2, vAngle-angleSpan, hAngle-angleSpan);
				
				float x4=getCoor(0, vAngle, hAngle-angleSpan);
				float y4=getCoor(1, vAngle, hAngle-angleSpan);
				float z4=getCoor(2, vAngle, hAngle-angleSpan);
				
				alVertix.add(x1);alVertix.add(y1);alVertix.add(z1);
        		alVertix.add(x2);alVertix.add(y2);alVertix.add(z2);
        		alVertix.add(x4);alVertix.add(y4);alVertix.add(z4);        		
        		//构建第二三角形
        		alVertix.add(x4);alVertix.add(y4);alVertix.add(z4);
        		alVertix.add(x2);alVertix.add(y2);alVertix.add(z2);
        		alVertix.add(x3);alVertix.add(y3);alVertix.add(z3); 
			}
		}
		 vCount=alVertix.size()/3;
	        float vertices[]=new float[vCount*3];
	    	for(int i=0;i<alVertix.size();i++){
	    		vertices[i]=alVertix.get(i);
	    	}
	        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);
	}
	
	public float getCoor(int which,float vAngle,float hAngle){
		switch (which) {
		case 0://x
			return (float) (R*UNIT_SIZE*Math.cos(Math.toRadians(vAngle))*Math.cos(Math.toRadians(hAngle)));
		case 1://y
			return (float) (R*UNIT_SIZE*Math.sin(Math.toRadians(vAngle)));
		case 2://z
			return (float) (R*UNIT_SIZE*Math.cos(Math.toRadians(vAngle))*Math.sin(Math.toRadians(hAngle)));
		}
		return 0;
	}
	
}
二、定点着色器:
<pre name="code" class="plain">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;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值