Search code examples
openglglsllwjglopengl-3

LWJGL 3 Shaders version 150 and above rendering nothing


I'm using LWJGL 3 on OSX. The shaders work fine when using a version <150 but porting the code to 330 nothing renders.

My shaders are as simple as possible:

vertex shader:

#version 330 core

in vec3 position;

void main(void) {
  gl_Position = vec4(position, 1.0);
}

fragment shader:

#version 330 core

out vec4 outColour;

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

I create a simple triangle like this (Scala):

val vertices = Array(
   0.0f,  0.5f,  0.0f,
  -0.5f, -0.5f,  0.0f,
   0.5f, -0.5f,  0.0f
)
val vertexBuffer = BufferUtils.createFloatBuffer(vertices.length)
vertexBuffer.put(vertices)
vertexBuffer.flip()

val buffer = GL15.glGenBuffers()
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer, GL15.GL_STATIC_DRAW)

and I draw it like this:

GL20.glUseProgram(shader)
GL20.glEnableVertexAttribArray(0)

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer)
GL20.glBindAttribLocation(shader, 0, "position")
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0)
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 9)

GL20.glDisableVertexAttribArray(0)
GL20.glUseProgram(0)

The shaders compile fine and the program runs but I just get a blank screen! Is there anything obviously wrong with my code?


Solution

  • Vertex Array Objects (VAOs) are required for rendering in a Core context. In Compatibility contexts they're optional.

    However, you can just generate one at startup and leave it bound if you're feeling lazy :)