C99 Paste by dyh
Description: Opengl example (C)
|
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | /* * Example of a Win32 OpenGL program. * The OpenGL code is the same as that used in * the X Window System sample */ #include <tchar.h> #include <windows.h> #include <GL/gl.h> #include <GL/glu.h> //#include <GL/glaux.h> /* Windows globals, defines, and prototypes */ TCHAR szAppName[] = _T("Win OpenGL"); HWND ghWnd; HDC ghDC; HGLRC ghRC; #define WIDTH 300 #define HEIGHT 200 LONG WINAPI MainWndProc (HWND, UINT, WPARAM, LPARAM); BOOL bSetupPixelFormat(HDC); /* OpenGL globals, defines, and prototypes */ GLfloat latitude, longitude, latinc, longinc; GLdouble radius; #define GLOBE 1 #define CYLINDER 2 #define CONE 3 GLvoid resize(GLsizei, GLsizei); GLvoid initializeGL(GLsizei, GLsizei); GLvoid drawScene(GLvoid); void polarView( GLdouble, GLdouble, GLdouble, GLdouble); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASS wndclass; /* Register the frame class */ wndclass.style = 0; wndclass.lpfnWndProc = (WNDPROC)MainWndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon (hInstance, szAppName); wndclass.hCursor = LoadCursor (NULL,IDC_ARROW); wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wndclass.lpszMenuName = szAppName; wndclass.lpszClassName = szAppName; if (!RegisterClass (&wndclass) ) return FALSE; /* Create the frame */ ghWnd = CreateWindow (szAppName, _T("Generic OpenGL Sample"), WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL); /* make sure window was created */ if (!ghWnd) return FALSE; /* show and update main window */ ShowWindow (ghWnd, nCmdShow); UpdateWindow (ghWnd); /* animation loop */ while (1) { /* * Process all pending messages */ while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE) { if (GetMessage(&msg, NULL, 0, 0) ) { TranslateMessage(&msg); DispatchMessage(&msg); } else { return TRUE; } } drawScene(); } } /* main window procedure */ LONG WINAPI MainWndProc ( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LONG lRet = 1; PAINTSTRUCT ps; RECT rect; switch (uMsg) { case WM_CREATE: ghDC = GetDC(hWnd); if (!bSetupPixelFormat(ghDC)) PostQuitMessage (0); ghRC = wglCreateContext(ghDC); wglMakeCurrent(ghDC, ghRC); GetClientRect(hWnd, &rect); initializeGL(rect.right, rect.bottom); break; case WM_PAINT: BeginPaint(hWnd, &ps); EndPaint(hWnd, &ps); break; case WM_SIZE: GetClientRect(hWnd, &rect); resize(rect.right, rect.bottom); break; case WM_CLOSE: if (ghRC) wglDeleteContext(ghRC); if (ghDC) ReleaseDC(hWnd, ghDC); ghRC = 0; ghDC = 0; DestroyWindow (hWnd); break; case WM_DESTROY: if (ghRC) wglDeleteContext(ghRC); if (ghDC) ReleaseDC(hWnd, ghDC); PostQuitMessage (0); break; case WM_KEYDOWN: switch (wParam) { case VK_LEFT: longinc += 0.5F; break; case VK_RIGHT: longinc -= 0.5F; break; case VK_UP: latinc += 0.5F; break; case VK_DOWN: latinc -= 0.5F; break; } default: lRet = DefWindowProc (hWnd, uMsg, wParam, lParam); break; } return lRet; } BOOL bSetupPixelFormat(HDC hdc) { PIXELFORMATDESCRIPTOR pfd, *ppfd; int pixelformat; ppfd = &pfd; ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR); ppfd->nVersion = 1; ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; ppfd->dwLayerMask = PFD_MAIN_PLANE; ppfd->iPixelType = PFD_TYPE_COLORINDEX; ppfd->cColorBits = 8; ppfd->cDepthBits = 16; ppfd->cAccumBits = 0; ppfd->cStencilBits = 0; pixelformat = ChoosePixelFormat(hdc, ppfd); if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0 ) { MessageBox(NULL, _T("ChoosePixelFormat failed"), _T("Error"), MB_OK); return FALSE; } if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE) { MessageBox(NULL, _T("SetPixelFormat failed"), _T("Error"), MB_OK); return FALSE; } return TRUE; } /* OpenGL code */ GLvoid resize( GLsizei width, GLsizei height ) { GLfloat aspect; glViewport( 0, 0, width, height ); aspect = (GLfloat) width / height; glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 45.0, aspect, 3.0, 7.0 ); glMatrixMode( GL_MODELVIEW ); } GLvoid createObjects() { GLUquadricObj *quadObj; glNewList(GLOBE, GL_COMPILE); quadObj = gluNewQuadric (); gluQuadricDrawStyle (quadObj, GLU_LINE); gluSphere (quadObj, 1.5, 16, 16); glEndList(); glNewList(CONE, GL_COMPILE); quadObj = gluNewQuadric (); gluQuadricDrawStyle (quadObj, GLU_FILL); gluQuadricNormals (quadObj, GLU_SMOOTH); gluCylinder(quadObj, 0.3, 0.0, 0.6, 15, 10); glEndList(); glNewList(CYLINDER, GL_COMPILE); glPushMatrix (); glRotatef ((GLfloat)90.0, (GLfloat)1.0, (GLfloat)0.0, (GLfloat)0.0); glTranslatef ((GLfloat)0.0, (GLfloat)0.0, (GLfloat)-1.0); quadObj = gluNewQuadric (); gluQuadricDrawStyle (quadObj, GLU_FILL); gluQuadricNormals (quadObj, GLU_SMOOTH); gluCylinder (quadObj, 0.3, 0.3, 0.6, 12, 2); glPopMatrix (); glEndList(); } GLvoid initializeGL(GLsizei width, GLsizei height) { GLfloat maxObjectSize, aspect; GLdouble near_plane, far_plane; //glClearColor((GLfloat)AUX_BLACK); glClearColor(0.0, 0.0, 0.0, 0.0); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); aspect = (GLfloat)width / height; gluPerspective(45.0, aspect, 3.0, 7.0); glMatrixMode( GL_MODELVIEW ); near_plane = 3.0; far_plane = 7.0; maxObjectSize = 3.0F; radius = near_plane + maxObjectSize/2.0; latitude = 0.0F; longitude = 0.0F; latinc = 6.0F; longinc = 2.5F; createObjects(); } void polarView(GLdouble radius, GLdouble twist, GLdouble latitude, GLdouble longitude) { glTranslated(0.0, 0.0, -radius); glRotated(-twist, 0.0, 0.0, 1.0); glRotated(-latitude, 1.0, 0.0, 0.0); glRotated(longitude, 0.0, 0.0, 1.0); } GLvoid drawScene(GLvoid) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); latitude += latinc; longitude += longinc; polarView( radius, 0, latitude, longitude ); //glIndexi(AUX_RED); glColor3f(1.0, 0.0, 0.0); glCallList(CONE); //glIndexi(AUX_BLUE); glColor3f(0.0, 0.0, 1.0); glCallList(GLOBE); //glIndexi(AUX_GREEN); glColor3f(0.0, 1.0, 0.0); glPushMatrix(); glTranslatef(0.8F, -0.65F, 0.0F); glRotatef(30.0F, 1.0F, 0.5F, 1.0F); glCallList(CYLINDER); glPopMatrix(); glPopMatrix(); SwapBuffers(ghDC); } |