Running OpenGL3.0 – part1

Welcome to a series I have planned on OpenGL 3.0 regarding the forward compatible mode, or in layman’s terms, using OpenGLwithout all the stuff that got cut.
It’s quite a different beast to work with, even I have problems getting my head around it since so much things that you used for every other line has to be done differently, I find myself adding the line glTranslatef(…. only to suddenly stop thinking “o, right, doesn’t work anymore”.

It’s a bit harder also as you have to use shaders or it won’t be drawn, you cant just use a VBO like before but instead you have to bind it to a input variable.
In short everything is different, but I will try to guide you trough the basic stuff, but look at it from the bright side, now you have no more excuse not to code the way it should be done.

So what got cut well quite a bit, take a look at http://www.opengl.org/documentation/specs/ and download the 3.0 specification and then check under the section called “The Deprecation Model”, but here are some highlights

Immediate mode – glBegin, glEnd, glColor*, glFogCoord*, glIndex*, glNormal3*, glSecondaryColor3*, glTexCoord*, glVertex*.
Which means you have to use VBOs.

Vertex processing – glColorPointer, glEdgeFlag-Pointer, glFogCoordPointer, glIndexPointer, glNormalPointer, glSecondary-ColorPointer, glTexCoordPointer, glVertexPointer, glEnableClientState, glDisableClientState, and glClientActiveTexture.
Which means you have you use a vertex shader in order to render.

Matrix processing – glFrustum, glLoadIdentity,glLoadMatrix, glLoadTransposeMatrix, glMatrixMode, glMult-Matrix, glMultTransposeMatrix, glOrtho, glPopMatrix, glPushMatrix, glRotate, glScale, and glTranslate.
Which means you have to calculate all translations and rotations yourself.

Fixed function stuff – like alpha testing and lighting is now done by the shaders, but not blending.

Also display lists, evaluators, accumulation buffers and selection and feedback render modes, but they shouldn’t be used anyway.

Ok, so how about some code then, how do we set up a window so that all the old stuff doesn’t work.
It turns out it’s not that straight forward.

Essentially what we need to do is to call wglCreateContextAttribsARB instead of wglCreateContext, the tricky part is that wglCreateContextAttribsARB is an extension and to be able to use it with the help of wglGetProcAddress we need a rendering context, which is exactly what wglCreateContextAttribsARB does, so we need to trick it.

First we need to define our attribute list, basically a bunch of variables that dictate which version we need to run.


int attribList[8] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
WGL_CONTEXT_MINOR_VERSION_ARB, 0,
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
0, 0
};

Note that if you comment out the third line in this array it will allow you to use all the deprecated stuff, a good tip to check if all of this works.

Next we create a temporary context and make it current.

HGLRC tempContext = wglCreateContext(window->hDC);
wglMakeCurrent(window->hDC,tempContext);

then get the proc address for wglCreateContextAttribsARB and check if it works.


PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
 

wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress("wglCreateContextAttribsARB");

if(wglCreateContextAttribsARB == NULL) //OpenGL 3.0 is not supported
{
MessageBox(NULL,"Cannot get Proc Address for CreateContextAttribs", "ERROR",MB_OK);
wglDeleteContext(tempContext);

}

finally create the new 3.0 context delete the temp context and make the new once current

window->hRC=wglCreateContextAttribsARB(window->hDC,0, attribList);
wglDeleteContext(tempContext);
wglMakeCurrent (window->hDC, window->hRC);

and that should be it, though implementation may wary from code to code, this one is tested against the base code use in my previous tutorials and will probably also work with the old nehe base code.

In part 2 I will try to render something on the screen.

Part 2 | Part 3


1 Comment

  • By Peter Wallström, May 4, 2010 @ 17:06

    -old comments-

    Comment by MZIskandar on 2009:06:12 00:58
    Nice explanation.. I’m waiting for part 2.

    Comment by X on 2009:07:07 00:02
    Thanks for your hard work!

    Looking forward to more ;)

    Comment by pommes on 2010:03:15 18:44
    Hey

    Can I download this code in a projectfile somewhere or do I have to change it in your basecode?

    Comment by Overlord on 2010:03:15 21:23
    Yea, you have to change the base code as i don’t have any specific code for this tutorial.
    This tutorial was written as a bit more general than usual so you can apply this to any basecode or update your existing application.

Other Links to this Post

RSS feed for comments on this post. TrackBack URI

Leave a comment


WordPress Themes