Again with OpenGL

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Again with OpenGL

Post by Benjamin100 »

I've gotten back into programming.
I had done some work with SDL before,
but anything I try to learn with OpenGL I don't understand.

The tutorials on it don't seem complete, even the online book.

How do I START?
What goes at the BEGINNING of the program?
How do I use a window that I've set up?
How do I create a window with GLUT and use it for my program? (A question it seems that no tutorial on glut actually answers.)
What IS the rendering pipeline?

It seems as if every tutorial I have found online jumps right into some obscure code I've never seen before, tells me to type it in, and then when I do nothing works.
And how in the world do I create a window and use it WITH OpenGL?
Does anyone ACTUALLY KNOW how to EXPLAIN IT?
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Again with OpenGL

Post by short »

My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post by Benjamin100 »

Thanks.
I'll try out that first link.

I think I tried the other one before and it didn't make much sense to me.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Again with OpenGL

Post by Ginto8 »

You are asking a lot of questions, all at once, so I'll try to go through one at a time.

How do you start? That's too vague a question to answer, so I'm going to assume it was rhetorical.

What goes at the beginning of the program? Whatever you want. But if you want to use OpenGL, you're going to have to create a "context", which is basically an OpenGL rendering target. In SDL, you call SDL_SetVideoMode() with SDL_OPENGL as one of the flags. For more info on dealing with the SDL-OpenGL connection, lazyfoo has a pretty good tutorial.

How do you use the window you've set up? Well, in SDL, if you initialize it with SDL_OPENGL, you're all set. Otherwise, you have a few choices for how to get a context. If it's Qt, I know you can have an OpenGL widget of sorts. If it's windows, wgl is your friend; on *nix, glx is. I don't know for mac, but there'll be something available.

How do you create a window with GLUT and use it for your program? This tutorial walks you through it.

What is the rendering pipeline? This is a question with a VERY complicated answer, but the basic idea is that, in OpenGL, you describe your 3D "scene" in terms of transformations (such as translations and rotations) and primitives (shapes). The pipeline is a series of processes, usually carried out on the GPU, that takes that description and converts it into pixels on a screen.

How do you create a window and use it with OpenGL? Well, if you go deeper down, the process is like this: create a window, get a context, bind the context, do OpenGL stuff. If you're using libraries like SDL, SFML or GLUT, you can skip the "get a context" and "bind the context" steps, because those are (usually) handled for you.

Does anyone know how to explain it? Sure, I think I explained at least the basics pretty well here. There are plenty of resources out there, but it sounds like you either weren't looking at the right ones, were looking at the wrong parts, or weren't looking hard enough. Nehe is a FANTASTIC guide for OpenGL (look at the "Legacy Tutorials"); although most of his stuff is fixed-function pipeline and immediate mode, all the basic ideas are there.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post by Benjamin100 »

Thank you.

Also, does "freeglut" work the same as Glut?
User avatar
dr-snipe
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Sun Dec 19, 2010 10:09 pm
Programming Language of Choice: C++, Java, PHP
Contact:

Re: Again with OpenGL

Post by dr-snipe »

Yes, freeglut works the same as glut. It's the replacement for it that started back in '99.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post by Benjamin100 »

Thank you.

I'm apparently supposed to put a library file inside of a "PlatformSDK" folder, but I'm not finding the folder.
Any ideas?

EDIT: I think I found what I was looking for.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post by Benjamin100 »

I made a polygon! I did it! I made a window appear with a polygon inside of it!
Thanks, guys.
Ah, the feeling of learning something new, and visual, in programming. And without a bunch of bugs...for now.
Does it always feel this good?
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post by Benjamin100 »

So now some questions...
What, more specifically, is the point of matrix mode and projection and "glOrtho"?
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Again with OpenGL

Post by Ginto8 »

You certainly have a knack for asking questions with complicated answers. :lol: Those three functions are all parts of the vertex pipeline (processing the vertices of the polygon before actually drawing them to the screen).

The modelview matrix: This transforms the vertices you put in from "model" space to "world" space. The difference between these two is best described in this way: if you draw a cube, often the simplest way to do so is to move (translate) to center of the cube, and draw the cube relative to that location. Those relative coordinates would be in "model" space; the absolute coordinates of the vertices (from the world's origin rather than the model's origin, hence the names) are in "model space".

Projection matrix This transforms vertices from "world" space to "clip" space. This "clip" space is, which, as the name implies is clipped; all coordinates are in a [-1,1] range.

Viewport Matrix This is the final transformation; it takes "normalized device coordinates" (which are derived from clip space and maps it to an actual pixel on the screen.

glOrtho creates an "orthographic projection" matrix, and multiplies the current matrix by it. Usually it's used for the projection matrix, but you might be a crazy person and want to use it for another matrix in the pipeline. ;) For most basic usage of OpenGL (mainly 2D), ortho is all that is needed. However, for more complicated 3d (in general to make it "look right"), you'll want to create a perspective projection, either by gluPerspective or glFrustrum. The difference between orthographic and perspective projections is whether the clip space is in terms of a rectangular prism, or a frustrum (basically the 3d companion to a trapezoid). If you don't quite understand it, don't worry; it's a tough concept, and you're better off starting with 2D and glOrtho.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Again with OpenGL

Post by qpHalcy0n »

Some corrections.

The modelview matrix: Transforms from model to VIEW space...not world space.

Viewport matrix: There is no such thing. The viewport mapping is a very fast linear expansion. It would be silly to use a matrix for such an operation in a 2D space.

Orthographic projections are only used for just that "projections". They have no basis for any other transformation. Essentialy, Orthogonal projections preserve co-linearity (lines that are parallel will still be parallel) and may or may not preserve length (Oblique orthogonal projections). glOrtho is an orthographic projection and will preserve both co-linearity and length. Perspective projections preserve neither co-linearity nor length.
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Again with OpenGL

Post by TheBuzzSaw »

I feel it is worth noting that the viewport can be done with a matrix. As useless as an operation as it is, it was how I was taught to make my rasterizer in my 3D course in school. It's cool seeing every step be a matrix. But yeah, you get to OpenGL and realize that it doesn't use/need a matrix for that step.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post by Benjamin100 »

So, model space has the origin in the center?
And view space has its own origin... like the upper left corner of the viewing area?
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post by Benjamin100 »

So does view space only have an x and y, or does it have a z within the use of the library?
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Again with OpenGL

Post by Ginto8 »

Everything in OpenGL has an X, Y, and Z. The projection matrix usually adds a W (which is technically always there, but isn't really used outside this part of the pipeline), which is then used to convert clip space to normalized device coordinates.

Since you seem so interested in the internal workings of the vertex pipeline, I found a useful reference page that I used myself: http://www.songho.ca/opengl/gl_transform.html.

And qp, I was trying to portray everything as a matrix in the process. I realize that it's computationally simpler to do the calculations without the hassle of matrices, but I thought it was better to show each transform as a matrix to keep things consistent.
But then again, fudging the truth really only helps in politics, so I guess you're in the right :lol:
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Post Reply