// This only works with opengl!!!! #ifndef RUBIKSCUBE_H #define RUBIKSCUBE_H #include "Archimedes.h" #include "utils/Objects/Body.h" #include "utils/Renderer/Renderer.h" #include #include class RubiksCube { public: RubiksCube() {} ~RubiksCube() {} void init(Archimedes::Renderer* r) { shader = Archimedes::Shader(vs, fs, Archimedes::Shader::LoadType::FromSource); r->setupShader(shader); rt = Archimedes::RenderTarget( Archimedes::VertexBuffer(vertices), Archimedes::IndexArray(indices), layout, shader ); r->setupRenderTarget(rt); for(int i = 0; i < blocks.size(); i++) { blocks.at(i) = Archimedes::Body(rt); blocks.at(i).moveTo(positions.at(i)); } } std::vector getBlocks() { return blocks; } enum class Direction { X, Y, Z }; enum class Plate { Top = 1, Middle = 0, Bottom = -1 }; void orbit(Archimedes::Body& body, glm::vec3 center, glm::vec3 direction, float radians) { glm::vec3 o = body.getPosition() - center; direction = glm::normalize(glm::cross(glm::cross(o, direction), o)); body.setTransform(glm::rotate(body.getTransform(), radians, direction)); } std::vector selectPlate(Plate plate, Direction direction) { std::vector p; p.reserve(plate == Plate::Middle ? 8 : 9); switch(direction) { case Direction::X: for(auto& b : blocks) { if(b.getPosition().x == (float)plate) { p.push_back(b); } } break; case Direction::Y: break; case Direction::Z: break; default: break; } return p; } void turn(Plate plate, Direction direction) { switch(direction) { case Direction::X: break; case Direction::Y: break; case Direction::Z: break; default: break; } } void update() {} private: std::vector blocks = std::vector(26); std::vector positions = { glm::vec3(-1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 1.0f, 1.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(-1.0f, 1.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(1.0f, 1.0f, 0.0f), glm::vec3(-1.0f, 1.0f, -1.0f), glm::vec3(0.0f, 1.0f, -1.0f), glm::vec3(1.0f, 1.0f, -1.0f), glm::vec3(-1.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(1.0f, 0.0f, 1.0f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f, -1.0f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(1.0f, 0.0f, -1.0f), glm::vec3(-1.0f, -1.0f, 1.0f), glm::vec3(0.0f, -1.0f, 1.0f), glm::vec3(1.0f, -1.0f, 1.0f), glm::vec3(-1.0f, -1.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(1.0f, -1.0f, 0.0f), glm::vec3(-1.0f, -1.0f, -1.0f), glm::vec3(0.0f, -1.0f, -1.0f), glm::vec3(1.0f, -1.0f, -1.0f), }; Archimedes::RenderTarget rt; std::string vs = "#version 430 core\n" "layout (location = 0) in vec3 aPos;\n" "layout (location = 1) in vec3 aNorm;\n" "layout (location = 2) in vec4 aCol;\n" "uniform mat4 model;\n" "uniform mat4 view;\n" "uniform mat4 proj;\n" "uniform vec4 color;\n" "out vec3 Norm;\n" "out vec3 FragPos;\n" "out vec4 col;\n" "void main()\n" "{\n" " gl_Position = proj * view * model * vec4(aPos, 1.0f);\n" " Norm = mat3(transpose(inverse(model))) * aNorm;\n" " FragPos = vec3(model * vec4(aPos, 1.0f));\n" " col = aCol;\n" "}\0"; std::string fs = "#version 430 core\n" "in vec3 Norm;\n" "in vec3 FragPos;\n" "in vec4 col;\n" "out vec4 FragColor;\n" "uniform mat4 model;\n" "uniform mat4 view;\n" "uniform mat4 proj;\n" "uniform vec4 color;\n" "void main()\n" "{\n" " vec3 light = vec3(1.0f, 1.0f, 1.0f);\n" " vec3 amblight = 0.1f * light;\n" " vec3 norm = normalize(Norm);\n" " vec3 lightdir = normalize(vec3(10, 20, 20) - FragPos);\n" " vec3 diff = max(dot(norm, lightdir), 0.0) * light;\n" " FragColor = vec4((amblight + diff) * col.rgb, 1.0f);\n" "}\n\0"; Archimedes::Shader shader; Archimedes::VertexLayout layout = Archimedes::VertexLayout({ Archimedes::LayoutElement(Archimedes::LayoutElement::Type::Float, 3), Archimedes::LayoutElement(Archimedes::LayoutElement::Type::Float, 3), Archimedes::LayoutElement(Archimedes::LayoutElement::Type::Float, 4) }); std::vector vertices = { -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, //back 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, //front 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.0f, 1.0f, //left -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, -1.0f, 0.0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