rootUser

House

May 9th, 2018
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.41 KB | None | 0 0
  1. #include <windows.h>
  2. #include <gl/gl.h>
  3.  
  4. LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
  5. void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
  6. void DisableOpenGL(HWND, HDC, HGLRC);
  7.  
  8.  
  9. int WINAPI WinMain(HINSTANCE hInstance,
  10.                    HINSTANCE hPrevInstance,
  11.                    LPSTR lpCmdLine,
  12.                    int nCmdShow)
  13. {
  14.     WNDCLASSEX wcex;
  15.     HWND hwnd;
  16.     HDC hDC;
  17.     HGLRC hRC;
  18.     MSG msg;
  19.     BOOL bQuit = FALSE;
  20.     float theta = 0.0f;
  21.  
  22.     /* register window class */
  23.     wcex.cbSize = sizeof(WNDCLASSEX);
  24.     wcex.style = CS_OWNDC;
  25.     wcex.lpfnWndProc = WindowProc;
  26.     wcex.cbClsExtra = 0;
  27.     wcex.cbWndExtra = 0;
  28.     wcex.hInstance = hInstance;
  29.     wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  30.     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  31.     wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  32.     wcex.lpszMenuName = NULL;
  33.     wcex.lpszClassName = "GLSample";
  34.     wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;
  35.  
  36.  
  37.     if (!RegisterClassEx(&wcex))
  38.         return 0;
  39.  
  40.     /* create main window */
  41.     hwnd = CreateWindowEx(0,
  42.                           "GLSample",
  43.                           "OpenGL Sample",
  44.                           WS_OVERLAPPEDWINDOW,
  45.                           CW_USEDEFAULT,
  46.                           CW_USEDEFAULT,
  47.                           256,
  48.                           256,
  49.                           NULL,
  50.                           NULL,
  51.                           hInstance,
  52.                           NULL);
  53.  
  54.     ShowWindow(hwnd, nCmdShow);
  55.  
  56.     /* enable OpenGL for the window */
  57.     EnableOpenGL(hwnd, &hDC, &hRC);
  58.  
  59.     /* program main loop */
  60.     while (!bQuit)
  61.     {
  62.         /* check for messages */
  63.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  64.         {
  65.             /* handle or dispatch messages */
  66.             if (msg.message == WM_QUIT)
  67.             {
  68.                 bQuit = TRUE;
  69.             }
  70.             else
  71.             {
  72.                 TranslateMessage(&msg);
  73.                 DispatchMessage(&msg);
  74.             }
  75.         }
  76.         else
  77.         {
  78.             /* OpenGL animation code goes here */
  79.  
  80.             glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  81.             glClear(GL_COLOR_BUFFER_BIT);
  82.  
  83.             //glPushMatrix();
  84.             //glRotatef(theta, 0.0f, 0.0f, 1.0f);
  85.  
  86.             glColor3f(1.0f, 1.0f, 1.0f);
  87.  
  88.  
  89.             //polygon
  90.             float a=0,b=0.90;
  91.             glColor3f (1.0, 1.0, 0.0);
  92.             glBegin(GL_POLYGON);
  93.             glVertex2f(b,a);
  94.             glVertex2f(b,-b);
  95.             glVertex2f(-b,-b);
  96.             glVertex2f(-b,a);
  97.             glVertex2f(a,b);
  98.             glEnd();
  99.             //quad
  100.             float x=0.20,y=0.10,z=0.80;
  101.             glColor3f (0.0, 0.0, 0.0);
  102.             glBegin(GL_QUADS);
  103.             glVertex2f(x,-y);
  104.             glVertex2f(x,-z);
  105.             glVertex2f(z,-z);
  106.             glVertex2f(z,-y);
  107.             glEnd();
  108.             //triangles
  109.             float m=0.2,n=0.4,o=0.5,p=0.7;
  110.             glColor3f (0.0, 0.0, 0.0);
  111.             glBegin(GL_TRIANGLES);
  112.             //triangle - 1
  113.             glVertex2f(-p,-m);
  114.             glVertex2f(-o,-m);
  115.             glVertex2f(-o,-n);
  116.             //triangle - 2
  117.             glVertex2d(-o,-n);
  118.             glVertex2d(-p,-n);
  119.             glVertex2d(-p,-m);
  120.             glEnd();
  121.             glTranslatef(0,0.0005,0);
  122.             //glTranslatef(0,-0.0005,0);
  123.  
  124.             //glPopMatrix();
  125.  
  126.             SwapBuffers(hDC);
  127.  
  128.             //theta += 1.0f;
  129.             //Sleep (1);
  130.             glFlush();
  131.         }
  132.     }
  133.  
  134.     /* shutdown OpenGL */
  135.     DisableOpenGL(hwnd, hDC, hRC);
  136.  
  137.     /* destroy the window explicitly */
  138.     DestroyWindow(hwnd);
  139.  
  140.     return msg.wParam;
  141. }
  142.  
  143.  
  144.  
  145. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  146. {
  147.     switch (uMsg)
  148.     {
  149.     /*
  150.     case GLUT_KEY_DOWN:
  151.     //do something here
  152.     glTranslatef(0,-0.0005,0);
  153.     break;
  154.     */
  155.     case 's':
  156.         break;
  157.     case VK_UP:
  158.         break;
  159.     case VK_DOWN:
  160.         break;
  161.     case VK_LEFT:
  162.         break;
  163.     case VK_RIGHT:
  164.         break;
  165.  
  166.         glTranslatef(0,-0.0005,0);
  167.         break;
  168.  
  169.     case WM_CLOSE:
  170.         PostQuitMessage(0);
  171.         break;
  172.  
  173.     case WM_DESTROY:
  174.         return 0;
  175.  
  176.     case WM_KEYDOWN:
  177.     {
  178.         switch (wParam)
  179.         {
  180.         case VK_ESCAPE:
  181.             PostQuitMessage(0);
  182.             break;
  183.         }
  184.     }
  185.     break;
  186.  
  187.     default:
  188.         return DefWindowProc(hwnd, uMsg, wParam, lParam);
  189.     }
  190.  
  191.     return 0;
  192. }
  193.  
  194. void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
  195. {
  196.     PIXELFORMATDESCRIPTOR pfd;
  197.  
  198.     int iFormat;
  199.  
  200.     /* get the device context (DC) */
  201.     *hDC = GetDC(hwnd);
  202.  
  203.     /* set the pixel format for the DC */
  204.     ZeroMemory(&pfd, sizeof(pfd));
  205.  
  206.     pfd.nSize = sizeof(pfd);
  207.     pfd.nVersion = 1;
  208.     pfd.dwFlags = PFD_DRAW_TO_WINDOW |
  209.                   PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  210.     pfd.iPixelType = PFD_TYPE_RGBA;
  211.     pfd.cColorBits = 24;
  212.     pfd.cDepthBits = 16;
  213.     pfd.iLayerType = PFD_MAIN_PLANE;
  214.  
  215.     iFormat = ChoosePixelFormat(*hDC, &pfd);
  216.  
  217.     SetPixelFormat(*hDC, iFormat, &pfd);
  218.  
  219.     /* create and enable the render context (RC) */
  220.     *hRC = wglCreateContext(*hDC);
  221.  
  222.     wglMakeCurrent(*hDC, *hRC);
  223. }
  224.  
  225. void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
  226. {
  227.     wglMakeCurrent(NULL, NULL);
  228.     wglDeleteContext(hRC);
  229.     ReleaseDC(hwnd, hDC);
  230. }
Add Comment
Please, Sign In to add comment