Undestanding OpenGL fundamentals

Started by Dzentsetsu, August 24, 2018, 19:20:40

Previous topic - Next topic

Dzentsetsu

Greetings fellow OpenGL enthusiasts!

        First of all I use LWJGL 2, so it's Java code below because of ThinMatrix tuts.
        Second. I want to say that I googled every single question I will be asking in this post. The main problem is that answers differ from each other so I can't build a concrete undestanding of things that is happening in my code below.
        Third. I just started learning OpenGL and I'm also not at all experiences in Java. So if you have any suggestion besides explanations of my question feel free to criticize/advise.


        Now let's begin.



public class Loader {
public void createVAO(int[] indices) {
    int vaoID = glGenVertexArrays();

    glBindVertexArray(vaoID);
    createIndicesVBO(indices);
    glEnableVertexAttribArray(0);
}

public void createVBO(float[] vertices) {
    int vboID = glGenBuffers(); 
    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    FloatBuffer buffer = makeByteBufferFromArrayOfFloats(vertices);
    glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0,0);
}

public void createIndicesVBO(int[] indices) {
    int vboID = glGenBuffers();
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
    IntBuffer buffer = makeByteBufferFromArrayOfInts(indices);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
}

public void pleaseDraw(float[] vertices, int[] indices) {
    glClearColor(0, 0, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawElements(GL_TRIANGLES, indices.length, GL_UNSIGNED_INT,0);

}
public void cleanUp() {
    glDeleteBuffers(0);
    glDeleteVertexArrays(0);
}
public FloatBuffer makeByteBufferFromArrayOfFloats(float[] vertices) {

    FloatBuffer FloatByteBuffer = BufferUtils.createFloatBuffer(vertices.length);
    FloatByteBuffer.put(vertices);
    FloatByteBuffer.flip();
    return FloatByteBuffer;
}

public IntBuffer makeByteBufferFromArrayOfInts(int[] array) {

    IntBuffer IntByteBuffer = BufferUtils.createIntBuffer(array.length);
    IntByteBuffer.put(array);
    IntByteBuffer.flip();
    return IntByteBuffer;
}
}


Note - This code is working and outputs a blue window with a black quad in the middle. Questions:

glBindBuffer(GL_ARRAY_BUFFER, vboID);
I don't understand clearly what does GL_Array Buffer even means. As far as I can get it just shows to OpenGL current state that this VBO will store Array data at some point. But from the function below I can guess that it is not just a type of data but it is like a container for a data and this is only that can be active for a current state. Am I getting it right? Or am I missing something?

glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
Again this GL_Array_Thing. And it seems this funtion puts a data into GPU or It puts current VBO into VAO? I'm not sure.
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0,0);
So VAO consist of attributes and here we show to OpenGL how to read our 0 attrib. Correct me if I'm wrong.

If I got how attribs in VAO works correctly I assume I can change this lines
glEnableVertexAttribArray(0);
and
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0,0);
to
glEnableVertexAttribArray(1);
and
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0,0);
. Nothing should change in final render, but in reality black quad is not appearing. Can't figure out why.

I use indices to Draw a quad. But how in the world does OpenGL understand how to read the data I provide, because I did not specify how to use it. I only imagine this line help me to do it
glDrawElements(GL_TRIANGLES, indices.length, GL_UNSIGNED_INT,0);

There are a lot of extra questions but they either appears because of my lack of understanding about questions that I described above. Please help me to make out this mess in my head about fundamentals of OpenGL. For those who were able to read or even answer to this post - Thanks a lot!

KaiHH


Dzentsetsu

I actually just accept the answer to close the post there. It sure makes some kind of clarification to me, but not fully explains what is going on

mudlee

You should go to learnopengl.com and go through those articles. It will help you a lot.

Andrew Alfazy

Hi,
Advise : use the last lwjgl release cuz it's much better and again opengl is same every where (you can use any code from TM tutorials starts with gl or shaders code) you will just need to learn how to use GLFW it's really ez.
about GL_Array_Buffer it's let gpu understand what this buffer have/will do look in this link https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glBufferData.xhtml you may understand why we need GL_Array_thing and (glBufferData stores the data).

about glVertexAttribPointer and glEnableVertexAttribArray I'm not sure why it's not working I *guess* the default index of the vertices is 0. but you can change it when you use shaders and work with any number you want.

about rendering glDrawElements tells the gpu to render with the current VAO you can change it by glBindVertexArray(vaoID) and you don't need glVertexAttribPointer every frame call it once when you put vbo into vao. but you need to call glEnableVertexAttribArray before glDrawElements and glDisableVertexAttribArray after (I'm not sure why). you can msg me on discord : AndrewAlfazy#8927