Sign in to follow this  
Flimflam

OpenGL Getting started with OpenGL

Recommended Posts

Greetings, OpenGL forum!

I've decided to use OpenGL for the graphical back end in a multipurpose editor I need for my various projects. I'm using OpenGL here because I think it's a great way to get into it and create something of use to myself without having to worry about performance being perfect.

I'm using C# and OpenTK to access OpenGL. I've got a few questions I'd like to throw at you. I won't lie; they are newbie questions because honestly I'm sort of overwhelmed by the options available to me in OpenGL compared to Direct3D which I've been using for years. I'm also targeting GL 2.0/2.1, not he more recent versions as I want them to run on DX9 class hardware.

First things first. There is a dozen ways to render geometry in OpenGL compared to Direct3D. In OpenGL, there's the original deprecated glVertex3f(...), display lists, vertex lists, VBOs, etc. I'm not really sure if there's a preferred solution for general rendering (static and dynamic). I'm leaning towards VBOs since that seems most in line with what I'm familiar with in Direct3D. I do like what I've read about Display Lists though, and it seems like an interesting and downright neat way to render geometry. But since display lists seem to use the generally deprecated glVertex3f like commands, are they too considered deprecated? I realize they're not exactly deprecated in the version of GL I'm targeting, but I would like to be somewhat current.

The second question I have is rendering quads vs. triangles. Is there actually any determent or benefit to rendering quads compared to triangles? I know the driver has to convert quads into triangles since that's what hardware expects. There's a number of situations where I will want to render quads. I'm not sure if I should just render them as quads or bite the bullet and create two triangles instead. I've always loved the idea that you have access to more primitives in GL than just triangles...

The last question I have is regarding shaders. Does anyone have any good tutorials they have found helpful that might help put me in the right direction regarding shaders in GL?

Thanks in advance for any and all help I receive regarding these questions! I've been using Direct3D for so long that I thought it was time to take a look at the other side of the fence for a change.

Share this post


Link to post
Share on other sites
[quote]
Display Lists ... are they too considered deprecated?
[/quote]

Yes. Prefer VBO's over Display Lists.

[quote]
Is there actually any determent or benefit to rendering quads compared to triangles?
[/quote]

Not really. Rendering with quads is deprecated now as well, so you might as well go ahead and do it with triangles. If its easier for you to do it with quads though its not going to hurt you.

[quote]
Does anyone have any good tutorials they have found helpful that might help put me in the right direction regarding shaders in GL?
[/quote]

I like this site for GLSL: http://www.lighthouse3d.com/opengl/glsl/

