dykow

ENP

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