Page 1 of 3
Again with OpenGL
Posted: Sun Jan 08, 2012 1:40 pm
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?
Re: Again with OpenGL
Posted: Sun Jan 08, 2012 1:49 pm
by short
Re: Again with OpenGL
Posted: Sun Jan 08, 2012 1:54 pm
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.
Re: Again with OpenGL
Posted: Sun Jan 08, 2012 7:02 pm
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.
Re: Again with OpenGL
Posted: Sun Jan 08, 2012 7:19 pm
by Benjamin100
Thank you.
Also, does "freeglut" work the same as Glut?
Re: Again with OpenGL
Posted: Sun Jan 08, 2012 9:17 pm
by dr-snipe
Yes, freeglut works the same as glut. It's the replacement for it that started back in '99.
Re: Again with OpenGL
Posted: Sun Jan 08, 2012 9:23 pm
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.
Re: Again with OpenGL
Posted: Sun Jan 08, 2012 10:01 pm
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?
Re: Again with OpenGL
Posted: Sun Jan 08, 2012 10:03 pm
by Benjamin100
So now some questions...
What, more specifically, is the point of matrix mode and projection and "glOrtho"?
Re: Again with OpenGL
Posted: Mon Jan 09, 2012 8:57 am
by Ginto8
You certainly have a knack for asking questions with complicated answers.
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.
Re: Again with OpenGL
Posted: Mon Jan 09, 2012 12:33 pm
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.
Re: Again with OpenGL
Posted: Mon Jan 09, 2012 1:33 pm
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.
Re: Again with OpenGL
Posted: Mon Jan 09, 2012 6:41 pm
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?
Re: Again with OpenGL
Posted: Mon Jan 09, 2012 6:42 pm
by Benjamin100
So does view space only have an x and y, or does it have a z within the use of the library?
Re: Again with OpenGL
Posted: Mon Jan 09, 2012 8:31 pm
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