I have started learning OpenGL with LWJGL3 and some tutorials. Finally I have found the Anton's one on github and I started with most basic one: Hello Triangle
My problem is kinda similar to this one: Triangle not showing up
This is my problem. I create window with black background but triangle doesn't appear. I would be grateful for any info about my mistakes. Here's my code:
public class HelloWorld
{
public static void main(String[] args)
{
long window = NULL;
int vao, vbo;
final float[] points =
{
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
final String vertex_shader =
"#version 410\n" +
"in vec3 vp;" +
"void main () {" +
" gl_Position = vec4 (vp, 1.0);" +
"}";
final String fragment_shader =
"#version 410\n" +
"out vec4 frag_colour;" +
"void main () {" +
" frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);" +
"}";
int vs, fs, shader_programme;
if( glfwInit() == GL_FALSE )
{
System.err.println("Can't initialize glfw!");
return;
}
window = glfwCreateWindow (640, 480, "Hello Triangle", NULL, NULL);
if (window == 0) {
glfwTerminate();
return;
}
glfwMakeContextCurrent (window);
GL.createCapabilities();
glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LESS);
vbo = glGenBuffers();
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, FloatBuffer.wrap(points),
GL_STATIC_DRAW);
vao = glGenVertexArrays();
glBindVertexArray (vao);
glEnableVertexAttribArray (0);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer (0, 3, GL_FLOAT, false, 0, 0);
vs = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (vs, vertex_shader);
glCompileShader (vs);
fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (fs, fragment_shader);
glCompileShader (fs);
shader_programme = glCreateProgram ();
glAttachShader (shader_programme, fs);
glAttachShader (shader_programme, vs);
glLinkProgram (shader_programme);
while ( glfwWindowShouldClose(window) == GL_FALSE ) {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram (shader_programme);
glBindVertexArray (vao);
glDrawArrays (GL_TRIANGLES, 0, 3);
glfwPollEvents ();
glfwSwapBuffers (window);
}
glfwTerminate();
return;
}
}
So the solution of my problem was using
FloatBuffer buffer = BufferUtils.createFloatBuffer(points.length);
buffer.put(points);
buffer.rewind();
glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
instead of
glBufferData(GL_ARRAY_BUFFER, FloatBuffer.wrap(points),GL_STATIC_DRAW);
It works even without suggested change to shader (layout = 0). Thank you for all answers. I obtained solution posting the question on the LWJGL forum.