209 lines
7.1 KiB
C++
209 lines
7.1 KiB
C++
#include <raylib.h>
|
|
#include <raymath.h>
|
|
#include <rlgl.h>
|
|
#include "rlImGui.h"
|
|
#include "imgui.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, 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;
|
|
|
|
float cubeSize = 3.0f;
|
|
float previousCubeSize = cubeSize;
|
|
RubixCube cube = CreateRubixCube({0.0f, 0.0f, 0.0f}, cubeSize);
|
|
|
|
// Disable backface culling
|
|
rlDisableBackfaceCulling();
|
|
|
|
// Setup ImGui
|
|
rlImGuiSetup(true);
|
|
|
|
bool isControllingCamera = true;
|
|
|
|
while (!WindowShouldClose()) {
|
|
if (IsKeyPressed(KEY_SPACE)) isControllingCamera = !isControllingCamera;
|
|
if (isControllingCamera) UpdateCamera(&cam, CAMERA_FREE);
|
|
if (!isControllingCamera) DrawText("Camera paused", 10, 10, 32, BLACK);
|
|
|
|
BeginDrawing();
|
|
ClearBackground(GRAY);
|
|
|
|
rlImGuiBegin();
|
|
ImGui::Begin("Controls");
|
|
ImGui::SliderFloat("Cube Size", &cubeSize, 1.0f, 10.0f);
|
|
ImGui::End();
|
|
rlImGuiEnd();
|
|
|
|
if (cubeSize != previousCubeSize) {
|
|
cube = CreateRubixCube({0.0f, 0.0f, 0.0f}, cubeSize);
|
|
previousCubeSize = cubeSize;
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
for (int face = 0; face < 6; face++) {
|
|
faceIndex = 0;
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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, up;
|
|
|
|
if (Vector3Equals(normal, {0, 1, 0}) || Vector3Equals(normal, {0, -1, 0})) {
|
|
right = {1, 0, 0};
|
|
up = {0, 0, (normal.y > 0) ? -1 : 1};
|
|
} else {
|
|
right = {normal.z, 0, -normal.x};
|
|
up = {0, 1, 0};
|
|
}
|
|
|
|
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));
|
|
|
|
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;
|
|
} |