Advertisement
MateuszGrabarczyk

Untitled

Mar 20th, 2023
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.69 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. struct Point {
  8. float x, y, z;
  9. };
  10.  
  11. struct Vector {
  12. float x, y, z;
  13. };
  14.  
  15. struct Triangle {
  16. Point p1, p2, p3;
  17. };
  18.  
  19. struct Square {
  20. Point p1, p2, p3, p4;
  21. };
  22.  
  23. struct Color {
  24. float c1, c2, c3, c4 = 1;
  25. };
  26.  
  27.  
  28. void drawRectangle(Point observer);
  29.  
  30. void glVertex3f(Point point) {
  31. glVertex3f(point.x, point.y, point.z);
  32. }
  33.  
  34. void loadTriangle(Color color, Triangle tr) {
  35. glBegin(GL_TRIANGLES);
  36. glColor4f(color.c1, color.c2, color.c3, color.c4);
  37. glVertex3f(tr.p1);
  38. glVertex3f(tr.p2);
  39. glVertex3f(tr.p3);
  40. glEnd();
  41. }
  42.  
  43. void loadRectangle(Color color, struct Square r) {
  44. glBegin(GL_QUADS);
  45. glColor4f(color.c1, color.c2, color.c3, color.c4);
  46. glVertex3f(r.p1);
  47. glVertex3f(r.p2);
  48. glVertex3f(r.p3);
  49. glVertex3f(r.p4);
  50. glEnd();
  51. }
  52.  
  53. Point findCenter(struct Square rect) {
  54. Point center = {
  55. (rect.p1.x + rect.p3.x) / 2,
  56. (rect.p1.y + rect.p3.y) / 2,
  57. (rect.p1.z + rect.p3.z) / 2
  58. };
  59.  
  60. return center;
  61. }
  62.  
  63. void drawObserverToShape(Point observer, struct Square rect) {
  64. Point center = findCenter(rect);
  65.  
  66. glBegin(GL_LINES);
  67. glColor3f(0.04f, 0.98f, 0.835f);
  68. glVertex3f(center.x, center.y, center.z);
  69. glVertex3f(observer.x, observer.y, observer.z);
  70. glEnd();
  71. }
  72.  
  73. Vector calculateNormalVector(Triangle tr) {
  74.  
  75. Vector vector1 = {
  76. tr.p2.x - tr.p1.x,
  77. tr.p2.y - tr.p1.y,
  78. tr.p2.z - tr.p1.z
  79. };
  80.  
  81. Vector vector2 = {
  82. tr.p3.x - tr.p1.x,
  83. tr.p3.y - tr.p1.y,
  84. tr.p3.z - tr.p1.z
  85. };
  86.  
  87. Vector normal = {
  88. vector1.y * vector2.z - vector2.y * vector1.z,
  89. vector2.x * vector1.z - vector1.x * vector2.z,
  90. vector1.x * vector2.y - vector2.x * vector1.y
  91. };
  92.  
  93. return normal;
  94. }
  95.  
  96. Vector calculateNormalVector(struct Square tr) {
  97. return calculateNormalVector(Triangle{
  98. tr.p1,
  99. tr.p2,
  100. tr.p3
  101. });
  102. }
  103.  
  104. Vector calculateObservatorVector(Point observer, Point w1) {
  105. Vector v = {
  106. observer.x - w1.x,
  107. observer.y - w1.y,
  108. observer.z - w1.z
  109. };
  110.  
  111. return v;
  112. }
  113.  
  114. bool checkVisibility(Vector observer, Vector normal) {
  115. return (observer.x * normal.x + observer.y * normal.y + observer.z * normal.z) > 0;
  116. }
  117.  
  118. bool checkTriangleVisibility(Point observer, struct Triangle rectangle) {
  119. Vector observerVector = calculateObservatorVector(observer, rectangle.p1);
  120. Vector normalVector = calculateNormalVector(rectangle);
  121.  
  122. return checkVisibility(observerVector, normalVector);
  123. }
  124.  
  125. bool checkRectangleVisibility(Point observer, struct Square rectangle) {
  126.  
  127. drawObserverToShape(observer, rectangle);
  128.  
  129. return checkTriangleVisibility(observer, Triangle{
  130. rectangle.p1,
  131. rectangle.p2,
  132. rectangle.p3
  133. });
  134. }
  135.  
  136. Point observer = {
  137. 0, 5, 5
  138. };
  139.  
  140. void DrawScene(GLfloat xRot, GLfloat yRot)
  141. {
  142. int i;
  143. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  144. glLoadIdentity();
  145.  
  146. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  147. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  148.  
  149. glBegin(GL_LINES);
  150. glColor3f(1, 1, 1);
  151. glVertex3f(10, 0, 0);
  152. glVertex3f(-10, 0, 0);
  153. glVertex3f(0, 10, 0);
  154. glVertex3f(0, -10, 0);
  155. glVertex3f(0, 0, 10);
  156. glVertex3f(0, 0, -10);
  157. glEnd();
  158.  
  159. glBegin(GL_LINES);
  160. glColor3f(1, 0, 0);
  161. glVertex3f(observer.x - 1, observer.y, observer.z);
  162. glVertex3f(observer.x + 1, observer.y, observer.z);
  163. glVertex3f(observer.x, observer.y - 1, observer.z);
  164. glVertex3f(observer.x, observer.y + 1, observer.z);
  165. glVertex3f(observer.x, observer.y, observer.z - 1);
  166. glVertex3f(observer.x, observer.y, observer.z + 1);
  167.  
  168. glEnd();
  169.  
  170. drawRectangle(observer);
  171.  
  172. glFlush();
  173.  
  174. glEnd();
  175. glFinish();
  176. }
  177.  
  178.  
  179. void drawRectangle(Point observer) {
  180.  
  181. float a = 6, b = 6, c = 6;
  182. float centerA = (float)a / 2, centerB = (float)b / 2, centerC = (float)c / 2;
  183.  
  184. struct Square r1 {
  185. Point{ -centerA, centerB, centerC },
  186. Point{ -centerA, -centerB, centerC },
  187. Point{ centerA,-centerB, centerC },
  188. Point{ centerA, centerB, centerC }
  189. };
  190.  
  191. struct Square r2 {
  192. Point{ -centerA, -centerB, -centerC },
  193. Point{ -centerA, centerB, -centerC },
  194. Point{ centerA, centerB, -centerC },
  195. Point{ centerA, -centerB, -centerC }
  196. };
  197.  
  198. struct Square r3 {
  199. Point{ -centerA, -centerB, centerC },
  200. Point{ -centerA, -centerB, -centerC },
  201. Point{ centerA, -centerB, -centerC },
  202. Point{ centerA, -centerB, centerC }
  203. };
  204.  
  205. struct Square r4 {
  206. Point{ -centerA, centerB, -centerC },
  207. Point{ -centerA, centerB, centerC },
  208. Point{ centerA, centerB, centerC },
  209. Point{ centerA, centerB, -centerC }
  210. };
  211.  
  212. struct Square r5 {
  213. Point{ -centerA, centerB, -centerC },
  214. Point{ -centerA, -centerB, -centerC },
  215. Point{ -centerA, -centerB, centerC },
  216. Point{ -centerA, centerB, centerC }
  217. };
  218.  
  219. struct Square r6 {
  220. Point{ centerA, centerB, centerC },
  221. Point{ centerA, -centerB, centerC },
  222. Point{ centerA, -centerB, -centerC },
  223. Point{ centerA, centerB, -centerC }
  224. };
  225.  
  226. if (checkRectangleVisibility(observer, r1)) {
  227. loadRectangle(Color{ 1.0, 1.0, 1.0, 0.6 }, r1);
  228. }
  229.  
  230. if (checkRectangleVisibility(observer, r2)) {
  231. loadRectangle(Color{ 1.0, 0, 1.0 }, r2);
  232. }
  233.  
  234.  
  235. if (checkRectangleVisibility(observer, r3)) {
  236. loadRectangle(Color{ 1.0f, 1.0f, 0.0f }, r3);
  237. }
  238.  
  239.  
  240. if (checkRectangleVisibility(observer, r4)) {
  241. loadRectangle(Color{ 0.0f, 1.0f, 1.0f }, r4);
  242. }
  243.  
  244.  
  245. if (checkRectangleVisibility(observer, r5)) {
  246. loadRectangle(Color{ 1.0f, 0.0f, 0.0f }, r5);
  247. }
  248.  
  249.  
  250. if (checkRectangleVisibility(observer, r6)) {
  251. loadRectangle(Color{ 0.93f, 0.545f, 0.074f }, r6);
  252. }
  253. }
  254.  
  255. void SetMyPixelFormat(HDC hdc)
  256. {
  257. PIXELFORMATDESCRIPTOR pfd;
  258. ZeroMemory(&pfd, sizeof(pfd));
  259. pfd.nSize = sizeof(pfd);
  260. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  261. pfd.iPixelType = PFD_TYPE_RGBA;
  262. pfd.cColorBits = 32;
  263. pfd.cDepthBits = 16;
  264. pfd.iLayerType = PFD_MAIN_PLANE;
  265.  
  266. int nPixelFormat = ChoosePixelFormat(hdc, &pfd);
  267. SetPixelFormat(hdc, nPixelFormat, &pfd);
  268. }
  269. void ResizeWindow(int width, int height)
  270. {
  271. if (height * width == 0) return;
  272. glViewport(0, 0, width, height);
  273. glMatrixMode(GL_PROJECTION);
  274. glLoadIdentity();
  275. glOrtho(-10, 10, -10, 10, -10, 10);
  276. glMatrixMode(GL_MODELVIEW);
  277. glLoadIdentity();
  278. glEnable(GL_DEPTH_TEST);
  279.  
  280. glEnable(GL_LINE_SMOOTH);
  281.  
  282.  
  283. glShadeModel(GL_FLAT);
  284. }
  285. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  286. {
  287. PAINTSTRUCT ps;
  288. HDC hdc;
  289. static HGLRC hrc;
  290. static GLfloat xRot = 0.0f;
  291. static GLfloat yRot = 0.0f;
  292.  
  293. switch (message)
  294. {
  295. case WM_SIZE:
  296. ResizeWindow(LOWORD(lParam), HIWORD(lParam));
  297. break;
  298. case WM_CREATE:
  299. hdc = GetDC(hWnd);
  300. SetMyPixelFormat(hdc);
  301. hrc = wglCreateContext(hdc);
  302. wglMakeCurrent(hdc, hrc);
  303. ReleaseDC(hWnd, hdc);
  304. break;
  305. case WM_KEYDOWN:
  306. if (wParam == VK_UP) xRot -= 5.0f;
  307. if (wParam == VK_DOWN) xRot += 5.0f;
  308. if (wParam == VK_LEFT) yRot -= 5.0f;
  309. if (wParam == VK_RIGHT) yRot += 5.0f;
  310.  
  311. if (wParam == 'W') observer.z += 1.0f;
  312. if (wParam == 'S') observer.z -= 1.0f;
  313. if (wParam == 'A') observer.x -= 1.0f;
  314. if (wParam == 'D') observer.x += 1.0f;
  315. if (wParam == 'Q') observer.y -= 1.0f;
  316. if (wParam == 'E') observer.y += 1.0f;
  317.  
  318. if (xRot > 356.0f) xRot = 0.0f;
  319. if (xRot < -1.0f) xRot = 355.0f;
  320. if (yRot > 356.0f) yRot = 0.0f;
  321. if (yRot < -1.0f) yRot = 355.0f;
  322.  
  323. InvalidateRect(hWnd, NULL, FALSE);
  324. break;
  325. case WM_PAINT:
  326. hdc = BeginPaint(hWnd, &ps);
  327. DrawScene(xRot, yRot);
  328. SwapBuffers(hdc);
  329. EndPaint(hWnd, &ps);
  330. break;
  331. case WM_ERASEBKGND:
  332. return 1;
  333. break;
  334. case WM_DESTROY:
  335. wglMakeCurrent(NULL, NULL);
  336. wglDeleteContext(hrc);
  337. PostQuitMessage(0);
  338. break;
  339. default:
  340. return DefWindowProc(hWnd, message, wParam, lParam);
  341. }
  342. return 0;
  343. }
  344. ATOM MyRegisterClass(HINSTANCE hInstance)
  345. {
  346. WNDCLASSEX wcex;
  347. wcex.cbSize = sizeof(WNDCLASSEX);
  348. wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  349. wcex.lpfnWndProc = (WNDPROC)WndProc;
  350. wcex.cbClsExtra = 0;
  351. wcex.cbWndExtra = 0;
  352. wcex.hInstance = hInstance;
  353. wcex.hIcon = NULL;
  354. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  355. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  356. wcex.lpszMenuName = NULL;
  357. wcex.lpszClassName = "Primitives";
  358. wcex.hIconSm = NULL;
  359. return RegisterClassEx(&wcex);
  360. }
  361. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  362. {
  363. HWND hWnd;
  364. hWnd = CreateWindow("Primitives", "OGL color lab", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  365. if (!hWnd) return FALSE;
  366. ShowWindow(hWnd, nCmdShow);
  367. UpdateWindow(hWnd);
  368. return TRUE;
  369. }
  370. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
  371. {
  372. MSG msg;
  373. MyRegisterClass(hInstance);
  374. if (!InitInstance(hInstance, nCmdShow)) return FALSE;
  375. while (GetMessage(&msg, NULL, 0, 0)) {
  376. TranslateMessage(&msg);
  377. DispatchMessage(&msg);
  378. }
  379. return (int)msg.wParam;
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement