04-19 00:00
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[OpenGL] MFC-OpenGL 연동하기 본문

Programming/OpenGL

[OpenGL] MFC-OpenGL 연동하기

cinema4dr12 2014. 6. 11. 13:30

개발환경

  • Visual C++ V11 (2012)

  • GLUT V3.7.6 Download

glut-3.7.6-bin.zip


프로젝트 생성

  Templates

  MFC Application

  Name

  MFCOpenGL

  Application Type  Dialog Based

  Project Style

  MFC


OpenGL 개발환경 설정

  • 프로젝트 > Property Pages > C/C++ > General > Additional Include Directories : {GLUT_LIB_PATH}\glut-3.7.6-bin

  • 프로젝트 > Property Pages > Linker > General > Additional Library Directories : {GLUT_LIB_PATH}\glut-3.7.6-bin

  • 프로젝트 > Property Pages > Linker > Input > Additional Dependencies : glut32.lib 추가


CMGCOpenGLDlg Class에 멤버 추가

"MFCOpenGLDlg.h"

1
2
3
public:
    HGLRC m_hRC;
    HDC m_hDC;


CMFCOpenGLDlg 소스에 헤더 추가

"MFCOpenGLDlg.cpp"

1
2
3
#include <glut.h>
#include <gl\GL.h>
#include <gl\GLU.h>


OnCreate() 함수 작성

Class View > CMFCOpenGLDlg 선택

Properties > Messages > WM_CREATE > <Add>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
int CMFCOpenGLDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDialogEx::OnCreate(lpCreateStruct) == -1)
        return -1;
 
    // TODO:  Add your specialized creation code here
    int nPixelFormat;
    m_hDC = ::GetDC(m_hWnd);
 
    static PIXELFORMATDESCRIPTOR pfd =
    {
        sizeof(PIXELFORMATDESCRIPTOR),
        1,
        PFD_DRAW_TO_WINDOW |
        PFD_SUPPORT_OPENGL |
        PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,
        24,
        0, 0, 0, 0, 0, 0,
        0, 0,
        0, 0, 0, 0, 0,
        32,
        0,
        0,
        PFD_MAIN_PLANE,
        0,
        0, 0, 0
    };
 
    nPixelFormat = ChoosePixelFormat(m_hDC, &pfd);
 
    VERIFY(SetPixelFormat(m_hDC, nPixelFormat, &pfd));
 
    m_hRC = wglCreateContext(m_hDC);
 
    VERIFY(wglMakeCurrent(m_hDC, m_hRC));
 
    wglMakeCurrent(NULL, NULL);
 
    return 0;
}


GLResize() 함수 작성

Class View > CMFCOpenGLDlg 오른쪽 마우스 클릭

Add > Add Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void CMFCOpenGLDlg::GLResize(int cx, int cy)
{
    GLfloat fAspect;
 
    if(cy == 0) cy = 1;
 
    glViewport(0, 0, cx, cy);
 
    fAspect = (GLfloat) cx / (GLfloat) cy;
 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
 
    gluPerspective(60.0f, fAspect, 1.0f, 10000.0f);
 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}


OnSize() 함수 작성

Class View > CMFCOpenGLDlg 선택

Properties > Messages > WM_SIZE> <Add>

1
2
3
4
5
6
7
8
9
void CMFCOpenGLDlg::OnSize(UINT nType, int cx, int cy)
{
    CDialogEx::OnSize(nType, cx, cy);
 
    // TODO: Add your message handler code here
    VERIFY(wglMakeCurrent(m_hDC, m_hRC));
    GLResize(cx, cy);
    VERIFY(wglMakeCurrent(NULL, NULL));
}


GLRenderScene() 함수 작성

Class View > CMFCOpenGLDlg 오른쪽 마우스 클릭

Add > Add Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void CMFCOpenGLDlg::GLRenderScene(void)
{
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
    glColor3f(1.0f, 0.0f, 0.0f);
 
    glPushMatrix();
 
    glEnable(GL_DEPTH_TEST);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
 
    glShadeModel(GL_FLAT);
    glLoadIdentity();
 
    gluLookAt(0.0f, 0.0f, 1000.0f, 0.0f, 10.0f, 0.0f, 0.0f, 1.0f, 0.0f);
 
    glBegin(GL_TRIANGLES);
        glVertex3f(-100, 0, 0);
        glVertex3f(0, 100, 0);
        glVertex3f(100, 0, 0);
    glEnd();
 
    glPopMatrix();
    glFlush();
}


OnPaint() 함수 작성

Class View > CMFCOpenGLDlg 선택

Properties > Messages > WM_PAINT> <Add>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void CMFCOpenGLDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // 그리기를 위한 디바이스 컨텍스트입니다.
 
        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
 
        // 클라이언트 사각형에서 아이콘을 가운데에 맞춥니다.
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;
 
        // 아이콘을 그립니다.
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
 
        wglMakeCurrent(m_hDC, m_hRC);
        GLRenderScene();
        SwapBuffers(m_hDC);
        wglMakeCurrent(m_hDC, NULL);
    }
}


OnDestroy() 함수 작성

Class View > CMFCOpenGLDlg 선택

Properties > Messages > WM_DESTROY > <Add>

1
2
3
4
5
6
7
8
void CMFCOpenGLDlg::OnDestroy()
{
    CDialogEx::OnDestroy();
 
    // TODO: Add your message handler code here
    wglDeleteContext(m_hRC);
    ::ReleaseDC(m_hWnd, m_hDC);
}


빌드 및 실행

Ctrl + Shift + B

Ctrl + F5

Comments