PDA

View Full Version : Problem creating OpenGL3.0 context



Luuk van Venrooij
12-03-2009, 08:14 AM
I decided to experiment with some OpenGL 3.0. So I grabed the latest version of the dglOpenGL.pas headers and looked at some tutorials. The problem is that the main method for creating the OpenGL 3.0 context always fails. I mainly used this tutorial:

http://nehe.gamedev.net/wiki/GL30_Lesson01Win.ashx

This is the code I use to initialize my rendercontext:



procedure TGDWindow.InitOGL();
var
iPixelFormat : GLuint;
iPFD : TPIXELFORMATDESCRIPTOR;
iAttribList : array[0..5] of GLint;
iTempRC : HGLRC;
begin
InitOpenGL();

FDC := GetDC(FHandle);
if (FDC = 0) then
Raise Exception.Create('Failed to get a device context');

with iPFD do
begin
nSize := SizeOf(TPIXELFORMATDESCRIPTOR);
nVersion := 1;
dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
iPixelType := PFD_TYPE_RGBA;
cColorBits := 32;
cRedBits := 0;
cRedShift := 0;
cGreenBits := 0;
cGreenShift := 0;
cBlueBits := 0;
cBlueShift := 0;
cAlphaBits := 0;
cAlphaShift := 0;
cAccumBits := 0;
cAccumRedBits := 0;
cAccumGreenBits := 0;
cAccumBlueBits := 0;
cAccumAlphaBits := 0;
cDepthBits := 32;
cStencilBits := 0;
cAuxBuffers := 0;
iLayerType := PFD_MAIN_PLANE;
bReserved := 0;
dwLayerMask := 0;
dwVisibleMask := 0;
dwDamageMask := 0;
end;

iPixelFormat := ChoosePixelFormat(FDC, @iPFD);
if (iPixelFormat = 0) then
Raise Exception.Create('Failed to find a suitable pixel format');

if (not SetPixelFormat(FDC, iPixelFormat, @iPFD)) then
Raise Exception.Create('Failed to set the pixel format');

iTempRC := wglCreateContext(FDC);
if (iTempRC = 0) then
Raise Exception.Create('Failed to create an tempererary OpenGL rendering context');

if (not wglMakeCurrent(FDC, iTempRC)) then
Raise Exception.Create('Failed to activate tempererary OpenGL rendering context');

Read_WGL_ARB_create_context();
if Not(assigned(wglCreateContextAttribsARB)) then
raise Exception.Create('Opengl 3.0 is not supported');

iAttribList[0]:= WGL_CONTEXT_MAJOR_VERSION_ARB;
iAttribList[1]:= 3;
iAttribList[2]:= WGL_CONTEXT_MINOR_VERSION_ARB;
iAttribList[3]:= 0;
iAttribList[4]:= 0;//WGL_CONTEXT_FLAGS_ARB;
iAttribList[5]:= 0;//WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
FRC := wglCreateContextAttribsARB(FDC, 0, @iAttribList);
if (FRC = 0) then
Raise Exception.Create('Failed to create an OpenGL rendering context');

wglDeleteContext(iTempRC);
if (not wglMakeCurrent(FDC, FRC)) then
Raise Exception.Create('Failed to activate OpenGL rendering context');

ReadImplementationProperties();
ReadExtensions();

glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glDepthFunc(GL_LESS);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glEnable(GL_NORMALIZE);
end;


The problem is that the dglGetProcAddress in the Read_WGL_ARB_create_context function always returns nil. I have checked the names and function declarations and everything seems to be ok. I use Delphi 2009 and Windows Vista Ultimate 32 bit. Note that the Nehe code works fine so it doesnt seem to be a driver issue.

Greetz

Luuk

vgo
12-03-2009, 11:50 AM
You should call ReadOpenGLCore after InitOpenGL, I'm not sure if that's the problem though.

Lowercase
14-10-2009, 06:23 PM
what does the procedure Read_WGL_ARB_create_context() do ?

if you're using extentions here, they are automatically bound when you call ReadImplementationProperties(); and ReadExtensions();

I guess you need to create a dummy window, like the way you would do to initialize multisampling...