openGL es2.0 创建纹理球
来源:互联网 发布:农村淘宝.下载安装 编辑:程序博客网 时间:2024/06/07 15:46
一、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 DrawBallTexture {int mProgram;int muMVPMatrixHandle;int maPositionHandle;int maTexCoorHandle;FloatBuffer mVertexBuffer;FloatBuffer mTexCoorBuffer;int vCount = 0;final float UNIT_SIZE = 1f;final float angleSpan = 10f;float R = 0;public float roateX;public float roateY;public DrawBallTexture(float r, int mProgram) {// TODO Auto-generated constructor stubinitVertex(r);initShader(mProgram);}private void initVertex(float r) {// TODO Auto-generated method stubR = 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);float[] texCoor = generateTexCoor(// 获取切分整图的纹理数组(int) (360 / angleSpan), // 纹理图切分的列数(int) (180 / angleSpan) // 纹理图切分的行数);ByteBuffer llbb = ByteBuffer.allocateDirect(texCoor.length * 4);llbb.order(ByteOrder.nativeOrder());// 设置字节顺序mTexCoorBuffer = llbb.asFloatBuffer();mTexCoorBuffer.put(texCoor);mTexCoorBuffer.position(0);}private void initShader(int mProgram) {// TODO Auto-generated method stubthis.mProgram = mProgram;muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");maTexCoorHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoor");}public void drawSelf(int texId) {MatrixState.rotate(roateX, 1, 0, 0);MatrixState.rotate(roateY, 0, 1, 0);GLES20.glUseProgram(mProgram);GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer);GLES20.glVertexAttribPointer(maTexCoorHandle, 2, GLES20.GL_FLOAT, false, 2 * 4, mTexCoorBuffer);GLES20.glEnableVertexAttribArray(maPositionHandle);GLES20.glEnableVertexAttribArray(maTexCoorHandle);GLES20.glActiveTexture(GLES20.GL_TEXTURE0);GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);}private float getCoor(int which, float vAngle, float hAngle) {switch (which) {case 0:// xreturn (float) (R * UNIT_SIZE * Math.cos(Math.toRadians(vAngle)) * Math.cos(Math.toRadians(hAngle)));case 1:// yreturn (float) (R * UNIT_SIZE * Math.sin(Math.toRadians(vAngle)));case 2:// zreturn (float) (R * UNIT_SIZE * Math.cos(Math.toRadians(vAngle)) * Math.sin(Math.toRadians(hAngle)));}return 0;}// 自动切分纹理产生纹理数组的方法public float[] generateTexCoor(int bw, int bh) {float[] result = new float[bw * bh * 6 * 2];float sizew = 1.0f / bw;// 列数float sizeh = 1.0f / bh;// 行数int c = 0;for (int i = 0; i < bh; i++) {for (int j = 0; j < bw; j++) {// 每行列一个矩形,由两个三角形构成,共六个点,12个纹理坐标float s = j * sizew;float t = i * sizeh;result[c++] = s;result[c++] = t;result[c++] = s;result[c++] = t + sizeh;result[c++] = s + sizew;result[c++] = t;result[c++] = s + sizew;result[c++] = t;result[c++] = s;result[c++] = t + sizeh;result[c++] = s + sizew;result[c++] = t + sizeh;}}return result;}}
二、顶点着色器:
uniform mat4 uMVPMatrix;attribute vec3 aPosition;attribute vec2 aTexCoor;varying vec2 vTextureCoord;void main(){ gl_Position=uMVPMatrix*vec4(aPosition,1); vTextureCoord=aTexCoor;}三、片源着色器:
precision mediump float;varying vec2 vTextureCoord;uniform sampler2D sTexture;void main(){ gl_FragColor=texture2D(sTexture,vTextureCoord);}
0 0
- openGL es2.0 创建纹理球
- openGL es2.0 创建纹理灯光球
- openGL es2.0 创建纹理灯光球
- openGL es2.0 创建可移动的纹理平面
- openGL es2.0 创建颜色球
- openGL es2.0 创建灯光颜色球
- OpenGL ES2.0 生成Mipmap纹理
- OpenGL ES2.0教程:纹理贴图(6)
- Opengl ES2.0之纹理拉伸采样小结
- Opengl ES2.0之mipmap纹理技术小结
- 17、OpenGL ES2.0 生成Mipmap纹理(2012-08-02)
- OpenGL ES2.0 生成Mipmap纹理(2012-08-02)
- OpenGL es2.0 加载纹理图片和CubMap
- OpenGL es2.0 加载纹理图片和CubMap
- OpenGL es2.0 加载纹理图片和CubMap
- Cocos2dx-OpenGL ES2.0教程:纹理贴图(6)
- OpenGL es2.0 创建颜色线圆
- openGL es2.0 创建和管理shader
- Vertex and Fragment Shader(可编程渲染管线)
- Android-PullToRefresh
- Ubuntu下安装eclipse的Could not load SWT library问题
- 2440启动文件分析
- error: expected class-name before '{' token
- openGL es2.0 创建纹理球
- 无意间的意识到 自己在做根本不喜欢的事情,你反思过吗
- Hduoj1251【字典树】
- Surface Shader(可编程渲染管线 表面着色器)
- Ubuntu12挂载扩充/home
- In-app purchase verification
- nodejs 创建http server
- Web服务器推送技术
- PullToRefreshListView 去掉item 默认点击颜色