3
\$\begingroup\$

I'm playing around in LWJGL3 and I'm experiencing an issue regarding glDrawArrays.

At glDrawArrays the JVM crashes.

I'm using modern OpenGL and therefore I have my own shaders and matrix calculations. The pipeline I've programmed is quite complex so pasting all the code here isn't of any use.

Vertex shader:

#version 330 core

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

layout (location = 0) in vec3 in_position;

void main() {
    gl_Position = (projectionMatrix * viewMatrix * modelMatrix) * vec4(in_position, 1.0f);
}

Fragment shader:

#version 330 core

void main() {
    gl_FragColor = vec4(1.0f, 0.0, 1.0f, 1.0f);
}

I'm trying to draw 1 point to the screen using a VBO. Creation of the VBO:

float[] vboData = new float[]{0.0f, 0.0f, 0.0f};
FloatBuffer buffer = BufferUtils.createFloatBuffer(vboData);

vbo_id = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

And the rendering of the point using glDrawArrays:

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    testShader.bind();
        pipeline.applyMatrices(testShader);

        glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
            glVertexAttribPointer(0, 3, GL_FLOAT, false, 12, 0);
            glDrawArrays(GL_POINTS, 0, 1);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        pipeline.setCameraPosition(new Vector3f(0.0f, 0.0f, 0.0f), new Vector3f(0.0f, 0.0f, 0.0f));
    testShader.unbind();

    GLFWUtils.swapBuffers();

Using GLIntercept I managed to get a log of every OpenGL call made:

glClearColor(1.000000,0.000000,1.000000,1.000000)
glPointSize(10.000000)
glGenBuffers(1,000000001E0F6FC0)
glBindBuffer(GL_ARRAY_BUFFER,1)
glBufferData(GL_ARRAY_BUFFER,12,0000000025EB8D90,GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER,0)
glCreateProgram()=1 
glCreateShader(GL_VERTEX_SHADER)=2 
glCreateShader(GL_FRAGMENT_SHADER)=3 
glShaderSource(2,1,000000001E0F6FC0,000000001E0F6FC8)
glShaderSource(3,1,000000001E0F6FC0,000000001E0F6FC8)
glCompileShader(2)
glCompileShader(3)
glGetShaderiv(2,GL_COMPILE_STATUS,000000001E0F6FC0)
glGetShaderiv(3,GL_COMPILE_STATUS,000000001E0F6FC0)
glAttachShader(1,2)
glAttachShader(1,3)
glLinkProgram(1)
glEnableVertexAttribArray(0)
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
glUseProgram(1)
glGetUniformLocation(1,"projectionMatrix")=1 
glUniformMatrix4fv(1,1,false,[-0.671312,-0.000000,-0.000000,-0.000000,-0.000000,-0.895083,-0.000000,-0.000000,-0.000000,-0.000000,-1.000020,-1.000000,-0.000000,-0.000000,-0.020000,-0.000000])
glGetUniformLocation(1,"viewMatrix")=2 
glUniformMatrix4fv(2,1,false,[1.000000,0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,0.000000,1.000000])
glGetUniformLocation(1,"modelMatrix")=0 
glUniformMatrix4fv(0,1,false,[1.000000,0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,0.000000,1.000000])
glBindBuffer(GL_ARRAY_BUFFER,0)
glVertexAttribPointer(0,3,GL_FLOAT,false,12,0000000000000000)
glDrawArrays(GL_POINTS,0,1)

I've carefully tested a few things:

  • The shaders compile and link fine
  • The matrices calculated look correct (you can double check inside the GLIntercept log)
  • The VBO is correctly created and filled with the correct data
  • The attribute pointer ( = layout 1) is enabled
  • The matrices are correctly sent to the shader (this happens at pipeline.applyMatrices)

What am I missing?

Interesting debug finding: I'm running this on nVidia. On a Intel graphics CPU it runs, but nothing shows on the screen!

\$\endgroup\$
3
\$\begingroup\$

Your GLIntercept says glBindBuffer(GL_ARRAY_BUFFER,0) right before the call to glVertexAttribPointer. So you have no buffer bound to attirbute 0 which will cause a memory violation error causing JVM to crash. You should look into why vbo_id is 0.

\$\endgroup\$
  • \$\begingroup\$ The BufferUtils class you see there isn't Java's one, it's my own helper class which creates the FloatBuffer from a float array. \$\endgroup\$ – Basaa Dec 14 '15 at 9:17
  • \$\begingroup\$ Ah ok. Also BufferUtils exist in LWJGL. Could you expand your answer with more detailts on what is going wrong? Maybe include your BufferUtils class as well. \$\endgroup\$ – Christer Dec 14 '15 at 9:20
  • \$\begingroup\$ Hmm. I don't quite know what to add to my question. What goes wrong is that glDrawArrays crashes. No error codes after any of the OpenGL calls, and no other indication that anything is wrong. The BufferUtils class I created does the exact same thing as you posted. It creates a FloatBuffer, puts in the data and flips it. Please let me know what other information you would like. \$\endgroup\$ – Basaa Dec 14 '15 at 9:39
  • \$\begingroup\$ I tried running the exact same code on an Intel Graphics CPU and bizarrely enough it runs, but it's a blank screen! So there is a difference between the Nvidia drivers and Intel drivers that might be causing this issue. \$\endgroup\$ – Basaa Dec 14 '15 at 13:42
  • \$\begingroup\$ @Basaa Your problem seem to be elsewhere, I implemented most of your code and it's working for me, I also have an Nvidia GPU btw. I did not pass the matrices and such so there are some differences. I also copy-pasted the setupWindow() so it might be a bit too big. gist.github.com/chrjen/3a239f4dab47caea227d Here is the result imgur.com/pMZwwWj I don't know what your are doing so differently. But try my code and see if it's working. \$\endgroup\$ – Christer Dec 14 '15 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.