Advertisement
bayu1st

Tugas Grafika

Dec 17th, 2020
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.47 KB | None | 0 0
  1. /**************************-
  2.  * Includes
  3.  *
  4.  **************************/
  5.  
  6. #include <windows.h>
  7. #include <gl/gl.h>
  8.  
  9.  
  10. /**************************
  11.  * Function Declarations
  12.  *
  13.  **************************/
  14.  
  15. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
  16. WPARAM wParam, LPARAM lParam);
  17. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
  18. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
  19.  
  20.  
  21. /**************************
  22.  * WinMain
  23.  *
  24.  **************************/
  25.  
  26. int WINAPI WinMain (HINSTANCE hInstance,
  27.                     HINSTANCE hPrevInstance,
  28.                     LPSTR lpCmdLine,
  29.                     int iCmdShow)
  30. {
  31.     WNDCLASS wc;
  32.     HWND hWnd;
  33.     HDC hDC;
  34.     HGLRC hRC;        
  35.     MSG msg;
  36.     BOOL bQuit = FALSE;
  37.     float theta = 0.0f;
  38.  
  39.     /* register window class */
  40.     wc.style = CS_OWNDC;
  41.     wc.lpfnWndProc = WndProc;
  42.     wc.cbClsExtra = 0;
  43.     wc.cbWndExtra = 0;
  44.     wc.hInstance = hInstance;
  45.     wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  46.     wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  47.     wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
  48.     wc.lpszMenuName = NULL;
  49.     wc.lpszClassName = "GLSample";
  50.     RegisterClass (&wc);
  51.  
  52.     /* create main window */
  53.     hWnd = CreateWindow (
  54.       "GLSample", "Tugas OpenGL - GRAFIS BALING-BALING SEDERHANA (ADINDA W.N & M.BAYU.M)",
  55.       WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
  56.       10, 10, 550, 550,
  57.       NULL, NULL, hInstance, NULL);
  58.  
  59.     /* enable OpenGL for the window */
  60.     EnableOpenGL (hWnd, &hDC, &hRC);
  61.  
  62.     /* program main loop */
  63.     while (!bQuit)
  64.     {
  65.         /* check for messages */
  66.         if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  67.         {
  68.             /* handle or dispatch messages */
  69.             if (msg.message == WM_QUIT)
  70.             {
  71.                 bQuit = TRUE;
  72.             }
  73.             else
  74.             {
  75.                 TranslateMessage (&msg);
  76.                 DispatchMessage (&msg);
  77.             }
  78.         }
  79.         else
  80.         {
  81.             /* OpenGL animation code goes here */
  82.  
  83.             glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
  84.             glClear (GL_COLOR_BUFFER_BIT);
  85.  
  86.             glPushMatrix ();
  87.             //PENGATURAN ARAH ROTASI
  88.             glRotatef (theta, 0.0f, 0.0f, 1.0f);
  89.             glBegin (GL_POLYGON);
  90.             //PENGATURAN WARNA
  91.             glColor3f(0,0,1);
  92.             //TITIK-TITIK KOORDINAT
  93.             glVertex2i(0,0);
  94.             glVertex2i(-50,125);
  95.             glVertex2i(50,125);
  96.             glVertex2i(0,0);
  97.             glVertex2i(-50,-125);
  98.             glVertex2i(50,-125);
  99.             glEnd ();
  100.             glPopMatrix ();
  101.  
  102.             SwapBuffers (hDC);
  103.             //PENGATURAN KECEPATAN PUTARAN
  104.             theta += 1.0f;
  105.             Sleep (1);
  106.         }
  107.     }
  108.  
  109.     /* shutdown OpenGL */
  110.     DisableOpenGL (hWnd, hDC, hRC);
  111.  
  112.     /* destroy the window explicitly */
  113.     DestroyWindow (hWnd);
  114.  
  115.     return msg.wParam;
  116. }
  117.  
  118.  
  119. /********************
  120.  * Window Procedure
  121.  *
  122.  ********************/
  123.  
  124. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
  125.                           WPARAM wParam, LPARAM lParam)
  126. {
  127.  
  128.     switch (message)
  129.     {
  130.     case WM_CREATE:
  131.         return 0;
  132.     case WM_CLOSE:
  133.         PostQuitMessage (0);
  134.         return 0;
  135.  
  136.     case WM_DESTROY:
  137.         return 0;
  138.  
  139.     case WM_KEYDOWN:
  140.         switch (wParam)
  141.         {
  142.         case VK_ESCAPE:
  143.             PostQuitMessage(0);
  144.             return 0;
  145.         }
  146.         return 0;
  147.  
  148.     default:
  149.         return DefWindowProc (hWnd, message, wParam, lParam);
  150.     }
  151. }
  152.  
  153.  
  154. /*******************
  155.  * Enable OpenGL
  156.  *
  157.  *******************/
  158.  
  159. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
  160. {
  161.     PIXELFORMATDESCRIPTOR pfd;
  162.     int iFormat;
  163.  
  164.     /* get the device context (DC) */
  165.     *hDC = GetDC (hWnd);
  166.  
  167.     /* set the pixel format for the DC */
  168.     ZeroMemory (&pfd, sizeof (pfd));
  169.     pfd.nSize = sizeof (pfd);
  170.     pfd.nVersion = 1;
  171.     pfd.dwFlags = PFD_DRAW_TO_WINDOW |
  172.       PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  173.     pfd.iPixelType = PFD_TYPE_RGBA;
  174.     pfd.cColorBits = 24;
  175.     pfd.cDepthBits = 16;
  176.     pfd.iLayerType = PFD_MAIN_PLANE;
  177.     iFormat = ChoosePixelFormat (*hDC, &pfd);
  178.     SetPixelFormat (*hDC, iFormat, &pfd);
  179.  
  180.     /* create and enable the render context (RC) */
  181.     *hRC = wglCreateContext( *hDC );
  182.     wglMakeCurrent( *hDC, *hRC );
  183.  
  184. }
  185.  
  186.  
  187. /******************
  188.  * Disable OpenGL
  189.  *
  190.  ******************/
  191.  
  192. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
  193. {
  194.     wglMakeCurrent (NULL, NULL);
  195.     wglDeleteContext (hRC);
  196.     ReleaseDC (hWnd, hDC);
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement