标签:
public void initVertexData(){ int angleSpan = 90;//// 将球进行切分的角度 float r = 0.6f;//球的半径 final float UNIT_SIZE = 1.0f; for (int vAngle = 0; vAngle < 180; vAngle = vAngle + angleSpan) { for (int hAngle = 0; hAngle <= 360; hAngle = hAngle + angleSpan) { float x0 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle)) * Math.cos(Math .toRadians(hAngle))); float y0 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle)) * Math.sin(Math .toRadians(hAngle))); float z0 = (float) (r * UNIT_SIZE * Math.cos(Math .toRadians(vAngle))); } } }每次循环我们都得到一个四边形的左上角的顶点坐标。我们知道如何求该顶点坐标后,其他三个坐标就很容易求得了,
package com.cumt.shape; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.util.ArrayList; public class Ball { private static final float UNIT_SIZE = 1.0f;// 单位尺寸 private float r = 0.6f; // 球的半径 final int angleSpan = 10;// 将球进行单位切分的角度 private FloatBuffer vertexBuffer;// 顶点坐标 int vCount = 0;// 顶点个数,先初始化为0 // float类型的字节数 private static final int BYTES_PER_FLOAT = 4; // 数组中每个顶点的坐标数 private static final int COORDS_PER_VERTEX = 3; public void initVertexData() { ArrayList<Float> alVertix = new ArrayList<Float>();// 存放顶点坐标的ArrayList for (int vAngle = 0; vAngle < 180; vAngle = vAngle + angleSpan)// 垂直方向angleSpan度一份 { for (int hAngle = 0; hAngle <= 360; hAngle = hAngle + angleSpan)// 水平方向angleSpan度一份 { // 纵向横向各到一个角度后计算对应的此点在球面上的坐标 float x0 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle)) * Math.cos(Math .toRadians(hAngle))); float y0 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle)) * Math.sin(Math .toRadians(hAngle))); float z0 = (float) (r * UNIT_SIZE * Math.cos(Math .toRadians(vAngle))); // Log.w("x0 y0 z0","" + x0 + " "+y0+ " " +z0); float x1 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle)) * Math.cos(Math .toRadians(hAngle + angleSpan))); float y1 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle)) * Math.sin(Math .toRadians(hAngle + angleSpan))); float z1 = (float) (r * UNIT_SIZE * Math.cos(Math .toRadians(vAngle))); // Log.w("x1 y1 z1","" + x1 + " "+y1+ " " +z1); float x2 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle + angleSpan)) * Math .cos(Math.toRadians(hAngle + angleSpan))); float y2 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle + angleSpan)) * Math .sin(Math.toRadians(hAngle + angleSpan))); float z2 = (float) (r * UNIT_SIZE * Math.cos(Math .toRadians(vAngle + angleSpan))); // Log.w("x2 y2 z2","" + x2 + " "+y2+ " " +z2); float x3 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle + angleSpan)) * Math .cos(Math.toRadians(hAngle))); float y3 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle + angleSpan)) * Math .sin(Math.toRadians(hAngle))); float z3 = (float) (r * UNIT_SIZE * Math.cos(Math .toRadians(vAngle + angleSpan))); // Log.w("x3 y3 z3","" + x3 + " "+y3+ " " +z3); // 将计算出来的XYZ坐标加入存放顶点坐标的ArrayList alVertix.add(x1); alVertix.add(y1); alVertix.add(z1); alVertix.add(x3); alVertix.add(y3); alVertix.add(z3); alVertix.add(x0); alVertix.add(y0); alVertix.add(z0); alVertix.add(x1); alVertix.add(y1); alVertix.add(z1); alVertix.add(x2); alVertix.add(y2); alVertix.add(z2); alVertix.add(x3); alVertix.add(y3); alVertix.add(z3); } } vCount = alVertix.size() / COORDS_PER_VERTEX;// 顶点的数量 // 将alVertix中的坐标值转存到一个float数组中 float vertices[] = new float[vCount * COORDS_PER_VERTEX]; for (int i = 0; i < alVertix.size(); i++) { vertices[i] = alVertix.get(i); } vertexBuffer = ByteBuffer .allocateDirect(vertices.length * BYTES_PER_FLOAT) .order(ByteOrder.nativeOrder()) .asFloatBuffer(); // 把坐标们加入FloatBuffer中 vertexBuffer.put(vertices); // 设置buffer,从第一个坐标开始读 vertexBuffer.position(0); } }我们使用ArrayList来保存顶点坐标,要注意顺序的问题,因为我们的四边形也是由三角形组成的,我们实际上还是绘制两个三角形。
<span style="font-size:14px;">//vertex_shader_ball.glsl uniform mat4 u_Matrix;//最终的变换矩阵 attribute vec4 a_Position;//顶点位置 void main() { gl_Position = u_Matrix * a_Position; } </span>
<span style="font-size:14px;">precision mediump float; void main() { gl_FragColor=vec4(0.2,1.0,0.129,0); }</span>接下来和前面一样,编译链接~~ 等等,此时代码如下 (Ball.java ):
package com.cumt.shape; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.util.ArrayList; import android.content.Context; import android.opengl.GLES20; import com.cumt.opengeschange.R; import com.cumt.utils.MatrixState; import com.cumt.utils.ShaderHelper; import com.cumt.utils.TextResourceReader; public class Ball { private Context context; private static final float UNIT_SIZE = 1.0f;// 单位尺寸 private float r = 0.6f; // 球的半径 final int angleSpan = 10;// 将球进行单位切分的角度 private FloatBuffer vertexBuffer;// 顶点坐标 int vCount = 0;// 顶点个数,先初始化为0 // float类型的字节数 private static final int BYTES_PER_FLOAT = 4; // 数组中每个顶点的坐标数 private static final int COORDS_PER_VERTEX = 3; private int program; private static final String A_POSITION = "a_Position"; private static final String U_MATRIX = "u_Matrix"; private int uMatrixLocation; private int aPositionLocation; public Ball(Context context){ this.context = context; initVertexData(); getProgram(); aPositionLocation = GLES20.glGetAttribLocation(program, A_POSITION); uMatrixLocation = GLES20.glGetUniformLocation(program, U_MATRIX); //---------传入顶点数据数据 GLES20.glVertexAttribPointer(aPositionLocation, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, vertexBuffer); GLES20.glEnableVertexAttribArray(aPositionLocation); } public void initVertexData() { ArrayList<Float> alVertix = new ArrayList<Float>();// 存放顶点坐标的ArrayList for (int vAngle = 0; vAngle < 180; vAngle = vAngle + angleSpan)// 垂直方向angleSpan度一份 { for (int hAngle = 0; hAngle <= 360; hAngle = hAngle + angleSpan)// 水平方向angleSpan度一份 { // 纵向横向各到一个角度后计算对应的此点在球面上的坐标 float x0 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle)) * Math.cos(Math .toRadians(hAngle))); float y0 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle)) * Math.sin(Math .toRadians(hAngle))); float z0 = (float) (r * UNIT_SIZE * Math.cos(Math .toRadians(vAngle))); // Log.w("x0 y0 z0","" + x0 + " "+y0+ " " +z0); float x1 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle)) * Math.cos(Math .toRadians(hAngle + angleSpan))); float y1 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle)) * Math.sin(Math .toRadians(hAngle + angleSpan))); float z1 = (float) (r * UNIT_SIZE * Math.cos(Math .toRadians(vAngle))); // Log.w("x1 y1 z1","" + x1 + " "+y1+ " " +z1); float x2 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle + angleSpan)) * Math .cos(Math.toRadians(hAngle + angleSpan))); float y2 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle + angleSpan)) * Math .sin(Math.toRadians(hAngle + angleSpan))); float z2 = (float) (r * UNIT_SIZE * Math.cos(Math .toRadians(vAngle + angleSpan))); // Log.w("x2 y2 z2","" + x2 + " "+y2+ " " +z2); float x3 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle + angleSpan)) * Math .cos(Math.toRadians(hAngle))); float y3 = (float) (r * UNIT_SIZE * Math.sin(Math.toRadians(vAngle + angleSpan)) * Math .sin(Math.toRadians(hAngle))); float z3 = (float) (r * UNIT_SIZE * Math.cos(Math .toRadians(vAngle + angleSpan))); // Log.w("x3 y3 z3","" + x3 + " "+y3+ " " +z3); // 将计算出来的XYZ坐标加入存放顶点坐标的ArrayList alVertix.add(x1); alVertix.add(y1); alVertix.add(z1); alVertix.add(x3); alVertix.add(y3); alVertix.add(z3); alVertix.add(x0); alVertix.add(y0); alVertix.add(z0); alVertix.add(x1); alVertix.add(y1); alVertix.add(z1); alVertix.add(x2); alVertix.add(y2); alVertix.add(z2); alVertix.add(x3); alVertix.add(y3); alVertix.add(z3); } } vCount = alVertix.size() / COORDS_PER_VERTEX;// 顶点的数量 // 将alVertix中的坐标值转存到一个float数组中 float vertices[] = new float[vCount * COORDS_PER_VERTEX]; for (int i = 0; i < alVertix.size(); i++) { vertices[i] = alVertix.get(i); } vertexBuffer = ByteBuffer .allocateDirect(vertices.length * BYTES_PER_FLOAT) .order(ByteOrder.nativeOrder()) .asFloatBuffer(); // 把坐标们加入FloatBuffer中 vertexBuffer.put(vertices); // 设置buffer,从第一个坐标开始读 vertexBuffer.position(0); } //获取program private void getProgram(){ //获取顶点着色器文本 String vertexShaderSource = TextResourceReader .readTextFileFromResource(context, R.raw.vertex_shader_ball); //获取片段着色器文本 String fragmentShaderSource = TextResourceReader .readTextFileFromResource(context, R.raw.fragment_shader_ball); //获取program的id program = ShaderHelper.buildProgram(vertexShaderSource, fragmentShaderSource); GLES20.glUseProgram(program); } public void draw(){ //将最终变换矩阵写入 GLES20.glUniformMatrix4fv(uMatrixLocation, 1, false, MatrixState.getFinalMatrix(),0); GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount); } }
package com.cumt.render; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import com.cumt.shape.Ball; import com.cumt.utils.MatrixState; import android.content.Context; import android.opengl.GLES20; import android.opengl.GLSurfaceView.Renderer; import android.util.Log; import static android.opengl.GLES20.glClear; import static android.opengl.GLES20.glClearColor; import static android.opengl.GLES20.glViewport; public class MyRender implements Renderer { private Context context; Ball ball; public MyRender(Context context){ this.context = context; } public void onSurfaceCreated(GL10 gl, EGLConfig config) { Log.w("MyRender","onSurfaceCreated"); //设置屏幕背景色RGBA glClearColor(0.5f,0.5f,0.5f, 1.0f); //打开深度检测 GLES20.glEnable(GLES20.GL_DEPTH_TEST); //打开背面剪裁 GLES20.glEnable(GLES20.GL_CULL_FACE); ball = new Ball(context); } public void onSurfaceChanged(GL10 gl, int width, int height) { glViewport(0,0,width,height); float ratio = (float) width / height; // 调用此方法计算产生透视投影矩阵 MatrixState.setProjectFrustum(-ratio,ratio, -1, 1, 20, 100); // 调用此方法产生摄像机9参数位置矩阵 MatrixState.setCamera(0, 0, 30, 0f, 0f, 0f, 0f, 1.0f, 0.0f); } public void onDrawFrame(GL10 gl) { //清除深度缓冲与颜色缓冲 glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); ball.draw(); } }
package com.cumt.opengeschange; import com.cumt.render.MyRender; import com.cumt.utils.MatrixState; import android.content.Context; import android.opengl.GLSurfaceView; import android.view.MotionEvent; import android.view.View; public class MySurfaceView extends GLSurfaceView { private MyRender myRender; public MySurfaceView(Context context) { super(context); // TODO Auto-generated constructor stub myRender = new MyRender(context); this.setEGLContextClientVersion(2); this.setRenderer(myRender); // 设置渲染模式为主动渲染 this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); this.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN://检测到点击事件时 MatrixState.rotate(20f, 0, 1, 0);//绕y轴旋转 } return true; } }); } }
//vertex_shader_ball.glsl uniform mat4 u_Matrix;//最终的变换矩阵 attribute vec4 a_Position;//顶点位置 varying vec4 vPosition;//用于传递给片元着色器的顶点位置 void main() { gl_Position = u_Matrix * a_Position; vPosition = a_Position; }
precision mediump float; varying vec4 vPosition;//接收从顶点着色器过来的顶点位置 void main() { float uR = 0.6;//球的半径 vec4 color; float n = 8.0;//分为n层n列n行 float span = 2.0*uR/n;//正方形长度 //计算行列层数 int i = int((vPosition.x + uR)/span);//行数 int j = int((vPosition.y + uR)/span);//层数 int k = int((vPosition.z + uR)/span);//列数 int colorType = int(mod(float(i+j+k),2.0)); if(colorType == 1) {//奇数时为绿色 color = vec4(0.2,1.0,0.129,0); } else {//偶数时为白色 color = vec4(1.0,1.0,1.0,0);//白色 } //将计算出的颜色给此片元 gl_FragColor=color; }
标签:
原文地址:http://blog.csdn.net/cassiepython/article/details/51620114