psychotrip

OpenGL.ChessFloor

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