【发布时间】:2017-07-18 12:38:05
【问题描述】:
我正在关注 Antonio Bejarano 的《使用 LWJGL 开发 3D 游戏》一书。我已经达到了能够在屏幕上渲染四边形的地步。实现顶点索引缓冲区后,只有一个四边形的三角形呈现到屏幕上,所以我认为可以安全地假设问题出在该代码上。
// Vertex Index Buffer
idxVboId = glGenBuffers();
vertsIdxBuffer = MemoryUtil.memAllocInt(indices.length);
vertsIdxBuffer.put(indices).flip();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,idxVboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,vertsIdxBuffer,GL_STATIC_DRAW);
顶点缓冲区和颜色缓冲区的代码有效,因为我删除了对索引缓冲区的引用并出现了完整的四边形。我还更改了渲染方法以考虑要渲染的新类型:
shaderProgram.bind();
//bind to vao
glBindVertexArray(mesh.getVaoId());
//enable positions buffer
glEnableVertexAttribArray(0);
//enable colour buffer
glEnableVertexAttribArray(1);
//draw verts
glDrawElements(GL_TRIANGLES,
mesh.getVertexCount(),GL_UNSIGNED_INT,0);
//restore state (?)
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindVertexArray(0);
shaderProgram.unbind();
感谢任何建议,因为我已经盯着这个看了好几个小时,并不能真正看出我在哪里犯了错误。
编辑:四网格代码
renderer.init();
float[] positions = new float[]{
-0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
};
float[] colours = new float[]{0.5f,0f,0f,0f,0.5f,0f,0f,0f,0.5f,0f,0.5f,0.5f};
int[] indices = new int[]{
0, 1, 3, 3, 1, 2,
};
mesh = new BMesh(positions,colours,indices);
第二次编辑 前后位置数组变化
【问题讨论】:
-
可能是因为背面剔除?您是否启用了
GL_CULL_FACE,您也设置了哪个约定? (见khronos.org/opengl/wiki/Face_Culling)。此外,请显示您声明四边形网格数据的位置和方式。 -
感谢您的回复,我现在要研究剔除,我已经添加了形成四边形网格的代码