我对OpenGL编程非常陌生,所以如果我忽略了明显的内容,请原谅我。我使用VBO创建了一个多维数据集,并加载了顶点,绘制顺序和颜色信息。渲染多维数据集时,它是完全黑色的。谁能建议为什么不显示颜色信息?我正在创建4个缓冲对象,但仅使用3个(法线将是第四个)。
1)创建VBO
GLES31.glGenBuffers ( 4, mVBOIds, 0 );
// Bind the first buffer to put vertices info in
GLES31.glBindBuffer ( GLES31.GL_ARRAY_BUFFER, mVBOIds[0] );
//Transfer data to buffer
GLES31.glBufferData(GLES31.GL_ARRAY_BUFFER, mCubePositions.capacity() * mBytesPerFloat,
mCubePositions, GLES31.GL_STATIC_DRAW);
// Unbind buffer
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
// Bind the second buffer to put draw order info in
GLES31.glBindBuffer ( GLES31.GL_ELEMENT_ARRAY_BUFFER, mVBOIds[1] );
// Transfer data to the buffer
GLES31.glBufferData(GLES31.GL_ELEMENT_ARRAY_BUFFER, mDrawOrder.capacity() * mBytesPerShort,
mDrawOrder, GLES31.GL_STATIC_DRAW);
// Unbind buffer
GLES31.glBindBuffer(GLES31.GL_ELEMENT_ARRAY_BUFFER, 0);
// Bind the third buffer to put the colour data in
GLES31.glBindBuffer ( GLES31.GL_ELEMENT_ARRAY_BUFFER, mVBOIds[2] );
// Transfer the colour data to the buffer
GLES31.glBufferData(GLES31.GL_ARRAY_BUFFER, mCubeColors.capacity() * mBytesPerFloat,
mCubeColors, GLES31.GL_STATIC_DRAW);
// Unbind buffer
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
现在,我创建VAO并将其绑定到VBO:
// Generate VAO Id
GLES31.glGenVertexArrays ( 1, mVAOId, 0 );
// Bind the VAO and then setup the vertex
// attributes
GLES31.glBindVertexArray ( mVAOId[0] );
// Bind the vertex buffer
GLES31.glBindBuffer ( GLES31.GL_ARRAY_BUFFER, mVBOIds[0] );
// Bind the draw order buffer
GLES31.glBindBuffer ( GLES31.GL_ELEMENT_ARRAY_BUFFER, mVBOIds[1] );
GLES31.glEnableVertexAttribArray ( VERTEX_POS_INDEX );
GLES31.glEnableVertexAttribArray ( VERTEX_DRAW_ORDER_INDEX );
GLES31.glVertexAttribPointer ( VERTEX_POS_INDEX, VERTEX_POS_SIZE, GLES31.GL_FLOAT, false, VERTEX_STRIDE, 0 );
GLES31.glVertexAttribPointer ( VERTEX_DRAW_ORDER_INDEX, VERTEX_DRAW_ORDER_SIZE, GLES31.GL_SHORT, false, VERTEX_STRIDE, 0 );
// Now bind the colour buffer
GLES31.glBindBuffer ( GLES31.GL_ARRAY_BUFFER, mVBOIds[2] );
GLES31.glEnableVertexAttribArray(VERTEX_COLOUR_INDEX);
GLES31.glVertexAttribPointer(VERTEX_COLOUR_INDEX, VERTEX_COLOUR_SIZE, GLES31.GL_FLOAT, false, VERTEX_STRIDE, 0 );
// Reset to the default VAO
GLES31.glBindVertexArray ( 0 );
我的顶点和片段着色器看起来像:
private final String vertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
// have now added uMVP projection matrix for making square stuff square
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"void main() {" +
" gl_Position = uMVPMatrix * vPosition;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
我在另一个堆栈溢出问题中看到,您可能需要将VBO的属性号绑定到着色器变量,因此尝试了下面的行,但没有成功(VERTEX_COLOUR_INDEX
设置为“ 2”- glVertexAtttribPointer
绑定颜色缓冲区时。
GLES31.glBindAttribLocation(mProgram, 2, "vColor");
我已尽力做到这一点,但现在已经用尽了很多想法。
谢谢