openGL not rendering VAOs

Started by Rotten_potato, March 08, 2021, 04:55:55

Previous topic - Next topic

Rotten_potato

I'm attempting to write my first game engine with lwjgl and i have a class to easily create new VAOs for different shapes, i tried to implement index buffer rendering and it stopped working, i decided to temporarily delete the index buffer code but still nothing is being rendered to the screen. Everything else is working fine.

public class GraphicsObject {
    private int vaoID,vboID,stride,offset;
    public int vertexCount;

    public GraphicsObject(){
        vaoID = glGenVertexArrays();
        vboID = glGenBuffers();
    }

    public void bind(){
        glBindVertexArray(vaoID);
        glBindBuffer(GL_ARRAY_BUFFER,vboID);
        System.out.println(vboID);
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);
    }

    public void unbind(){
        glBindVertexArray(0);
        glBindBuffer(GL_ARRAY_BUFFER,0);
        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);
    }

    public void setVertexPositions(float[] vertexPositions){
        vertexCount = vertexPositions.length/3;
        stride = vertexPositions.length * 4;
        offset = glGetBufferParameteri(GL_ARRAY_BUFFER,GL_BUFFER_SIZE);

        glBindVertexArray(vaoID);
        glBindBuffer(GL_ARRAY_BUFFER,vboID);

        glBufferData(GL_ARRAY_BUFFER, ArrayUtils.convertToBuffer(vertexPositions),GL_STATIC_DRAW);
        glVertexAttribPointer(0,3,GL_FLOAT,false,stride,offset);
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER,0);

    }
That's the code to create new VAOs, here is the code to render them:
public class Renderer {

    //Temporary code, just for testing purposes.

    private static GraphicsObject polygon;

    public static void init(){
        polygon = new GraphicsObject();
    }

    public static void setColor(float r, float g , float b){
        glColor3f(r,g,b);
    }

    public static void drawPolygon(float[] vertices){
        polygon.setVertexPositions(vertices);
        polygon.bind();
        glDrawArrays(GL_POLYGON,0, polygon.vertexCount);
        polygon.unbind();
    }

}
and lastly here is the code for making the window:
public class Window {

    private long window;
    private int width,height;

    float[] vertices;
    String title;

    public Window(int width, int height, String title){ //creates the window and displays it

        this.height = height;
        this.width = width;
        this.title = title;


        if(!glfwInit())
        {
            return;
        }

        vertices = new float[]{
                -0.5f, 0.5f, 1.0f,
                -0.5f,-0.5f,1.0f,
                0.5f,-0.5f,1.0f,
                //Top triangle

                0.5f,-0.5f,1.0f,
                0.5f,0.5f,1.0f,
                -0.5f,0.5f,1.0f
                //Bottom triangle

        };


        window = glfwCreateWindow(this.width,this.height,this.title,0,0);

        if(window==0)
        {
            return;
        }

        glfwMakeContextCurrent(window);
        GL.createCapabilities();
        glfwShowWindow(window);

        Renderer.init();

        update();
        destroy();
    }

    private void update(){ //updates the window

        while(!glfwWindowShouldClose(window))
        {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glfwPollEvents();

            Renderer.setColor(1.0f,0.0f,0.0f);
            Renderer.drawPolygon(vertices);

            glGetError();

            glfwSwapBuffers(window);
        }

    }

    private void destroy(){ //destroys the window and terminates glfw

        glfwFreeCallbacks(window);
        glfwDestroyWindow(window);

        glfwTerminate();
        glfwSetErrorCallback(null);
    }

    public void createInputListeners(){
        glfwSetKeyCallback(window, new Input());
    }
}
I don't believe the problem is in the GraphicsObject class as i rolled it back to how it was before it stopped working thanks to a screenshot i had of it. However i'm not so sure about the renderer code.