Cuber/main.cpp

186 lines
7.0 KiB
C++
Raw Normal View History

#include <raylib.h>
#include <raymath.h>
#include <rlgl.h>
enum CubeFace {
FRONT = 0,
RIGHT,
BACK,
LEFT,
BOTTOM,
TOP
};
struct VecTriangle {
Vector3 pointOne;
Vector3 pointTwo;
Vector3 pointThree;
};
struct Plane3D {
VecTriangle one;
VecTriangle two;
Color color;
};
struct RubixCube {
Plane3D faces[6][9];
Vector3 pos;
};
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() {
SetConfigFlags(FLAG_VSYNC_HINT);
InitWindow(1920, 1080, "Rubix Cube");
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()) {
UpdateCamera(&cam, CAMERA_FREE);
BeginDrawing();
ClearBackground(GRAY);
BeginMode3D(cam);
DrawRubixCube(&cube);
DrawGrid(10, 1.0f);
EndMode3D();
EndDrawing();
}
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;
}