Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include "ResourcePath.hpp"
- #define FPS 40
- struct Vector
- {
- float x, y, z;
- Vector() : x(0), y(0), z(0) {}
- Vector(float x, float y, float z = 0) : x(x), y(y), z(z) {}
- operator sf::Vertex() const { return sf::Vector2f(x, y); }
- Vector operator+(Vector v) { return Vector(x + v.x, y + v.y, z + v.z); }
- Vector operator-(Vector v) { return Vector(x - v.x, y - v.y, z - v.z); }
- Vector operator*(float r) { return Vector(x*r,y*r,z*r); }
- Vector operator/(float r) { return Vector(x/r,y/r,z/r); }
- float magnitude() { return sqrt(x*x+y*y+z*z); }
- void rotateX(float angle)
- {
- float y = this->y, z = this->z;
- this->y = y * cos(angle) - z * sin(angle);
- this->z = y * sin(angle) + z * cos(angle);
- }
- void rotateY(float angle)
- {
- float x = this->x, z = this->z;
- this->x = x * cos(angle) + z * sin(angle);
- this->z = - x * sin(angle) + z * cos(angle);
- }
- void rotateZ(float angle)
- {
- float x = this->x, y = this->y;
- this->x = x * cos(angle) - y * sin(angle);
- this->y = x * sin(angle) + y * cos(angle);
- }
- };
- sf::CircleShape point(Vector v, float r)
- {
- sf::CircleShape shape;
- shape.setFillColor(sf::Color::White);
- shape.setPosition(v.x-r, v.y-r);
- shape.setRadius(r);
- return shape;
- }
- int main(int, char const**)
- {
- sf::RenderWindow window(sf::VideoMode(600, 400), "test3d");
- window.setFramerateLimit(FPS);
- sf::View view = window.getView();
- window.setView(view);
- Vector camera = Vector(0,0,-2);
- Vector theta(0,0);
- Vector points[8];
- points[0] = Vector(-0.5, -0.5, -0.5);
- points[1] = Vector(0.5, -0.5, -0.5);
- points[2] = Vector(0.5, 0.5, -0.5);
- points[3] = Vector(-0.5, 0.5, -0.5);
- points[4] = Vector(-0.5, -0.5, 0.5);
- points[5] = Vector(0.5, -0.5, 0.5);
- points[6] = Vector(0.5, 0.5, 0.5);
- points[7] = Vector(-0.5, 0.5, 0.5);
- float mouseX = 0;
- float mouseY = 0;
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- }
- if (event.type == sf::Event::MouseMoved)
- {
- float dx = (mouseY - event.mouseMove.y);
- float dy = (event.mouseMove.x - mouseX);
- if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
- {
- theta.x += dx/200;
- theta.y += dy/100;
- // for (int i = 0; i < 8; i++)
- // {
- // points[i].rotateX(-dx/100);
- // points[i].rotateY(-dy/100);
- // }
- }
- mouseX = event.mouseMove.x;
- mouseY = event.mouseMove.y;
- }
- float alpha = theta.x;
- float beta = theta.y;
- Vector dir(cos(beta)*sin(alpha), sin(beta), cos(beta)*cos(alpha));
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A))
- {
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D))
- {
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W))
- {
- camera = camera + dir/10;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S))
- {
- camera = camera - dir/10;
- }
- window.clear();
- Vector projected[8];
- for (int i = 0; i < 8; i++)
- {
- Vector a = points[i] - camera;
- float r = a.magnitude();
- Vector e(3,2,3);
- Vector s(sin(theta.x), sin(theta.y));
- Vector c(cos(theta.x), cos(theta.y));
- float dx = c.y*a.x - s.y*a.z;
- float dy = s.x*(c.y*a.z + s.y*a.x) + c.x*a.y;
- float dz = c.x*(c.y*a.z + s.y*a.x) - s.x*a.y;
- projected[i] = Vector(e.z*dx/dz+e.x, e.z*dy/dz+e.y) * 100;
- window.draw(point(projected[i], 5*e.z/r));
- }
- // window.draw((sf::Vertex[]){projected[0], projected[1]}, 2, sf::Lines);
- // window.draw((sf::Vertex[]){projected[1], projected[2]}, 2, sf::Lines);
- // window.draw((sf::Vertex[]){projected[2], projected[3]}, 2, sf::Lines);
- // window.draw((sf::Vertex[]){projected[3], projected[0]}, 2, sf::Lines);
- // window.draw((sf::Vertex[]){projected[4], projected[5]}, 2, sf::Lines);
- // window.draw((sf::Vertex[]){projected[5], projected[6]}, 2, sf::Lines);
- // window.draw((sf::Vertex[]){projected[6], projected[7]}, 2, sf::Lines);
- // window.draw((sf::Vertex[]){projected[7], projected[4]}, 2, sf::Lines);
- // window.draw((sf::Vertex[]){projected[0], projected[4]}, 2, sf::Lines);
- // window.draw((sf::Vertex[]){projected[1], projected[5]}, 2, sf::Lines);
- // window.draw((sf::Vertex[]){projected[2], projected[6]}, 2, sf::Lines);
- // window.draw((sf::Vertex[]){projected[3], projected[7]}, 2, sf::Lines);
- sf::Font font;
- font.loadFromFile(resourcePath() + "sansation.ttf");
- sf::Text degrees("x: " + std::to_string(theta.x*180/acos(-1)) + "\n"
- "y: " + std::to_string(theta.y*180/acos(-1)) + "\n"
- "z: " + std::to_string(theta.z*180/acos(-1)), font);
- degrees.setFillColor(sf::Color::White);
- degrees.setCharacterSize(20);
- degrees.setPosition(10, 10);
- window.draw(degrees);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement