Sign in to follow this  
Flimflam

OpenGL Getting started with OpenGL

Recommended Posts

Flimflam    665
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
karwosts    840
[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
Trienco    2555
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
szecs    2990
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
Flimflam    665
[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
V-man    813
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
mhagain    13430
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
gregsidelnikov    102
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
V-man    813

[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
Trienco    2555
[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
gregsidelnikov    102
[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  

  • 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