bool GLGraphicsEngine::Init()
{
// OutputDebugString(L"GL Init called\n");
// now that we have a window, setup the pixel format descriptor
m_glHDC = GetDC(m_hWnd);
int height, width, x, y;
x = m_MainWindow->GetClientX();
y = m_MainWindow->GetClientY();
height = m_MainWindow->GetClientHeight();
width = m_MainWindow->GetClientWidth();
std::wstringstream ws;
ws << "X: " << x << "\tY: " << y << "\tHeight: " << height << "\tWidth: " << width << std::endl;
OutputDebugString(ws.str().c_str());
// Set a dummy pixel format so that we can get access to wgl functions
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory(&pfd, sizeof(pfd));
GLint iPixelFormat = 1;
if(!SetPixelFormat(m_glHDC, iPixelFormat, &pfd/*&m_glpfd*/))
{
OutputDebugString(L"SetPixelFormatFailed");
return false;
}
// Create OGL context and make it current
m_glRC = wglCreateContext( m_glHDC );
wglMakeCurrent( m_glHDC, m_glRC );
if (m_glHDC == 0 || m_glRC == 0)
{
OutputDebugString(L"An error occured creating an OpenGL window.\n");
return false;
}
// Setup GLEW which loads OGL function pointers
GLenum err = glewInit();
if(GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
std::cerr << "GLEW Init Error: " << glewGetErrorString(err) << std::endl;
return false;
}
m_oglVersion = glGetString(GL_VERSION);
#ifdef _DEBUG
DebugPrintVersion();
#endif
// Now that extensions are setup,
// delete window and start over picking a real format.
wglMakeCurrent(NULL, NULL);
wglDeleteContext(m_glRC);
ReleaseDC(m_hWnd, m_glHDC);
DestroyWindow(m_hWnd);
// Create the window again
m_hWnd = m_MainWindow->InitWindow();
m_glHDC = GetDC(m_hWnd);
int nPixCount =0;
int nPixelFormat;
// Specify the important attributes we care about
int pixAttribs[] =
{
WGL_SUPPORT_OPENGL_ARB, 1, // Must support OGL rendering
WGL_DRAW_TO_WINDOW_ARB, 1, // pf that can run a window
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB, // must be HW accelerated
WGL_COLOR_BITS_ARB, 24, // 8 bits of each R, G and B
WGL_DEPTH_BITS_ARB, 16, // 16 bits of depth precision for window
WGL_DOUBLE_BUFFER_ARB, GL_TRUE, // Double buffered context
WGL_SAMPLE_BUFFERS_ARB, GL_TRUE, // MSAA on
WGL_SAMPLES_ARB, 8, // 8x MSAA
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, // pf should be RGBA type
0 // NULL termination
};
// Ask OpenGL to find the most relevant format matching our attribs
// Only get one format back.
BOOL lolRet = wglChoosePixelFormatARB(m_glHDC, pixAttribs, NULL, 1, &nPixelFormat, (UINT*)&nPixCount);
if(nPixelFormat != -1)
{
if (!lolRet)
OutputDebugString(L"wglChoosePixelFormatARB returns FALSE\n");
if (nPixelFormat == 0)
OutputDebugString(L"nPixelFormat is 0!\n");
// Got a format, now set it as the current one
SetPixelFormat(m_glHDC, nPixelFormat, &m_glpfd);
int attribs[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, 2,
WGL_CONTEXT_MINOR_VERSION_ARB, 2,
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
0
};
OutputDebugString(L"Pre ARB checking...\n");
if(wglewIsSupported("WGL_ARB_create_context") == 1)
OutputDebugString(L"wglCreateContextAttribsARB supported!\n");
else
OutputDebugString(L"wglCreateContextAttribsARB not supported!\n");
// m_glRC = wglCreateContext( m_glHDC );//wglCreateContextAttribsARB(m_glHDC, 0, NULL);
m_glRC = wglCreateContextAttribsARB(m_glHDC, 0, attribs);
if (m_glRC == NULL)
{
// Handle Error . . .
DWORD err = GetLastError();
std::wstringstream ws3;
ws3 << "Rendering context is null: " << err << std::endl;
OutputDebugString(ws3.str().c_str());
}
wglMakeCurrent( m_glHDC, m_glRC );
}
ShowWindow( m_hWnd, SW_SHOW );
SetForegroundWindow( m_hWnd );
SetFocus( m_hWnd );
return true;
}