Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 紋理定距離移動

Android 紋理定距離移動

編輯:關於Android編程

效果圖:右邊的文字欄上下移動,沒有文字會自動停止移動。這和之前我寫的紋理移動不同,之前的是循環移動,這次是定位移動。

\

頂點著色器:

 

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);
}

java代碼:(這裡是核心)

 

 

package com.hl.paints;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import com.hl.utils.MatrixState;

import android.opengl.GLES20;

public class DrawRectMoveStop {

	int mProgram;
	int muMVPMatrixHandle;
	int maPositionHandle;
	int maTexCoorHandle;
	int muSpanHandle;
	
	FloatBuffer mVertexBuffer;
	FloatBuffer mTexCoorBuffer;
	
	int vCount=0;
	
	public DrawRectMoveStop(float width,float height,float s,float t,int mProgram) {
		// TODO Auto-generated constructor stub
		initVertex(width,height,s,t);
		initShader(mProgram);
	}
	
	private void initVertex(float width, float height,float s,float t) {// 紋理的傳入,目的是在最開始是不是將整個圖片放進矩形框中,而是一部分
		// 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, t, 
				s, t, 
				s, t, 
				s, 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);
	}
}

使用代碼:

 

        private DrawRectMoveStop benRightText;
        BUTTON_BEN_RIGHT3_WIDTH = 2.0f * ratio * 0.23f;
	BUTTON_BEN_RIGHT3_HEIGHT = 1.4f;
	BUTTON_BEN_RIGHT3_XOFFSET = ratio - 2.0f * ratio * 0.23f / 2;
	BUTTON_BEN_RIGHT3_YOFFSET = 1.0f - 0.15f - 0.37f - 0.02f - 0.7f;
	benRightText = new DrawRectMoveStop(BUTTON_BEN_RIGHT3_WIDTH, BUTTON_BEN_RIGHT3_HEIGHT, 1.0f, 0.7f, ShaderManager.getMoveTextureShaderProgram());
       //1.0f and 0.7f 是根據紋理圖片和寬度計算的。效果圖中的右邊文字部分,是圖片形式的。
        
MatrixState.pushMatrix(); MatrixState.translate(BUTTON_BEN_RIGHT3_XOFFSET, BUTTON_BEN_RIGHT3_YOFFSET, 0); benRightText.drawSelf(rText[condition], textYOffset); MatrixState.popMatrix();
private float textYOffset = 0;
        if (UtilConfigArea.isInArea(x, y, AREA_BEN_RIGHT3)) {//onTouchEvent ACTION_MOVE:
					textYOffset -= dy * TOUCH_SCALE_FACTOR * 0.002f;
					if (textYOffset > 0.3f) {
						textYOffset = 0.3f;
					}
					if (textYOffset < 0.0f) {
						textYOffset = 0.0f;
					}
				}


注:本文裡面用到一些方法,在我的其它博文中有提到,若用到,請查相關博文。
 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved