Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <GL/glut.h>
  3.  
  4. #include <vector>
  5.  
  6. #define GLM_ENABLE_EXPERIMENTAL
  7.  
  8. #include <glm/glm.hpp>
  9. #include <glm/ext.hpp>
  10. #include <glm/gtx/string_cast.hpp>
  11.  
  12. typedef glm::vec3 point3;
  13. // using point3 = glm::vec3
  14.  
  15.  
  16. ////////
  17. struct triangle
  18. {
  19. triangle() = default;
  20.  
  21. triangle(const point3& pt1,
  22. const point3& pt2,
  23. const point3& pt3) : A(pt1), B(pt2), C(pt3){}
  24. point3 A, B, C;
  25.  
  26. };
  27. //////////
  28.  
  29. point3 projekcija(const triangle& t, const point3& tocka)
  30. {
  31. glm::vec3 vektor = tocka - t.A;
  32.  
  33. const float distance = glm::dot(vektor,normal(t));
  34. std::cout<<distance;
  35. return tocka - distance*normal(t);
  36. }
  37. glm::vec3 normal(const triangle& t)
  38. {
  39. const glm::vec3 v1 = t.B - t.A;
  40. const glm::vec3 v2 = t.C - t.A;
  41. const auto n = glm::cross(v1, v2);
  42. return glm::normalize(n); //normalize skracuje na duljinu 1
  43. }
  44. point3 teziste(const triangle& t)
  45. {
  46. return (1.f/3.f)*(t.A + t.B + t.C);
  47. }
  48. std::vector<triangle> triangles;
  49.  
  50. void changeSize(int w, int h)
  51. {
  52. if(h == 0) { h = 1;}
  53.  
  54. float ratio = 1.0* w / h;
  55.  
  56. glMatrixMode ( GL_PROJECTION );
  57. glLoadIdentity();
  58. glViewport ( 0, 0, w, h );
  59. gluPerspective ( 45,ratio,1,1000 );
  60. }
  61.  
  62. void drawScene()
  63. {
  64. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  65.  
  66. glMatrixMode(GL_MODELVIEW); // idemo u perspektivu
  67. glLoadIdentity(); // resetiranje
  68. gluLookAt(5.0, 4.0, -10.0, // camera
  69. 0.0, 0.0, -1.0, // where
  70. 0.0f, 1.0, 0.0); // up vector
  71.  
  72.  
  73. /////////
  74. glPointSize(5);
  75. for(const triangle& t : triangles)
  76. {
  77. glColor3f(1, 1, 1);
  78. glBegin(GL_LINE_LOOP);// crta poliliniju, zadnja i prva (tocka?) se spajaju
  79. glVertex3f(t.A.x, t.A.y, t.A.z);
  80. glVertex3f(t.B.x, t.B.y, t.B.z);
  81. glVertex3f(t.C.x, t.C.y, t.C.z);
  82. glEnd();
  83. glColor3f(0, 1, 1);
  84. const auto cg = teziste(t);
  85. const auto n = normal(t);
  86. glBegin(GL_POINTS);
  87. glVertex3f(cg.x, cg.y, cg.z);
  88. glEnd();
  89. glColor3f(1, 0, 0);
  90. glBegin(GL_LINES);
  91. glVertex3f(cg.x, cg.y, cg.z);
  92. glVertex3f(cg.x + n.x, cg.y + n.y, cg.z + n.z);
  93. glEnd();
  94.  
  95. const auto pro = projekcija(t, tocka);
  96. glColor(0, 1, 0);
  97. glBegin(GL_LINES);
  98. glVertex3f(pro.x, pro.y, pro.z);
  99. glEnd();
  100.  
  101.  
  102. }
  103.  
  104.  
  105. /////////
  106. /*
  107. glBegin(GL_TRIANGLES);
  108. //Triangle
  109. glVertex3f(0.5f, -0.5f, 0.0f);
  110. glVertex3f(0.0f, 0.5f, 0.0f);
  111. glVertex3f(-0.5f, -0.5f, 0.0f);
  112. glEnd();
  113. */
  114. glutSwapBuffers();
  115. }
  116.  
  117. int main(int argc, char **argv)
  118. {
  119.  
  120.  
  121. ///////
  122. point3 pt1(0.5, -0.5, 0);
  123. point3 pt2(0., 0.5, 0);
  124. point3 pt3(-0.5, -0.5, 0);
  125. point3 pt4(0, 0.2, -1);
  126. point3 tocka(0, -0.5, 2);
  127. triangles.push_back(triangle(pt1, pt2, pt3));
  128. triangles.push_back(triangle(pt2, pt3, pt4));
  129.  
  130.  
  131.  
  132. glutInit(&argc, argv);
  133. glutInitDisplayMode(GLUT_DOUBLE);
  134. glutInitWindowSize(400, 400);
  135.  
  136. glutCreateWindow("v01 - p1");
  137. glutReshapeFunc(changeSize);
  138. glutDisplayFunc(drawScene);
  139. glutMainLoop();
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement