Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <stdlib.h>
- #include <math.h>
- #include<GL/gl.h>
- #include<GL/glu.h>
- bool isVisible(float v[3], float u[3])
- {
- float sum = 0;
- for (int i = 0; i < 3; i++)
- sum += v[i] * u[i];
- return sum > 0 ? true : false;
- }
- void DrawScene(GLfloat xRot, GLfloat yRot, GLfloat* pos)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glRotatef(xRot, 1.0f, 0.0f, 0.0f);
- glRotatef(yRot, 0.0f, 1.0f, 0.0f);
- float fn[3] = { 0.0f, 0.0f, 3.0f }; //wektor normalny przod
- float bn[3] = { 0.0f, 0.0f, -3.0f }; // wektor normalny tyl
- float ln[3] = { -3.0f, 0.0f, 0.0f }; // wektor normalny lewy
- float rn[3] = { 3.0f, 0.0f, 0.0f }; //wektor normalny prawy
- float un[3] = { 0.0f, 3.0f, 0.0f }; // wektor normalny gora
- float dn[3] = { 0.0f, -3.0f, 0.0f }; // wektor normalny dol
- //wektor od obserwatora do sciany przedniej
- float fv[3] = { pos[0] - fn[0], pos[1] - fn[1], pos[2] - fn[2] };
- //wektor od obserwatora do sciany tylnej
- float bv[3] = { pos[0] - bn[0], pos[1] - bn[1], pos[2] - bn[2] };
- //wektor od obserwatora do sciany lewej
- float lv[3] = { pos[0] - ln[0], pos[1] - ln[1], pos[2] - ln[2] };
- //wektor od obserwatora do sciany prawej
- float rv[3] = { pos[0] - rn[0], pos[1] - rn[1], pos[2] - rn[2] };
- //wektor od obserwatora do sciany gornej
- float uv[3] = { pos[0] - un[0], pos[1] - un[1], pos[2] - un[2] };
- //wektor od obserwatora do sciany dolnej
- float dv[3] = { pos[0] - dn[0], pos[1] - dn[1], pos[2] - dn[2] };
- glBegin(GL_LINES);
- glColor3f(1, 1, 1);
- glVertex3f(10, 0, 0);
- glVertex3f(-10, 0, 0);
- glVertex3f(0, 10, 0);
- glVertex3f(0, -10, 0);
- glVertex3f(0, 0, 10);
- glVertex3f(0, 0, -10);
- glEnd();
- //sciana przednia
- if (isVisible(fn, fv))
- {
- glBegin(GL_QUADS);
- glColor4f(1, 0, 0, 0.6);
- glVertex3f(-3, -3, 3);
- glVertex3f(-3, 3, 3);
- glVertex3f(3, 3, 3);
- glVertex3f(3, -3, 3);
- glEnd();
- }
- //sciana tylna
- if (isVisible(bn, bv))
- {
- glBegin(GL_QUADS);
- glColor4f(0, 1, 0, 0.6);
- glVertex3f(-3, -3, -3);
- glVertex3f(-3, 3, -3);
- glVertex3f(3, 3, -3);
- glVertex3f(3, -3, -3);
- glEnd();
- }
- //sciana lewa
- if (isVisible(ln, lv))
- {
- glBegin(GL_QUADS);
- glColor4f(0, 0, 1, 0.6);
- glVertex3f(-3, -3, -3);
- glVertex3f(-3, 3, -3);
- glVertex3f(-3, 3, 3);
- glVertex3f(-3, -3, 3);
- glEnd();
- }
- //sciana prawa
- if (isVisible(rn, rv))
- {
- glBegin(GL_QUADS);
- glColor4f(0.5, 0, 0, 0.6);
- glVertex3f(3, -3, 3);
- glVertex3f(3, 3, 3);
- glVertex3f(3, 3, -3);
- glVertex3f(3, -3, -3);
- glEnd();
- }
- //sciana gorna
- if (isVisible(un, uv))
- {
- glBegin(GL_QUADS);
- glColor4f(0, 0.5, 0, 0.6);
- glVertex3f(-3, 3, 3);
- glVertex3f(3, 3, 3);
- glVertex3f(3, 3, -3);
- glVertex3f(-3, 3, -3);
- glEnd();
- }
- //sciana dolna
- if (isVisible(dn, dv))
- {
- glBegin(GL_QUADS);
- glColor4f(0, 0, 0.5, 0.6);
- glVertex3f(-3, -3, 3);
- glVertex3f(-3, -3, -3);
- glVertex3f(3, -3, -3);
- glVertex3f(3, -3, 3);
- glEnd();
- }
- //obserwator
- glBegin(GL_POINTS);
- glPointSize(2);
- glColor3f(1, 1, 0);
- glVertex3f(pos[0], pos[1], pos[2]);
- glEnd();
- //rysuj wektory od obserwatora do srodkow scian
- glBegin(GL_LINES);
- glColor3f(1, 1, 0);
- glVertex3f(pos[0], pos[1], pos[2]);
- glVertex3f(fn[0], fn[1], fn[2]);
- glVertex3f(pos[0], pos[1], pos[2]);
- glVertex3f(fn[0], fn[1], bn[2]);
- glVertex3f(pos[0], pos[1], pos[2]);
- glVertex3f(ln[0], ln[1], ln[2]);
- glVertex3f(pos[0], pos[1], pos[2]);
- glVertex3f(rn[0], rn[1], rn[2]);
- glVertex3f(pos[0], pos[1], pos[2]);
- glVertex3f(un[0], un[1], un[2]);
- glVertex3f(pos[0], pos[1], pos[2]);
- glVertex3f(dn[0], dn[1], dn[2]);
- glEnd();
- glFlush();
- glEnd();
- glFinish();
- }
- void SetMyPixelFormat(HDC hdc)
- {
- PIXELFORMATDESCRIPTOR pfd;
- ZeroMemory(&pfd, sizeof(pfd));
- pfd.nSize = sizeof(pfd);
- pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
- PFD_DOUBLEBUFFER;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 32;
- pfd.cDepthBits = 16;
- pfd.iLayerType = PFD_MAIN_PLANE;
- int nPixelFormat = ChoosePixelFormat(hdc, &pfd);
- SetPixelFormat(hdc, nPixelFormat, &pfd);
- }
- void ResizeWindow(int width, int height)
- {
- if (height * width == 0) return;
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-10, 10, -10, 10, -10, 10);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glEnable(GL_DEPTH_TEST);
- //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);// set
- the blend mode
- //glEnable(GL_BLEND); //obligatory for blending and
- transparencies
- glEnable(GL_LINE_SMOOTH);
- //glEnable(GL_CULL_FACE);
- glShadeModel(GL_FLAT);
- //glShadeModel(GL_SMOOTH);
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
- LPARAM lParam)
- {
- PAINTSTRUCT ps;
- HDC hdc;
- static HGLRC hrc;
- static GLfloat xRot = 0.0f;
- static GLfloat yRot = 0.0f;
- static GLfloat pos[3] = { 6.0f, 6.0f, 6.0f };
- switch (message)
- {
- case WM_SIZE:
- ResizeWindow(LOWORD(lParam), HIWORD(lParam));
- break;
- case WM_CREATE:
- hdc = GetDC(hWnd);
- SetMyPixelFormat(hdc);
- hrc = wglCreateContext(hdc);
- wglMakeCurrent(hdc, hrc);
- ReleaseDC(hWnd, hdc);
- break;
- case WM_KEYDOWN:
- if (wParam == VK_UP) xRot -= 5.0f;
- if (wParam == VK_DOWN) xRot += 5.0f;
- if (wParam == VK_LEFT) yRot -= 5.0f;
- if (wParam == VK_RIGHT) yRot += 5.0f;
- if (xRot > 356.0f) xRot = 0.0f;
- if (xRot < -1.0f) xRot = 355.0f;
- if (yRot > 356.0f) yRot = 0.0f;
- if (yRot < -1.0f) yRot = 355.0f;
- if (wParam == 'W') pos[2] -= 1.0f;
- if (wParam == 'S') pos[2] += 1.0f;
- if (wParam == 'A') pos[0] -= 1.0f;
- if (wParam == 'D') pos[0] += 1.0f;
- if (wParam == 'Q') pos[1] -= 1.0f;
- if (wParam == 'E') pos[1] += 1.0f;
- InvalidateRect(hWnd, NULL, FALSE);
- break;
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- DrawScene(xRot, yRot, pos);
- SwapBuffers(hdc);
- EndPaint(hWnd, &ps);
- break;
- case WM_ERASEBKGND:
- return 1;
- break;
- case WM_DESTROY:
- wglMakeCurrent(NULL, NULL);
- wglDeleteContext(hrc);
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
- wcex.lpfnWndProc = (WNDPROC)WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = NULL;
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = "Primitives";
- wcex.hIconSm = NULL;
- return RegisterClassEx(&wcex);
- }
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
- hWnd = CreateWindow("Primitives", "OGL color lab",
- WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL,
- NULL, hInstance, NULL);
- if (!hWnd) return FALSE;
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine, int nCmdShow)
- {
- MSG msg;
- MyRegisterClass(hInstance);
- if (!InitInstance(hInstance, nCmdShow)) return FALSE;
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int)msg.wParam;
- }
Add Comment
Please, Sign In to add comment