• Create Account

## So I started writing Example Programs

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

18 replies to this topic

### #1japro  Members

Posted 28 June 2012 - 03:46 PM

POPULAR

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).

Edited by japro, 28 June 2012 - 03:49 PM.

### #2vNeeki  Members

Posted 29 June 2012 - 01:22 AM

Very nice work ...and i just learned about GLFW's existence
Maybe it is about time to take divorce with SDL

Quick-edit--
Suggestion : Maybe add a common module for the (common) shader stuff ?

Edited by vNeeki, 29 June 2012 - 01:23 AM.

### #3larspensjo  Members

Posted 29 June 2012 - 02:43 AM

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.
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

### #4japro  Members

Posted 29 June 2012 - 06:34 AM

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 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?

### #5vNeeki  Members

Posted 30 June 2012 - 05:58 AM

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 z-w: vbo setup
* ...
*/

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

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

CShader shader;



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?

### #6japro  Members

Posted 30 June 2012 - 06:34 PM

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".

### #7larspensjo  Members

Posted 01 July 2012 - 01:51 PM

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.
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

### #8larspensjo  Members

Posted 02 July 2012 - 07:30 AM

One idea would be to do a deferred shader

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

Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

### #9japro  Members

Posted 02 July 2012 - 06:19 PM

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.

### #10japro  Members

Posted 04 July 2012 - 06:15 PM

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 ;)

### #11vNeeki  Members

Posted 04 July 2012 - 11:30 PM

Well done , well done.

I'm still following the project ;)

### #12NickWiggill  Members

Posted 09 July 2012 - 06:19 AM

Having a look now at how you encapsulate your shaders, thanks for sharing.

The tutorials out there are indeed pathetic, it has to be said. It's depressing to know how many others will suffer through that stuff just to get a working shader-based app running. There are precious few good tutorials, Durian Software's and lighthouse3d's being some of the best. For anyone else using LWJGL, this was also immensely useful. I'm learning the programmable OpenGL 2.0 ES subset of OpenGL 2.1 using VBOs, and in spite of the usefulness of this approach due to its applicability to desktop and mobile and the fact that it is "modern", it's still incredibly hard to find a single, comprehensive tutorial that foregoes all the fixed pipeline cruft. The iOS and Android tutorials don't help either, as they encapsulate too much, hiding the basic OpenGL function calls from you. I had to use a few 3.0 tutorials to help me get up to speed with 2.0; fortunately the only difference was in & out vs. varying in GLSL 1.5, AFAICT.

Edited by NickWiggill, 09 July 2012 - 06:31 AM.

### #13l0calh05t  Members

Posted 09 July 2012 - 08:21 AM

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.

Since you want to use Forward-Compatible GL 3+ only, gl3w might be a better (and lighter) choice than glew.
Personally I'd also take Eigen over glm any day (although it admittedly does not include perspective projections... yet)

### #14japro  Members

Posted 09 July 2012 - 05:49 PM

I got the suggestion to use gl3w more than once . For the moment I'll keep on this track, but since the examples are already partially "generated" (they are run through a simple program to create the index at the beginning) i guess i could also have that generate a gl3w version...

Somewhat related, after having completed the occlusion culling example I kept playing with it and after some amount of copy pasting from my own code and adding a deferred pipeline I ended up with this:

http://youtu.be/j9QLwqNs6pk

Edited by japro, 09 July 2012 - 05:50 PM.

### #15junkyska  Members

Posted 10 July 2012 - 04:01 PM

Very good examples, good job!

Japro it's really nice!! I like it!

### #16Tili_us  Members

Posted 11 July 2012 - 02:14 AM

Really nice samples! Did anybody find a decent wrapper for shaders/programs in OpenGL?

I might consider not using SDL..

### #17japro  Members

Posted 25 July 2012 - 04:19 PM

I just added the first OpenGL 4 example. It shows tessellation: https://github.com/p...tesselation.cpp

It also has a simple phong lighting implementation and is funny in that it doesn't use a vbo at all. It generates the sample points by "abusing" instanced rendering and only uses gl_VertexID and gl_InstanceID and the texture as inputs so to speak.

Here is a video:

Edited by japro, 25 July 2012 - 04:19 PM.

### #18FireInDark  Members

Posted 25 July 2012 - 09:23 PM

Great!

### #19japro  Members

Posted 01 August 2012 - 07:26 AM