Need help with OpenGL rendering

Hi,

I’m right now trying just to render a simple rectangle using OpenGL, however, when calling glDrawElements(), an exception gets thrown. My code is written in Java using LWJGL.

Blockquote

OpenGL Error:
Source: 33350
Type: 33361
ID: 131185
Severity: 33387
Message: Buffer detailed info: Buffer object 1 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) will use VIDEO memory as the source for buffer object operations.

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:Main.main()’.
    Process ‘command ‘C:\Users\niklas.jdks\azul-1.8.0_382\bin\java.exe’’ finished with non-zero exit value 1

I’ve checked my code multiple times but couldn’t find my mistake.

My code is built like this:

public static void main(String[] args)
{
    String vertexSource = "#version 330 core\n" +
            "layout (location = 0) in vec3 aPos;\n" +
            "void main()\n" +
            "{\n" +
            "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" +
            "}\0";
    String fragmentSource = "#version 330 core\n" +
            "out vec4 FragColor;\n" +
            "void main()\n" +
            "{\n" +
            "   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" +
            "}\n\0";

    float[] vertices = {
            0.5f,  0.5f, 0.0f,  // top right
            0.5f, -0.5f, 0.0f,  // bottom right
            -0.5f, -0.5f, 0.0f,  // bottom left
            -0.5f,  0.5f, 0.0f   // top left
    };
    int[] indices = {  // note that we start from 0!
        0, 1, 3,  // first Triangle
        1, 2, 3   // second Triangle
    };

    RenderObject r = new RenderObject(vertices, indices, vertexSource, fragmentSource);

    // Initializing GLFW/OpenGL
    while(!glfwWindowShouldClose(window)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        r.draw();

        glfwPollEvents();
        glfwSwapBuffers(window);
    }
}

Here’s the RenderObject class:

package org.graphics;

import static org.lwjgl.opengl.GL33.*;


public class RenderObject
{
    public ShaderProgram shaderProgram;
    public VAO vao;


    public RenderObject()
    {
        shaderProgram = new ShaderProgram();
        vao = new VAO();
    }
    public RenderObject(float[] vertices, int[] indices, String vertexSource, String fragmentSource)
    {
        shaderProgram = new ShaderProgram(vertexSource, fragmentSource);
        vao = new VAO(vertices, indices);
    }

    public void draw()
    {
        shaderProgram.activate();
        vao.bind();

        System.out.println(vao.ebo.indexCount());

        glDrawElements(GL_TRIANGLES, vao.ebo.indexCount(), GL_UNSIGNED_INT, 0);
    }

    public class ShaderProgram
    {
        int shaderProgram;

        VertexShader vertexShader;
        FragmentShader fragmentShader;

        public ShaderProgram()
        {
            shaderProgram = glCreateProgram();
        }

        public ShaderProgram(String vertexSource, String fragmentSource)
        {
            this();

            vertexShader = new VertexShader(vertexSource);
            fragmentShader = new FragmentShader(fragmentSource);

            link(vertexShader, fragmentShader);
        }

        public void link(VertexShader vertexShader, FragmentShader fragmentShader)
        {
            glAttachShader(shaderProgram, vertexShader.id());
            glAttachShader(shaderProgram, fragmentShader.id());

            glLinkProgram(shaderProgram);

            // Error checking
            if(glGetProgrami(shaderProgram, GL_LINK_STATUS ) == GL_FALSE)
            {
                String info = glGetProgramInfoLog(shaderProgram);
                throw new RuntimeException("Failed to link shader!\n" +
                        "Vertex shader compilation: " + glGetShaderInfoLog(vertexShader.id()) + "\n" +
                        "Fragment shader compilation: " + glGetShaderInfoLog(fragmentShader.id()) + "\n" +
                        "Program linking: " + glGetProgramInfoLog(shaderProgram));
            }

            vertexShader.delete();
            fragmentShader.delete();
        }

        public void activate()
        {
            glUseProgram(shaderProgram);
        }

        public void delete()
        {
            glDeleteProgram(shaderProgram);
            shaderProgram = 0;
        }

        public int id()
        {
            return shaderProgram;
        }

        @Override
        protected void finalize()
        {
            if(shaderProgram != 0)
            {
                System.out.println("WARNING: VAO not deleted properly. deleting...");
                delete();
            }
        }

        public class VertexShader
        {
            int vertexShader;

            public VertexShader()
            {
                vertexShader = glCreateShader(GL_VERTEX_SHADER);
            }

            public VertexShader(String source)
            {
                this();
                compileShader(source);
            }

