标签:
package com.cumt.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import android.util.Log;
public class TextureHelper {
public static final String TAG = "TextureHelper";
public static int loadTexture(Context context,int resourceId,boolean isRepeat){
/*
* 第一步 : 创建纹理对象
*/
final int[] textureObjectId = new int[1];//用于存储返回的纹理对象ID
GLES20.glGenTextures(1,textureObjectId, 0);
if(textureObjectId[0] == 0){//若返回为0,,则创建失败
if(LoggerConfig.ON){
Log.w(TAG,"Could not generate a new Opengl texture object");
}
return 0;
}
/*
* 第二步: 加载位图数据并与纹理绑定
*/
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;//Opengl需要非压缩形式的原始数据
final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),resourceId, options);
if(bitmap == null){
if(LoggerConfig.ON){
Log.w(TAG,"ResourceId:"+resourceId+"could not be decoded");
}
GLES20.glDeleteTextures(1, textureObjectId, 0);
return 0;
}
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,textureObjectId[0]);//通过纹理ID进行绑定
if(isRepeat){
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
}
/*
* 第三步: 设置纹理过滤
*/
//设置缩小时为三线性过滤
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_LINEAR_MIPMAP_LINEAR);
//设置放大时为双线性过滤
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
/*
* 第四步: 加载纹理到Opengl并返回ID
*/
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
return textureObjectId[0];
}
}
//矩形顶点坐标 与 纹理坐标
static float squareCoords[] = { //以三角形扇的形式绘制
//x y s t
-0.5f, 0.5f , 0 , 0 , // top left
0.5f, 0.5f , 4 , 0 ,// top right
0.5f, -0.5f , 4 , 4 ,// bottom right
-0.5f, -0.5f , 0 , 4}; // bottom left if(isRepeat){
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
}其它代码不变 。我们将S轴方向与T轴方向都设置为截取拉伸 ,运行效果如下 :if(isRepeat){
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
}
//vertex_shader_cube.glsl
uniform mat4 u_Matrix;
attribute vec4 a_Position;
attribute vec2 a_TextureCoordinates;
varying vec2 v_TextureCoordinates;
void main()
{
gl_Position = u_Matrix * a_Position;
v_TextureCoordinates = a_TextureCoordinates;
} //fragment_shader_cube.glsl
precision mediump float;
uniform sampler2D u_TextureUnit;
varying vec2 v_TextureCoordinates;
void main()
{
gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates);
}package com.cumt.shape;
import static android.opengl.GLES20.GL_TEXTURE0;
import static android.opengl.GLES20.GL_TEXTURE_2D;
import static android.opengl.GLES20.glActiveTexture;
import static android.opengl.GLES20.glBindTexture;
import static android.opengl.GLES20.glUniform1i;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import com.cumt.openglescubewen.R;
import com.cumt.utils.MatrixState;
import com.cumt.utils.ShaderHelper;
import com.cumt.utils.TextResourceReader;
import com.cumt.utils.TextureHelper;
import android.content.Context;
import android.opengl.GLES20;
public class Cube {
//顶点坐标
private FloatBuffer vertexBuffer;
private Context context;
//float类型的字节数
private static final int BYTES_PER_FLOAT = 4;
//共有72个顶点坐标,每个面包含12个顶点坐标
private static final int POSITION_COMPONENT_COUNT = 6*12;
// 数组中每个顶点的坐标数
private static final int COORDS_PER_VERTEX = 3;
// 颜色数组中每个颜色的值数
private static final String A_POSITION = "a_Position";
private static final String U_MATRIX = "u_Matrix";
private int uMatrixLocation;
private int aPositionLocation;
private int program;
private static final int TEXTURE_COORDIANTES_COMPONENT_COUNT = 2; //一个纹理坐标含有的元素个数
private static final int STRIDE = (COORDS_PER_VERTEX + TEXTURE_COORDIANTES_COMPONENT_COUNT)
* BYTES_PER_FLOAT;
private static final String A_TEXTURE_COORDINATES = "a_TextureCoordinates";//纹理
private static final String U_TEXTURE_UNIT = "u_TextureUnit";//纹理
private int uTextureUnitLocation;
private int aTextureCoordinates;
private int texture;
static float vertices[] = {
//X Y Z S T
//前面
0,0,1.0f, 0.5f,0.5f,
1.0f,1.0f,1.0f, 1.0f,0,
-1.0f,1.0f,1.0f, 0,0,
0,0,1.0f, 0.5f,0.5f,
-1.0f,1.0f,1.0f, 0,0,
-1.0f,-1.0f,1.0f, 0,1.0f,
0,0,1.0f, 0.5f,0.5f,
-1.0f,-1.0f,1.0f, 0,1.0f,
1.0f,-1.0f,1.0f, 1.0f,1.0f,
0,0,1.0f, 0.5f,0.5f,
1.0f,-1.0f,1.0f, 1.0f,1.0f,
1.0f,1.0f,1.0f, 1.0f,0,
//后面
0,0,-1.0f, 0.5f,0.5f,
1.0f,1.0f,-1.0f, 1.0f,0,
1.0f,-1.0f,-1.0f, 1.0f,1.0f,
0,0,-1.0f, 0.5f,0.5f,
1.0f,-1.0f,-1.0f, 1.0f,1.0f,
-1.0f,-1.0f,-1.0f, 0,1.0f,
0,0,-1.0f, 0.5f,0.5f,
-1.0f,-1.0f,-1.0f, 0,1.0f,
-1.0f,1.0f,-1.0f,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,
0,-1.0f,0, 0.5f,0.5f,
-1.0f,-1.0f,1.0f, 0,0,
-1.0f,-1.0f,-1.0f, 0,1.0f,
0,-1.0f,0, 0.5f,0.5f,
-1.0f,-1.0f,-1.0f, 0,1.0f,
1.0f,-1.0f,-1.0f, 1.0f,1.0f,
0,-1.0f,0, 0.5f,0.5f,
1.0f,-1.0f,-1.0f, 1.0f,1.0f,
1.0f,-1.0f,1.0f , 1.0f,0,
};
public Cube(Context context){
this.context = context;
vertexBuffer = ByteBuffer
.allocateDirect(vertices.length * BYTES_PER_FLOAT)
.order(ByteOrder.nativeOrder())
.asFloatBuffer();
// 把坐标们加入FloatBuffer中
vertexBuffer.put(vertices);
// 设置buffer,从第一个坐标开始读
vertexBuffer.position(0);
getProgram();
aPositionLocation = GLES20.glGetAttribLocation(program, A_POSITION);
uMatrixLocation = GLES20.glGetUniformLocation(program, U_MATRIX);
aTextureCoordinates = GLES20.glGetAttribLocation(program, A_TEXTURE_COORDINATES);
uTextureUnitLocation = GLES20.glGetAttribLocation(program, U_TEXTURE_UNIT);
texture = TextureHelper.loadTexture(context, R.drawable.umei,false);
// Set the active texture unit to texture unit 0.
glActiveTexture(GL_TEXTURE0);
// Bind the texture to this unit.
glBindTexture(GL_TEXTURE_2D, texture);
// Tell the texture uniform sampler to use this texture in the shader by
// telling it to read from texture unit 0.
glUniform1i(uTextureUnitLocation, 0);
//---------传入顶点数据数据
GLES20.glVertexAttribPointer(aPositionLocation, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false, STRIDE, vertexBuffer);
GLES20.glEnableVertexAttribArray(aPositionLocation);
//设置从第二个元素开始读取,因为从第二个元素开始才是纹理坐标
vertexBuffer.position(COORDS_PER_VERTEX);
GLES20.glVertexAttribPointer(aTextureCoordinates, TEXTURE_COORDIANTES_COMPONENT_COUNT,
GLES20.GL_FLOAT, false, STRIDE, vertexBuffer);
GLES20.glEnableVertexAttribArray(aTextureCoordinates);
}
//获取program
private void getProgram(){
//获取顶点着色器文本
String vertexShaderSource = TextResourceReader
.readTextFileFromResource(context, R.raw.vertex_shader_cube);
//获取片段着色器文本
String fragmentShaderSource = TextResourceReader
.readTextFileFromResource(context, R.raw.fragment_shader_cube);
//获取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, POSITION_COMPONENT_COUNT);
}
}
OpenglES2.0 for Android:再谈纹理映射
标签:
原文地址:http://blog.csdn.net/cassiepython/article/details/51645791