gpt4 book ai didi

java - 无法同时运行两个着色器程序,但是每个单独使用时都可以正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 09:09:56 26 4
gpt4 key购买 nike

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

当我只尝试渲染这些对象之一(并且只编译和链接一个着色器程序)时,一切工作正常,当我对两个对象执行此操作时,仅渲染第二个对象(纹理方 block )。是的,我在渲染相应的对象之前确实切换了着色器程序。有一点值得注意。当我调试时;一个着色器程序中的顶点属性指针 = 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);
}

最佳答案

问题是绘制第一个对象时启用纹理坐标的顶点属性数据数组(索引 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);

关于java - 无法同时运行两个着色器程序,但是每个单独使用时都可以正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59769601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com