Need help with OpenGL and MFC

Started by
8 comments, last by iNsAn1tY 19 years, 7 months ago
Hi folks! I'm a total newbie in the field of OpenGL and MFC programming. I managed to run an OpenGL child window within a dialog window, added some controls to it and made some objects rotate by mouse. Now I have two problems: 1 - Where can I place OpenGL commands to set up the OpenGL scene and the camera? I tried to put them everywhere but nothing ever happened. 2 - Where can I create display lists? It's like with problem number one, I put them everywhere and nothing happened. I adapted my code from the tutorial "How to Create a CHILD OpenGL Window in a Dialog" by Marc Guy on www.codeguru.com I hope these questions are not too stupid and maybe someone can answer them. Thank you in advance, Elsenhans
Advertisement
Are you using the Document/View architecture, or not?
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
No, I don't use the Document/View architecture. I figured out that I've got a problem with the device and render contexts (OpenGL in dialog window). In my OnPaint() function the contexts are set like this:

void COpenGL::OnPaint()
{
CPaintDC dc(this);
HDC hdc = ::GetDC(m_hWnd);
HGLRC hglrc;
hglrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hglrc);

//OpenGL commands here
SwapBuffers(hdc);

wglMakeCurrent(NULL, NULL) ;
::ReleaseDC (m_hWnd, hdc) ;
wglDeleteContext(hglrc);
}

Everything works fine. Now I put OpenGL commands (setting up the scene and camera) enclosed by the same context stuff as in the OnPaint() function into other functions and nothing happens.
You should create your OpenGL context once and make it current (put it in OnCreate). Don't delete your OpenGL context until the app is done (put it in OnClose or OnDestroy). Put your drawing code in OnPaint (or have OnPaint call your drawing functions).
Yeah, like Dave says, you only need to create your rendering context (hRC) and select it with wglMakeCurrent once, in OnCreate. You delete it in OnDestroy. What you are doing in the code above is creating a hRC, doing something to it, then destroying it. It's not persistant, so changes you make to a hRC in one function aren't carried into other functions. In MFC code that uses OnDraw to render, instead of OnPaint, you will see that at the start of a function that uses OpenGL, people call wglMakeCurrent(hDC, hRC), and at the end of the function they call wglMakeCurrent(0, 0). This just deselects the rendering context, but it doesn't destroy it. It's an important difference. Your hRC should be a member variable of your CDialog.

OnCreate is where you put your initialization code for the window, so code for creating display lists would go in there too. Again, don't forget to delete the lists in OnDestroy.
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
I changed my code the way you guys told me and it seems to work. I can execute OpenGL commands in other functions now. But there is another problem: From within the OnPaint() function I'm only able to change the background color of my OpenGL window. I cannot see the white rectangle which should be displayed now. What am I doing wrong now? Sorry for this stupid newbie question.



int COpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;
m_pCDC= new CClientDC(this);
MySetPixelFormat(m_pCDC->GetSafeHdc());
m_hRC = wglCreateContext(m_pCDC->GetSafeHdc());
wglMakeCurrent(m_pCDC->GetSafeHdc(), m_hRC);

glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

return 0;
}

void COpenGL::OnPaint()
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glFlush();

SwapBuffers(m_pCDC->GetSafeHdc());
}

void COpenGL::OnDestroy()
{
wglMakeCurrent(NULL, NULL);
if (m_hRC!=NULL) ::wglDeleteContext(m_hRC);
if(m_pCDC) delete m_pCDC;
CWnd::OnDestroy();
}
Quote:Original post by Elsenhans2
int COpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;
m_pCDC= new CClientDC(this);
MySetPixelFormat(m_pCDC->GetSafeHdc());
m_hRC = wglCreateContext(m_pCDC->GetSafeHdc());
wglMakeCurrent(m_pCDC->GetSafeHdc(), m_hRC);

glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

return 0;
}


Looks like you're not returning to the modelview stack after you set the projection.

...glOrtho(...);glMatrixMode(GL_MODELVIEW);glLoadIdentity();return 0;}
Why does the login never stay?! Seems I always end up with an anonymous post :(
I set the clouds in motion, turn up light and sound...Activate the window, and watch the world go 'round
It has nothing to do with returning to the modelview stack. I put these commands in and still I see just a black window. When I make the context current within OnPaint(), add the camera settings and delete the context after that everything works fine. Making the context current in OnCreate() and deleting it in OnDestroy() seems to have no effect. I can only change the background color but cannot see the white rectangle which should be displayed in the OpenGL window.
You see that call you make to glOrtho in OnCreate? That's completely wrong. It creates an orthographic projection that's impossibly small (1 pixel in size). No viewport is established either. You need to add an OnSize handler, and use the following code to create an orthographic projection:
glViewport(0, 0, cx, cy);    glMatrixMode(GL_PROJECTION);glLoadIdentity();    glOrtho(0, cx, 0, cy, -1.0f, 1.0f);    glMatrixMode(GL_MODELVIEW);glLoadIdentity();
This way, every time the window changes size, your projection adjusts to cope. There are certain fundamental things you must do for OpenGL to display properly. Creating a viewport and setting a projection is one of them. I suggest that you get a good OpenGL manual (for Win32, you won't find one for MFC; I learnt from The OpenGL Superbible), or look at NeHe's more basic tutorials. Either will show you what you need to do, and the order you need to do it in, to get OpenGL working properly. Translating that to MFC shouldn't be too much of a challenge after that...

EDIT: There's also this GameDev tutorial that pretty much covers setting up OpenGL in MFC...
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]

This topic is closed to new replies.

Advertisement