Closed PhoenixXKN closed 4 years ago
You're doing a relative put on the FloatBuffer without flipping it back, so it's going to be 0 bytes long.
You have multiple issues in your code. Let's dissect the following:
glBindVertexArray(VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, Float.SIZE * 9, GL_FLOAT, false, 12, b);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindVertexArray(0);
VBO
generated via glGenBuffers
as a VAO via glBindVertexArray
. This is wrong. You should bind VAO
(generated via glGenVertexArrays()
) via glBindVertexArray()
.GL_ARRAY_BUFFER
but you never upload any data to it.glVertexAttribPointer()
by supplying the Java NIO ByteBuffer as the last argument. This is also wrong, because it will call the underlying glVertexAttribPointer()
OpenGL function with the address of the Java NIO ByteBuffer. This overload is meant for client-side arrays, which you are not using. You use server-side buffer objects, however because you have a buffer object bound to GL_ARRAY_BUFFER, this address will be treated as an offset into that buffer object (which has no data uploaded to it).glVertexAttribPointer()
is not the number of bytes! (which by the way is also not what you supply. Please read what Float.SIZE
actually means!)In general: Please read the OpenGL reference documentation of all functions you are calling!
So, I looked at the code again and it was a combination of typos and my attempt to translate C++ Code into Java(The reason i used Float.SIZE, for example). I also somehow managed to overlook the call to glBufferData, which i added back into the code. After the direct translation from C++ didn't work, i played around with a lot of values and changed them to whatever might've worked... The original glVertexAttribPointer() call was glVertexAttribPointer(0, 3, GL_FLOAT, false, 12, 0);
, which i changed it back to, but it still crashes. I know this sounds like a stupid question, but what do I need to change? I:
glBindVertexArray(VBO);
to glBindVertexArray(VAO)
;
2.added a glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
3.Restored the original glVertexAttribPointer(0, 3, GL_FLOAT, false, 12, 0);
But it still keeps crashing. I looked at the OpenGL documentation for glVertexAttribPointer but i didn't see anything wrong with the form it has now. I am fairly new to OpenGL and can understand if you deem this thread the wrong place to ask such questions, in that case if you could recommend another useful resource, i would be very thankful.Stackoverflow is generally preferred for "why does my code not work?" kind of questions. I highly recommend you pose your question there and you will definitely get an answer quickly.
@httpdigest Thanks, i'll be sure to ask there from now on.
Environment
Description
So, i played around a bit and wanted to draw a simple triangle using org.lwjgl.opengl.GL33C (The version of OpenGL that was used in the book i use to learn OpenGL). But since the book is based on C++, there have been a few complications. I could solve most of them, but one keeps staying: The JVM crashes when i call either glVertexAttribPointer or glDrawArrays. Here's my code(it's a bit messy, i playes around with different things trying to solve the issue):
The Referenced ShaderLoader is just a helper class that reads shader files, and i can confirm that the issue is not caused by it.
What am i doing wrong here?