Advertisement
dykow

ENP1

Oct 19th, 2021
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.51 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include<GL/gl.h>
  5. #include<GL/glu.h>
  6.  
  7.  
  8. void DrawScene(GLfloat xRot, GLfloat yRot, GLfloat* pos)
  9. {
  10.     int i;
  11.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  12.     glLoadIdentity();
  13.  
  14.     glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  15.     glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  16.  
  17.     glBegin(GL_LINES);
  18.         glColor3f(1,1,1);
  19.         glVertex3f(10,0,0);
  20.         glVertex3f(-10,0,0);
  21.         glVertex3f(0,10,0);
  22.         glVertex3f(0,-10,0);
  23.         glVertex3f(0,0,10);
  24.         glVertex3f(0,0,-10);
  25.     glEnd();
  26.  
  27.     glBegin(GL_QUADS);
  28.         glColor3f(1, 0, 0);
  29.         glVertex3f(-5, -5, 5);
  30.         glVertex3f(-5, 5, 5);
  31.         glVertex3f(5, 5, 5);
  32.         glVertex3f(5, -5, 5);
  33.     glEnd();
  34.  
  35.     glBegin(GL_QUADS);
  36.         glColor3f(0, 1, 0);
  37.         glVertex3f(-5, -5, -5);
  38.         glVertex3f(-5, 5, -5);
  39.         glVertex3f(5, 5, -5);
  40.         glVertex3f(5, -5, -5);
  41.     glEnd();
  42.  
  43.     glBegin(GL_QUADS);
  44.         glColor3f(0, 0, 1);
  45.         glVertex3f(-5, -5, -5);
  46.         glVertex3f(-5, 5, -5);
  47.         glVertex3f(-5, 5, 5);
  48.         glVertex3f(-5, -5, 5);
  49.     glEnd();
  50.  
  51.     glBegin(GL_QUADS);
  52.         glColor3f(0.5, 0, 1);
  53.         glVertex3f(5, -5, 5);
  54.         glVertex3f(5, 5, 5);
  55.         glVertex3f(5, 5, -5);
  56.         glVertex3f(5, -5, -5);
  57.     glEnd();
  58.  
  59.     glBegin(GL_QUADS);
  60.         glColor3f(0.5, 0.5, 0);
  61.         glVertex3f(-5, 5, 5);
  62.         glVertex3f(5, 5, 5);
  63.         glVertex3f(5, 5, -5);
  64.         glVertex3f(-5, 5, -5);
  65.     glEnd();
  66.  
  67.     glBegin(GL_QUADS);
  68.         glColor3f(0, 0.5, 0.5);
  69.         glVertex3f(-5, -5, 5);
  70.         glVertex3f(-5, -5, -5);
  71.         glVertex3f(5, -5, -5);
  72.         glVertex3f(5, -5, 5);
  73.     glEnd();
  74.  
  75.     glBegin(GL_POINTS);
  76.     glPointSize(2);
  77.     glColor3f(1, 1, 0);
  78.     glVertex3f(pos[0], pos[1], pos[2]);
  79.     glEnd();
  80.  
  81.     /*glBegin(GL_QUADS);
  82.         glColor4f(0,1.0,0,0.6);
  83.         glVertex3f(-3,-3,3);
  84.         glVertex3f(-3,3,3);
  85.         glVertex3f(3,3,3);
  86.         glVertex3f(3,-3,3);
  87.     glEnd();
  88.  
  89.     glBegin(GL_QUADS);
  90.         glColor3f(0, 0, 0);
  91.         glVertex3f(-3, -3, -3);
  92.         glColor3f(0, 1, 0);
  93.         glVertex3f(-3, 3, -3);
  94.         glColor3f(1, 1, 0);
  95.         glVertex3f(3, 3, -3);
  96.         glColor3f(1, 0, 0);
  97.         glVertex3f(3, -3, -3);
  98.     glEnd();*/
  99.  
  100.     glFlush();
  101.  
  102.     glEnd();
  103.     glFinish();
  104. }
  105.  
  106. void SetMyPixelFormat(HDC hdc)
  107. {
  108.     PIXELFORMATDESCRIPTOR pfd;
  109.     ZeroMemory(&pfd, sizeof(pfd));
  110.     pfd.nSize = sizeof(pfd);
  111.     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  112.     pfd.iPixelType = PFD_TYPE_RGBA;
  113.     pfd.cColorBits = 32;
  114.     pfd.cDepthBits = 16;
  115.     pfd.iLayerType = PFD_MAIN_PLANE;
  116.  
  117.     int nPixelFormat = ChoosePixelFormat(hdc, &pfd);
  118.     SetPixelFormat(hdc, nPixelFormat, &pfd);
  119. }
  120. void ResizeWindow(int width, int height)
  121. {
  122.     if (height*width==0) return;
  123.     glViewport(0,0,width,height);
  124.     glMatrixMode(GL_PROJECTION);
  125.     glLoadIdentity();
  126.     glOrtho(-10, 10, -10, 10, -10, 10);
  127.     glMatrixMode(GL_MODELVIEW);
  128.     glLoadIdentity();
  129.     glEnable(GL_DEPTH_TEST);
  130.    
  131.     //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);// set the blend mode
  132.     //glEnable(GL_BLEND); //obligatory for blending and transparencies
  133.     glEnable(GL_LINE_SMOOTH);
  134.  
  135.     //glEnable(GL_CULL_FACE);
  136.    
  137.     glShadeModel(GL_FLAT);
  138.     //glShadeModel(GL_SMOOTH);
  139. }
  140. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  141. {
  142.     PAINTSTRUCT ps;
  143.     HDC hdc;
  144.     static HGLRC hrc;
  145.     static GLfloat xRot = 0.0f;
  146.     static GLfloat yRot = 0.0f;
  147.     static GLfloat pos[3] = { 6.0f, 6.0f, 6.0f };
  148.  
  149.     switch (message)
  150.     {
  151.     case WM_SIZE:
  152.         ResizeWindow(LOWORD(lParam),HIWORD(lParam));
  153.         break;
  154.     case WM_CREATE:
  155.         hdc = GetDC(hWnd);
  156.         SetMyPixelFormat(hdc);
  157.         hrc = wglCreateContext(hdc);
  158.         wglMakeCurrent(hdc, hrc);
  159.         ReleaseDC(hWnd, hdc);
  160.         break;
  161.     case WM_KEYDOWN:
  162.         if (wParam == VK_UP) xRot -= 5.0f;
  163.         if (wParam == VK_DOWN) xRot += 5.0f;
  164.         if (wParam == VK_LEFT) yRot -= 5.0f;
  165.         if (wParam == VK_RIGHT) yRot += 5.0f;
  166.  
  167.         if (xRot > 356.0f) xRot = 0.0f;
  168.         if (xRot < -1.0f) xRot = 355.0f;
  169.         if (yRot > 356.0f) yRot = 0.0f;
  170.         if (yRot < -1.0f) yRot = 355.0f;
  171.  
  172.         if (wParam == 'W') pos[2] -= 0.5f;
  173.         if (wParam == 'S') pos[2] += 0.5f;
  174.         if (wParam == 'A') pos[0] -= 0.5f;
  175.         if (wParam == 'D') pos[0] += 0.5f;
  176.         if (wParam == 'Q') pos[1] -= 0.5f;
  177.         if (wParam == 'E') pos[1] += 0.5f;
  178.  
  179.         InvalidateRect(hWnd, NULL, FALSE);
  180.         break;
  181.     case WM_PAINT:
  182.         hdc = BeginPaint(hWnd, &ps);
  183.         DrawScene(xRot, yRot, pos);
  184.         SwapBuffers(hdc);
  185.         EndPaint(hWnd, &ps);       
  186.         break;
  187.     case WM_ERASEBKGND:
  188.         return 1;
  189.         break;
  190.     case WM_DESTROY:
  191.         wglMakeCurrent(NULL, NULL);
  192.         wglDeleteContext(hrc);
  193.         PostQuitMessage(0);
  194.         break;
  195.     default:
  196.         return DefWindowProc(hWnd, message, wParam, lParam);
  197.     }
  198.     return 0;
  199. }
  200. ATOM MyRegisterClass(HINSTANCE hInstance)
  201. {
  202.     WNDCLASSEX wcex;
  203.     wcex.cbSize = sizeof(WNDCLASSEX);
  204.     wcex.style          = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  205.     wcex.lpfnWndProc    = (WNDPROC)WndProc;
  206.     wcex.cbClsExtra     = 0;
  207.     wcex.cbWndExtra     = 0;
  208.     wcex.hInstance      = hInstance;
  209.     wcex.hIcon          = NULL;
  210.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  211.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  212.     wcex.lpszMenuName   = NULL;
  213.     wcex.lpszClassName  = "Primitives";
  214.     wcex.hIconSm        = NULL;
  215.     return RegisterClassEx(&wcex);
  216. }
  217. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  218. {
  219.    HWND hWnd;
  220.    hWnd = CreateWindow("Primitives", "OGL color lab", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  221.    if (!hWnd) return FALSE;
  222.    ShowWindow(hWnd, nCmdShow);
  223.    UpdateWindow(hWnd);
  224.    return TRUE;
  225. }
  226. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
  227. {
  228.     MSG msg;
  229.     MyRegisterClass(hInstance);
  230.     if (!InitInstance(hInstance, nCmdShow)) return FALSE;
  231.     while (GetMessage(&msg, NULL, 0, 0)) {
  232.         TranslateMessage(&msg);
  233.         DispatchMessage(&msg);
  234.     }
  235.     return (int) msg.wParam;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement