Search code examples
javaopenglgraphicslwjgl

LWJGL3 OpenGL Triangle wont draw


Hello I have been trying to figure out why this triangle will not draw! Here is my code. It is just basic LWJGL OpenGL code using VBO's and VAO's and i can not figure out why it is not drawing. I have confirmed that the shaders are compiling properly

//bindings available for use.
    GL.createCapabilities();

    // Set the clear color
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    float vertices[] = {
             0.0f,  0.5f, // Vertex 1 (X, Y)
             0.5f, -0.5f, // Vertex 2 (X, Y)
            -0.5f, -0.5f  // Vertex 3 (X, Y)
        };
    int vbo;
    vbo = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    FloatBuffer vboBuff = BufferUtils.createFloatBuffer(vertices.length);
    vboBuff.put(vertices);
    vboBuff.flip();
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vboBuff, GL15.GL_STATIC_DRAW);
    int vertShade = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertShade,Reader.read("shaders/main.frag"));
    glCompileShader(vertShade);
    int fragShade = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragShade,Reader.read("shaders/main.frag"));
    glCompileShader(fragShade);
    String failed = glGetShaderInfoLog(fragShade);
    System.err.println(failed);
    int shaderprogram = glCreateProgram();
    glAttachShader(shaderprogram,vertShade);
    glAttachShader(shaderprogram,fragShade);
    glLinkProgram(shaderprogram);
    glUseProgram(shaderprogram);
    int posAttrib = glGetAttribLocation(shaderprogram,"position");
    glVertexAttribPointer(posAttrib,2,GL_FLOAT,false,0,0);
    glEnableVertexAttribArray(posAttrib);
    int vao;
    vao = glGenVertexArrays();
    // Run the rendering loop until the user has attempted to close
    // the window or has pressed the ESCAPE key.
    while ( glfwWindowShouldClose(window) == GL_FALSE ) {;
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
     GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);

        glfwSwapBuffers(window); // swap the color buffers

        // Poll for window events. The key callback above will only be
        // invoked during this call.
        glfwPollEvents();
    }

SHADERS:///THESE ARE IN SEPARATE FILES Fragment shader:

#version 150

out vec4 outColor;

void main()
{
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}

Vertex

#version 150

in vec2 position;

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

EDIT the last few lines before the while loop now look like this:

    int shaderprogram = glCreateProgram();
    glAttachShader(shaderprogram,vertShade);
    glAttachShader(shaderprogram,fragShade);
    glLinkProgram(shaderprogram);
    glUseProgram(shaderprogram);
    int vao;
    vao = glGenVertexArrays();
    glBindVertexArray(vbo);
    int posAttrib = glGetAttribLocation(shaderprogram,"position");
    glVertexAttribPointer(posAttrib,2,GL_FLOAT,false,0,0);
    glEnableVertexAttribArray(posAttrib);

Solution

  • Looking at your own code should make the problem fairly obvious:

    int vertShade = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertShade,Reader.read("shaders/main.frag"));
    glCompileShader(vertShade);
    int fragShade = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragShade,Reader.read("shaders/main.frag"));
    glCompileShader(fragShade);
    

    Since you use the same file name when reading both shaders, you're using the fragment shader code for the vertex shader, and never load the vertex shader code.

    If you fix the file name, things should start to work much better. Make sure that you always check the compile and link status after building shaders during development. Then you will be able to recognize these types of issues quickly.