Its a little bit out of date compared to the latest and greatest, but it does a good job showing you how to setup shaders in your project (and things haven't changed that much). You also have to option to use Cg since you're probably already familiar with HLSL (they are very similar), though that requires an external library and isn't part of the OpenGL specification like GLSL is. I've never used it, but its an option if you're interested.

Share this post


Link to post
Share on other sites
I actually think shaders might be tricky when using tutorials, because many don't bother to mention which version they are based on and you can end up mixing old and new stuff in ways that will cause funny error messages. For example new syntax uses in, out and inout, older versions use varying. Some features require explicitely declaring a version to use, but using a new version "invalidates" old syntax (I ended up cursing a lot finding a way to use certain extensions AND still use syntax that worked on my desktop nvidia and my notebook ati). Since you target 2.x that shouldn't be an issue, but you might run into sources that don't mention they are using 3+. So if you run into unfamiliar syntax and lots of errors in your shaders, that's the most likely reason.

In terms of quads. The main benefit would be laziness and if you're not using indexed primitives they save you 33% of vertex data. Of course you still save 33% of indices, but that usually doesn't matter as much. You should also make sure that all points lie on a plane or you really don't care in which direction your quad is split into triangles.

Share this post


Link to post
Share on other sites
I think the problem with quads is that they are not triangles... I mean if you import a model and use quads, you'd have to be careful that your imported mesh only contains quads, or you'd have to handle quads and triangles as well. Then it's simpler to use only triangles and split quads/polygons at loading the mesh (split like a triangle-fan for example, but maybe it depends on the editor/exporter and the file format).

Anyway, for other stuff I use quads, like GUI (well, for an editor I prefer native windows GUI), or HUD like things. Okay, in this case I usually just use immediate mode too...

Share this post


Link to post
Share on other sites
[quote name='szecs' timestamp='1302588613' post='4797394']
I think the problem with quads is that they are not triangles... I mean if you import a model and use quads, you'd have to be careful that your imported mesh only contains quads, or you'd have to handle quads and triangles as well. Then it's simpler to use only triangles and split quads/polygons at loading the mesh (split like a triangle-fan for example, but maybe it depends on the editor/exporter and the file format).

Anyway, for other stuff I use quads, like GUI (well, for an editor I prefer native windows GUI), or HUD like things. Okay, in this case I usually just use immediate mode too...
[/quote]

First of all, thank you everyone for your replies. I really appreciate it! This thread has really helped me get started in GL.

Regarding my intended use of quads, I didn't intend on using them for things like models. I was considering their use for screen space quads (text, hud, etc), 3d/2d debug overlays, etc. All my model work has always been in indexed triangles. Basically their use is extremely limited, but I like the concept of them for things like that. =)

Share this post


Link to post
Share on other sites
Gaming GPUs don't support quads. They support triangles. OpenGL is from 1992 and it implemented technology that may or may not have been available on SGI workstations.
Whatever D3D offers is exaclty what todays GPU can do. Therefore I recommend limiting yourself to what D3D offers since it is a good indication for the "best path".

Render your stuff with glDrawRangeElements or glDrawElements. Use shaders to render everything.
Use IBO/VBO just as you would in D3D.
Use your own math lib to create matrices and upload them to your shader.
Do not use built in stuff in your shader like ftransform() or gl_ModelViewProjectionMatrix.
Use generic vertex attributes and not the old glVertexPointer.

PS : choosing GL 2.1 is a good idea if you want to aim for DX9 hardware.

Share this post


Link to post
Share on other sites
I prefer using triangles but it's just a personal thing; there's nothing wrong with using quads and they work just fine for the kind of use case you have.

Regarding the rendering functions to use, I'd say that you'll find the transition easier if you stick with what's roughly equivalent to what you know from D3D. Hardware tends to be optimized to favour this kind of drawing anyway so longer term it's what you'll want to be doing. DrawIndexedPrimitive kinda translates to glDraw(Range)Elements and DrawPrimitive kinda translates to glDrawArrays; the -UP versions would be client-side vertex arrays, the standard versions are VBOs. There's one subtle difference in that GL takes the actual vertex/index count as params, whereas D3D takes the primitive count; another difference is that D3D separates vertex layout from data; GL doesn't.

For the GUI/HUD stuff glBegin/glEnd may be perfectly adequate; it's not going to be a bottleneck so long as you don't overdo it, and it's quite powerful to be able to specify any kind of vertex data without having to worry about your VBO strategy for it.

Congragulations on the decision to learn both, by the way. I went in the opposite direction and knowledge of both has been something I've definitely found useful.

Share this post


Link to post
Share on other sites
Quite a while ago I have written a number of articles about learning OpenGL.

Based on what you said in your thread, I bolded the two tutorials that might be of interest to you (dealing with rendering OpenGL primitives).

[url="http://www.falloutsoftware.com/tutorials/gl/gl0.htm"]Introduction to 3D[/url]
[url="http://www.falloutsoftware.com/tutorials/gl/gl1.htm"]OpenGL Compiler Setup[/url]
[url="http://www.falloutsoftware.com/tutorials/gl/gl2.htm"]Creating an OpenGL Window[/url]
[b][url="http://www.falloutsoftware.com/tutorials/gl/gl2p5.htm"]Intro to OpenGL Primitives[/url]
[url="http://www.falloutsoftware.com/tutorials/gl/gl3.htm"]Drawing OpenGL Primitives[/url][/b]
[url="http://www.falloutsoftware.com/tutorials/gl/gl4.htm"]OpenGL Color[/url]
[url="http://www.falloutsoftware.com/tutorials/gl/gl5.htm"]OpenGL 3D Transformations[/url]
[url="http://www.falloutsoftware.com/tutorials/gl/gl6.htm"]Creating 3D Models in OpenGL[/url]
[url="http://www.falloutsoftware.com/tutorials/gl/gl8.htm"]OpenGL Light[/url] how light works in OpenGL, etc
Create an OpenGL window from scratch: [url="http://www.falloutsoftware.com/tutorials/gl/gl7.htm"]OpenGL base code[/url]
[url="http://www.falloutsoftware.com/tutorials/win/win2.htm"]Create a Win32 Window in C++[/url]



Hope this helps

Share this post


Link to post
Share on other sites

[b][url="../../user/172006-gregsidelnikov/"]gregsidelnikov[/url] [url="../../user/172006-gregsidelnikov/page__f__25"][img]http://public.gamedev.net/public/style_images/master/user_popup.png[/img][/url] that is old. The OP wants to stay modern even though he will be using GL 2.1. You might want to write about GL 3.x
[/b]

Share this post


Link to post
Share on other sites
[quote name='V-man' timestamp='1302626173' post='4797554']
Gaming GPUs don't support quads. They support triangles.[/quote]

They or their drivers support them well enough that non-indexed quads outperform non-indexed triangles by reducing the amount of data being moved around. They handle the primitive task of duplicating vertices to triangulate a quad quite well. I guess if someone wants to be completely paranoid about quads being dropped in every way by the vendors one can still add a few wrapping functions that handle it somewhere convenient. For all the simple GUI stuff (not to mention current trend of Minecraft clones) quads are just fine for now.

Share this post


Link to post
Share on other sites
[quote name='V-man' timestamp='1302646755' post='4797682']
[b]gregsidelnikov [img]http://public.gamedev.net/public/style_images/master/user_popup.png[/img] that is old. The OP wants to stay modern even though he will be using GL 2.1. You might want to write about GL 3.x
[/b]

[/quote]

Not old enough to be referenced in a recently published book on Android development.

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
      628333
    • Total Posts
      2982142
  • Similar Content

    • 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.
    • By Abecederia
      So I've recently started learning some GLSL and now I'm toying with a POM shader. I'm trying to optimize it and notice that it starts having issues at high texture sizes, especially with self-shadowing.
      Now I know POM is expensive either way, but would pulling the heightmap out of the normalmap alpha channel and in it's own 8bit texture make doing all those dozens of texture fetches more cheap? Or is everything in the cache aligned to 32bit anyway? I haven't implemented texture compression yet, I think that would help? But regardless, should there be a performance boost from decoupling the heightmap? I could also keep it in a lower resolution than the normalmap if that would improve performance.
      Any help is much appreciated, please keep in mind I'm somewhat of a newbie. Thanks!
  • Popular Now