            public void compileShader(String source)
            {
                glShaderSource(vertexShader, source);
                glCompileShader(vertexShader);

                // Error checking
                if(glGetShaderi(vertexShader, GL_COMPILE_STATUS ) == GL_FALSE)
                {
                    throw new RuntimeException("Failed to compile vertex shader:\n" + glGetShaderInfoLog(vertexShader));
                }
            }

            public void delete()
            {
                glDeleteShader(vertexShader);
                vertexShader = 0;
            }

            public int id()
            {
                return vertexShader;
            }

            @Override
            protected void finalize()
            {
                if(vertexShader != 0)
                {
                    System.out.println("WARNING: vertex shader not deleted properly. deleting...");
                    delete();
                }
            }
        }

        public class FragmentShader
        {
            int fragmentShader;

            public FragmentShader()
            {
                fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
            }

            public FragmentShader(String source)
            {
                this();
                compileShader(source);
            }

            public void compileShader(String source)
            {
                glShaderSource(fragmentShader, source);
                glCompileShader(fragmentShader);

                // Error checking
                if(glGetShaderi(fragmentShader, GL_COMPILE_STATUS ) == GL_FALSE)
                {
                    throw new RuntimeException("Failed to compile fragment shader:\n" + glGetShaderInfoLog(fragmentShader));
                }
            }

            public void delete()
            {
                glDeleteShader(fragmentShader);
                fragmentShader = 0;
            }

            public int id()
            {
                return fragmentShader;
            }

            @Override
            protected void finalize()
            {
                if(fragmentShader != 0)
                {
                    System.out.println("WARNING: fragment shader not deleted properly. deleting...");
                    delete();
                }
            }
        }
    }

    public class VAO
    {
        private int VAO;

        public VBO vbo;
        public EBO ebo;

        public VAO()
        {
            VAO = glGenVertexArrays();
            bind();

            vbo = new VBO();  // Initialize vbo
            ebo = new EBO();  // Initialize ebo
        }

        public VAO(float[] vertices, int[] indices)
        {
            //this();

            VAO = glGenVertexArrays();
            bind();

            vbo = new VBO(vertices);  // Set vertices for vbo
            ebo = new EBO(indices);     // Set indices for ebo

            linkAttributes();
            unbind();
        }

        public void bind()
        {
            glBindVertexArray(VAO);
        }

        public void unbind()
        {
            glBindVertexArray(0);
        }

        public void linkAttributes()
        {
            glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
            glBindBuffer(GL_ARRAY_BUFFER, 0);
        }

        public void delete()
        {
            glDeleteVertexArrays(VAO);
            VAO = 0;
        }

        public int id()
        {
            return VAO;
        }

        @Override
        protected void finalize()
        {
            if(VAO != 0)
            {
                System.out.println("WARNING: VAO not deleted properly. deleting...");
                delete();
            }
        }

        public class VBO
        {
            private int VBO;

            private float vertexCount;

            public VBO()
            {
                VBO = glGenBuffers();
            }

            public VBO(float[] vertices)
            {
                this();
                setVertices(vertices);
            }

            public void bind()
            {
                glBindBuffer(GL_ARRAY_BUFFER, VBO);
            }

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

            public void setVertices(float[] vertices)
            {
                bind();
                glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
                vertexCount = vertices.length;
                unbind();
            }

            public void delete()
            {
                glDeleteBuffers(VBO);
                VBO = 0;
            }

            public float vertexCount()
            {
                return vertexCount;
            }

            public int id()
            {
                return VBO;
            }

            @Override
            protected void finalize()
            {
                if(VBO != 0)
                {
                    System.out.println("WARNING: VBO not deleted properly. deleting...");
                    delete();
                }
            }

        }
        public class EBO
        {
            private int EBO;

            public int indexCount;

            public EBO()
            {
                EBO = glGenBuffers();
            }

            public EBO(int[] indices)
            {
                this();
                setIndices(indices);
            }

            public void bind()
            {
                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
            }

            public void unbind()
            {
                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
            }

            public void setIndices(int[] indices)
            {
                bind();
                glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW);
                indexCount = indices.length;
                unbind();
            }

            public void delete()
            {
                glDeleteBuffers(EBO);
                EBO = 0;
            }

            public int indexCount()
            {
                return indexCount;
            }

            public int id()
            {
                return EBO;
            }

            @Override
            protected void finalize()
            {
                if(EBO != 0)
                {
                    System.out.println("WARNING: EBO not deleted properly. deleting...");
                    delete();
                }
            }
        }
    }
}