[toc]

在c语言程序中使用openGL

配置环境

  1. 下载glut库
  2. 将头文件添加项目里,将静态库与动态库放置项目文件夹里
  3. 编译版本选择x86,库文件只支持x86不支持x64

使用openGL画一个茶壶

OpenGL(十六) 鼠标、键盘交互响应事件

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "glut.h"
#include <Windows.h>

GLfloat angle = 10.0f;
GLfloat xDirection = 0.0f;
GLfloat yDirection = 0.0f;
GLfloat zDirection = 10.0f;

void InitEnvironment()
{
glEnable(GL_DEPTH);
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor4f(0, 0, 1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65, 1, 1, 50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection, yDirection, zDirection, 0, 0, 0, 0, 1, 0);
}

void KeyBoards(unsigned char key, int x, int y)
{
switch (key)
{
case 'w':
glMatrixMode(GL_MODELVIEW);
glRotatef(angle, -1, 0, 0);
glutPostRedisplay();
break;
case 'a':
glMatrixMode(GL_MODELVIEW);
glRotatef(angle, 0, 0, -1);
glutPostRedisplay();
break;
case 's':
glMatrixMode(GL_MODELVIEW);
glRotatef(angle, 1, 0, 0);
glutPostRedisplay();
break;
case 'd':
glMatrixMode(GL_MODELVIEW);
glRotatef(angle, 0, 0, 1);
glutPostRedisplay();
break;
case '4':
xDirection += 0.5;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(65, 1, 1, 50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection, yDirection, zDirection, 0, 0, 0, 0, 1, 0);
glutPostRedisplay();
break;
case '5':
yDirection += 0.5;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(65, 1, 1, 50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection, yDirection, zDirection, 0, 0, 0, 0, 1, 0);
glutPostRedisplay();
break;

case '6':
zDirection += 0.5;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(65, 1, 1, 50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection, yDirection, zDirection, 0, 0, 0, 0, 1, 0);
glutPostRedisplay();
break;

case '1':
xDirection -= 0.5;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(65, 1, 1, 50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection, yDirection, zDirection, 0, 0, 0, 0, 1, 0);
glutPostRedisplay();
break;
case '2':
xDirection -= 0.5;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(65, 1, 1, 50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection, yDirection, zDirection, 0, 0, 0, 0, 1, 0);
glutPostRedisplay();
break;
case '3':
xDirection -= 0.5;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(65, 1, 1, 50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection, yDirection, zDirection, 0, 0, 0, 0, 1, 0);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
}
}

void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutWireTeapot(4);
glutSwapBuffers();
}

void RotateRect()
{
angle += 0.5;
if (angle >= 360)
{
angle = 0.0f;
}
Sleep(30);
myDisplay();
}

void OnMouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
glutIdleFunc(RotateRect);
}

if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
glutIdleFunc(NULL);
}
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv); //初始化GLUT
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(500, 200);
glutInitWindowSize(500, 500);
glutCreateWindow("OpenGL");
InitEnvironment(); //初始化显示环境
glutKeyboardFunc(&KeyBoards); //注册键盘事件
glutDisplayFunc(&myDisplay); //回调函数
glutMainLoop(); //持续显示,当窗口改变会重新绘制图形
return 0;
}

使用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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<Windows.h>
#include<gl\GL.h>

#pragma comment(lib,"opengl32.lib")
int main()
{
PIXELFORMATDESCRIPTOR pfd;
pfd.nSize = 40;
pfd.nVersion = 1;
//支持绘制到窗口、支持OPENGL、支持GDI
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_SUPPORT_GDI;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 0;
pfd.cRedBits = 0;
pfd.cRedShift = 0;
pfd.cGreenBits = 0;
pfd.cGreenShift = 0;
pfd.cBlueBits = 0;
pfd.cBlueShift = 0;
pfd.cAlphaBits = 0;
pfd.cAlphaShift = 0;
pfd.cAccumBits = 0;
pfd.cAccumRedBits = 0;
pfd.cAccumGreenBits = 0;
pfd.cAccumBlueBits = 0;
pfd.cAccumAlphaBits = 0;
pfd.cDepthBits = 0;
pfd.cStencilBits = 0;
pfd.cAuxBuffers = 0;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.bReserved = 0;
pfd.dwLayerMask = 0;
pfd.dwVisibleMask = 0;
pfd.dwDamageMask = 0;

//获取屏幕的设备环境
HDC hdc = GetDC(NULL);
//选择像素格式
int pixelFormat = ChoosePixelFormat(hdc, &pfd);
//设置像素格式
SetPixelFormat(hdc, pixelFormat, &pfd);
//创建OpenGL渲染环境
HGLRC hglrc = wglCreateContext(hdc);
//为当前线程指定设备环境和渲染环境
wglMakeCurrent(hdc, hglrc);
int i = 0;
while (i < 1000)
{

i++;
glViewport(0, 0, 800, 800);
//glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.0f, 0.5f);
glVertex2f(0.5f, -0.5f);
glEnd();
glFlush();
}
return 0;
}

去除控制台

去除控制台方法一设置与代码

  1. 设置项目properites->Linker->System->SubSystem->Windows.
  2. 设置项目properites->Linker->Advanced->Entry Point->mainCRTStartup

去除控制台方法二在代码中使用预处理命令

  • 此方法在IDE中运行时依旧会显示控制台,单独运行编译后的exe文件就不会显示控制台
1
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置入口地址

在控制台中使用opengl画红色三角形

cmd可以成功显示,terminal不能显示。管理员权限运行程序显示的是cmd而非terminal

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<Windows.h>
#include<gl\GL.h>


#pragma comment(lib,"opengl32.lib")
int main()
{
PIXELFORMATDESCRIPTOR pfd;
pfd.nSize = 40;
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 0;
pfd.cRedBits = 0;
pfd.cRedShift = 0;
pfd.cGreenBits = 0;
pfd.cGreenShift = 0;
pfd.cBlueBits = 0;
pfd.cBlueShift = 0;
pfd.cAlphaBits = 0;
pfd.cAlphaShift = 0;
pfd.cAccumBits = 0;
pfd.cAccumRedBits = 0;
pfd.cAccumGreenBits = 0;
pfd.cAccumBlueBits = 0;
pfd.cAccumAlphaBits = 0;
pfd.cDepthBits = 0;
pfd.cStencilBits = 0;
pfd.cAuxBuffers = 0;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.bReserved = 0;
pfd.dwLayerMask = 0;
pfd.dwVisibleMask = 0;
pfd.dwDamageMask = 0;
//获取当前线程的控制台窗口句柄
HWND hwnd = GetConsoleWindow();
//获取控制台窗口的设备环境句柄
HDC hdc = GetDC(hwnd);
//选择适合的像素格式
int pixelFormat = ChoosePixelFormat(hdc, &pfd);
//设置像素格式
SetPixelFormat(hdc, pixelFormat, &pfd);
//创建OpenGL渲染环境
HGLRC hglrc = wglCreateContext(hdc);
//为当前线程指定设备环境和渲染环境
wglMakeCurrent(hdc, hglrc);

//glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.0f, 0.5f);
glVertex2f(0.5f, -0.5f);
glEnd();
glFlush();
SwapBuffers(hdc);

return 0;
}

在win32程序中使用openGL

使用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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>

#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")

LONG WINAPI WndProc(HWND, UINT, WPARAM, LPARAM); // Window procedure
HGLRC SetUpOpenGL(HWND hwnd); // Initialize OpenGL
void DrawOpenGLScene(void); // Actual Drawing code

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
static wchar_t szAppName[] = L"OpenGL";
static wchar_t szTitle[] = L"Getting Started With OpenGL";
WNDCLASSW wc; // Windows class struct
MSG msg; // Message struct
HWND hWnd; // Main window handle.

// Fill in window class structure with parameters that
// describe the main window.
wc.style = CS_HREDRAW | CS_VREDRAW; // Class style(s).
wc.lpfnWndProc = (WNDPROC)WndProc; // Window Procedure
wc.cbClsExtra = 0; // No per-class extra data.
wc.cbWndExtra = 0; // No per-window extra data.
wc.hInstance = hInstance; // Owner of this class
wc.hIcon = NULL; // Icon name
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Cursor
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // Default color
wc.lpszMenuName = NULL; // Menu from .RC
wc.lpszClassName = szAppName; // Name to register as

// Register the window class
RegisterClassW(&wc);

// Create a main window for this application instance.
hWnd = CreateWindowW(
szAppName, // App name
szTitle, // Text for window title bar
WS_OVERLAPPEDWINDOW // Window style
| WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
NULL, // No parent window
NULL, // Use the window class menu.
hInstance, // This instance owns this window
NULL // We don't use any extra data
);

// If window could not be created, return zero
if (!hWnd)
{
return(0);
}

// Make the window visible & update its client area
ShowWindow(hWnd, 1); // Show the window
UpdateWindow(hWnd); // Sends WM_PAINT message

// Enter the Windows message loop
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg); // Translates messages
DispatchMessage(&msg); // Then dispatches
}

return(msg.wParam);
}

LONG WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
static HGLRC hRC; // Note this is STATIC!
PAINTSTRUCT ps;
GLdouble gldAspect;
GLsizei glnWidth, glnHeight;

switch (msg)
{
case WM_CREATE:
// Select a pixel format and then create a rendering context from it.
hRC = SetUpOpenGL(hWnd);
return 0;

case WM_SIZE:
// Redefine the viewing volume and viewport when the window size changes.
hDC = GetDC(hWnd);
wglMakeCurrent(hDC, hRC);

// Get the new size of the client window
glnWidth = (GLsizei)LOWORD(lParam);
glnHeight = (GLsizei)HIWORD(lParam);

// Set the viewport to the new window dimensions
glViewport(0, 0, glnWidth, glnHeight);

// Set up a projection matrix to fill the client window
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (GLdouble)glnWidth / (GLdouble)glnHeight, 1.0, 10.0);

// Switch back to modelview matrix
glMatrixMode(GL_MODELVIEW);
wglMakeCurrent(NULL, NULL);
ReleaseDC(hWnd, hDC);
return 0;

case WM_PAINT:
// Draw the scene.
hDC = BeginPaint(hWnd, &ps);
wglMakeCurrent(hDC, hRC);

DrawOpenGLScene();

wglMakeCurrent(NULL, NULL);
EndPaint(hWnd, &ps);
return 0;

case WM_DESTROY:
// Clean up and terminate.
wglDeleteContext(hRC);
PostQuitMessage(0);
return 0;
}

return DefWindowProc(hWnd, msg, wParam, lParam);
}

HGLRC SetUpOpenGL(HWND hWnd)
{
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // Struct size
1, // Version number
PFD_DRAW_TO_WINDOW | // Flags, draw to a window,
PFD_SUPPORT_OPENGL, // Use OpenGL
PFD_TYPE_RGBA, // RGBA pixel values
24, // 24-bit color
0, 0, 0, // RGB bits & shift sizes.
0, 0, 0, // Don't care about them
0, 0, // No alpha buffer info
0, 0, 0, 0, 0, // No accumulation buffer
32, // 32-bit depth buffer
0, // No stencil buffer
0, // No auxiliary buffers
PFD_MAIN_PLANE, // Layer type
0, // Reserved (must be 0)
0, // No layer mask
0, // No visible mask
0 // No damage mask
};

int nMyPixelFormatID;
HDC hDC;
HGLRC hRC;

hDC = GetDC(hWnd);
nMyPixelFormatID = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, nMyPixelFormatID, &pfd);

hRC = wglCreateContext(hDC);
ReleaseDC(hWnd, hDC);

return hRC;
}

void DrawOpenGLScene()
{
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Define the modelview transformation.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Move the viewpoint out to where we can see everything
glTranslatef(0.0f, 0.0f, -5.0f);

// Draw a triangle
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0); // Red color for the triangle
glVertex3f(0.0, 0.0, 0);
glVertex3f(0.0, 1.0, 0);
glVertex3f(1.0, 0.0, 0);

glColor3f(0.0, 1.0, 0.0); // Green color for the triangle
glVertex3f(0.0, 0.0, 0);
glVertex3f(0.0, 1.0, 0);
glVertex3f(-1.0, 0.0, 0);

glColor3f(0.0, 0.0, 1.0); // Blue color for the triangle
glVertex3f(1.0, 0.0, 0);
glVertex3f(0.0, -1.0, 0);
glVertex3f(-1.0, 0.0, 0);
glEnd();

// Flush the OpenGL commands
glFlush();
}

在win32窗口程序中使用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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>
#include <gl/gl.h>

#pragma comment(lib,"opengl32.lib")

void drawScene(HDC* hdc);
void EnableOpenGL(HWND hWnd, HDC* hDC, HGLRC* hRC);
void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

float backgroundColor[4] = { 0.3f, 0.3f, 0.3f, 0.0f }; // 背景色

int WINAPI wWinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PWSTR pCmdLine,
int nCmdShow
)
{
// Register the window class
// -------------------------
const wchar_t CLASS_NAME[] = L"GLSample";
WNDCLASS wc;
wc.style = CS_OWNDC;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = CLASS_NAME;

RegisterClass(&wc);

// Create the window
// -----------------
HWND hWnd = CreateWindowEx(
0, // Optional window styles
CLASS_NAME, // Window class
L"OpenGL Sample", // Window header
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, 512, 512,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hWnd == NULL)
{
return 0;
}

ShowWindow(hWnd, nCmdShow);

// enable OpenGL for the window
// ----------------------------

HDC hDC;
HGLRC hRC;

EnableOpenGL(hWnd, &hDC, &hRC);

// Run the message loop
// --------------------
MSG msg;
while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;

TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
drawScene(&hDC);
}
}

// shutdown OpenGL
DisableOpenGL(hWnd, hDC, hRC);

// destroy the window explicitly
DestroyWindow(hWnd);

return 0;

}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
RECT rcClient;
switch (uMsg)
{
case WM_CREATE:
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);

// 使用背景色填充窗口
HBRUSH hBrush = CreateSolidBrush(RGB((int)(backgroundColor[0] * 255),
(int)(backgroundColor[1] * 255),
(int)(backgroundColor[2] * 255)));
FillRect(hdc, &ps.rcPaint, hBrush);
DeleteObject(hBrush); // 删除画刷以释放资源

EndPaint(hWnd, &ps);
} break;

case WM_DESTROY:
PostQuitMessage(0);
return 0;

case WM_SIZE: // main window changed size
GetClientRect(hWnd, &rcClient);
int w = rcClient.right - rcClient.left; // 获得客户区宽度
int h = rcClient.bottom - rcClient.top; // 获得客户区高度
glViewport(0, 0, w, h);
return 0;
}

return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

void drawScene(HDC* hDC)
{
// 使用背景色清除颜色缓冲区
glClearColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]);
glClear(GL_COLOR_BUFFER_BIT);

// 画一个矩形面
glColor4f(1.0f, 0.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(-0.6, -0.6f, 0.0f);
glVertex3f(0.6, -0.6f, 0.0f);
glVertex3f(0.6, 0.6f, 0.0f);
glVertex3f(-0.6, 0.6f, 0.0f);
glEnd();

SwapBuffers(*hDC);

Sleep(1);
}

// Enable OpenGL
// -------------
void EnableOpenGL(HWND hWnd, HDC* hDC, HGLRC* hRC)
{
// get the device context (DC)
*hDC = GetDC(hWnd);

PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit 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, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};

int iPixelFormat = ChoosePixelFormat(*hDC, &pfd);
SetPixelFormat(*hDC, iPixelFormat, &pfd);

// create and enable the render context (RC)
*hRC = wglCreateContext(*hDC);
wglMakeCurrent(*hDC, *hRC);
}

// Disable OpenGL
// --------------
void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
ReleaseDC(hWnd, hDC);
}