Android 紋理定距離移動

來源:互聯網
上載者:User

標籤:shader   opengl es   紋理移動   

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


頂點著色器:

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 stubinitVertex(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 stubvCount = 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 stubthis.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>


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


Android 紋理定距離移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.