【问题标题】:OpenGl basic Vertex ShaderOpenGL基本顶点着色器
【发布时间】:2023-04-01 02:50:01
【问题描述】:

我是着色器概念的新手,我正在尝试在 OpenGL ES 中实现一个 8x8 的精灵。

我想在顶点着色器中移动纹理,但我不知道怎么做,我的代码可能是错误的,请随时纠正我

如果我在顶点着色器中更改这条线,纹理会缩放但我想移动而不是缩放!:

v_TexCoordinate = a_TexCoordinate*vec2(1.5,1.5);

所以我应该申请附加,但我不知道怎么做(也许还有其他方法)

顶点着色器:

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

【问题讨论】:

  • 不知道加法是什么意思? v_TexCoordinate = a_TexCoordinate+vec2(-pixelsToMoveX/widthInPixels,-pixelsToMoveY/heightInPixels);由于纹理坐标定义在 [0,1] 范围内,因此减去以校正视觉表示和划分。
  • 我的意思是如果我这样做 v_TexCoordinate = a_TexCoordinate + vec2(1.5,1.5);什么都没发生
  • 那么还有其他问题。你确定你正在使用你正在修改的着色器吗?也许如果这是一个文件,旧的文件仍然在缓存中。无论如何,这应该可行。
  • 感谢 Matic 的提示,我将修改我的代码并更新问题

标签: opengl-es vertex-shader


【解决方案1】:

您可以添加一些偏移量而不是相乘。

v_TexCoordinate = a_TexCoordinate + vec2(1.5,1.5);

你的纹理也应该被夹住

【讨论】:

  • 我试过了,但没有发生相同的纹理
  • 这不是完全正确的答案,因为 vec2 的 X、Y 值应该小于 1 但仍然有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 2016-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多