PDA

View Full Version : Problem : OpenGL with MFC Dialog.



kccc
07-21-2005, 11:33 PM
Hi to ALL,
I am a newbie in OpenGL programming.
i m trying to integrate OpenGL with MFC(Dialog Based Appn).

Problem :
1.OpenGL window gets created but i am not able to create any drawing in the window.
2. When i try to put SwapBuffers(hDC) in DrawGLScreen() function, my whole screen will get painted with black.


Can you please tell me, where my code goes wrong?

//----------MyAppDlg.cpp--------------------------

BOOL MyAPP : OnInitDialog()
{
CRect rect;
CMyOpenGL oMyOpenGL;
GetDlgItem(IDC_OPENGL_WIN)->GetWindowRect(rect); //IDC_OPENGL_WIN >> static control.
oMyOpenGL.Create(rect,this);
}

//-------MyOpenGL.cpp ----------------------------------

int CMyOpenGL::InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations*/
return TRUE;
}

void DrawHLine() ;
float fTranslate;
float fRotate;

void DrawHLine()
{
glBegin(GL_POLYGON);
glVertex2f(0.0, 0.0);
glVertex2f(0.0, 30.0f);
glVertex2f(50.0, 30.0f);
glVertex2f(50.0, 0.0);
glEnd();
}

int CMyOpenGL::DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix

//-----------------------------
//DRAWING CODE STARTS HERE
//-----------------------------

glPushMatrix();
glTranslatef(0.0f, 0.0f,-4.0f);
glRotatef(fRotate,0,0,1); //Rotate about Z axis
DrawHLine();
glPopMatrix();

fRotate += 0.5f;
//SwapBuffers(hDC); // Swap Buffers (Double Buffering)
return TRUE;
}

//Create Window
BOOL CMyOpenGL::Create(CRect rect, CWnd* pParentWnd)
{
CString szClassName = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_OWNDC,NULL,(HBRUSH)GetStockObject(BLACK_BRUSH), NULL);

//Create Window
CreateEx(
0,
szClassName,
"OpenGL",
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
rect,
pParentWnd,
0);
return TRUE;
}

BOOL CMyOpenGL::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default

return CWnd::OnEraseBkgnd(pDC);
}

int CMyOpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;

//OpenGl...............
GLuint PixelFormat;
static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
16, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};

hDC=::GetDC(hWnd))) //Get A Device Context?

PixelFormat=ChoosePixelFormat(hDC,&pfd))) //Find A Matching Pixel Format?

SetPixelFormat(hDC,PixelFormat,&pfd)) //Set The Pixel Format?

hRC=wglCreateContext(hDC))) //Get A Rendering Context?

wglMakeCurrent(hDC,hRC)) //Activate The Rendering Context

::ShowWindow(hWnd,SW_SHOW);
::SetForegroundWindow(hWnd);
::UpdateWindow(hWnd);
::SetFocus(hWnd);

iInitGL())
return 0;
}

void CMyOpenGL::OnSize(UINT nType, int cx, int cy) //cx:width, cy:height
{
CWnd::OnSize(nType, cx, cy);

GLsizei width,height;
width = cx;
height = cy;

if (cy==0) // Prevent A Divide By Zero By
{
cy=1;
}
wglMakeCurrent(hDC, hRC);

glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f,(GLfloat)width/ // Calculate The Aspect Ratio Of The Window
(GLfloat)height,0.1f,100.0f);
glTranslatef(0.0f,0.0f,-4.f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity();

}

void CMyOpenGL::OnPaint()
{
CPaintDC dc(this);
if (hRC)
{
wglMakeCurrent(NULL,NULL);
wglMakeCurrent(hDC, hRC);
}

DrawGLScene();
//SwapBuffers(hDC);
}

CWiC
07-22-2005, 11:30 PM
Try to start with OpenGL Basecode NeHeGL MFC, which you can download from http://nehe.gamedev.net/ or using direct link: http://nehe.gamedev.net/files/basecode/nehegl_mfc.zip