Advertisement
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>
- struct Point {
- float x, y, z;
- };
- struct Vector {
- float x, y, z;
- };
- struct Triangle {
- Point p1, p2, p3;
- };
- struct Square {
- Point p1, p2, p3, p4;
- };
- struct Color {
- float c1, c2, c3, c4 = 1;
- };
- void drawRectangle(Point observer);
- void glVertex3f(Point point) {
- glVertex3f(point.x, point.y, point.z);
- }
- void loadTriangle(Color color, Triangle tr) {
- glBegin(GL_TRIANGLES);
- glColor4f(color.c1, color.c2, color.c3, color.c4);
- glVertex3f(tr.p1);
- glVertex3f(tr.p2);
- glVertex3f(tr.p3);
- glEnd();
- }
- void loadRectangle(Color color, struct Square r) {
- glBegin(GL_QUADS);
- glColor4f(color.c1, color.c2, color.c3, color.c4);
- glVertex3f(r.p1);
- glVertex3f(r.p2);
- glVertex3f(r.p3);
- glVertex3f(r.p4);
- glEnd();
- }
- Point findCenter(struct Square rect) {
- Point center = {
- (rect.p1.x + rect.p3.x) / 2,
- (rect.p1.y + rect.p3.y) / 2,
- (rect.p1.z + rect.p3.z) / 2
- };
- return center;
- }
- void drawObserverToShape(Point observer, struct Square rect) {
- Point center = findCenter(rect);
- glBegin(GL_LINES);
- glColor3f(0.04f, 0.98f, 0.835f);
- glVertex3f(center.x, center.y, center.z);
- glVertex3f(observer.x, observer.y, observer.z);
- glEnd();
- }
- Vector calculateNormalVector(Triangle tr) {
- Vector vector1 = {
- tr.p2.x - tr.p1.x,
- tr.p2.y - tr.p1.y,
- tr.p2.z - tr.p1.z
- };
- Vector vector2 = {
- tr.p3.x - tr.p1.x,
- tr.p3.y - tr.p1.y,
- tr.p3.z - tr.p1.z
- };
- Vector normal = {
- vector1.y * vector2.z - vector2.y * vector1.z,
- vector2.x * vector1.z - vector1.x * vector2.z,
- vector1.x * vector2.y - vector2.x * vector1.y
- };
- return normal;
- }
- Vector calculateNormalVector(struct Square tr) {
- return calculateNormalVector(Triangle{
- tr.p1,
- tr.p2,
- tr.p3
- });
- }
- Vector calculateObservatorVector(Point observer, Point w1) {
- Vector v = {
- observer.x - w1.x,
- observer.y - w1.y,
- observer.z - w1.z
- };
- return v;
- }
- bool checkVisibility(Vector observer, Vector normal) {
- return (observer.x * normal.x + observer.y * normal.y + observer.z * normal.z) > 0;
- }
- bool checkTriangleVisibility(Point observer, struct Triangle rectangle) {
- Vector observerVector = calculateObservatorVector(observer, rectangle.p1);
- Vector normalVector = calculateNormalVector(rectangle);
- return checkVisibility(observerVector, normalVector);
- }
- bool checkRectangleVisibility(Point observer, struct Square rectangle) {
- drawObserverToShape(observer, rectangle);
- return checkTriangleVisibility(observer, Triangle{
- rectangle.p1,
- rectangle.p2,
- rectangle.p3
- });
- }
- Point observer = {
- 0, 5, 5
- };
- void DrawScene(GLfloat xRot, GLfloat yRot)
- {
- int i;
- 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);
- 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();
- glBegin(GL_LINES);
- glColor3f(1, 0, 0);
- glVertex3f(observer.x - 1, observer.y, observer.z);
- glVertex3f(observer.x + 1, observer.y, observer.z);
- glVertex3f(observer.x, observer.y - 1, observer.z);
- glVertex3f(observer.x, observer.y + 1, observer.z);
- glVertex3f(observer.x, observer.y, observer.z - 1);
- glVertex3f(observer.x, observer.y, observer.z + 1);
- glEnd();
- drawRectangle(observer);
- glFlush();
- glEnd();
- glFinish();
- }
- void drawRectangle(Point observer) {
- float a = 6, b = 6, c = 6;
- float centerA = (float)a / 2, centerB = (float)b / 2, centerC = (float)c / 2;
- struct Square r1 {
- Point{ -centerA, centerB, centerC },
- Point{ -centerA, -centerB, centerC },
- Point{ centerA,-centerB, centerC },
- Point{ centerA, centerB, centerC }
- };
- struct Square r2 {
- Point{ -centerA, -centerB, -centerC },
- Point{ -centerA, centerB, -centerC },
- Point{ centerA, centerB, -centerC },
- Point{ centerA, -centerB, -centerC }
- };
- struct Square r3 {
- Point{ -centerA, -centerB, centerC },
- Point{ -centerA, -centerB, -centerC },
- Point{ centerA, -centerB, -centerC },
- Point{ centerA, -centerB, centerC }
- };
- struct Square r4 {
- Point{ -centerA, centerB, -centerC },
- Point{ -centerA, centerB, centerC },
- Point{ centerA, centerB, centerC },
- Point{ centerA, centerB, -centerC }
- };
- struct Square r5 {
- Point{ -centerA, centerB, -centerC },
- Point{ -centerA, -centerB, -centerC },
- Point{ -centerA, -centerB, centerC },
- Point{ -centerA, centerB, centerC }
- };
- struct Square r6 {
- Point{ centerA, centerB, centerC },
- Point{ centerA, -centerB, centerC },
- Point{ centerA, -centerB, -centerC },
- Point{ centerA, centerB, -centerC }
- };
- if (checkRectangleVisibility(observer, r1)) {
- loadRectangle(Color{ 1.0, 1.0, 1.0, 0.6 }, r1);
- }
- if (checkRectangleVisibility(observer, r2)) {
- loadRectangle(Color{ 1.0, 0, 1.0 }, r2);
- }
- if (checkRectangleVisibility(observer, r3)) {
- loadRectangle(Color{ 1.0f, 1.0f, 0.0f }, r3);
- }
- if (checkRectangleVisibility(observer, r4)) {
- loadRectangle(Color{ 0.0f, 1.0f, 1.0f }, r4);
- }
- if (checkRectangleVisibility(observer, r5)) {
- loadRectangle(Color{ 1.0f, 0.0f, 0.0f }, r5);
- }
- if (checkRectangleVisibility(observer, r6)) {
- loadRectangle(Color{ 0.93f, 0.545f, 0.074f }, r6);
- }
- }
- 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);
- glEnable(GL_LINE_SMOOTH);
- glShadeModel(GL_FLAT);
- }
- 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;
- 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 (wParam == 'W') observer.z += 1.0f;
- if (wParam == 'S') observer.z -= 1.0f;
- if (wParam == 'A') observer.x -= 1.0f;
- if (wParam == 'D') observer.x += 1.0f;
- if (wParam == 'Q') observer.y -= 1.0f;
- if (wParam == 'E') observer.y += 1.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;
- InvalidateRect(hWnd, NULL, FALSE);
- break;
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- DrawScene(xRot, yRot);
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement