Following code is
OpenGL initialization example using WGL.
First, it creates a GL surface for rendering and associate it with
current windows Device Context. WGL(an Windows version of EGL) is
used to perform this work. [tag:
OpenGL Initialization]
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
// Adjust the window size.
RECT kRect = { 0, 0, g_iWidth-1, g_iHeight-1 };
AdjustWindowRect(&kRect, WS_OVERLAPPEDWINDOW, true);
hInst = hInstance;
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
0, 0, kRect.right - kRect.left + 1, kRect.bottom - kRect.top + 1, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
// Get DC for OpenGL.
g_hDC = GetDC(hWnd);
{
// Select format for drawing surface.
PIXELFORMATDESCRIPTOR kPFD;
int iPixelFormat;
BOOL bSuccess;
memset(&kPFD, 0, sizeof(PIXELFORMATDESCRIPTOR));
kPFD.nSize = sizeof(PIXELFORMATDESCRIPTOR);
kPFD.nVersion = 1;
kPFD.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_GENERIC_ACCELERATED |
PFD_DOUBLEBUFFER;
kPFD.iPixelType = PFD_TYPE_RGBA;
kPFD.cColorBits = 24;
kPFD.cDepthBits = 16;
kPFD.cStencilBits = 8;
// Choose pixel format.
iPixelFormat = ChoosePixelFormat(g_hDC, &kPFD);
if( iPixelFormat == 0 )
{
ReleaseDC(hWnd, g_hDC);
return -1;
}
// Set pixel format.
bSuccess = SetPixelFormat(g_hDC, iPixelFormat, &kPFD);
if( !bSuccess )
{
ReleaseDC(hWnd, g_hDC);
return -1;
}
// Create openGL context.
g_hGLRC = wglCreateContext(g_hDC);
if( !g_hGLRC )
{
ReleaseDC(hWnd, g_hDC);
return -3;
}
// Make current context.
bSuccess = wglMakeCurrent(g_hDC, g_hGLRC);
if( !bSuccess )
{
wglDeleteContext(g_hGLRC);
ReleaseDC(hWnd, g_hDC);
return -4;
}
// Initialize OpenGL Environment.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set viewport.
glViewport(0, 0, g_iWidth, g_iHeight);
// Set ModelView matrix.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set clear color.
glClearColor(0.2f, 0.0f, 0.0f, 1.0f);
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}