PDA

View Full Version : OpenGL 4.x and MFC



anycast
11-14-2014, 03:50 PM
Hi,

I'm re-reading the OpenGL Superbible (6th edition) and I'm trying to get the examples to work under MFC. There's a simple example that simply renders a triangle and I'm having some difficulties getting it to run properly :/.

Here's my code in CView::OnCreate:



int CMFCApplication1View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here

m_hDC = ::GetDC(m_hWnd);

PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;

int nPixelFormat = ChoosePixelFormat(m_hDC, &pfd);

if (nPixelFormat == 0) return false;

BOOL bResult = SetPixelFormat(m_hDC, nPixelFormat, &pfd);

if (!bResult) return false;

HGLRC tempContext = wglCreateContext(m_hDC);
wglMakeCurrent(m_hDC, tempContext);

GLenum err = glewInit();
if (GLEW_OK != err)
{
AfxMessageBox(_T("GLEW is not initialized!"));
}

//Get a GL 4,4 context
int attribs[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
WGL_CONTEXT_MINOR_VERSION_ARB, 2,
WGL_CONTEXT_FLAGS_ARB, 0,
0
};

if (wglewIsSupported("WGL_ARB_create_context") == 1)
{
m_hRC = wglCreateContextAttribsARB(m_hDC, 0, attribs);
wglMakeCurrent(NULL, NULL);
wglDeleteContext(tempContext);
wglMakeCurrent(m_hDC, m_hRC);
}
else
{ //It's not possible to make a GL 4.x context. Use the old style context (GL 2.1 and before)
m_hRC = tempContext;
}


if (!m_hRC) return false;

static const char * vs_source[] =
{
"#version 420 core \n"
" \n"
"void main(void) \n"
"{ \n"
" const vec4 vertices[] = vec4[](vec4( 2.25, -2.25, 0.5, 1.0), \n"
" vec4(-2.25, -2.25, 0.5, 1.0), \n"
" vec4( 2.25, 2.25, 0.5, 1.0)); \n"
" \n"
" gl_Position = vertices[gl_VertexID]; \n"
"} \n"
};

static const char * fs_source[] =
{
"#version 420 core \n"
" \n"
"out vec4 color; \n"
" \n"
"void main(void) \n"
"{ \n"
" color = vec4(1.0, 0.8, 1.0, 1.0); \n"
"} \n"
};

program = glCreateProgram();
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, fs_source, NULL);
glCompileShader(fs);

GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, vs_source, NULL);
glCompileShader(vs);

glAttachShader(program, vs);
glAttachShader(program, fs);

glLinkProgram(program);

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);


return 0;
}



and in my OnDraw I do:


void CMFCApplication1View::OnDraw(CDC* /*pDC*/)
{
CMFCApplication1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;

// TODO: add draw code for native data here
static const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, green);

glUseProgram(program);
glDrawArrays(GL_TRIANGLES, 0, 3);

SwapBuffers(m_hDC);

GLenum error = glGetError();
}


All I'm getting is a nice green screen :).

Any ideas on what might be going on?

Cheers,
Alex