在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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
HWND hWnd;
6
HDC hDC;
7
HGLRC hRC;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
// Set up pixel format for graphics initialization
10
void SetupPixelFormat()
11
{
12
PIXELFORMATDESCRIPTOR pfd, *ppfd;
13
int pixelformat;
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
ppfd = &pfd;
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
pixelformat = ChoosePixelFormat(hDC, ppfd);
28
SetPixelFormat(hDC, pixelformat, ppfd);
29
}
30![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
// Initialize OpenGL graphics
32
void InitGraphics()
33
{
34
hDC = GetDC(hWnd);
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
SetupPixelFormat();
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
hRC = wglCreateContext(hDC);
39
wglMakeCurrent(hDC, hRC);
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
glClearColor(0, 0, 0, 0.5);
42
glClearDepth(1.0);
43
glEnable(GL_DEPTH_TEST);
44
}
45![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
GLfloat aspect = (GLfloat) width / height;
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65
// Draw frame
66
void DrawGraphics()
67
{
68
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
// Set location in front of camera
71
glLoadIdentity();
72
glTranslated(0, 0, -10);
73![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
// Show the new scene
84
SwapBuffers(hDC);
85
}
86![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
113
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
114
{
115![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
116
const LPCWSTR appname = TEXT("OpenGL Sample");
117![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
152
// Initialize OpenGL
153
InitGraphics();
154![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
155
// Display the window
156
ShowWindow(hWnd, nCmdShow);
157
UpdateWindow(hWnd);
158![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
166
TranslateMessage(&msg);
167
DispatchMessage(&msg);
168
}
169
DrawGraphics();
170
}
171![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
172
wglDeleteContext(hRC);
173
ReleaseDC(hWnd, hDC);
174
}
175![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
100
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
104
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
106
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
109
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
112
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
113
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
114
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
115
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
116
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
117
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
118
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
119
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
120
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
121
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
122
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
123
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
124
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
125
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
126
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
127
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
128
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
130
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
131
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
132
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
133
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
134
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
135
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
136
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
137
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
138
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
139
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
140
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
141
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
142
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
143
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
144
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
145
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
146
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
147
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
148
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
149
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
150
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
151
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
152
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
153
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
154
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
155
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
156
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
157
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
158
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
159
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
160
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
161
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
162
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
163
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
164
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
165
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
166
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
167
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
168
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
169
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
170
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
171
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
172
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
173
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
174
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
175
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)