◆ 윈도우즈 OPENGL - 창 만들기
장치 컨텍스트 얻기 및 해제
- 장치 컨텍스를 얻고 드로잉 후 가능한 빨리 해제 하는 것이 좋음 (시스템 자원 관리)
HDC hDC = GetDC(hWnd);
ReleaseDC(hWnd, hDC);
윈도우즈 OPenGL 프로그램을 만들기 위한 단계를 다음과 같다.
[step 1] 랜더링 컨텍스트 초기화
[step 2] 픽셀 포맷 지정
[step 3] 랜더링 컨텍스트 활성화
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 | int COpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; int nPixelFormat; // Pixel format index m_hDC = ::GetDC(m_hWnd); // Get the Device context static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure 1, // Version of this structure PFD_DRAW_TO_WINDOW | // Draw to Window (not to bitmap) PFD_SUPPORT_OPENGL | // Support OpenGL calls in window PFD_DOUBLEBUFFER, // Double buffered mode PFD_TYPE_RGBA, // RGBA Color mode 24, // Want 24bit color 0,0,0,0, 0,0,0,0, 0,0,0,0,0, // Not used to select mode 32, // Size of depth buffer 0,0, PFD_MAIN_PLANE, // Draw in main plane 0,0,0,0}; // Not used to select mode // Choose a pixel format that best matches that described in pfd nPixelFormat = ChoosePixelFormat(m_hDC, &pfd); // Set the pixel format for the device context VERIFY(SetPixelFormat(m_hDC, nPixelFormat, &pfd)); // Create the rendering context m_hRC = wglCreateContext(m_hDC); // Make the rendering context current, perform initialization, then // deselect it VERIFY(wglMakeCurrent(m_hDC,m_hRC)); GLSetupRC(m_hDC); wglMakeCurrent(NULL,NULL); // Create the palette if needed InitializePalette(); return 0; } |
[step 4] 드로잉 코드 호출
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | void COpenGLView::OnDraw(CDC* /*pDC*/) { CProjectOpenGLDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: 여기에 원시 데이터에 대한 그리기 코드를 추가합니다. // Make the rendering context current wglMakeCurrent(m_hDC,m_hRC); // Call our external OpenGL code GLRenderScene(); // Swap our scene to the front SwapBuffers(m_hDC); // Allow other rendering contexts to co-exist wglMakeCurrent(m_hDC,NULL); } |
1 2 3 4 5 | // OpenGL의 영역을 그립니다. void GLRenderScene() { //OpenGL 드로잉 코드 } |
[step 5] 종료시 현재 렌더링 컨텍스트 해제 및 제거
1 2 3 4 5 6 7 8 9 | void OnDestroy() { // Clean up rendering context stuff wglDeleteContext(m_hRC); ::ReleaseDC(m_hWnd,m_hDC); CView::OnDestroy(); // TODO: 여기에 메시지 처리기 코드를 추가합니다. } |
주의사항
윈도우즈 프로그래밍에서 창을 만들때 반드시 아래 스타일을 선언해야 함
- WS_CLIPCHILDREN | WS_CLIPSIBLINGS
- OpenGL 랜더링 컨텍스트가 다른 창에 렌더링 하는 것을 막기 위함
GDI 렌더링과 OpenGL 이중 버퍼 페이지 전환 시 모든 장치 컨텍스트가 필요하기 때문에 창스타일에 CS_OWNDC 설정 필요
1 2 3 4 5 6 7 | BOOL PreCreateWindow(CREATESTRUCT& cs) { // TODO: CREATESTRUCT cs를 수정하여 여기에서 // Window 클래스 또는 스타일을 수정합니다. cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS | CS_OWNDC); return CView::PreCreateWindow(cs); } |
1 2 3 4 5 6 7 | BOOL COpenGLView::OnEraseBkgnd(CDC* pDC) { // TODO: 여기에 메시지 처리기 코드를 추가 및/또는 기본값을 호출합니다. // return CView::OnEraseBkgnd(pDC); return true; } |
'OpenGL' 카테고리의 다른 글
OpenGL: Setting Clipping region (0) | 2014.04.19 |
---|---|
OpenGL: Basic knowledge for 3D programing (0) | 2014.04.19 |
Windows OpenGL: Rendering Context (0) | 2014.04.19 |
Windows OpenGL: Setting and selecting Pixel format (0) | 2014.04.19 |
OPENGL color define (0) | 2014.04.16 |
◆ 윈도우즈 OPENGL - 랜더링 컨텍스트
OpenGL 렌더링 컨텍스트: OpenGL 설정과 명령을 기억함
OpenGL 명령은 작업할 목적지 창을 알아야 한 스레드 당 하나의 렌더링 컨텍스트만을 활성화 할 수 있음
- 따라서 OpenGL 명령 이전 렌더링 창을 지정해야 함
[step 1] HGLRC hRC; // OpenGL 랜더링 컨텍스트 핸들러 변수 선언
[step 2] HDC hDC; // 개별적인 윈도우즈 장치 컨텍스트 선언
[step 3] 픽셀 포맷 지정 // 표현할 창의 속성 정의
[step 4] hRC = wglCreateContext(hDC); // OpenGL 랜더링 컨텍스트 핸들러 생성(개별적인 윈도우즈 장치 컨텍스트)
[step 5] wglMakeCurrent(hDC, hRC); // OpenGL 렌더링 컨텍스를 활성화하고 창의 장치컨텍스트와 연결
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 | HGLRC m_hRC; //Rendering Context HDC m_hDC; // Device Context m_hDC = ::GetDC(m_hWnd); // Get the Device context int nPixelFormat; // Pixel format index static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR),// Size of this structure 1, // Version of this structure PFD_DRAW_TO_WINDOW | // Draw to Window (not to bitmap) PFD_SUPPORT_OPENGL | // Support OpenGL calls in window PFD_DOUBLEBUFFER, // Double buffered mode PFD_TYPE_RGBA, // RGBA Color mode 24, // Want 24bit color 0,0,0,0, 0,0,0,0, 0,0,0,0,0, // Not used to select mode 32, // Size of depth buffer 0,0, PFD_MAIN_PLANE, // Draw in main plane 0,0,0,0}; // Not used to select mode // Choose a pixel format that best matches that described in pfd nPixelFormat = ChoosePixelFormat(m_hDC, &pfd); // Set the pixel format for the device context VERIFY(SetPixelFormat(m_hDC, nPixelFormat, &pfd)); // Create the rendering context m_hRC = wglCreateContext(m_hDC); // Make the rendering context current, perform initialization, then // deselect it VERIFY(wglMakeCurrent(m_hDC,m_hRC)); |
윈도우즈 프로그램은 다수의 창을 포함함. 즉, 각각 창들이 자신의 장치 컨텍스트를 가지고 이는 개별적은 픽셀 포맷을 가질 수 있음
- 윈도우즈 GDI 함수(장치 컨텍스트): GDI를 위한 드로잉 모드와 명령을 기억함 즉, OPenGL 랜더링 컨텍스트와 유사함
결론: OpenGL 랜더링 컨텍스트를 통해서 화면을 제어하는데 윈도우즈 프로그램에서 이를 활용하기 위해서는 윈도우즈 장비 컨텍스트를 OpenGL 랜더링 컨텍스트와 연결 해줘야 제어가 가능함.
'OpenGL' 카테고리의 다른 글
OpenGL: Basic knowledge for 3D programing (0) | 2014.04.19 |
---|---|
Windows OpenGL: Making Window Application with OpenGL (0) | 2014.04.19 |
Windows OpenGL: Setting and selecting Pixel format (0) | 2014.04.19 |
OPENGL color define (0) | 2014.04.16 |
Setting Up OpenGL in an MFC control (0) | 2014.04.15 |
◆ 윈도우즈 OPENGL - 픽셀 포맷 설정
필요에 맞는 픽셀 포맷을 찾기 위해 모든 픽셀 포맷을 나열하고 하나씩 확인하는 과정은 상당히 복잡함
따라서 단축함수 ChoosePixelFormat를 제공
- 먼저 사용자가 원하는 3D 창의 속성들을 포함하는 픽셀 포맷 구조체를 만듬
- ChoosePixelFormat는 만들어진 사용자 요구 픽셀 포맷 구조체에 가장 유사한 픽셀 포맷을 찾아 이에 해당하는 Index를 리턴함
- 이 index를 SetPixelFormat에 전달하여 픽셀 포맷을 지정함
[step 1] 원하는 PIXELFORMATDESCRIPTOR을 만듬
[step 2] ChoosePixelFormat 로 유사한 픽셀 포맷 Index를 얻음
[step 3] SetPixelFormat에 획득한 index를 넣어 픽셀 포맷을 지정
----소스 코드
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 | // Select the pixel format for a given device context void SetDCPixelFormat(HDC hDC) { int nPixelFormat; static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure 1, // Version of this structure PFD_DRAW_TO_WINDOW | // Draw to Window (not to bitmap) PFD_SUPPORT_OPENGL | // Support OpenGL calls in window PFD_DOUBLEBUFFER, // Double buffered mode PFD_TYPE_RGBA, // RGBA Color mode 8, // Want 8 bit color 0,0,0,0,0,0, // Not used to select mode 0,0, // Not used to select mode 0,0,0,0,0, // Not used to select mode 16, // Size of depth buffer 0, // Not used to select mode 0, // Not used to select mode PFD_MAIN_PLANE, // Draw in main plane 0, // Not used to select mode 0,0,0 }; // Not used to select mode // Choose a pixel format that best matches that described in pfd nPixelFormat = ChoosePixelFormat(hDC, &pfd); // Set the pixel format for the device context SetPixelFormat(hDC, nPixelFormat, &pfd); } |
'OpenGL' 카테고리의 다른 글
OpenGL: Basic knowledge for 3D programing (0) | 2014.04.19 |
---|---|
Windows OpenGL: Making Window Application with OpenGL (0) | 2014.04.19 |
Windows OpenGL: Rendering Context (0) | 2014.04.19 |
OPENGL color define (0) | 2014.04.16 |
Setting Up OpenGL in an MFC control (0) | 2014.04.15 |
#declare Red = rgb <1, 0, 0>
#declare Green = rgb <0, 1, 0>
#declare Blue = rgb <0, 0, 1>
#declare Yellow = rgb <1,1,0>
#declare Cyan = rgb <0, 1, 1>
#declare Magenta = rgb <1, 0, 1>
#declare Clear = rgbf 1
#declare White = rgb 1
#declare Black = rgb 0
// These grays are useful for fine-tuning lighting color values
// and for other areas where subtle variations of grays are needed.
// PERCENTAGE GRAYS:
#declare Gray05 = White*0.05
#declare Gray10 = White*0.10
#declare Gray15 = White*0.15
#declare Gray20 = White*0.20
#declare Gray25 = White*0.25
#declare Gray30 = White*0.30
#declare Gray35 = White*0.35
#declare Gray40 = White*0.40
#declare Gray45 = White*0.45
#declare Gray50 = White*0.50
#declare Gray55 = White*0.55
#declare Gray60 = White*0.60
#declare Gray65 = White*0.65
#declare Gray70 = White*0.70
#declare Gray75 = White*0.75
#declare Gray80 = White*0.80
#declare Gray85 = White*0.85
#declare Gray90 = White*0.90
#declare Gray95 = White*0.95
// OTHER GRAYS
#declare DimGray = color red 0.329412 green 0.329412 blue 0.329412
#declare DimGrey = color red 0.329412 green 0.329412 blue 0.329412
#declare Gray = color red 0.752941 green 0.752941 blue 0.752941
#declare Grey = color red 0.752941 green 0.752941 blue 0.752941
#declare LightGray = color red 0.658824 green 0.658824 blue 0.658824
#declare LightGrey = color red 0.658824 green 0.658824 blue 0.658824
#declare VLightGray = color red 0.80 green 0.80 blue 0.80
#declare VLightGrey = color red 0.80 green 0.80 blue 0.80
#declare Aquamarine = color red 0.439216 green 0.858824 blue 0.576471
#declare BlueViolet = color red 0.62352 green 0.372549 blue 0.623529
#declare Brown = color red 0.647059 green 0.164706 blue 0.164706
#declare CadetBlue = color red 0.372549 green 0.623529 blue 0.623529
#declare Coral = color red 1.0 green 0.498039 blue 0.0
#declare CornflowerBlue = color red 0.258824 green 0.258824 blue 0.435294
#declare DarkGreen = color red 0.184314 green 0.309804 blue 0.184314
#declare DarkOliveGreen = color red 0.309804 green 0.309804 blue 0.184314
#declare DarkOrchid = color red 0.6 green 0.196078 blue 0.8
#declare DarkSlateBlue = color red 0.419608 green 0.137255 blue 0.556863
#declare DarkSlateGray = color red 0.184314 green 0.309804 blue 0.309804
#declare DarkSlateGrey = color red 0.184314 green 0.309804 blue 0.309804
#declare DarkTurquoise = color red 0.439216 green 0.576471 blue 0.858824
#declare Firebrick = color red 0.556863 green 0.137255 blue 0.137255
#declare ForestGreen = color red 0.137255 green 0.556863 blue 0.137255
#declare Gold = color red 0.8 green 0.498039 blue 0.196078
#declare Goldenrod = color red 0.858824 green 0.858824 blue 0.439216
#declare GreenYellow = color red 0.576471 green 0.858824 blue 0.439216
#declare IndianRed = color red 0.309804 green 0.184314 blue 0.184314
#declare Khaki = color red 0.623529 green 0.623529 blue 0.372549
#declare LightBlue = color red 0.74902 green 0.847059 blue 0.847059
#declare LightSteelBlue = color red 0.560784 green 0.560784 blue 0.737255
#declare LimeGreen = color red 0.196078 green 0.8 blue 0.196078
#declare Maroon = color red 0.556863 green 0.137255 blue 0.419608
#declare MediumAquamarine = color red 0.196078 green 0.8 blue 0.6
#declare MediumBlue = color red 0.196078 green 0.196078 blue 0.8
#declare MediumForestGreen = color red 0.419608 green 0.556863 blue 0.137255
#declare MediumGoldenrod = color red 0.917647 green 0.917647 blue 0.678431
#declare MediumOrchid = color red 0.576471 green 0.439216 blue 0.858824
#declare MediumSeaGreen = color red 0.258824 green 0.435294 blue 0.258824
#declare MediumSlateBlue = color red 0.498039 blue 1.0
#declare MediumSpringGreen = color red 0.498039 green 1.0
#declare MediumTurquoise = color red 0.439216 green 0.858824 blue 0.858824
#declare MediumVioletRed = color red 0.858824 green 0.439216 blue 0.576471
#declare MidnightBlue = color red 0.184314 green 0.184314 blue 0.309804
#declare Navy = color red 0.137255 green 0.137255 blue 0.556863
#declare NavyBlue = color red 0.137255 green 0.137255 blue 0.556863
#declare Orange = color red 1 green 0.5 blue 0.0
#declare OrangeRed = color red 1.0 green 0.25
#declare Orchid = color red 0.858824 green 0.439216 blue 0.858824
#declare PaleGreen = color red 0.560784 green 0.737255 blue 0.560784
#declare Pink = color red 0.737255 green 0.560784 blue 0.560784
#declare Plum = color red 0.917647 green 0.678431 blue 0.917647
#declare Salmon = color red 0.435294 green 0.258824 blue 0.258824
#declare SeaGreen = color red 0.137255 green 0.556863 blue 0.419608
#declare Sienna = color red 0.556863 green 0.419608 blue 0.137255
#declare SkyBlue = color red 0.196078 green 0.6 blue 0.8
#declare SlateBlue = color green 0.498039 blue 1.0
#declare SpringGreen = color green 1.0 blue 0.498039
#declare SteelBlue = color red 0.137255 green 0.419608 blue 0.556863
#declare Tan = color red 0.858824 green 0.576471 blue 0.439216
#declare Thistle = color red 0.847059 green 0.74902 blue 0.847059
#declare Turquoise = color red 0.678431 green 0.917647 blue 0.917647
#declare Violet = color red 0.309804 green 0.184314 blue 0.309804
#declare VioletRed = color red 0.8 green 0.196078 blue 0.6
#declare Wheat = color red 0.847059 green 0.847059 blue 0.74902
#declare YellowGreen = color red 0.6 green 0.8 blue 0.196078
#declare SummerSky = color red 0.22 green 0.69 blue 0.87
#declare RichBlue = color red 0.35 green 0.35 blue 0.67
#declare Brass = color red 0.71 green 0.65 blue 0.26
#declare Copper = color red 0.72 green 0.45 blue 0.20
#declare Bronze = color red 0.55 green 0.47 blue 0.14
#declare Bronze2 = color red 0.65 green 0.49 blue 0.24
#declare Silver = color red 0.90 green 0.91 blue 0.98
#declare BrightGold = color red 0.85 green 0.85 blue 0.10
#declare OldGold = color red 0.81 green 0.71 blue 0.23
#declare Feldspar = color red 0.82 green 0.57 blue 0.46
#declare Quartz = color red 0.85 green 0.85 blue 0.95
#declare Mica = color Black // needed in textures.inc
#declare NeonPink = color red 1.00 green 0.43 blue 0.78
#declare DarkPurple = color red 0.53 green 0.12 blue 0.47
#declare NeonBlue = color red 0.30 green 0.30 blue 1.00
#declare CoolCopper = color red 0.85 green 0.53 blue 0.10
#declare MandarinOrange = color red 0.89 green 0.47 blue 0.20
#declare LightWood = color red 0.91 green 0.76 blue 0.65
#declare MediumWood = color red 0.65 green 0.50 blue 0.39
#declare DarkWood = color red 0.52 green 0.37 blue 0.26
#declare SpicyPink = color red 1.00 green 0.11 blue 0.68
#declare SemiSweetChoc = color red 0.42 green 0.26 blue 0.15
#declare BakersChoc = color red 0.36 green 0.20 blue 0.09
#declare Flesh = color red 0.96 green 0.80 blue 0.69
#declare NewTan = color red 0.92 green 0.78 blue 0.62
#declare NewMidnightBlue = color red 0.00 green 0.00 blue 0.61
#declare VeryDarkBrown = color red 0.35 green 0.16 blue 0.14
#declare DarkBrown = color red 0.36 green 0.25 blue 0.20
#declare DarkTan = color red 0.59 green 0.41 blue 0.31
#declare GreenCopper = color red 0.32 green 0.49 blue 0.46
#declare DkGreenCopper = color red 0.29 green 0.46 blue 0.43
#declare DustyRose = color red 0.52 green 0.39 blue 0.39
#declare HuntersGreen = color red 0.13 green 0.37 blue 0.31
#declare Scarlet = color red 0.55 green 0.09 blue 0.09
#declare Med_Purple = colour red 0.73 green 0.16 blue 0.96
#declare Light_Purple = colour red 0.87 green 0.58 blue 0.98
#declare Very_Light_Purple = colour red 0.94 green 0.81 blue 0.99
http://www.opengl.org/discussion_boards/showthread.php/132502-Color-tables
http://www.dreamincode.net/forums/topic/177850-rgb-color-chart-for-opengl/
There's a linear mapping. That is, you can think of the values to be x/255. So if the R value is 255, fractionally, that's 255/255 = 1.0f. Do you see now?
'OpenGL' 카테고리의 다른 글
OpenGL: Basic knowledge for 3D programing (0) | 2014.04.19 |
---|---|
Windows OpenGL: Making Window Application with OpenGL (0) | 2014.04.19 |
Windows OpenGL: Rendering Context (0) | 2014.04.19 |
Windows OpenGL: Setting and selecting Pixel format (0) | 2014.04.19 |
Setting Up OpenGL in an MFC control (0) | 2014.04.15 |
빨강 링크로 가면 잘나와 있음
http://pickup79.tistory.com/entry/MFC-OpenGL-%EC%B4%88%EA%B8%B0%ED%99%94
http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c10975/Setting-Up-OpenGL-in-an-MFC-Control.htm
***************************
http://www.codeproject.com/Tips/569928/OpenGL-MFC-CDialog-Basic
Points of Interest
I searched, Googled & read a lot, but failed to come across a complete example that displayed the above without using glBegin()
/glEnd()
and/or int main()
/winmain()
as the entry point. Very frustrating.
History
I don't plan on maintaining this, unless something serious warrants updating. There is probably more efficient ways to implement this. I just wanted to share this as I struggled to find something similar that contained a full yet basic working example.
glBegin()
/glEnd()
and/or int main()
/winmain()
흠 그렇군... 바이블책 신나게 앞장에 보다가 MFC에 어떻게 연결시키지 열심히 구글링 했는데 바이블 책 13챕터에 윈도우 프로그래밍과 OPENGL 에서 설명하고 있구만 어허허허허
***************************
'OpenGL' 카테고리의 다른 글
OpenGL: Basic knowledge for 3D programing (0) | 2014.04.19 |
---|---|
Windows OpenGL: Making Window Application with OpenGL (0) | 2014.04.19 |
Windows OpenGL: Rendering Context (0) | 2014.04.19 |
Windows OpenGL: Setting and selecting Pixel format (0) | 2014.04.19 |
OPENGL color define (0) | 2014.04.16 |