Cuber/main.cpp

152 lines
6.0 KiB
C++
Raw Normal View History

#include <raylib.h>
#include <rlgl.h>
enum Faces {
FRONT = 0,
RIGHT,
BACK,
LEFT,
BOTTOM,
TOP
};
struct RubikCube {
Faces faces[6];
};
void DrawCubeFace(Vector3 position, float size, Color color, Faces face);
// Function to draw a quad using four vertices and a color
void DrawQuad(Vector3 v1, Vector3 v2, Vector3 v3, Vector3 v4, Color color) {
rlSetTexture(0); // Disable textures
rlBegin(RL_QUADS); // Begin drawing quads
rlColor4ub(color.r, color.g, color.b, color.a); // Set quad color
// Define the four vertices of the quad
rlVertex3f(v1.x, v1.y, v1.z);
rlVertex3f(v2.x, v2.y, v2.z);
rlVertex3f(v3.x, v3.y, v3.z);
rlVertex3f(v4.x, v4.y, v4.z);
rlEnd(); // End drawing quads
}
// Function to draw a smaller cube at a given position with different colored faces
void DrawRubikCubePart(Vector3 position, float size) {
// Draw individual colored faces for each side
DrawCubeFace(position, size, GREEN, FRONT); // FRONT face
DrawCubeFace(position, size, RED, BACK); // BACK face
DrawCubeFace(position, size, BLUE, RIGHT); // RIGHT face
DrawCubeFace(position, size, ORANGE, LEFT); // LEFT face
DrawCubeFace(position, size, YELLOW, TOP); // TOP face
DrawCubeFace(position, size, WHITE, BOTTOM); // BOTTOM face
}
// Function to draw a colored face of the cube
void DrawCubeFace(Vector3 position, float size, Color color, Faces face) {
float halfSize = size / 2;
switch (face) {
case FRONT: // Z is negative, facing front
DrawQuad(
(Vector3){ position.x - halfSize, position.y + halfSize, position.z - halfSize }, // Top-left
(Vector3){ position.x + halfSize, position.y + halfSize, position.z - halfSize }, // Top-right
(Vector3){ position.x + halfSize, position.y - halfSize, position.z - halfSize }, // Bottom-right
(Vector3){ position.x - halfSize, position.y - halfSize, position.z - halfSize }, // Bottom-left
color
);
break;
case BACK: // Z is positive, facing back
DrawQuad(
(Vector3){ position.x + halfSize, position.y + halfSize, position.z + halfSize }, // Top-right
(Vector3){ position.x - halfSize, position.y + halfSize, position.z + halfSize }, // Top-left
(Vector3){ position.x - halfSize, position.y - halfSize, position.z + halfSize }, // Bottom-left
(Vector3){ position.x + halfSize, position.y - halfSize, position.z + halfSize }, // Bottom-right
color
);
break;
case RIGHT: // X is positive, facing right
DrawQuad(
(Vector3){ position.x + halfSize, position.y + halfSize, position.z - halfSize }, // Top-front
(Vector3){ position.x + halfSize, position.y + halfSize, position.z + halfSize }, // Top-back
(Vector3){ position.x + halfSize, position.y - halfSize, position.z + halfSize }, // Bottom-back
(Vector3){ position.x + halfSize, position.y - halfSize, position.z - halfSize }, // Bottom-front
color
);
break;
case LEFT: // X is negative, facing left
DrawQuad(
(Vector3){ position.x - halfSize, position.y + halfSize, position.z + halfSize }, // Top-back
(Vector3){ position.x - halfSize, position.y + halfSize, position.z - halfSize }, // Top-front
(Vector3){ position.x - halfSize, position.y - halfSize, position.z - halfSize }, // Bottom-front
(Vector3){ position.x - halfSize, position.y - halfSize, position.z + halfSize }, // Bottom-back
color
);
break;
case TOP: // Y is positive, facing up
DrawQuad(
(Vector3){ position.x - halfSize, position.y + halfSize, position.z + halfSize }, // Back-left
(Vector3){ position.x + halfSize, position.y + halfSize, position.z + halfSize }, // Back-right
(Vector3){ position.x + halfSize, position.y + halfSize, position.z - halfSize }, // Front-right
(Vector3){ position.x - halfSize, position.y + halfSize, position.z - halfSize }, // Front-left
color
);
break;
case BOTTOM: // Y is negative, facing down
DrawQuad(
(Vector3){ position.x - halfSize, position.y - halfSize, position.z - halfSize }, // Front-left
(Vector3){ position.x + halfSize, position.y - halfSize, position.z - halfSize }, // Front-right
(Vector3){ position.x + halfSize, position.y - halfSize, position.z + halfSize }, // Back-right
(Vector3){ position.x - halfSize, position.y - halfSize, position.z + halfSize }, // Back-left
color
);
break;
}
}
int main() {
InitWindow(1920, 1080, "Rubik's Cube");
Camera3D camera = { 0 };
camera.position = (Vector3){ 10.0f, 10.0f, 10.0f };
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
camera.fovy = 45.0f;
camera.projection = CAMERA_PERSPECTIVE;
SetTargetFPS(60);
DisableCursor();
while (!WindowShouldClose()) {
// Update
UpdateCamera(&camera, CAMERA_FREE);
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
// Draw the 3x3x3 Rubik's Cube
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -1; z <= 1; z++) {
Vector3 position = { x * 2.1f, y * 2.1f, z * 2.1f };
DrawRubikCubePart(position, 2.0f);
}
}
}
DrawGrid(10, 1.0f); // Optional grid for reference
EndMode3D();
DrawText("Rubik's Cube", 10, 10, 20, DARKGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}