Sign in to follow this  
japro

OpenGL So I started writing Example Programs

Recommended Posts

vNeeki    194
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
larspensjo    1561
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
japro    887
[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
vNeeki    194
[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
japro    887
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
larspensjo    1561
[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
larspensjo    1561
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
japro    887
[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
japro    887
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
Nick of ZA    177
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
l0calh05t    1797
[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
japro    887
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
junkyska    143
Very good examples, good job!

Japro it's really nice!! I like it! [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]

Share this post


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

  • Partner Spotlight

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now