openGL es2.0 创建颜色球

时间:2022-11-08 03:58:16

创建带有颜色的球体:

一、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;
}