openGL es2.0 创建可移动的纹理平面
一、Java部分代码
package com.gzdxid.utils;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import android.opengl.GLES20;
public class DrawRectMove {
int mProgram;
int muMVPMatrixHandle;
int maPositionHandle;
int maTexCoorHandle;
int muSpanHandle;
FloatBuffer mVertexBuffer;
FloatBuffer mTexCoorBuffer;
int vCount=0;
public DrawRectMove(float width,float height,int mProgram) {
// TODO Auto-generated constructor stub
initVertex(width,height);
initShader(mProgram);
}
private void initVertex(float width, float height) {
// TODO Auto-generated method stub
vCount = 6;
float w = width / 2;
float h = height / 2;
float vertices[] = new float[] {
-w, h, 0,
-w, -h, 0,
w, -h, 0,
w, -h, 0,
w, h, 0,
-w, h, 0,
};
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
float texCoor[] = new float[] { 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0 };
ByteBuffer cbb = ByteBuffer.allocateDirect(texCoor.length * 4);
cbb.order(ByteOrder.nativeOrder());
mTexCoorBuffer = cbb.asFloatBuffer();
mTexCoorBuffer.put(texCoor);
mTexCoorBuffer.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");
maTexCoorHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoor");
muSpanHandle=GLES20.glGetUniformLocation(mProgram, "uSpan");
}
public void drawSelf(int texId,float currStart){
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.glUniform1f(muSpanHandle, currStart);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
}
}
二、定点着色器:
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;
uniform float uSpan;
void main()
{
vec2 st_Result=vec2(0,0);
st_Result.x=vTextureCoord.x;
st_Result.y=vTextureCoord.y+uSpan;
gl_FragColor=texture2D(sTexture,st_Result);
}
这里面是移动y轴参数,当然也可以移动x轴的。在Java代码中的drawSelf()中,currStart参数改变影响图片在指定大小平面上移动。
还没有评论,来说两句吧...