效果图:右边的文字栏上下移动,没有文字会自动停止移动。这和之前我写的纹理移动不同,之前的是循环移动,这次是定位移动。
顶点着色器:
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); }
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,<span style="color:#ff0000;">float s,float t</span>) {// 纹理的传入,目的是在最开始是不是将整个图片放进矩形框中,而是一部分 // 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,<span style="color:#ff0000;"> 1.0f, 0.7f</span>, ShaderManager.getMoveTextureShaderProgram());
//1.0f and 0.7f 是根据纹理图片和宽度计算的。效果图中的右边文字部分,是图片形式的。
<pre name="code" class="java"> MatrixState.pushMatrix(); MatrixState.translate(BUTTON_BEN_RIGHT3_XOFFSET, BUTTON_BEN_RIGHT3_YOFFSET, 0); benRightText.drawSelf(rText[condition], textYOffset); MatrixState.popMatrix();private float textYOffset = 0;
<span style="color:#ff0000;">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; } }</span>
注:本文里面用到一些方法,在我的其它博文中有提到,若用到,请查相关博文。
原文地址:http://blog.csdn.net/sh15285118586/article/details/46284947