1

好吧,这已经扼杀了 2 天。我有一个小的 GLES2/Android 应用程序......两个对象(都是正方形,但只有一个具有纹理坐标)。我有两个着色器程序,一个处理纹理及其坐标,另一个只在片段着色器中使用颜色。

当我只尝试渲染其中一个对象(并且只编译和链接一个着色器程序)时,一切正常,当我为这两个对象执行此操作时,仅渲染第二个对象(纹理正方形)。是的,我在渲染相应对象之前确实切换了着色器程序。值得注意的一件事。当我调试时;一个着色器程序中的顶点属性指针 = 0 = 下面另一个程序中的纹理属性指针是一些代码,感谢任何帮助

public abstract class SquareShader extends ShaderProgram {

public static int ShaderProgramObjectId;

public static int VertexPositionHandle;
public static int ColorUniformHandle;
public static int ProjectionMatrixHandle;
public static int ModelMatrixHandle;

public static void Initialize()
{
    ShaderProgramObjectId = LinkShaderProgram( R.raw.square_vertex_shader, 
 R.raw.square_fragment_shader);

    ColorUniformHandle = GLES20.glGetUniformLocation(ShaderProgramObjectId, "u_Color");
    ProjectionMatrixHandle = GLES20.glGetUniformLocation(ShaderProgramObjectId, 
   "u_ProjectionMatrix");
    ModelMatrixHandle = GLES20.glGetUniformLocation(ShaderProgramObjectId, "u_ModelMatrix");
    VertexPositionHandle = GLES20.glGetAttribLocation(ShaderProgramObjectId, "sq_VertexPosition");

    GLES20.glEnableVertexAttribArray(VertexPositionHandle);
}
}

public abstract class TextureShader extends ShaderProgram {
public static int ShaderProgramObjectId;

public static int VertexPositionHandle;
public static int TexturePositionHandle;
public static int ProjectionMatrixHandle;
public static int ModelMatrixHandle;
public static int TextureUnitHandle;

public static void Initialize()
{
    ShaderProgramObjectId = LinkShaderProgram( R.raw.texture_vertex_shader, 
    R.raw.texture_fragment_shader);

    ProjectionMatrixHandle = GLES20.glGetUniformLocation(ShaderProgramObjectId, 
    "u_ProjectionMatrix");
    ModelMatrixHandle = GLES20.glGetUniformLocation(ShaderProgramObjectId, "u_ModelMatrix");
    TextureUnitHandle = GLES20.glGetUniformLocation(ShaderProgramObjectId, "u_TextureUnit");

    VertexPositionHandle = GLES20.glGetAttribLocation(ShaderProgramObjectId, "a_VertexPosition");
    TexturePositionHandle = GLES20.glGetAttribLocation(ShaderProgramObjectId, "a_TexturePosition");

    GLES20.glEnableVertexAttribArray(VertexPositionHandle);
    GLES20.glEnableVertexAttribArray(TexturePositionHandle);

}
}

public void onDrawFrame(GL10 gl10) {
//Shade Programs are Initialized in (onSurfaceChanged)
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    GLES20.glUseProgram(SquareShader.ShaderProgramObjectId);
    GLES20.glUniformMatrix4fv(SquareShader.ProjectionMatrixHandle, 1, false, 
    DisplayUtility.ProjectionMatrix, 0);
    SquarePlain.Draw();



    GLES20.glUseProgram(TextureShader.ShaderProgramObjectId);
    GLES20.glUniformMatrix4fv(TextureShader.ProjectionMatrixHandle, 1, false, 
  DisplayUtility.ProjectionMatrix, 0);
    SquareTextured.Draw();
}


  //SquarePlain
  public static void Draw()
 {

    Matrix.setIdentityM(DisplayUtility.ModelMatrix, 0);
    Matrix.translateM(DisplayUtility.ModelMatrix, 0, 100 , 0, 0);
    GLES20.glUniformMatrix4fv(SquareShader.ModelMatrixHandle, 1, false, DisplayUtility.ModelMatrix, 0);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, VBO_VertexBufferID[0]);
    GLES20.glVertexAttribPointer(SquareShader.VertexPositionHandle, COORDS_PER_VERTEX,
            GLES20.GL_FLOAT, false,
            vertexStride, 0);


    GLES20.glUniform4f(SquareShader.ColorUniformHandle, 0.0f, 0.0f, 1.0f, 1.0f);
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length, GLES20.GL_UNSIGNED_SHORT, 
 indicesBuffer);
 }


 //SquareTextured 
 public static void Draw()
 {
    Matrix.setIdentityM(DisplayUtility.ModelMatrix, 0);
    Matrix.translateM(DisplayUtility.ModelMatrix, 0, 600 , 200, 0);

    GLES20.glUniformMatrix4fv(TextureShader.ModelMatrixHandle, 1, false, DisplayUtility.ModelMatrix, 0);

    //Bind vertices & Texture VBO

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, VBO_VertexBufferID[0]);
    GLES20.glVertexAttribPointer(TextureShader.VertexPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, 0);
    GLES20.glVertexAttribPointer(TextureShader.TexturePositionHandle, COORDS_PER_TexTure, GLES20.GL_FLOAT, false, vertexStride, COORDS_PER_VERTEX * Float.BYTES);


    //Switch Texture Buffer
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureBufferId[0]);

    //Bind Indices VBO
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, VBO_IndicesBufferID[0]);
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, 0);
}
4

1 回答 1

0

TexturePositionHandle问题是在绘制第一个对象时启用了纹理坐标(索引)的顶点属性数据数组。
请注意,OpenGL 是一个状态引擎,并且在再次更改时会保留状态,甚至超出帧。

您必须启用着色器程序所需的通用顶点属性数组,并且必须确保在绘制对象时禁用其他顶点属性数组:

GLES20.glEnableVertexAttribArray(VertexPositionHandle);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length, GLES20.GL_UNSIGNED_SHORT, 
                      indicesBuffer);
GLES20.glDisableVertexAttribArray(VertexPositionHandle);
GLES20.glEnableVertexAttribArray(VertexPositionHandle);
GLES20.glEnableVertexAttribArray(TexturePositionHandle);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, 0);
GLES20.glDisableVertexAttribArray(VertexPositionHandle);
GLES20.glDisableVertexAttribArray(TexturePositionHandle);
于 2020-01-16T13:55:47.980 回答