我有关于glBufferData和glVertexAttribPointer的问题。出于某种原因,他们只是不能一起工作。这是代码:
float[] triangleArray = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
IntBuffer vacantNameBuffer = IntBuffer.allocate(2);
gl.glGenBuffers(1, vacantNameBuffer);
int bufferIndex = vacantNameBuffer.get();
FloatBuffer triangleVertexBuffer = Buffers.newDirectFloatBuffer(triangleArray);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, bufferIndex);
IntBuffer paramBuffer = IntBuffer.allocate(2);
gl.glGetBufferParameteriv(GL2.GL_ARRAY_BUFFER, GL2.GL_BUFFER_SIZE, paramBuffer);
System.out.println(paramBuffer.get());
int triangleBufferSize = triangleVertexBuffer.capacity() * Buffers.SIZEOF_FLOAT;
gl.glBufferData(
GL2.GL_ARRAY_BUFFER,
triangleBufferSize,
triangleVertexBuffer,
GL2.GL_STATIC_DRAW);
gl.glGetBufferParameteriv(GL2.GL_ARRAY_BUFFER, GL2.GL_BUFFER_SIZE, paramBuffer);
System.out.println(paramBuffer.get());
gl.glEnableVertexAttribArray(0);
gl.glVertexAttribPointer(
0,
3,
GL2.GL_FLOAT,
false,
0,
0);
gl.glDrawArrays(GL2.GL_TRIANGLES, 0, 3);
gl.glDisableVertexAttribArray(0);
然而,这完美地运作:
float[] triangleArray = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
FloatBuffer triangleVertexBuffer = Buffers.newDirectFloatBuffer(triangleArray);
/*
IntBuffer vacantNameBuffer = IntBuffer.allocate(2);
gl.glGenBuffers(1, vacantNameBuffer);
int bufferIndex = vacantNameBuffer.get();
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, bufferIndex);
IntBuffer paramBuffer = IntBuffer.allocate(2);
gl.glGetBufferParameteriv(GL2.GL_ARRAY_BUFFER, GL2.GL_BUFFER_SIZE, paramBuffer);
System.out.println(paramBuffer.get());
int triangleBufferSize = triangleVertexBuffer.capacity() * Buffers.SIZEOF_FLOAT;
gl.glBufferData(
GL2.GL_ARRAY_BUFFER,
triangleBufferSize,
triangleVertexBuffer,
GL2.GL_STATIC_DRAW);
gl.glGetBufferParameteriv(GL2.GL_ARRAY_BUFFER, GL2.GL_BUFFER_SIZE, paramBuffer);
System.out.println(paramBuffer.get());
*/
gl.glEnableVertexAttribArray(0);
gl.glVertexAttribPointer(
0,
3,
GL2.GL_FLOAT,
false,
0,
triangleVertexBuffer);
gl.glDrawArrays(GL2.GL_TRIANGLES, 0, 3);
gl.glDisableVertexAttribArray(0);
如果有人愿意指出为什么我的代码不起作用。我将非常感激。
答案 0 :(得分:1)
我发现了问题,这不是我的代码,但它有jogl的限制,需要使用gl着色器程序。 在这篇文章中找到答案:glDrawArrays() behaving weirdly on Mac OS X;