GithubHelp home page GithubHelp logo

danieldotwav / ascii-cube Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 15 KB

This program renders a rotating 3D cube using ASCII art in real-time, showcasing basic concepts of 3D graphics like perspective projection and depth buffering on a 2D screen.

C 100.00%
3d-rendering ascii-art c computer-graphics perspective-projection depth-buffering real-time-animation

ascii-cube's Introduction

ASCII Cube

This program is a simple yet fascinating exploration into the realm of computer graphics using ASCII art. It renders 3D objects (cubes, in this case) in real-time, projecting them onto a 2D screen using ASCII characters. This renderer showcases the fundamental concepts of 3D graphics such as perspective projection, depth buffering, and basic animation.

Features

  • Real-time 3D to 2D projection: Converts 3D coordinates of cubes into 2D screen coordinates.
  • Depth buffering: Implements a depth buffer to correctly render the visibility of objects based on their distance from the viewer.
  • Dynamic viewing: The cubes rotate, providing a dynamic view of the objects from different angles.
  • Scalable rendering: Adjusts the scale of projection to fit the screen size.

Usage

After compilation, run the program. It will automatically start rendering the rotating cubes. The rendering window is set to a width of 160 characters and a height of 44 lines by default. You can adjust the width and height variables in the code to match your console or terminal window size for optimal viewing.

Code Overview

  • Main Variables:

    • A, B, C: Rotation angles around the x, y, and z axes.
    • x, y, z: 3D coordinates after rotation.
    • cubeWidth: The size of the cubes.
    • width, height: The dimensions of the rendering window.
    • depthBuffer: An array used for depth testing, ensuring proper object overlap.
    • asciiRenderBuffer: An array to hold the ASCII characters for rendering.
    • backgroundASCIICode: The ASCII code for rendering the background.
    • distanceFromCam: How far away the camera is from the origin.
    • viewScale: A scaling factor for adjusting the projection size.
    • incrementSpeed: How fast the cubes rotate.
    • horizontalOffset: Horizontal displacement of the cubes on the screen.
    • inverseDepth: Used for scaling objects based on their distance.
    • screenX, screenY: The screen coordinates for rendering.
    • bufferIndex: Index for writing to the buffer arrays.
  • Functions:

    • calculateX, calculateY, calculateZ: Compute the rotated coordinates.
    • calculateForSurface: Calculates screen position and updates buffers for rendering surfaces of the cubes.

Customization

You can customize various parameters such as cubeWidth, backgroundASCIICode, and viewScale to modify the appearance of the rendered scene. You can also experiment with different objects by changing the calculateForSurface function.

ascii-cube's People

Contributors

danieldotwav avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.