From c1dbeff8d56b1602d4df80e1e46ac4dbf2dca981 Mon Sep 17 00:00:00 2001 From: dawidg81 Date: Fri, 13 Feb 2026 09:47:34 +0100 Subject: Added more samples --- .sample3.cpp.swp | Bin 0 -> 20480 bytes sample1.cpp | 6 +-- sample2.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++ sample3.cpp | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 235 insertions(+), 3 deletions(-) create mode 100644 .sample3.cpp.swp create mode 100644 sample3.cpp diff --git a/.sample3.cpp.swp b/.sample3.cpp.swp new file mode 100644 index 0000000..44c7e09 Binary files /dev/null and b/.sample3.cpp.swp differ diff --git a/sample1.cpp b/sample1.cpp index d2f17b8..490b0a1 100644 --- a/sample1.cpp +++ b/sample1.cpp @@ -22,7 +22,7 @@ int main(int argc, char* argv[]) { clearScreen(window, Color(0, 0, 0)); // Draw a grid - Color gridColor(50, 50, 70); + Color gridColor(255, 255, 255); for (int x = 0; x < 800; x += 50) { drawLine(window, x, 0, x, 600, gridColor); } @@ -53,7 +53,7 @@ int main(int argc, char* argv[]) { drawFilledCircle(window, bounceX, bounceY, 30, Color(100, 200, 255)); int prevX = bounceX + bounceX + time * 20; - int prevY = 300 + static_cast(100 * sin(bounceX + time * 20 * 2)); + int prevY = 300 + static_cast(100 * sin((bounceX + time * 20) * 2)); for (int i = bounceX + time * 20; i > bounceX; i--) { int x = bounceX + bounceX + time * 20 - i; @@ -120,4 +120,4 @@ int main(int argc, char* argv[]) { destroyWindow(window); return 0; -} \ No newline at end of file +} diff --git a/sample2.cpp b/sample2.cpp index e69de29..02e1718 100644 --- a/sample2.cpp +++ b/sample2.cpp @@ -0,0 +1,102 @@ +#include "graphics.h" +#include +#include + +struct Vec3 { + float x, y, z; +}; + +// Rotate point around X axis +Vec3 rotateX(const Vec3& v, float angle) { + float c = std::cos(angle); + float s = std::sin(angle); + return { + v.x, + v.y * c - v.z * s, + v.y * s + v.z * c + }; +} + +// Rotate point around Y axis +Vec3 rotateY(const Vec3& v, float angle) { + float c = std::cos(angle); + float s = std::sin(angle); + return { + v.x * c + v.z * s, + v.y, + -v.x * s + v.z * c + }; +} + +// Perspective projection +void project(const Vec3& v, int& x2d, int& y2d, float width, float height) { + float distance = 2.0f; // camera distance + float scale = 100.0f; // zoom factor + + float z = v.z + distance; + float factor = scale / z; + + x2d = static_cast(v.x * factor + width / 2); + y2d = static_cast(v.y * factor + height / 2); +} + +int main() { + const int width = 640; + const int height = 480; + + WindowHandle* window = createWindow("3D Spinning Cube", width, height); + if (!window) return -1; + + // Cube vertices + std::vector vertices = { + {-1, -1, -1}, + { 1, -1, -1}, + { 1, 1, -1}, + {-1, 1, -1}, + {-1, -1, 1}, + { 1, -1, 1}, + { 1, 1, 1}, + {-1, 1, 1} + }; + + // Cube edges (pairs of vertex indices) + std::vector> edges = { + {0,1},{1,2},{2,3},{3,0}, // back face + {4,5},{5,6},{6,7},{7,4}, // front face + {0,4},{1,5},{2,6},{3,7} // connecting edges + }; + + float angle = 0.0f; + + while (!windowShouldClose(window)) { + pollEvents(window); + clearScreen(window, Color(0, 0, 0)); // RGB 20, 20, 30 + + std::vector transformed; + + // Rotate cube + for (const auto& v : vertices) { + Vec3 r = rotateX(v, angle); + r = rotateY(r, angle * 0.7f); + transformed.push_back(r); + } + + // Draw edges + for (const auto& edge : edges) { + int x1, y1, x2, y2; + + project(transformed[edge.first], x1, y1, width, height); + project(transformed[edge.second], x2, y2, width, height); + + drawLine(window, x1, y1, x2, y2, Color(255, 255, 255)); // RGB 0 255 180 + } + + swapBuffers(window); + + angle += 0.01f; + delay(16); // ~60 FPS + } + + destroyWindow(window); + return 0; +} diff --git a/sample3.cpp b/sample3.cpp new file mode 100644 index 0000000..70c1e13 --- /dev/null +++ b/sample3.cpp @@ -0,0 +1,130 @@ +#include "graphics.h" +#include +#include +#include + +struct Vec3 { + float x, y, z; +}; + +Vec3 rotateX(const Vec3& v, float angle) { + float c = std::cos(angle); + float s = std::sin(angle); + return { v.x, v.y * c - v.z * s, v.y * s + v.z * c }; +} + +Vec3 rotateY(const Vec3& v, float angle) { + float c = std::cos(angle); + float s = std::sin(angle); + return { v.x * c + v.z * s, v.y, -v.x * s + v.z * c }; +} + +Vec3 rotateZ(const Vec3& v, float angle) { + float c = std::cos(angle); + float s = std::sin(angle); + return { v.x * c - v.y * s, v.x * s + v.y * c, v.z }; +} + +void project(const Vec3& v, int& x2d, int& y2d, float width, float height, float zoom) { + float distance = 4.0f; + float z = v.z + distance; + float factor = zoom / z; + + x2d = static_cast(v.x * factor + width / 2); + y2d = static_cast(v.y * factor + height / 2); +} + +struct Star { + float x, y, z; +}; + +int main() { + const int width = 800; + const int height = 600; + + WindowHandle* window = createWindow("DEMOSCENE CUBE", width, height); + if (!window) return -1; + + std::vector vertices = { + {-1,-1,-1},{1,-1,-1},{1,1,-1},{-1,1,-1}, + {-1,-1,1},{1,-1,1},{1,1,1},{-1,1,1} + }; + + std::vector> edges = { + {0,1},{1,2},{2,3},{3,0}, + {4,5},{5,6},{6,7},{7,4}, + {0,4},{1,5},{2,6},{3,7} + }; + + // Starfield + const int starCount = 300; + std::vector stars; + for (int i = 0; i < starCount; ++i) { + stars.push_back({ + ((rand() % 200) - 100) / 10.0f, + ((rand() % 200) - 100) / 10.0f, + (rand() % 100) / 10.0f + }); + } + + float time = 0.0f; + + while (!windowShouldClose(window)) { + pollEvents(window); + + // Pulsing background + int bg = static_cast((std::sin(time * 0.5f) + 1.0f) * 20); + clearScreen(window, Color(bg, 0, bg + 20)); + + // --- STARFIELD --- + for (auto& star : stars) { + star.z -= 0.05f; + if (star.z <= 0.1f) + star.z = 10.0f; + + int sx = static_cast((star.x / star.z) * 200 + width/2); + int sy = static_cast((star.y / star.z) * 200 + height/2); + + if (sx >= 0 && sx < width && sy >= 0 && sy < height) + drawPixel(window, sx, sy, Color(255,255,255)); + } + + // Color cycling (rainbow) + int r = static_cast((std::sin(time) + 1) * 127); + int g = static_cast((std::sin(time + 2) + 1) * 127); + int b = static_cast((std::sin(time + 4) + 1) * 127); + + float zoom = 150.0f + std::sin(time) * 50.0f; + + // --- MULTI-CUBE EFFECT --- + for (int c = 0; c < 3; ++c) { + + float offset = c * 2.5f - 2.5f; + + std::vector transformed; + + for (const auto& v : vertices) { + Vec3 r3 = rotateX(v, time + c); + r3 = rotateY(r3, time * 0.7f + c); + r3 = rotateZ(r3, time * 0.5f); + r3.x += offset; + transformed.push_back(r3); + } + + for (const auto& edge : edges) { + int x1, y1, x2, y2; + project(transformed[edge.first], x1, y1, width, height, zoom); + project(transformed[edge.second], x2, y2, width, height, zoom); + drawLine(window, x1, y1, x2, y2, Color(r, g, b)); + } + } + + swapBuffers(window); + + time += 0.02f; + delay(16); + } + + destroyWindow(window); + return 0; +} -- cgit v1.2.3