Page 1 of 3

SGL (Simple GL)

Posted: Sat Jun 03, 2023 10:55 pm
by luis
SGL (Simple GL)
A module written on top of GLFW to assist with OpenGL programming in PureBasic.

The idea is to have a sort of GLFW but easily usable in PB (offering keyboard and mouse input, windowing, etc) while adding something missing in the base GLFW (easier debugging, shaders loading/execution/error handling, easier texture loading, easier implementation of font rendering, some system info).

Windows and Linux.
Supports legacy and modern OpenGL up to version 4.6.

SGL is included in my Gamedev repository.

Download


Image

Image

Image

Re: SGL (Simple GL)

Posted: Sun Jun 04, 2023 7:22 pm
by Saboteur
Tested on linux with an integrated card and works great. Someone, please, make a game engine xD
¿Anyway, is possible to detect glfw library installed? I had to hardcoded the library path.

Re: SGL (Simple GL)

Posted: Sun Jun 04, 2023 7:48 pm
by luis
Saboteur wrote: Sun Jun 04, 2023 7:22 pm Tested on linux with an integrated card and works great.
Hi, thanks for letting me know.
Saboteur wrote: Sun Jun 04, 2023 7:22 pm Someone, please, make a game engine xD
Speaking for myself that's the idea, but I do this to learn this stuff so who knows what will happen and when. :wink:
Saboteur wrote: Sun Jun 04, 2023 7:22 pm ¿Anyway, is possible to detect glfw library installed? I had to hardcoded the library path.
I'm not sure I understand what you mean ... SGL is buit on top of GLFW 3.3.8 and you can't switch it to another version.
If you run the programs from the IDE, all should be working automagically and dynamically link to sgl/glfw/lib/glfw3.x64.so.
If you compile a binary and try to run that, the same library must be copied to the same directory of the executable, or under ./lib or ./bin (under the directory of the executable).

BTW, could you run ./examples/007 Sysinfo.pb and send me its output using a private message ?
I would like to see the capabilities of the graphic cards other people have to guide future development.

To anyone else trying this stuff, please to do the same if you can.

Thanks!

Re: SGL (Simple GL)

Posted: Sun Jun 04, 2023 8:55 pm
by Saboteur
luis wrote: Sun Jun 04, 2023 7:48 pm I'm not sure I understand what you mean ... SGL is buit on top of GLFW 3.3.8 and you can't switch it to another version.
If you run the programs from the IDE, all should be working automagically and dynamically link to sgl/glfw/lib/glfw3.x64.so.
If you compile a binary and try to run that, the same library must be copied to the same directory of the executable, or under ./lib or ./bin (under the directory of the executable).
Ah ok, that have sense and avoid versions problems.
I'll send you the sysinfo output.

Re: SGL (Simple GL)

Posted: Tue Jun 06, 2023 6:57 pm
by ricardo_sdl
Nice job!

Re: SGL (Simple GL)

Posted: Sat Jun 10, 2023 11:50 pm
by luis
ricardo_sdl wrote: Tue Jun 06, 2023 6:57 pm Nice job!
Thanks !

Coming next: an example of how to make a batch renderer for quads (useful for text, gui, sprites, etc.)

Working on that one right now.

Re: SGL (Simple GL)

Posted: Sun Jun 11, 2023 6:24 pm
by luis
Added a batch renderer for quads under "extras".

Short version:
This batch renderer stores the quads geometry you build up while rendering your frame and then draws it with a (potentially) single draw call.

Longer version:
Every time you have do draw a quad, you send the vertex data to the batch renderer instead of issuing a drawing call.
The renderer is dimensioned at the start, when you specify how much space you want to reserve in your memory buffer for your quads. The renderer stores the vertex data, the colors (including alpha for transparency) and the OpenGL texture ID required by the quad (if the quad is textured).
Every time a new texture ID is seen by the renderer, it verifies if it has already reserved a texture unit for it.
If the answer if yes it simply puts the texture unit number in the vertex data buffer.
If the texture is a new one it reserves a new texture unit for it and decrements the number of available texture units. and then puts it in the vertex data buffer.
While you pump your data into it, the renderer limits may be exceeded.
For example you dimensioned it for 1000 quads but you are pushing 1300 quads per frame,
In that case the renderer pause at 1000, executes a flush of its data and a single drawing call to send it to the GPU. Then it resets itself and goes on with accepting new data,
This is transparent to you, and the ending result is you will have executed two drawing calls (1000 quads + 300 quads) instead of just one to draw your stuff.
The other case causing a flush it's if you run out of texture units.
Suppose your GPU supports 16 texture units for the fragment shader stage.
If you are drawing 900 quads using just 10 textures, no flush is required. But if you use more textures the renderer as in the case above has to pause, flush, free the texture units and go on from there.
Again this is transparent to you.

That's why if you have a lot of different textured quads it's a good idea to use a texture atlas.
You make a very big texture with many smaller images inside it and you draw your quads grabbing the texels from there. The very big texture will require a single texture unit instead of many,
Incidentally the RenderText modules included as extras in SGL are using a texture atlas to store the characters of a single font, exactly for this reason.

The batch renderer includes two types of DrawQuad().
One is for simple colored quads, optionally with some transparency and optionally textured with a single texture.
The other version supports the use of atlases, can do all the above but it has a further parameter specifying the normalized texture coordinate defining the area inside the big texture to sample from.

To see an example to how to use the "atlas" version look at the modified version of the RenderText module, named "RenderTextBatch_330". It's the same as "RenderText_330" but uses the batch renderer instead of locally buffering the text strings by itself.


The batch render dynamically pumping textured quads (10 unique textures used) with a semi-transparent extra quad moving around.
Note the single drawing call in the stats.

Image

Re: SGL (Simple GL)

Posted: Sun Jun 11, 2023 6:35 pm
by luis
The RenderText modules performance differences.

The basic version, using old style immediate mode, so a lot of function calls to push the geometry to the GPU.
No buffering at all.

Image


The OpenGL 3.30 version using a VBO.
Every string is processed and the geometry of the whole string is rendered in a single shot.
A big improvement.

Image

The OpenGL 3.30 version above but modified to use the batch renderer.
All the chars on screen are rendered (potentially) in a single shot.

Image

Please note in the last case the program is becoming even more CPU limited, because we have 1 drawing call but a lot of work preparing the data and processing the strings on the CPU side, so compiling with the C backend is very beneficial because the gained speed help us to get a higher frame rate.

Also the actual result on different hardware and different drivers can vary a lot, because the driver can be software compliant with the specification but it may do some thing suboptimally or emulate it on the CPU side if the hardware is older but the OpenGL support level is quite high.

But this is what you should expect on modern GPUs.

Again please send me your sysinfo results viewtopic.php?p=602215#p602215 if you can, thanks.

Re: SGL (Simple GL)

Posted: Sun Jun 11, 2023 10:03 pm
by Kuron
I just finally got a chance to test this (most of my time is spent on a Chromebook nowadays) and all the examples I have tried so far work wonderfully. SysInfo also sent. Keep up the great work.

Re: SGL (Simple GL)

Posted: Thu Jun 22, 2023 12:27 am
by luis
Hi, I have added a variant of the Batch Renderer under extras\Batch_ ArrayTexture.

This one uses Array Textures instead of normal textures.
Just wanted to test it because this type of textures is what I will probably use in my 2D "engine".
That's because in my intentions it will be very sprite-oriented, almost anything will be a sprite and array textures are very versatile.

My idea is to have just one type of sprite which can have different characteristics
  • use a single texture for a simple sprite (so a GL_TEXTURE_2D_ARRAY with a single layer)
  • use multiple textures like in an indexed array, for example for different frames of an animation but still counting as a single texture from the OpenGL point of view (so a GL_TEXTURE_2D_ARRAY with multiple layers)
  • use one or multiple atlas containing multiple sub-textures (a GL_TEXTURE_2D_ARRAY with one or multiple layers and each layer acting as an atlas)
The functions to draw the quad have now another parameter to select the layer we want to draw from the texture.
An example can be seen in how is drawn the semitransparent quad (built with 3 layers) in the example extras\Batch_ ArrayTexture\test_textured_quads.pb

The only real limit I know of is the layers must all have the same size (for example all the frames of the animation of a sprite must be 256x256).

When this is a problem, there is the atlas where any sprite can have different sizes.

So... now that I've tested them with the batch renderer to draw quads and text and checked that all this seem to work, I would probably use them as described above.

Re: SGL (Simple GL)

Posted: Sat Jul 01, 2023 9:59 pm
by luis
Hi, I wanted to do some experiments with the Immediate Mode GUI paradigm, so I pulled out SGL again and wrote a very minimalistic module implementing some widgets using the IMGUI concepts.

I followed the ideas described in this video: https://youtu.be/Z1qyvQsjK5Y

I've implemented some of the widgets I thought would be useful when writing demos in SGL, but once you saw how these are made, it's possible to write almost anything. BTW: I've used the batch renderer again for this, and it's really fast.

Image

The above is a GUI you may have in a game or in game settings, and it's fixed in position.

Without implementing a proper windowing system I faked it using a simple background resembling a window and anchoring the GUI to it, this is how it looks.

Image

I must say I'm impressed with the easy of use and versatility of an IMGUI for games and demos compared to a retained mode gui. Maybe in the future I'll use this as a starting point to make one more extended.

Or you can start from this one !

You can find it under "extras".

Re: SGL (Simple GL)

Posted: Sat Jul 01, 2023 10:43 pm
by idle
that's looking really good Luis.

Re: SGL (Simple GL)

Posted: Mon Jul 03, 2023 7:27 pm
by skinkairewalker
this is amazing.

Re: SGL (Simple GL)

Posted: Thu Jul 06, 2023 10:17 pm
by luis
Hi, I've made an example of how to implement rendering to a texture in OpenGL.

Basically the scene inside the front of the cube comes from one of the first demos, a simple textured cube rotating.

But it is rendered to a texture instead of a window, and then the texture is used in the main scene and applied to one of the sides of the cube.

This can be useful for many things, for example a rear-view mirror, where you render the same scene but looking back and then apply the texture to the mirror. Or to make e surveillance monitor, where you render the scene the camera connected to the monitor is seeing. Or to render to a sprite and then use the sprite.
The uses are limitless.

As a bonus I've added an "old TV effect" through a shader I've got from Shadertoy.

The source is under /demos/013

Image

Re: SGL (Simple GL)

Posted: Mon Jul 31, 2023 7:33 pm
by skinkairewalker
Is it possible to use this ImGui with Ogre in pb?