I am practically pretty new to OpenGL development and right now doing some research in Java with LWJGL. But it's really not a question about Java or C++ (GLFW, etc.). It is about OpenGL > 3 api.
Raw circumstances: I want to create a game (using LWJGL), that is kind of a tile based game, but right now i don't want to use textures. I just need colored squares. The squares can be of any size (depending on the zoom level). But they are just colored with one color, no texture.
What I need is defining objects that have size, position, and color (from a visual point of view).
I would like to have a class (sth. like Model
or VertexArray
or ...
) that I can feed with vertex data and an array of indices. The class itself provides a constructor
and a render
method that take care of all the OpenGL internals (eg. binding buffers, enable vertex attrib arrays, ... and finally doing the glDrawElements(GL_TRIANGLES, this.count, GL_UNSIGNED_BYTE, 0);
or similar).
Now I am a bit confused about Shaders (as I am a beginner and my needs are basic, it's just the Vertex and Fragment shaders).
From what I have read, seen and heard, I learned the following.
I can provide the shader program (vertex and fragment) a buffer containing information. With locations I can put in geometric data (x, y, z) as well as coloring info (r, g, b, a) and (I think) anything else I need. The vertex shader will consume the data it needs and pass thru the data it is not interested in (eg. color). The Fragment shader is the next element in OpenGL's rendering pipeline, which will get the color as an input, if defined in the shader program itself. So it will be able to color each pixel in the according color.
I can provide the shader program a buffer with just information about geometric positions. I don't want to put in colors into the buffer which is bound and consumed by the shader(s). I want to provide / change the color via uniforms.
For me it feels more natural to generate vertex array objects based on pure geometric data and change the color by setting uniforms with values that change while updating my game state.
Which one would be the preferred way of achieving this. Or am I on a wrong path?
While I am asking this, I feel that I don't understand the concepts of uniforms and even putting data into buffers, that are present to shaders like magic.
Edit: (thanks to @DMGregory) I just don't know what is the difference in performance or in developer-friendly-wise between defining the squares' colors via uniforms and via buffer data.
Is it just "Many roads lead to Rome" or ...?
glDrawTriangles
, a single draw call. This collecting, then drawing, is typically done every frame. You can cull if you want. \$\endgroup\$