2015-05-25 137 views
0

我是着色器概念的新手,我試圖在OpenGL ES中實現一個8x8的精靈。OpenGl基本頂點着色器

我想移動紋理在頂點着色器,但我不能弄清楚如何這一點,我的代碼可能是錯的,隨時糾正我

如果我更改了頂點着色器這一行,紋理規模,但我想移動無法形成規模!:

v_TexCoordinate = a_TexCoordinate*vec2(1.5,1.5); 

所以我應該申請ADITION但我不知道該怎麼做(也許還有另一種途徑)

頂點着色器:

uniform mat4 u_MVPMatrix;  // A constant representing the combined model/view/projection matrix.     
uniform mat4 u_MVMatrix;  // A constant representing the combined model/view matrix.    
uniform mat4 u_TextureMatrix; 


attribute vec4 a_Position;  // Per-vertex position information we will pass in.        
attribute vec3 a_Normal;  // Per-vertex normal information we will pass in.  
attribute vec2 a_TexCoordinate; // Per-vertex texture coordinate information we will pass in.  

varying vec3 v_Position;  // This will be passed into the fragment shader.        
varying vec3 v_Normal;   // This will be passed into the fragment shader. 
varying vec2 v_TexCoordinate; // This will be passed into the fragment shader.    

// The entry point for our vertex shader. 
void main()              
{               
// Transform the vertex into eye space.  
    v_Position = vec3(u_MVMatrix * a_Position);     

// Pass through the texture coordinate. 
    v_TexCoordinate = a_TexCoordinate;          

// Transform the normal's orientation into eye space. 
    v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0)); 

// gl_Position is a special variable used to store the final position. 
// Multiply the vertex by the matrix to get the final point in normalized screen coordinates. 
    gl_Position = u_MVPMatrix * a_Position;        
} 

這是我畫機能的研究

private void drawMagia() 
{ 
    GLES20.glUseProgram(mMagiaProgramHandle); 
    mTextureMatrixHandle = GLES20.glGetUniformLocation(mMagiaProgramHandle, "u_TextureMatrix"); 
    mMagiaTextureCoordinateHandle = GLES20.glGetAttribLocation(mMagiaProgramHandle, "a_TexCoordinate"); 



    mMagiaPositions.position(0);   
    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 
      0, mMagiaPositions);   

    GLES20.glEnableVertexAttribArray(mPositionHandle);   


    // Pass in the normal information 
    mMagiaNormals.position(0); 
    GLES20.glVertexAttribPointer(mNormalHandle, mNormalDataSize, GLES20.GL_FLOAT, false, 
      0, mMagiaNormals); 

    GLES20.glEnableVertexAttribArray(mNormalHandle); 

    // Pass in the texture coordinate information 
    mMagiaTextureCoordinates.position(0); 
    GLES20.glVertexAttribPointer(mTextureCoordinateHandle, mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 
      0, mMagiaTextureCoordinates); 

    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle); 

    // This multiplies the view matrix by the model matrix, and stores the 
    // result in the MVP matrix 
    // (which currently contains model * view). 
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); 

    // Pass in the modelview matrix. 
    GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0); 

    GLES20.glUniformMatrix4fv(mTextureMatrixHandle, 1, false, mTextureMatrix, 0); 

    // This multiplies the modelview matrix by the projection matrix, and 
    // stores the result in the MVP matrix 
    // (which now contains model * view * projection). 
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); 

    // Pass in the combined matrix. 
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); 

    // Pass in the light position in eye space. 
    GLES20.glUniform3f(mLightPosHandle, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], mLightPosInEyeSpace[2]); 


    // Draw the square. 
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6); 
} 
+0

你是什麼意思你不知道如何應用除了做? v_TexCoordinate = a_TexCoordinate + vec2(-pixelsToMoveX/widthInPixels,-pixelsToMoveY/heightInPixels);由於在[0,1]範圍內定義了紋理座標,所以減去校正視覺表示和分割。 –

+0

我的意思是,如果我做v_TexCoordinate = a_TexCoordinate + vec2(1.5,1.5);什麼也沒有發生 – D4rWiNS

+1

然後,其他的東西是錯的。你確定你正在使用你正在修改的着色器嗎?也許如果這是一個文件,舊的文件仍然在緩存或什麼東西。無論如何,這應該工作。 –

回答

1

您可以添加的,而不是乘以一定的偏移。

v_TexCoordinate = a_TexCoordinate + vec2(1.5,1.5); 

而且你的紋理應鎖定

+0

我試過了,但沒有發生同樣的紋理 – D4rWiNS

+0

這不是完全正確的答案,因爲vec2的X,Y值應小於1但仍然有效 – D4rWiNS

相關問題