Sign in to follow this  
japro

OpenGL So I started writing Example Programs

Recommended Posts

Very nice work ...and i just learned about GLFW's existence [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
Maybe it is about time to take divorce with SDL [img]http://public.gamedev.net//public/style_emoticons/default/wub.png[/img]


Quick-edit--
Suggestion : Maybe add a common module for the (common) shader stuff ? [img]http://public.gamedev.net//public/style_emoticons/default/dry.png[/img] Edited by vNeeki

Share this post


Link to post
Share on other sites
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.

[quote name='japro' timestamp='1340919993' post='4953771']
Sadly I still have to rely on some libraries for context creation etc. so the examples depend on glfw, glew and glm
[/quote]
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.

Share this post


Link to post
Share on other sites
[quote name='vNeeki' timestamp='1340954539' post='4953869']
Maybe add a common module for the (common) shader stuff ? [img]http://public.gamedev.net//public/style_emoticons/default/dry.png[/img]
[/quote]
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:
[code]
/*
* lines x-y: shader setup
* lines z-w: vbo setup
* ...
*/
[/code]
to make it easier to find the relevant parts.
Can you elaborate on what you mean with modules?

[quote name='larspensjo' timestamp='1340959419' post='4953890']
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.
[/quote]
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?

Share this post


Link to post
Share on other sites
[quote name='japro' timestamp='1340973240' post='4953939']
[quote name='vNeeki' timestamp='1340954539' post='4953869']
Maybe add a common module for the (common) shader stuff ?
[/quote]
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:
[code]
/*
* lines x-y: shader setup
* lines z-w: vbo setup
* ...
*/
[/code]
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 :

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

int uniform = shader.GetUniformLoc("variable");
[/CODE]


[quote name='japro' timestamp='1340973240' post='4953939']
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).
[/quote]

Woahah sounds amazing!

[quote name='japro' timestamp='1340973240' post='4953939']
There might be other features I'm currently not thinkingof?
[/quote]

How about assimp model loading/rendering and multiple opengl contexts?

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
[quote name='japro' timestamp='1341102842' post='4954409']
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".
[/quote]
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.

Share this post


Link to post
Share on other sites
One idea would be to do a deferred shader[quote name='japro' timestamp='1340973240' post='4953939']
There might be other features I'm currently not thinkingof?
[/quote]
How about a deferred shader?

Share this post


Link to post
Share on other sites
[quote name='larspensjo' timestamp='1341235801' post='4954895']
How about a deferred shader?
[/quote]
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 [url="https://github.com/progschj/OpenGL-Examples/blob/master/8map_buffer.cpp"]mapped buffers[/url] and the other doing the same but staying on the gpu and moving the particles via [url="https://github.com/progschj/OpenGL-Examples/blob/master/9transform_feedback.cpp"]transform feedback[/url].
[attachment=9769:particles2.png]

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
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, [url="http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Chapter-3:-3D-transformation-and-projection.html"]Durian Software's[/url] and [url="http://www.lighthouse3d.com/"]lighthouse3d's[/url] being some of the best. For anyone else using LWJGL, [url="http://lwjgl.blogspot.co.uk/2012/04/chapter-one-triangle.html"]this[/url] 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 [i]in & out [/i]vs. [i]varying [/i]in GLSL 1.5, AFAICT[i].[/i] Edited by NickWiggill

Share this post


Link to post
Share on other sites
[quote name='japro' timestamp='1340919993' post='4953771']
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.
[/quote]

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)

Share this post


Link to post
Share on other sites
I got the suggestion to use gl3w more than once [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]. 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:

[media]http://youtu.be/j9QLwqNs6pk[/media] Edited by japro

Share this post


Link to post
Share on other sites
I just added the first OpenGL 4 example. It shows tessellation: [url="https://github.com/progschj/OpenGL-Examples/blob/master/11tesselation.cpp"]https://github.com/p...tesselation.cpp[/url]

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: http://youtu.be/cJsZL6BRP_s Edited by japro

Share this post


Link to post
Share on other sites
Today I had some fun with ARB_shader_image_load_store (core in 4+): [url="https://github.com/progschj/OpenGL-Examples/blob/master/12shader_image_load_store.cpp"]https://github.com/p..._load_store.cpp[/url]
It solves the electromagnetic wave equation on a 2d grid with shaders. Thanks to the extension the update can be done in place so there is no fbo ping pong rendering required. Just a single texture.
[attachment=10404:wave_equation.png]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628388
    • Total Posts
      2982403
  • Similar Content

    • By test opty
      Hi all,
       
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
       
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
  • Popular Now