So I started writing Example Programs

Started by
17 comments, last by japro 11 years, 8 months ago
Hi,

so about two years back I again got into OpenGL (the first time was a failure being way back at the beginning of my programming career). Specifically I was only interested in learning "modern OpenGL" so 3 and later. Since at that time I already was pretty competent with C++ and math I figured it would be relatively easy. But then I got quickly frustrated with the online resources that are available. The majority is outdated and littered with deprecated functionality and most or the tutorials are more graphics programming tutorials that happen to use OpenGL instead of actually focusing on OpenGL. You know the ones that go: "how to open a empty window", "how to draw a triangle", "how to load a model"... wait? what has loading a model to do with OpenGL?
So I often ended up reading reference pages and specs but that is somewhat frustrating without examples to go along with them. And searching for examples again resulted in lots of deprecated code and otherwise frustrating stuff like OpenGL functionality wrapped away under custom frameworks. I want to learn about OpenGL after all and not about how the author likes to wrap it.

So I finally started to write examples like I would have wanted them myself. Partly so I have code lying around that I can refer to myself but also because I think there might be other people like me out there. The current state is available on github:

https://github.com/p...OpenGL-Examples

The basic Idea is that each example is self contained (not reliant on some base code or obscure libraries) in a single file and is meant to provide a clean usage of one or multiple OpenGL 3+ features. Sadly I still have to rely on some libraries for context creation etc. so the examples depend on glfw, glew and glm.

The reason why I post this here is that I want more feedback and suggestions. So please let me know what you think. Also tell me if there is something wrong with my English in the comments, I'm not a native speaker after all. And since I'm already shamelessly plugging my stuff I usually announce new examples on my twitter (see signature).
Advertisement
Very nice work ...and i just learned about GLFW's existence smile.png
Maybe it is about time to take divorce with SDL wub.png


Quick-edit--
Suggestion : Maybe add a common module for the (common) shader stuff ? dry.png
Good examples and nice reference implementations!

I didn't find anything to complain on, it is code that is easy to follow and understand, with just the important things.

Possibly, the attribute locations could be specified using shader layout commands instead of using glBindAttribLocation(). But I suppose using glBindAttribLocation() can be an advantage for some use-cases.


Sadly I still have to rely on some libraries for context creation etc. so the examples depend on glfw, glew and glm

Those are the 3 "core" libraries I always recommend. Using them, your source code is portable to Windows and Linux, which is a good advantage.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

Maybe add a common module for the (common) shader stuff ? dry.png

You mean as in a separate file or just separate functions? I'm aware that the examples aren't exactly best coding practice in terms of structuring etc. But I wanted to avoid introducing to much of my structure since that is one of the things that I often don't like in tutorials. That the author structures the code in some way that is intuitive to him but not me. Which in the end makes it harder to find what I'm actually looking for. Also I wanted the examples to be self contained without external includes other than the libs. I was considering putting something like a index on the top of the examples like:

/*
* lines x-y: shader setup
* lines z-w: vbo setup
* ...
*/

to make it easier to find the relevant parts.
Can you elaborate on what you mean with modules?


Possibly, the attribute locations could be specified using shader layout commands instead of using glBindAttribLocation(). But I suppose using glBindAttribLocation() can be an advantage for some use-cases.

I guess I could add a variation of example 1 to show that possibility. Maybe I will move the other example also to use the layout version instead of BindAttribLocation. It indeed looks cleaner somehow.

Thank you for the feedback guys :) keep it coming.

The next examples I'm considering to add are sync queries and buffer mapping (for texture streaming), timer queries, occlusion queries and conditional render as well as transform feedback. For the moment I'm going through OpenGL3.3 features and after having covered those I'll also look into 4+ stuff (tessellation most notably). There might be other features I'm currently not thinkingof?

[quote name='vNeeki' timestamp='1340954539' post='4953869']
Maybe add a common module for the (common) shader stuff ?

You mean as in a separate file or just separate functions? I'm aware that the examples aren't exactly best coding practice in terms of structuring etc. But I wanted to avoid introducing to much of my structure since that is one of the things that I often don't like in tutorials. That the author structures the code in some way that is intuitive to him but not me. Which in the end makes it harder to find what I'm actually looking for. Also I wanted the examples to be self contained without external includes other than the libs. I was considering putting something like a index on the top of the examples like:

/*
* lines x-y: shader setup
* lines z-w: vbo setup
* ...
*/

to make it easier to find the relevant parts.
Can you elaborate on what you mean with modules?

[/quote]

I meant a simple wrapper class to handle shaders.Something like :


CShader shader;
shader.SetVSource("vert code");
shader.SetFSource("frag code");
shader.Compile();
shader.Bind();
shader.UnBind();

int uniform = shader.GetUniformLoc("variable");




The next examples I'm considering to add are sync queries and buffer mapping (for texture streaming), timer queries, occlusion queries and conditional render as well as transform feedback. For the moment I'm going through OpenGL3.3 features and after having covered those I'll also look into 4+ stuff (tessellation most notably).


Woahah sounds amazing!


There might be other features I'm currently not thinkingof?


How about assimp model loading/rendering and multiple opengl contexts?
Small update: The examples now use layout qualifiers for the attribute locations (except for a variation of example 1 that is there to show the explicit binding). More importantly the examples now have a short index at the beginning that points out the line numbers of the newly introduced features.

The shader encapsulation is of course something that one would do in actual code (and I of course have wrappers that do exactly that). But as far as the examples are concerned I will not do that since I want to keep them self contained and free of my own "encapsulation design".

The shader encapsulation is of course something that one would do in actual code (and I of course have wrappers that do exactly that). But as far as the examples are concerned I will not do that since I want to keep them self contained and free of my own "encapsulation design".

I think that is the best decision. Thus, every example is complete in itself, and easy to follow. Even though much logic is in the main function, an experienced programmer will easily abstract the design and generalize.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
One idea would be to do a deferred shader

There might be other features I'm currently not thinkingof?

How about a deferred shader?
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

How about a deferred shader?

My problem there is that it doesn't rally show anything new in terms of OpenGL. The FBO FXAA already does that sort of. On the other hand I could use that to explicitly show multiple render targets.

In the meantime, I added two examples that both show a particle system. One implemented by moving the particles on CPU side and copying them to the vram via multiple mapped buffers and the other doing the same but staying on the gpu and moving the particles via transform feedback.
[attachment=9769:particles2.png]
Timer/Occlusion queries + conditional render: https://github.com/progschj/OpenGL-Examples/blob/master/10queries_conditional_render.cpp
Slightly bloaty since for occlusion queries to make sense you need enough stuff to... well occlude each other. I ended up doing a voxel/cube renderer for that. On the one hand 700 lines for an example is a little long. But at the same time 700 lines for a voxel renderer with occlusion queries is fairly short I would say ;)

This topic is closed to new replies.

Advertisement