일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- probability
- 몽고디비
- 인공지능
- c++
- 빅 데이터
- 김양재 목사
- 빅데이타
- nodeJS
- data science
- Artificial Intelligence
- Machine Learning
- No SQL
- MongoDB
- openCV
- 통계
- 김양재 목사님
- 확률
- WebGL
- 딥러닝
- 우리들교회
- Deep learning
- 주일설교
- Statistics
- 데이터 과학
- 김양재
- Big Data
- 빅 데이타
- node.js
- 빅데이터
- R
- Today
- Total
Scientific Computing & Data Science
[OpenGL] MFC-OpenGL 연동하기 본문
개발환경
Visual C++ V11 (2012)
GLUT V3.7.6 Download
프로젝트 생성
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