0
\$\begingroup\$

I am trying to draw a triangle using shaders in LWJGL, but nothing is being drawn on the screen, and no error is being produces. I can't figure out what I'm doing wrong.

To create a vao, I use:

int buffer = glGenBuffers();
int vertexArray = glGenVertexArrays();

ByteBuffer data = ByteBuffer.allocateDirect(6 * 8).order(ByteOrder.nativeOrder());
data.putFloat(-0.5f);
data.putFloat(0.5f);

data.putFloat(-0.5f);
data.putFloat(-0.5f);

data.putFloat(0.5f);
data.putFloat(-0.5f);

glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, data, GL_STATIC_DRAW);

glBindVertexArray(vertexArray);
int positionAttributeLocation = glGetAttribLocation(program, "position");
glEnableVertexAttribArray(positionAttributeLocation);
glVertexAttribPointer(positionAttributeLocation, 2, GL_FLOAT, false, 8, 0);

and then I draw using:

glUseProgram(program);
glDrawArrays(GL_TRIANGLES, 0, 3);

Here's my vertex shader:

#version 110

in vec2 position;

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

and fragment shader:

#version 110

void main(void) {
  gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

I finally figured it out. The ByteBuffer needs to be reset to position 0 before passing it to glBufferData, by using data.position(0)...

\$\endgroup\$
0
\$\begingroup\$
gl_Position = vec4(position.xy, 1, 1.0);

This is drawing with a Z of 1, which, depending on how you have your projection matrix set up, is almost certainly not what you want. Try using a Z of 0 instead:

gl_Position = vec4(position.xy, 0, 1.0);

Also cross-check this with how you create your depth buffer, your current depth write and test state, and how you're clearing it each frame.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .