我有一个打包的顶点缓冲区,其中包含位置坐标以及格式为{X,Y,Z,R,G,B,A}的顶点的颜色值。
当我通过取出a_Color属性并为gl_FragColor硬编码vec4值而更改片段着色器时,我能够使用硬编码颜色正确显示矩形但是我无法将颜色vec4属性传递到片段着色器(该场景中不会显示矩形)。
使用glVertexAttribPointer(...)和glDrawElements(...)从OpenGL ES 2.0中的压缩顶点缓冲区绘制的正确方法是什么?
请参阅下面的代码:
public class GameRenderer implements Renderer {
public static final int POS_SZ = 3;
public static final int COL_SZ = 4;
public static final int FLOAT_SZ = 4;
public static final int SHORT_SZ = 2;
private FloatBuffer gridVB;
private ShortBuffer gridIndices;
int programCode, paPositionHandle, paColorHandle, puMVPMatrixHandle;
private final String vertexShaderCode =
"uniform mat4 u_MVPMatrix; \n" +
"attribute vec4 a_Position; \n" +
"void main(){ \n" +
" gl_Position = u_MVPMatrix * a_Position; \n" +
"} \n";
private String fragmentShaderCode =
"precision mediump float; \n" +
"attribute vec4 a_Color; \n" +
"void main(){ \n" +
" gl_FragColor = a_Color; \n" +
"} \n";
public void staticGrid() {
float vertexArray[] = {
-0.75f, 0.75f, 0.0f, // position
0.0f, 0.0f, 1.0f, 1.0f, // colour
0.75f, 0.75f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.75f, -0.75f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f,
-0.75f, -0.75f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f
};
short indicesArray[] = {
0, 1, 2, 0, 2, 3
};
ByteBuffer vbb = ByteBuffer.allocateDirect(vertexArray.length * FLOAT_SZ);
vbb.order(ByteOrder.nativeOrder());
gridVB = vbb.asFloatBuffer();
gridVB.put(vertexArray);
gridVB.position(0);
ByteBuffer ibb = ByteBuffer.allocateDirect(indicesArray.length * SHORT_SZ);
ibb.order(ByteOrder.nativeOrder());
gridIndices = ibb.asShortBuffer();
gridIndices.put(indicesArray);
gridIndices.position(0);
}
public void onSurfaceCreated(GL10 unused, EGLConfig arg1) {
GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
staticGrid();
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
programCode = GLES20.glCreateProgram();
GLES20.glAttachShader(programCode, vertexShader);
GLES20.glAttachShader(programCode, fragmentShader);
GLES20.glLinkProgram(programCode);
paPositionHandle = GLES20.glGetAttribLocation(programCode, "a_Position");
paColorHandle = GLES20.glGetAttribLocation(programCode, "a_Color");
puMVPMatrixHandle = GLES20.glGetUniformLocation(programCode, "u_MVPMatrix");
}
public void onDrawFrame(GL10 unused) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
GLES20.glUseProgram(programCode);
int stride = (POS_SZ + COL_SZ) * FLOAT_SZ;
int indices_cnt = 6;
gridVB.position(0);
GLES20.glEnableVertexAttribArray(paPositionHandle);
GLES20.glVertexAttribPointer(paPositionHandle, POS_SZ, GLES20.GL_FLOAT, false, stride, gridVB);
gridVB.position(POS_SZ);
GLES20.glEnableVertexAttribArray(paColorHandle);
GLES20.glVertexAttribPointer(paColorHandle, COL_SZ, GLES20.GL_FLOAT, false, stride, gridVB);
// matrix manipulation ...
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices_cnt, GLES20.GL_UNSIGNED_SHORT, gridIndices);
}
}
答案 0 :(得分:6)
片段着色器不使用属性。属性是每顶点值,片段着色器不会知道任何内容。您要做的是将颜色作为attribute
到顶点着色器,然后使用varying
将颜色传递给片段着色器。
此外,请开始使用着色器的错误检查,它会告诉您何时出错:
http://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetProgramiv.xml(检查GL_LINK_STATUS
)
http://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetShaderiv.xml(检查GL_COMPILE_STATUS
)
http://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetProgramInfoLog.xml
http://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetShaderInfoLog.xml