Advertisement
Guest User

Untitled

a guest
Jan 26th, 2021
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. #include "ResourcePath.hpp"
  4.  
  5. #define FPS 40
  6.  
  7. struct Vector
  8. {
  9. float x, y, z;
  10.  
  11. Vector() : x(0), y(0), z(0) {}
  12.  
  13. Vector(float x, float y, float z = 0) : x(x), y(y), z(z) {}
  14.  
  15. operator sf::Vertex() const { return sf::Vector2f(x, y); }
  16.  
  17. Vector operator+(Vector v) { return Vector(x + v.x, y + v.y, z + v.z); }
  18.  
  19. Vector operator-(Vector v) { return Vector(x - v.x, y - v.y, z - v.z); }
  20.  
  21. Vector operator*(float r) { return Vector(x*r,y*r,z*r); }
  22.  
  23. Vector operator/(float r) { return Vector(x/r,y/r,z/r); }
  24.  
  25. float magnitude() { return sqrt(x*x+y*y+z*z); }
  26.  
  27. void rotateX(float angle)
  28. {
  29. float y = this->y, z = this->z;
  30. this->y = y * cos(angle) - z * sin(angle);
  31. this->z = y * sin(angle) + z * cos(angle);
  32. }
  33.  
  34. void rotateY(float angle)
  35. {
  36. float x = this->x, z = this->z;
  37. this->x = x * cos(angle) + z * sin(angle);
  38. this->z = - x * sin(angle) + z * cos(angle);
  39. }
  40.  
  41. void rotateZ(float angle)
  42. {
  43. float x = this->x, y = this->y;
  44. this->x = x * cos(angle) - y * sin(angle);
  45. this->y = x * sin(angle) + y * cos(angle);
  46. }
  47.  
  48. };
  49.  
  50. sf::CircleShape point(Vector v, float r)
  51. {
  52. sf::CircleShape shape;
  53. shape.setFillColor(sf::Color::White);
  54. shape.setPosition(v.x-r, v.y-r);
  55. shape.setRadius(r);
  56. return shape;
  57. }
  58.  
  59. int main(int, char const**)
  60. {
  61. sf::RenderWindow window(sf::VideoMode(600, 400), "test3d");
  62. window.setFramerateLimit(FPS);
  63. sf::View view = window.getView();
  64. window.setView(view);
  65.  
  66. Vector camera = Vector(0,0,-2);
  67. Vector theta(0,0);
  68.  
  69.  
  70. Vector points[8];
  71. points[0] = Vector(-0.5, -0.5, -0.5);
  72. points[1] = Vector(0.5, -0.5, -0.5);
  73. points[2] = Vector(0.5, 0.5, -0.5);
  74. points[3] = Vector(-0.5, 0.5, -0.5);
  75. points[4] = Vector(-0.5, -0.5, 0.5);
  76. points[5] = Vector(0.5, -0.5, 0.5);
  77. points[6] = Vector(0.5, 0.5, 0.5);
  78. points[7] = Vector(-0.5, 0.5, 0.5);
  79.  
  80. float mouseX = 0;
  81. float mouseY = 0;
  82.  
  83. while (window.isOpen())
  84. {
  85. sf::Event event;
  86. while (window.pollEvent(event))
  87. {
  88. if (event.type == sf::Event::Closed)
  89. window.close();
  90.  
  91. }
  92.  
  93. if (event.type == sf::Event::MouseMoved)
  94. {
  95. float dx = (mouseY - event.mouseMove.y);
  96. float dy = (event.mouseMove.x - mouseX);
  97. if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
  98. {
  99. theta.x += dx/200;
  100. theta.y += dy/100;
  101. // for (int i = 0; i < 8; i++)
  102. // {
  103. // points[i].rotateX(-dx/100);
  104. // points[i].rotateY(-dy/100);
  105. // }
  106. }
  107. mouseX = event.mouseMove.x;
  108. mouseY = event.mouseMove.y;
  109. }
  110. float alpha = theta.x;
  111. float beta = theta.y;
  112. Vector dir(cos(beta)*sin(alpha), sin(beta), cos(beta)*cos(alpha));
  113. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A))
  114. {
  115.  
  116. }
  117. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D))
  118. {
  119. }
  120. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W))
  121. {
  122. camera = camera + dir/10;
  123. }
  124. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S))
  125. {
  126. camera = camera - dir/10;
  127. }
  128.  
  129. window.clear();
  130.  
  131. Vector projected[8];
  132. for (int i = 0; i < 8; i++)
  133. {
  134. Vector a = points[i] - camera;
  135. float r = a.magnitude();
  136. Vector e(3,2,3);
  137. Vector s(sin(theta.x), sin(theta.y));
  138. Vector c(cos(theta.x), cos(theta.y));
  139. float dx = c.y*a.x - s.y*a.z;
  140. float dy = s.x*(c.y*a.z + s.y*a.x) + c.x*a.y;
  141. float dz = c.x*(c.y*a.z + s.y*a.x) - s.x*a.y;
  142. projected[i] = Vector(e.z*dx/dz+e.x, e.z*dy/dz+e.y) * 100;
  143. window.draw(point(projected[i], 5*e.z/r));
  144. }
  145.  
  146. // window.draw((sf::Vertex[]){projected[0], projected[1]}, 2, sf::Lines);
  147. // window.draw((sf::Vertex[]){projected[1], projected[2]}, 2, sf::Lines);
  148. // window.draw((sf::Vertex[]){projected[2], projected[3]}, 2, sf::Lines);
  149. // window.draw((sf::Vertex[]){projected[3], projected[0]}, 2, sf::Lines);
  150. // window.draw((sf::Vertex[]){projected[4], projected[5]}, 2, sf::Lines);
  151. // window.draw((sf::Vertex[]){projected[5], projected[6]}, 2, sf::Lines);
  152. // window.draw((sf::Vertex[]){projected[6], projected[7]}, 2, sf::Lines);
  153. // window.draw((sf::Vertex[]){projected[7], projected[4]}, 2, sf::Lines);
  154. // window.draw((sf::Vertex[]){projected[0], projected[4]}, 2, sf::Lines);
  155. // window.draw((sf::Vertex[]){projected[1], projected[5]}, 2, sf::Lines);
  156. // window.draw((sf::Vertex[]){projected[2], projected[6]}, 2, sf::Lines);
  157. // window.draw((sf::Vertex[]){projected[3], projected[7]}, 2, sf::Lines);
  158.  
  159. sf::Font font;
  160. font.loadFromFile(resourcePath() + "sansation.ttf");
  161. sf::Text degrees("x: " + std::to_string(theta.x*180/acos(-1)) + "\n"
  162. "y: " + std::to_string(theta.y*180/acos(-1)) + "\n"
  163. "z: " + std::to_string(theta.z*180/acos(-1)), font);
  164. degrees.setFillColor(sf::Color::White);
  165. degrees.setCharacterSize(20);
  166. degrees.setPosition(10, 10);
  167. window.draw(degrees);
  168.  
  169. window.display();
  170. }
  171.  
  172. return 0;
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement