Win32 OpenGL标准例子

  在VS2008的MSDN中有一个标准的OpenGL例子,记录如下:

  1 /*
  2  * Example of a Win32 OpenGL program.
  3  * The OpenGL code is the same as that used in
  4  * the X Window System sample
  5  */
  6 #include <windows.h>
  7 #include <GL/gl.h>
  8 #include <GL/glu.h>
  9 
 10 /* Windows globals, defines, and prototypes */
 11 CHAR szAppName[]="Win OpenGL";
 12 HWND  ghWnd;
 13 HDC   ghDC;
 14 HGLRC ghRC;
 15 
 16 #define SWAPBUFFERS SwapBuffers(ghDC)
 17 #define BLACK_INDEX     0
 18 #define RED_INDEX       13
 19 #define GREEN_INDEX     14
 20 #define BLUE_INDEX      16
 21 #define WIDTH           640
 22 #define HEIGHT          480
 23 
 24 LONG WINAPI MainWndProc (HWND, UINT, WPARAM, LPARAM);
 25 BOOL bSetupPixelFormat(HDC);
 26 
 27 /* OpenGL globals, defines, and prototypes */
 28 GLfloat latitude, longitude, latinc, longinc;
 29 GLdouble radius;
 30 
 31 #define GLOBE    1
 32 #define CYLINDER 2
 33 #define CONE     3
 34 
 35 GLvoid resize(GLsizei, GLsizei);
 36 GLvoid initializeGL(GLsizei, GLsizei);
 37 GLvoid drawScene(GLvoid);
 38 void polarView( GLdouble, GLdouble, GLdouble, GLdouble);
 39 
 40 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
 41 {
 42     MSG        msg;
 43     WNDCLASS   wndclass;
 44 
 45     /* Register the frame class */
 46     wndclass.style         = 0;
 47     wndclass.lpfnWndProc   = (WNDPROC)MainWndProc;
 48     wndclass.cbClsExtra    = 0;
 49     wndclass.cbWndExtra    = 0;
 50     wndclass.hInstance     = hInstance;
 51     wndclass.hIcon         = LoadIcon (hInstance, szAppName);
 52     wndclass.hCursor       = LoadCursor (NULL,IDC_ARROW);
 53     wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 54     wndclass.lpszMenuName  = szAppName;
 55     wndclass.lpszClassName = szAppName;
 56 
 57     if (!RegisterClass (&wndclass) )
 58         return FALSE;
 59 
 60     /* Create the frame */
 61     ghWnd = CreateWindow (szAppName,
 62                           "Generic OpenGL Sample",
 63                           WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
 64                           CW_USEDEFAULT,
 65                           CW_USEDEFAULT,
 66                           WIDTH,
 67                           HEIGHT,
 68                           NULL,
 69                           NULL,
 70                           hInstance,
 71                           NULL);
 72 
 73     /* make sure window was created */
 74     if (!ghWnd)
 75         return FALSE;
 76 
 77     /* show and update main window */
 78     ShowWindow (ghWnd, nCmdShow);
 79 
 80     UpdateWindow (ghWnd);
 81 
 82     /* animation loop */
 83     while (1)
 84     {
 85         /*
 86          *  Process all pending messages
 87          */
 88 
 89         while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
 90         {
 91             if (GetMessage(&msg, NULL, 0, 0) )
 92             {
 93                 TranslateMessage(&msg);
 94                 DispatchMessage(&msg);
 95             }
 96             else
 97             {
 98                 return TRUE;
 99             }
100         }
101         drawScene();
102         Sleep(10);
103     }
104 }
105 
106 /* main window procedure */
107 LONG WINAPI MainWndProc (
108     HWND    hWnd,
109     UINT    uMsg,
110     WPARAM  wParam,
111     LPARAM  lParam)
112 {
113     LONG    lRet = 1;
114     PAINTSTRUCT    ps;
115     RECT rect;
116 
117     switch (uMsg)
118     {
119 
120     case WM_CREATE:
121         ghDC = GetDC(hWnd);
122         if (!bSetupPixelFormat(ghDC))
123             PostQuitMessage (0);
124 
125         ghRC = wglCreateContext(ghDC);
126         wglMakeCurrent(ghDC, ghRC);
127         GetClientRect(hWnd, &rect);
128         initializeGL(rect.right, rect.bottom);
129         break;
130 
131     case WM_PAINT:
132         BeginPaint(hWnd, &ps);
133         EndPaint(hWnd, &ps);
134         break;
135 
136     case WM_SIZE:
137         GetClientRect(hWnd, &rect);
138         resize(rect.right, rect.bottom);
139         break;
140 
141     case WM_CLOSE:
142         if (ghRC)
143             wglDeleteContext(ghRC);
144         if (ghDC)
145             ReleaseDC(hWnd, ghDC);
146         ghRC = 0;
147         ghDC = 0;
148 
149         DestroyWindow (hWnd);
150         break;
151 
152     case WM_DESTROY:
153         if (ghRC)
154             wglDeleteContext(ghRC);
155         if (ghDC)
156             ReleaseDC(hWnd, ghDC);
157 
158         PostQuitMessage (0);
159         break;
160 
161     case WM_KEYDOWN:
162         switch (wParam)
163         {
164         case VK_LEFT:
165             longinc += 0.5F;
166             break;
167         case VK_RIGHT:
168             longinc -= 0.5F;
169             break;
170         case VK_UP:
171             latinc += 0.5F;
172             break;
173         case VK_DOWN:
174             latinc -= 0.5F;
175             break;
176         }
177 
178     default:
179         lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
180         break;
181     }
182 
183     return lRet;
184 }
185 
186 BOOL bSetupPixelFormat(HDC hdc)
187 {
188     PIXELFORMATDESCRIPTOR pfd, *ppfd;
189     int pixelformat;
190 
191     ppfd = &pfd;
192 
193     ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
194     ppfd->nVersion = 1;
195     ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
196                     PFD_DOUBLEBUFFER;
197     ppfd->dwLayerMask = PFD_MAIN_PLANE;
198     ppfd->iPixelType = PFD_TYPE_COLORINDEX;
199     ppfd->cColorBits = 8;
200     ppfd->cDepthBits = 16;
201     ppfd->cAccumBits = 0;
202     ppfd->cStencilBits = 0;
203 
204     pixelformat = ChoosePixelFormat(hdc, ppfd);
205 
206     if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0 )
207     {
208         MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
209         return FALSE;
210     }
211 
212     if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE)
213     {
214         MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
215         return FALSE;
216     }
217 
218     return TRUE;
219 }
220 
221 /* OpenGL code */
222 
223 GLvoid resize( GLsizei width, GLsizei height )
224 {
225     GLfloat aspect;
226 
227     glViewport( 0, 0, width, height );
228 
229     aspect = (GLfloat) width / height;
230 
231     glMatrixMode( GL_PROJECTION );
232     glLoadIdentity();
233     gluPerspective( 45.0, aspect, 3.0, 7.0 );
234     glMatrixMode( GL_MODELVIEW );
235 }
236 
237 GLvoid createObjects()
238 {
239     GLUquadricObj *quadObj;
240 
241     glNewList(GLOBE, GL_COMPILE);
242     quadObj = gluNewQuadric ();
243     gluQuadricDrawStyle (quadObj, GLU_LINE);
244     gluSphere (quadObj, 1.5, 16, 16);
245     glEndList();
246 
247     glNewList(CONE, GL_COMPILE);
248     quadObj = gluNewQuadric ();
249     gluQuadricDrawStyle (quadObj, GLU_FILL);
250     gluQuadricNormals (quadObj, GLU_SMOOTH);
251     gluCylinder(quadObj, 0.3, 0.0, 0.6, 15, 10);
252     glEndList();
253 
254     glNewList(CYLINDER, GL_COMPILE);
255     glPushMatrix ();
256     glRotatef ((GLfloat)90.0, (GLfloat)1.0, (GLfloat)0.0, (GLfloat)0.0);
257     glTranslatef ((GLfloat)0.0, (GLfloat)0.0, (GLfloat)-1.0);
258     quadObj = gluNewQuadric ();
259     gluQuadricDrawStyle (quadObj, GLU_FILL);
260     gluQuadricNormals (quadObj, GLU_SMOOTH);
261     gluCylinder (quadObj, 0.3, 0.3, 0.6, 12, 2);
262     glPopMatrix ();
263     glEndList();
264 }
265 
266 GLvoid initializeGL(GLsizei width, GLsizei height)
267 {
268     GLfloat     maxObjectSize, aspect;
269     GLdouble    near_plane, far_plane;
270 
271     glClearIndex( (GLfloat)BLACK_INDEX);
272     glClearDepth( 1.0 );
273 
274     glEnable(GL_DEPTH_TEST);
275 
276     glMatrixMode( GL_PROJECTION );
277     aspect = (GLfloat) width / height;
278     gluPerspective( 45.0, aspect, 3.0, 7.0 );
279     glMatrixMode( GL_MODELVIEW );
280 
281     near_plane = 3.0;
282     far_plane = 7.0;
283     maxObjectSize = 3.0F;
284     radius = near_plane + maxObjectSize/2.0;
285 
286     latitude = 0.0F;
287     longitude = 0.0F;
288     latinc = 6.0F;
289     longinc = 2.5F;
290 
291     createObjects();
292 }
293 
294 void polarView(GLdouble radius, GLdouble twist, GLdouble latitude,
295                GLdouble longitude)
296 {
297     glTranslated(0.0, 0.0, -radius);
298     glRotated(-twist, 0.0, 0.0, 1.0);
299     glRotated(-latitude, 1.0, 0.0, 0.0);
300     glRotated(longitude, 0.0, 0.0, 1.0);
301 
302 }
303 
304 GLvoid drawScene(GLvoid)
305 {
306     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
307 
308     glPushMatrix();
309 
310     latitude += latinc;
311     longitude += longinc;
312 
313     polarView( radius, 0, latitude, longitude );
314 
315     // glIndexi(RED_INDEX);
316     glColor3f(1,0,0);
317     glCallList(CONE);
318 
319     // glIndexi(BLUE_INDEX);
320     glColor3f(0,0,1);
321     glCallList(GLOBE);
322 
323     // glIndexi(GREEN_INDEX);
324     glColor3f(0,1,0);
325     glPushMatrix();
326     glTranslatef(0.8F, -0.65F, 0.0F);
327     glRotatef(30.0F, 1.0F, 0.5F, 1.0F);
328     glCallList(CYLINDER);
329     glPopMatrix();
330 
331     glPopMatrix();
332 
333     SWAPBUFFERS;
334 }

posted @ 2016-11-13 16:21  星云的彼岸  阅读(1284)  评论(0编辑  收藏  举报