Add most of the cube, missing top and bottom

This commit is contained in:
illyum 2024-09-18 16:34:44 -06:00
parent 00507bc269
commit abc4e02a4b
2 changed files with 160 additions and 120 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"xiosbase": "cpp"
}
}

275
main.cpp
View File

@ -1,7 +1,8 @@
#include <raylib.h>
#include <raymath.h>
#include <rlgl.h>
enum Faces {
enum CubeFace {
FRONT = 0,
RIGHT,
BACK,
@ -10,142 +11,176 @@ enum Faces {
TOP
};
struct RubikCube {
Faces faces[6];
struct VecTriangle {
Vector3 pointOne;
Vector3 pointTwo;
Vector3 pointThree;
};
void DrawCubeFace(Vector3 position, float size, Color color, Faces face);
struct Plane3D {
VecTriangle one;
VecTriangle two;
Color color;
};
// 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
struct RubixCube {
Plane3D faces[6][9];
Vector3 pos;
};
// 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;
}
}
void DrawRubixCube(RubixCube* cube);
void DrawPlane3D(Plane3D* plane);
Plane3D CreatePlane3D(float width, float height);
Plane3D CreatePlane3D(float width, float height, Vector3 position, Color color);
Plane3D CreatePlane3D(float width, float height, Vector3 position, Color color, Vector3 normal);
RubixCube CreateRubixCube(Vector3 position, float size);
int main() {
InitWindow(1920, 1080, "Rubik's Cube");
SetConfigFlags(FLAG_VSYNC_HINT);
InitWindow(1920, 1080, "Rubix 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();
Camera3D cam = { 0 };
cam.position = { 10.0f, 10.0f, 10.0f };
cam.target = { 0.0f, 0.0f, 0.0f };
cam.up = { 0.0f, 1.0f, 0.0f };
cam.fovy = 45.0f;
cam.projection = CAMERA_PERSPECTIVE;
RubixCube cube = CreateRubixCube({0.0f, 0.0f, 0.0f}, 3.0f);
// Disable backface culling
rlDisableBackfaceCulling();
while (!WindowShouldClose()) {
// Update
UpdateCamera(&camera, CAMERA_FREE);
UpdateCamera(&cam, CAMERA_FREE);
BeginDrawing();
ClearBackground(RAYWHITE);
ClearBackground(GRAY);
BeginMode3D(cam);
BeginMode3D(camera);
DrawRubixCube(&cube);
// 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
DrawGrid(10, 1.0f);
EndMode3D();
DrawText("Rubik's Cube", 10, 10, 20, DARKGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}
void DrawRubixCube(RubixCube* cube) {
for (int face = 0; face < 6; face++) {
for (int i = 0; i < 9; i++) {
DrawPlane3D(&cube->faces[face][i]);
}
}
}
RubixCube CreateRubixCube(Vector3 position, float size) {
RubixCube cube;
cube.pos = position;
float planeSize = size / 3.0f;
int faceIndex;
// Loop through each face and assign the 3x3 planes
for (int face = 0; face < 6; face++) {
faceIndex = 0; // Reset faceIndex for each face
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
Vector3 planePos = { position.x + x * planeSize, position.y + y * planeSize, position.z };
switch (face) {
case FRONT: // FRONT face (Z positive) - RED
planePos = { position.x + x * planeSize, position.y + y * planeSize, position.z + size / 2 };
cube.faces[face][faceIndex++] = CreatePlane3D(planeSize, planeSize, planePos, RED, {0, 0, 1});
break;
case BACK: // BACK face (Z negative) - ORANGE
planePos = { position.x + x * planeSize, position.y + y * planeSize, position.z - size / 2 };
cube.faces[face][faceIndex++] = CreatePlane3D(planeSize, planeSize, planePos, ORANGE, {0, 0, -1});
break;
case LEFT: // LEFT face (X negative) - BLUE
planePos = { position.x - size / 2, position.y + y * planeSize, position.z + x * planeSize };
cube.faces[face][faceIndex++] = CreatePlane3D(planeSize, planeSize, planePos, BLUE, {-1, 0, 0});
break;
case RIGHT: // RIGHT face (X positive) - GREEN
planePos = { position.x + size / 2, position.y + y * planeSize, position.z + x * planeSize };
cube.faces[face][faceIndex++] = CreatePlane3D(planeSize, planeSize, planePos, GREEN, {1, 0, 0});
break;
case TOP: // TOP face (Y positive) - YELLOW
planePos = { position.x + x * planeSize, position.y + size / 2, position.z + y * planeSize };
cube.faces[face][faceIndex++] = CreatePlane3D(planeSize, planeSize, planePos, YELLOW, {0, 1, 0});
break;
case BOTTOM: // BOTTOM face (Y negative) - WHITE
planePos = { position.x + x * planeSize, position.y - size / 2, position.z + y * planeSize };
cube.faces[face][faceIndex++] = CreatePlane3D(planeSize, planeSize, planePos, WHITE, {0, -1, 0});
break;
}
}
}
}
return cube;
}
void DrawPlane3D(Plane3D* plane) {
DrawTriangle3D(plane->one.pointOne, plane->one.pointTwo, plane->one.pointThree, plane->color);
DrawTriangle3D(plane->two.pointOne, plane->two.pointTwo, plane->two.pointThree, plane->color);
}
Plane3D CreatePlane3D(float width, float height) {
Plane3D plane3D;
plane3D.color = BLUE;
plane3D.one.pointOne = { -width / 2.0f, -height / 2.0f, 0.0f };
plane3D.one.pointTwo = { width / 2.0f, -height / 2.0f, 0.0f };
plane3D.one.pointThree = { -width / 2.0f, height / 2.0f, 0.0f };
plane3D.two.pointOne = { width / 2.0f, -height / 2.0f, 0.0f };
plane3D.two.pointTwo = { width / 2.0f, height / 2.0f, 0.0f };
plane3D.two.pointThree = { -width / 2.0f, height / 2.0f, 0.0f };
return plane3D;
}
Plane3D CreatePlane3D(float width, float height, Vector3 position, Color color) {
Plane3D plane3D;
plane3D.color = color;
plane3D.one.pointOne = { position.x - width / 2.0f, position.y - height / 2.0f, position.z };
plane3D.one.pointTwo = { position.x + width / 2.0f, position.y - height / 2.0f, position.z };
plane3D.one.pointThree = { position.x - width / 2.0f, position.y + height / 2.0f, position.z };
plane3D.two.pointOne = { position.x + width / 2.0f, position.y - height / 2.0f, position.z };
plane3D.two.pointTwo = { position.x + width / 2.0f, position.y + height / 2.0f, position.z };
plane3D.two.pointThree = { position.x - width / 2.0f, position.y + height / 2.0f, position.z };
return plane3D;
}
Plane3D CreatePlane3D(float width, float height, Vector3 position, Color color, Vector3 normal) {
Plane3D plane3D;
plane3D.color = color;
Vector3 right = {normal.z, 0, -normal.x}; // Calculate right vector from normal
Vector3 up = {0, 1, 0}; // Default up direction
if (normal.x != 0 || normal.z != 0) {
up = {0, 1, 0}; // Keep vertical for non-top/bottom faces
}
// First triangle (bottom-left, bottom-right, top-left)
plane3D.one.pointOne = Vector3Add(Vector3Add(position, Vector3Scale(right, -width / 2)), Vector3Scale(up, -height / 2));
plane3D.one.pointTwo = Vector3Add(Vector3Add(position, Vector3Scale(right, width / 2)), Vector3Scale(up, -height / 2));
plane3D.one.pointThree = Vector3Add(Vector3Add(position, Vector3Scale(right, -width / 2)), Vector3Scale(up, height / 2));
// Second triangle (bottom-right, top-right, top-left)
plane3D.two.pointOne = Vector3Add(Vector3Add(position, Vector3Scale(right, width / 2)), Vector3Scale(up, -height / 2));
plane3D.two.pointTwo = Vector3Add(Vector3Add(position, Vector3Scale(right, width / 2)), Vector3Scale(up, height / 2));
plane3D.two.pointThree = Vector3Add(Vector3Add(position, Vector3Scale(right, -width / 2)), Vector3Scale(up, height / 2));
return plane3D;
}