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参数改变影响图片在指定大小平面上移动。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值