Android 纹理定距离移动(openGl可以看这货代码)

http://blog.csdn.net/sh15285118586/article/details/46284947

效果图:右边的文字栏上下移动,没有文字会自动停止移动。这和之前我写的纹理移动不同,之前的是循环移动,这次是定位移动。


顶点着色器:

[cpp]  view plain copy
  1. uniform mat4 uMVPMatrix;  
  2. attribute vec3 aPosition;  
  3. attribute vec2 aTexCoor;  
  4. varying vec2 vTextureCoord;  
  5. void main()  
  6. {  
  7.    gl_Position=uMVPMatrix*vec4(aPosition,1);  
  8.    vTextureCoord=aTexCoor;  
  9. }  

片元着色器:

[cpp]  view plain copy
  1. precision mediump float;  
  2. varying vec2 vTextureCoord;  
  3. uniform sampler2D sTexture;  
  4. uniform float uSpan;  
  5. void main()  
  6. {  
  7.    vec2 st_Result=vec2(0,0);  
  8.    st_Result.x=vTextureCoord.x;  
  9.    st_Result.y=vTextureCoord.y+uSpan;  
  10.    gl_FragColor=texture2D(sTexture,st_Result);  
  11. }  

java代码:(这里是核心)

[java]  view plain copy
  1. package com.hl.paints;  
  2.   
  3. import java.nio.ByteBuffer;  
  4. import java.nio.ByteOrder;  
  5. import java.nio.FloatBuffer;  
  6.   
  7. import com.hl.utils.MatrixState;  
  8.   
  9. import android.opengl.GLES20;  
  10.   
  11. public class DrawRectMoveStop {  
  12.   
  13.     int mProgram;  
  14.     int muMVPMatrixHandle;  
  15.     int maPositionHandle;  
  16.     int maTexCoorHandle;  
  17.     int muSpanHandle;  
  18.       
  19.     FloatBuffer mVertexBuffer;  
  20.     FloatBuffer mTexCoorBuffer;  
  21.       
  22.     int vCount=0;  
  23.       
  24.     public DrawRectMoveStop(float width,float height,float s,float t,int mProgram) {  
  25.         // TODO Auto-generated constructor stub  
  26.         initVertex(width,height,s,t);  
  27.         initShader(mProgram);  
  28.     }  
  29.       
  30.     private void initVertex(float width, float height,<span style="color:#ff0000;">float s,float t</span>) {// 纹理的传入,目的是在最开始是不是将整个图片放进矩形框中,而是一部分  
  31.         // TODO Auto-generated method stub  
  32.         vCount = 6;  
  33.         float w = width / 2;  
  34.         float h = height / 2;  
  35.         float vertices[] = new float[] {   
  36.         -w,  h, 0,  
  37.         -w, -h, 0,  
  38.          w, -h, 0,  
  39.          w, -h, 0,  
  40.          w,  h, 0,  
  41.         -w,  h, 0,  
  42.   
  43.         };  
  44.         ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);  
  45.         vbb.order(ByteOrder.nativeOrder());  
  46.         mVertexBuffer = vbb.asFloatBuffer();  
  47.         mVertexBuffer.put(vertices);  
  48.         mVertexBuffer.position(0);  
  49.   
  50.         float texCoor[] = new float[] {   
  51.                 00,   
  52.                 0, t,   
  53.                 s, t,   
  54.                 s, t,   
  55.                 s, 0,   
  56.                 00 };  
  57.         ByteBuffer cbb = ByteBuffer.allocateDirect(texCoor.length * 4);  
  58.         cbb.order(ByteOrder.nativeOrder());  
  59.         mTexCoorBuffer = cbb.asFloatBuffer();  
  60.         mTexCoorBuffer.put(texCoor);  
  61.         mTexCoorBuffer.position(0);  
  62.     }  
  63.   
  64.     private void initShader(int mProgram) {  
  65.         // TODO Auto-generated method stub  
  66.         this.mProgram = mProgram;  
  67.         muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");  
  68.         maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");  
  69.         maTexCoorHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoor");  
  70.         muSpanHandle=GLES20.glGetUniformLocation(mProgram, "uSpan");  
  71.     }  
  72.       
  73.     public void drawSelf(int texId,float currStart){  
  74.         GLES20.glUseProgram(mProgram);  
  75.         GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1false, MatrixState.getFinalMatrix(), 0);  
  76.         GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false3*4, mVertexBuffer);  
  77.         GLES20.glVertexAttribPointer(maTexCoorHandle, 2, GLES20.GL_FLOAT, false2*4, mTexCoorBuffer);  
  78.         GLES20.glEnableVertexAttribArray(maPositionHandle);  
  79.         GLES20.glEnableVertexAttribArray(maTexCoorHandle);  
  80.         GLES20.glUniform1f(muSpanHandle, currStart);  
  81.         GLES20.glActiveTexture(GLES20.GL_TEXTURE0);  
  82.         GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);  
  83.         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);  
  84.     }  
  85. }  

使用代码:

[java]  view plain copy
  1.        private DrawRectMoveStop benRightText;  
  2.        BUTTON_BEN_RIGHT3_WIDTH = 2.0f * ratio * 0.23f;  
  3. BUTTON_BEN_RIGHT3_HEIGHT = 1.4f;  
  4. BUTTON_BEN_RIGHT3_XOFFSET = ratio - 2.0f * ratio * 0.23f / 2;  
  5. BUTTON_BEN_RIGHT3_YOFFSET = 1.0f - 0.15f - 0.37f - 0.02f - 0.7f;  
  6. benRightText = new DrawRectMoveStop(BUTTON_BEN_RIGHT3_WIDTH, BUTTON_BEN_RIGHT3_HEIGHT,<span style="color:#ff0000;"1.0f, 0.7f</span>, ShaderManager.getMoveTextureShaderProgram());  
[java]  view plain copy
  1. //1.0f and 0.7f 是根据纹理图片和宽度计算的。效果图中的右边文字部分,是图片形式的。  
[java]  view plain copy
  1.        <pre name="code" class="java">        MatrixState.pushMatrix();  
  2. MatrixState.translate(BUTTON_BEN_RIGHT3_XOFFSET, BUTTON_BEN_RIGHT3_YOFFSET, 0);  
  3. benRightText.drawSelf(rText[condition], textYOffset);  
  4. MatrixState.popMatrix();  
private float textYOffset = 0;
[java]  view plain copy
  1.     <span style="color:#ff0000;">if (UtilConfigArea.isInArea(x, y, AREA_BEN_RIGHT3)) {//onTouchEvent ACTION_MOVE:  
  2.     textYOffset -= dy * TOUCH_SCALE_FACTOR * 0.002f;  
  3.     if (textYOffset > 0.3f) {  
  4.         textYOffset = 0.3f;  
  5.     }  
  6.     if (textYOffset < 0.0f) {  
  7.         textYOffset = 0.0f;  
  8.     }  
  9. }</span>  

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值