Search code examples
javaopenglrenderinglwjgl

Java rendering LWJGL 3.0.0


In this code i try to render a simple triangle on the screen, but it only shows the red background, i have tried every thing i know to debug but i just can't understand why it doesn't work

Hope somebody can help me, thanks in advance.

public float vertices[] = new float[]
{
        -0.5f,-0.5f,0.0f,1.0f,

        0.0f,1.0f,0.0f,1.0f,

        0.0f,0.5f,0.0f,1.0f,

        0.0f,1.0f,0.0f,1.0f,

        0.5f,-0.5f,0.0f,1.0f,

        0.0f,1.0f,0.0f,1.0f
};



public TEST()
{

}


public int start() throws IOException
{
    glfwInit();

    long window = glfwCreateWindow(1000, 1000, "HAHA", NULL, NULL);
    glfwMakeContextCurrent(window);
    glfwShowWindow(window);
    GLFWKeyCallback keycallback = new GLFWKeyCallback()
    {

        @Override
        public void invoke(long window, int key, int scancode, int action, int mods)
        {
            if(key==GLFW_KEY_ESCAPE&&action==GLFW_PRESS)
            {
                glfwSetWindowShouldClose(window, GLFW_TRUE);
            }
        }
    };
    glfwSetKeyCallback(window,keycallback );
    GLFWErrorCallback errorcallback = new GLFWErrorCallback()
    {

        @Override
        public void invoke(int error, long description)
        {
            System.out.println("ERROR CODE: " + error + "  ERROR DESCRITPION: "+description);
        }
    };
    glfwSetErrorCallback(errorcallback);


    GL.createCapabilities();

    glViewport(0, 0, 1000, 1000);
    //////////////////////////
    int VAO = glGenVertexArrays();
    int VBO = glGenBuffers();
    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    FloatBuffer buffer = FloatBuffer.allocate(vertices.length);
    buffer.clear();
    buffer.put(vertices);
    buffer.flip();
    glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 4, GL_FLOAT, false, 8, 0);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 4, GL_FLOAT, false, 8, 4);
    glEnableVertexAttribArray(1);

    buffer.clear();
    buffer=null;
    System.gc();

    glBindVertexArray(0);
    ///////////////////////
    ///////////////////////
    LoadFile loader = new LoadFile("res/shader.vs", "res/shader.frag");

    int vertexshader = glCreateShader(GL_VERTEX_SHADER);
    int fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(vertexshader, loader.readVertex());
    glShaderSource(fragmentshader, loader.readFragment());

    glCompileShader(vertexshader);
    glCompileShader(fragmentshader);

    System.out.println(glGetShaderInfoLog(vertexshader));
    System.out.println(glGetShaderInfoLog(fragmentshader));


    int program = glCreateProgram();

    glAttachShader(program, vertexshader);
    glAttachShader(program, fragmentshader);

    glLinkProgram(program);

    glDeleteShader(vertexshader);
    glDeleteShader(fragmentshader);

    System.out.println(glGetProgramInfoLog(program));
    ///////////////////////////

    boolean running=true;
    while(running&&glfwWindowShouldClose(window)==GLFW_FALSE)
    {
        glfwPollEvents();
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        glClearColor(1.0f, 0.0f, 0.0f, 1.0f);

        glUseProgram(program);

        glBindVertexArray(VAO);

        glDrawArrays(GL_TRIANGLES, 0, 3);

        glBindVertexArray(0);

        glUseProgram(0);

        glfwSwapInterval(1);
        glfwSwapBuffers(window);
    }

    System.out.println(glGetError());
    glfwTerminate();

    return 0;
}

My vertex shader

#version 330 core

layout (location = 0) in vec4 position;
layout (location = 1) in vec4 colorin;

out vec4 colorout;

void main()
{
    gl_Position = position;
    colorout=colorin;
}

My fragment shader

#version 330 core

out vec4 color;

in vec4 colorout;

void main()
{
    color = colorout;
}

Solution

  • EDIT: I just saw that this was 1 day old question.. but oh well..

    I have my suspicion here:

    FloatBuffer buffer = FloatBuffer.allocate(vertices.length);
    buffer.clear();
    buffer.put(vertices);
    buffer.flip();
    glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
    

    Have you tried replacing this with BufferUtils from lwjgl library itself? It would turn out to be something like this:

    FloatBuffer buff = BufferUtils.createFloatBuffer(vertices.length);
    buff.put(vertices);
    buff.flip();
    

    Or, if you want the manual one..

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * Float.BYTES);
    byteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
    floatBuffer.put(vertices);
    floatBuffer.flip();