Ascii-graphics is a basic 3D graphics library designed to run in a linux terminal.
Below shows some key features of the ascii-graphics library.
- Outline meshes
- Wireframe mode
- Fill entire mesh with single character
- Basic lighting
- Clone the repo
git clone https://github.com/addr0x414b/ascii-graphics.git
- In the cloned folder, create a build directory and cd to it
mkdir build && cd build
- Run cmake
cmake ../
- Run make
make
- Run the program
./Ascii-Graphics
Below is the default code the repo comes with:
//ascii-graphics.cpp
int gScreenWidth = 200; // Define the screens width and height
int gScreenHeight = 50;
float gAspect = (float)gScreenWidth / (float)gScreenHeight;
int main() {
Camera camera(0.0f, 0.0f, 0.0f, gAspect); // Define the scenes camera
LightD light; // Define the scenes light
Screen screen(gScreenWidth, gScreenHeight, camera, light); // Define the screen of the scene
Cube cube; // Library comes with one default mesh
cube.translate(0.0f, 0.0f, -4.f); // Must translate the mesh away from the camera (-z is into the screen)
float deg = 0.1f;
while (1) { // Screen loop
screen.start(); // Begin calculating the delta time per frame
cube.rotate(0.0f, deg, deg); // Rotate cube
deg += 50 * screen.deltaTime; // Increase the degrees of rotation
screen.shadeMesh(cube); // Print the cube into the buffer
screen.print(); // Print the buffer
screen.clear(); // Clear the screen
}
return 0;
}
- Define size of the screen
int gScreenWidth = 200; // Width and height should be no larger than terminal
int gScreenHeight = 50; // Can increase size with decreased terminal font size
float gAspect = (float)gScreenWidth / (float)gScreenHeight;
- Define camera, light, and screen
Camera camera(0.0f, 0.0f, 0.0f, gAspect); // Constructor uses default projection values
LightD light; // Define the scenes light
Screen screen(gScreenWidth, gScreenHeight, camera, light); // Define the screen
- Mesh MUST be in OBJ format
- Mesh MUST be triangulated, with normals
- OBJ file should be in the same directory as the ascii-graphics.cpp file
- File path MUST have "../" in front (assuming file is in the same directory as ascii-graphics.cpp)
- Flat Shaded Mesh:
Mesh meshName("../path_to_flat_mesh.obj"); // Load a FLAT SHADED mesh
meshName.translate(0.0f, 0.0f, -4.0f); // Translate the mesh away from the camera
- Smooth Shaded Mesh:
Mesh meshName(1, "../path_to_smooth_mesh.obj") // Load a SMOOTH SHADED mesh
meshName.translate(0.0f, 0.0f, -4.0f); // Translate the mesh away from the camera
float deg = 0.1f;
while (1) { // Screen loop
screen.start(); // Begin calculating the delta time per frame
cube.rotate(0.0f, deg, deg); // Rotate cube
deg += 50 * screen.deltaTime; // Increase the degrees of rotation
screen.shadeMesh(cube); // Print the cube into the buffer
screen.print(); // Print the buffer
screen.clear(); // Clear the screen
}
- Display a smooth shaded mesh
screen.shadeMeshSmooth(meshName);
- Display a flat shaded mesh
screen.shadeMesh(meshName);
- Fill entire mesh with a single character, no shading
screen.fillMesh(meshName, '*'); // Pick character here
- Show mesh triangles
screen.drawMesh(meshName, '*'); // Pick character here
- Display wireframe of mesh
screen.drawMeshWire(meshName, '*'); // Pick character here
- Can combine different methods
// Results in an outlined cube
screen.drawMesh(cube, '#'); // Draw the triangles of the cube
screen.fillMesh(cube, '*'); // Fill in the cube
Distributed under the MIT License. See LICENSE
for more information.
YouTube: https://www.youtube.com/watch?v=X4QSm_p7Cy4
Thanks to the following for allowing use of their 3D models: