在Visual Studio 2008里使用OpenGL
打开 Visual C++并创建一个新工程
- 在文件菜单下选择 New → Project (Ctrl+Shift+N).
- 选择Win32 Project, 输入工程名字, 然后点击OK.
- 在工程创建向导里点击 Next, 然后选择 Empty Project, 点击 Finish.
给新建的工程中添加一个代码文件:
- 在工程菜单下选择 Add New Item (Ctrl+Shift+A).
- 选择 C++ File (.cpp), 输入文件名, 点击 OK.
- 在Project菜单下选择 Project Properties (Alt+F7) .
- 在左侧的导航面板中 选择 Configuration Properties → Linker → Input .
- 在对话框的左上角的下拉列表中选择 All Configurations . 这样就能确保你所作的更改对 Debug 以及Release配置都有效.
- 在Additional Dependencies 里输入“opengl32.lib glu32.lib” 然后点击 OK.
现在可以将下列代码粘贴在刚才创建的源代码中 运行。
1
#include <windows.h>
2
#include <gl/gl.h>
3
#include <gl/glu.h>
4
5
HWND hWnd;
6
HDC hDC;
7
HGLRC hRC;
8
9
// Set up pixel format for graphics initialization
10
void SetupPixelFormat()
11
{
12
PIXELFORMATDESCRIPTOR pfd, *ppfd;
13
int pixelformat;
14
15
ppfd = &pfd;
16
17
ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
18
ppfd->nVersion = 1;
19
ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
20
ppfd->dwLayerMask = PFD_MAIN_PLANE;
21
ppfd->iPixelType = PFD_TYPE_COLORINDEX;
22
ppfd->cColorBits = 16;
23
ppfd->cDepthBits = 16;
24
ppfd->cAccumBits = 0;
25
ppfd->cStencilBits = 0;
26
27
pixelformat = ChoosePixelFormat(hDC, ppfd);
28
SetPixelFormat(hDC, pixelformat, ppfd);
29
}
30
31
// Initialize OpenGL graphics
32
void InitGraphics()
33
{
34
hDC = GetDC(hWnd);
35
36
SetupPixelFormat();
37
38
hRC = wglCreateContext(hDC);
39
wglMakeCurrent(hDC, hRC);
40
41
glClearColor(0, 0, 0, 0.5);
42
glClearDepth(1.0);
43
glEnable(GL_DEPTH_TEST);
44
}
45
46
// Resize graphics to fit window
47
void ResizeGraphics()
48
{
49
// Get new window size
50
RECT rect;
51
GetClientRect(hWnd, &rect);
52
int width = rect.right;
53
int height = rect.bottom;
54
55
GLfloat aspect = (GLfloat) width / height;
56
57
// Adjust graphics to window size
58
glViewport(0, 0, width, height);
59
glMatrixMode(GL_PROJECTION);
60
glLoadIdentity();
61
gluPerspective(45.0, aspect, 1.0, 100.0);
62
glMatrixMode(GL_MODELVIEW);
63
}
64
65
// Draw frame
66
void DrawGraphics()
67
{
68
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
69
70
// Set location in front of camera
71
glLoadIdentity();
72
glTranslated(0, 0, -10);
73
74
// Draw a square
75
glBegin(GL_QUADS);
76
glColor3d(1, 0, 0);
77
glVertex3d(-2, 2, 0);
78
glVertex3d(2, 2, 0);
79
glVertex3d(2, -2, 0);
80
glVertex3d(-2, -2, 0);
81
glEnd();
82
83
// Show the new scene
84
SwapBuffers(hDC);
85
}
86
87
// Handle window events and messages
88
LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
89
{
90
switch (uMsg)
91
{
92
case WM_SIZE:
93
ResizeGraphics();
94
break;
95
96
case WM_CLOSE:
97
DestroyWindow(hWnd);
98
break;
99
100
case WM_DESTROY:
101
PostQuitMessage(0);
102
break;
103
104
// Default event handler
105
default:
106
return DefWindowProc (hWnd, uMsg, wParam, lParam);
107
break;
108
}
109
110
return 1;
111
}
112
113
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
114
{
115
116
const LPCWSTR appname = TEXT("OpenGL Sample");
117
118
WNDCLASS wndclass;
119
MSG msg;
120
121
// Define the window class
122
wndclass.style = 0;
123
wndclass.lpfnWndProc = (WNDPROC)MainWndProc;
124
wndclass.cbClsExtra = 0;
125
wndclass.cbWndExtra = 0;
126
wndclass.hInstance = hInstance;
127
wndclass.hIcon = LoadIcon(hInstance, appname);
128
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
129
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
130
wndclass.lpszMenuName = appname;
131
wndclass.lpszClassName = appname;
132
133
// Register the window class
134
if (!RegisterClass(&wndclass)) return FALSE;
135
136
// Create the window
137
hWnd = CreateWindow(
138
appname,
139
appname,
140
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
141
CW_USEDEFAULT,
142
CW_USEDEFAULT,
143
800,
144
600,
145
NULL,
146
NULL,
147
hInstance,
148
NULL);
149
150
if (!hWnd) return FALSE;
151
152
// Initialize OpenGL
153
InitGraphics();
154
155
// Display the window
156
ShowWindow(hWnd, nCmdShow);
157
UpdateWindow(hWnd);
158
159
// Event loop
160
while (1)
161
{
162
if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
163
{
164
if (!GetMessage(&msg, NULL, 0, 0)) return TRUE;
165
166
TranslateMessage(&msg);
167
DispatchMessage(&msg);
168
}
169
DrawGraphics();
170
}
171
172
wglDeleteContext(hRC);
173
ReleaseDC(hWnd, hDC);
174
}
175
#include <windows.h>2
#include <gl/gl.h>3
#include <gl/glu.h>4

5
HWND hWnd;6
HDC hDC;7
HGLRC hRC;8

9
// Set up pixel format for graphics initialization10
void SetupPixelFormat()11
{12
PIXELFORMATDESCRIPTOR pfd, *ppfd;13
int pixelformat;14

15
ppfd = &pfd;16

17
ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);18
ppfd->nVersion = 1;19
ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;20
ppfd->dwLayerMask = PFD_MAIN_PLANE;21
ppfd->iPixelType = PFD_TYPE_COLORINDEX;22
ppfd->cColorBits = 16;23
ppfd->cDepthBits = 16;24
ppfd->cAccumBits = 0;25
ppfd->cStencilBits = 0;26

27
pixelformat = ChoosePixelFormat(hDC, ppfd);28
SetPixelFormat(hDC, pixelformat, ppfd);29
}30

31
// Initialize OpenGL graphics32
void InitGraphics()33
{34
hDC = GetDC(hWnd);35

36
SetupPixelFormat();37

38
hRC = wglCreateContext(hDC);39
wglMakeCurrent(hDC, hRC);40

41
glClearColor(0, 0, 0, 0.5);42
glClearDepth(1.0);43
glEnable(GL_DEPTH_TEST);44
}45

46
// Resize graphics to fit window47
void ResizeGraphics()48
{49
// Get new window size50
RECT rect;51
GetClientRect(hWnd, &rect);52
int width = rect.right;53
int height = rect.bottom;54

55
GLfloat aspect = (GLfloat) width / height;56

57
// Adjust graphics to window size58
glViewport(0, 0, width, height);59
glMatrixMode(GL_PROJECTION);60
glLoadIdentity();61
gluPerspective(45.0, aspect, 1.0, 100.0);62
glMatrixMode(GL_MODELVIEW);63
}64

65
// Draw frame66
void DrawGraphics()67
{68
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);69

70
// Set location in front of camera71
glLoadIdentity();72
glTranslated(0, 0, -10);73

74
// Draw a square75
glBegin(GL_QUADS);76
glColor3d(1, 0, 0);77
glVertex3d(-2, 2, 0);78
glVertex3d(2, 2, 0);79
glVertex3d(2, -2, 0);80
glVertex3d(-2, -2, 0);81
glEnd();82

83
// Show the new scene84
SwapBuffers(hDC);85
}86

87
// Handle window events and messages88
LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)89
{90
switch (uMsg)91
{92
case WM_SIZE:93
ResizeGraphics();94
break;95

96
case WM_CLOSE: 97
DestroyWindow(hWnd);98
break;99
100
case WM_DESTROY:101
PostQuitMessage(0);102
break;103
104
// Default event handler105
default: 106
return DefWindowProc (hWnd, uMsg, wParam, lParam); 107
break; 108
} 109
110
return 1; 111
}112

113
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)114
{115

116
const LPCWSTR appname = TEXT("OpenGL Sample");117

118
WNDCLASS wndclass;119
MSG msg;120
121
// Define the window class122
wndclass.style = 0;123
wndclass.lpfnWndProc = (WNDPROC)MainWndProc;124
wndclass.cbClsExtra = 0;125
wndclass.cbWndExtra = 0;126
wndclass.hInstance = hInstance;127
wndclass.hIcon = LoadIcon(hInstance, appname);128
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);129
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);130
wndclass.lpszMenuName = appname;131
wndclass.lpszClassName = appname;132
133
// Register the window class134
if (!RegisterClass(&wndclass)) return FALSE;135
136
// Create the window137
hWnd = CreateWindow(138
appname,139
appname,140
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,141
CW_USEDEFAULT,142
CW_USEDEFAULT,143
800,144
600,145
NULL,146
NULL,147
hInstance,148
NULL);149
150
if (!hWnd) return FALSE;151

152
// Initialize OpenGL153
InitGraphics();154

155
// Display the window156
ShowWindow(hWnd, nCmdShow);157
UpdateWindow(hWnd);158

159
// Event loop160
while (1)161
{162
if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)163
{164
if (!GetMessage(&msg, NULL, 0, 0)) return TRUE;165

166
TranslateMessage(&msg);167
DispatchMessage(&msg);168
}169
DrawGraphics();170
}171

172
wglDeleteContext(hRC);173
ReleaseDC(hWnd, hDC);174
}175




浙公网安备 33010602011771号