(Feat): Initial Commit.

This commit is contained in:
2025-12-17 21:31:12 +00:00
commit 42529be0f8
16 changed files with 2597 additions and 0 deletions

170
include/config.h Normal file
View File

@@ -0,0 +1,170 @@
/**
* @file config.h
* @brief Configuration constants for ASCII 3D Renderer
* @author ASCII3D Project
* @version 2.0.0
*/
#ifndef ASCII3D_CONFIG_H
#define ASCII3D_CONFIG_H
#ifdef __cplusplus
extern "C" {
#endif
/*============================================================================
* SCREEN CONFIGURATION
*============================================================================*/
#define SCREEN_WIDTH 120
#define SCREEN_HEIGHT 45
/*============================================================================
* RENDERING QUALITY SETTINGS
*============================================================================*/
/* Depth buffer initialization value */
#define DEPTH_BUFFER_INIT 1e9f
/* Character extrusion depth (3D thickness) */
#define EXTRUSION_DEPTH 4.0f
/* Base character scale */
#define CHAR_SCALE 2.0f
/* Camera settings */
#define CAMERA_DISTANCE 30.0f
#define FIELD_OF_VIEW 50.0f
#define NEAR_PLANE 0.1f
#define FAR_PLANE 100.0f
/* Sub-pixel sampling for anti-aliasing (NxN samples per pixel) */
#define AA_SAMPLES 2
/* Voxel rendering step (smaller = higher quality, slower) */
#define VOXEL_STEP 0.15f
/* Surface smoothing iterations */
#define SMOOTH_PASSES 1
/*============================================================================
* ANIMATION PARAMETERS
*============================================================================*/
#define TARGET_FPS 60
#define FRAME_TIME_US (1000000 / TARGET_FPS)
/*============================================================================
* FONT CONFIGURATION
*============================================================================*/
/* Standard 5x7 font */
#define FONT_WIDTH 5
#define FONT_HEIGHT 7
#define FONT_CHAR_SPACING 2
/*============================================================================
* LIGHTING CONFIGURATION
*============================================================================*/
/* Ambient light intensity (0.0 - 1.0) */
#define AMBIENT_INTENSITY 0.15f
/* Diffuse light intensity */
#define DIFFUSE_INTENSITY 0.70f
/* Specular light intensity */
#define SPECULAR_INTENSITY 0.40f
/* Specular shininess exponent */
#define SPECULAR_POWER 32.0f
/* Number of light sources */
#define MAX_LIGHTS 3
/*============================================================================
* ASCII SHADING PALETTES
*============================================================================*/
/* Standard gradient (10 levels) */
#define SHADE_CHARS_STANDARD " .:-=+*#%@"
#define SHADE_COUNT_STANDARD 10
/* Extended gradient (16 levels) - more detail */
#define SHADE_CHARS_EXTENDED " .'`^\",:;Il!i><~+_-?][}{1)(|/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$"
#define SHADE_COUNT_EXTENDED 70
/* Block characters for solid look */
#define SHADE_CHARS_BLOCK " ░▒▓█"
#define SHADE_COUNT_BLOCK 5
/* Minimal gradient */
#define SHADE_CHARS_MINIMAL " .:+#@"
#define SHADE_COUNT_MINIMAL 6
/* Default palette */
#define SHADE_CHARS SHADE_CHARS_EXTENDED
#define SHADE_COUNT SHADE_COUNT_EXTENDED
/*============================================================================
* RENDER MODES
*============================================================================*/
typedef enum RenderMode {
RENDER_MODE_SOLID = 0, /* Filled solid rendering */
RENDER_MODE_WIREFRAME, /* Edge-only wireframe */
RENDER_MODE_POINTS, /* Point cloud */
RENDER_MODE_SHADED, /* Full Phong shading */
RENDER_MODE_COUNT
} RenderMode;
/*============================================================================
* COLOR MODES
*============================================================================*/
typedef enum ColorMode {
COLOR_MODE_MONO = 0, /* Monochrome ASCII */
COLOR_MODE_ANSI_16, /* 16-color ANSI */
COLOR_MODE_ANSI_256, /* 256-color ANSI */
COLOR_MODE_TRUECOLOR, /* 24-bit RGB */
COLOR_MODE_COUNT
} ColorMode;
/*============================================================================
* ANSI COLOR CODES
*============================================================================*/
#define ANSI_RESET "\033[0m"
#define ANSI_BOLD "\033[1m"
#define ANSI_DIM "\033[2m"
/* Foreground colors */
#define ANSI_FG_BLACK "\033[30m"
#define ANSI_FG_RED "\033[31m"
#define ANSI_FG_GREEN "\033[32m"
#define ANSI_FG_YELLOW "\033[33m"
#define ANSI_FG_BLUE "\033[34m"
#define ANSI_FG_MAGENTA "\033[35m"
#define ANSI_FG_CYAN "\033[36m"
#define ANSI_FG_WHITE "\033[37m"
/* Bright foreground colors */
#define ANSI_FG_BRIGHT_BLACK "\033[90m"
#define ANSI_FG_BRIGHT_RED "\033[91m"
#define ANSI_FG_BRIGHT_GREEN "\033[92m"
#define ANSI_FG_BRIGHT_YELLOW "\033[93m"
#define ANSI_FG_BRIGHT_BLUE "\033[94m"
#define ANSI_FG_BRIGHT_MAGENTA "\033[95m"
#define ANSI_FG_BRIGHT_CYAN "\033[96m"
#define ANSI_FG_BRIGHT_WHITE "\033[97m"
/* Background colors */
#define ANSI_BG_BLACK "\033[40m"
#define ANSI_BG_RED "\033[41m"
#define ANSI_BG_GREEN "\033[42m"
#define ANSI_BG_YELLOW "\033[43m"
#define ANSI_BG_BLUE "\033[44m"
#define ANSI_BG_MAGENTA "\033[45m"
#define ANSI_BG_CYAN "\033[46m"
#define ANSI_BG_WHITE "\033[47m"
#ifdef __cplusplus
}
#endif
#endif /* ASCII3D_CONFIG_H */

44
include/font.h Normal file
View File

@@ -0,0 +1,44 @@
/**
* @file font.h
* @brief Bitmap font data for ASCII 3D Renderer
* @author ASCII3D Project
* @version 1.0.0
*/
#ifndef ASCII3D_FONT_H
#define ASCII3D_FONT_H
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Get the font glyph data for a character
* @param c Character to look up (A-Z, a-z, 0-9)
* @return Pointer to 7-byte glyph data, or NULL if not found
*/
const unsigned char *font_get_glyph(char c);
/**
* @brief Check if a pixel is set in a glyph
* @param glyph Pointer to glyph data
* @param x X coordinate (0-4)
* @param y Y coordinate (0-6)
* @return true if pixel is set, false otherwise
*/
bool font_pixel_set(const unsigned char *glyph, int x, int y);
/**
* @brief Check if character is renderable
* @param c Character to check
* @return true if character can be rendered
*/
bool font_is_renderable(char c);
#ifdef __cplusplus
}
#endif
#endif /* ASCII3D_FONT_H */

170
include/lighting.h Normal file
View File

@@ -0,0 +1,170 @@
/**
* @file lighting.h
* @brief Advanced lighting system for ASCII 3D Renderer
* @author ASCII3D Project
* @version 2.0.0
*/
#ifndef ASCII3D_LIGHTING_H
#define ASCII3D_LIGHTING_H
#include "vec3.h"
#include "config.h"
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Light types
*/
typedef enum LightType {
LIGHT_DIRECTIONAL = 0, /* Parallel rays (sun-like) */
LIGHT_POINT, /* Point source with falloff */
LIGHT_SPOT /* Cone-shaped spotlight */
} LightType;
/**
* @brief RGB Color structure
*/
typedef struct Color {
float r, g, b;
} Color;
/**
* @brief Light source structure
*/
typedef struct Light {
LightType type;
Vec3 position; /* Position for point/spot lights */
Vec3 direction; /* Direction for directional/spot lights */
Color color; /* Light color */
float intensity; /* Light intensity multiplier */
float falloff; /* Attenuation for point lights */
float spot_angle; /* Cone angle for spotlights (radians) */
bool enabled;
} Light;
/**
* @brief Material properties for surfaces
*/
typedef struct Material {
Color ambient; /* Ambient color */
Color diffuse; /* Diffuse color */
Color specular; /* Specular highlight color */
float shininess; /* Specular exponent */
float reflectivity; /* For future use */
} Material;
/**
* @brief Lighting system state
*/
typedef struct LightingSystem {
Light lights[MAX_LIGHTS];
int light_count;
Color ambient_color;
float ambient_intensity;
Vec3 camera_position;
} LightingSystem;
/**
* @brief Initialize the lighting system with default lights
* @param system Lighting system to initialize
*/
void lighting_init(LightingSystem *system);
/**
* @brief Add a light to the system
* @param system Lighting system
* @param light Light to add
* @return Index of added light, or -1 if full
*/
int lighting_add_light(LightingSystem *system, const Light *light);
/**
* @brief Create a directional light
* @param direction Light direction (will be normalized)
* @param color Light color
* @param intensity Light intensity
* @return Configured light structure
*/
Light lighting_create_directional(Vec3 direction, Color color, float intensity);
/**
* @brief Create a point light
* @param position Light position
* @param color Light color
* @param intensity Light intensity
* @param falloff Attenuation factor
* @return Configured light structure
*/
Light lighting_create_point(Vec3 position, Color color, float intensity, float falloff);
/**
* @brief Calculate lighting at a surface point (Phong model)
* @param system Lighting system
* @param point Surface point position
* @param normal Surface normal (must be normalized)
* @param material Surface material
* @return Final illumination value (0.0 - 1.0+)
*/
float lighting_calculate(const LightingSystem *system, Vec3 point,
Vec3 normal, const Material *material);
/**
* @brief Calculate full color lighting
* @param system Lighting system
* @param point Surface point position
* @param normal Surface normal
* @param material Surface material
* @return Final color
*/
Color lighting_calculate_color(const LightingSystem *system, Vec3 point,
Vec3 normal, const Material *material);
/**
* @brief Create default material
* @return Default white material
*/
Material lighting_default_material(void);
/**
* @brief Create a color
* @param r Red (0-1)
* @param g Green (0-1)
* @param b Blue (0-1)
* @return Color structure
*/
Color color_create(float r, float g, float b);
/**
* @brief Multiply color by scalar
*/
Color color_scale(Color c, float s);
/**
* @brief Add two colors
*/
Color color_add(Color a, Color b);
/**
* @brief Multiply two colors component-wise
*/
Color color_multiply(Color a, Color b);
/**
* @brief Clamp color components to 0-1 range
*/
Color color_clamp(Color c);
/**
* @brief Convert color to grayscale intensity
*/
float color_to_intensity(Color c);
#ifdef __cplusplus
}
#endif
#endif /* ASCII3D_LIGHTING_H */

179
include/renderer.h Normal file
View File

@@ -0,0 +1,179 @@
/**
* @file renderer.h
* @brief Advanced rendering engine for ASCII 3D Renderer
* @author ASCII3D Project
* @version 2.0.0
*/
#ifndef ASCII3D_RENDERER_H
#define ASCII3D_RENDERER_H
#include "config.h"
#include "lighting.h"
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Rotation state for animation
*/
typedef struct RotationState {
float angle_x;
float angle_y;
float angle_z;
bool enable_x;
bool enable_y;
bool enable_z;
float speed;
} RotationState;
/**
* @brief Render settings structure
*/
typedef struct RenderSettings {
RenderMode mode; /* Rendering mode */
ColorMode color_mode; /* Color output mode */
bool anti_aliasing; /* Enable AA */
bool show_fps; /* Display FPS counter */
bool auto_rotate; /* Auto rotation enabled */
float quality; /* Quality multiplier (0.5 - 2.0) */
int palette_index; /* Shading palette selection */
Color base_color; /* Base color for colored modes */
Color highlight_color; /* Highlight/specular color */
} RenderSettings;
/**
* @brief Frame statistics
*/
typedef struct FrameStats {
double frame_time; /* Last frame time in ms */
double fps; /* Current FPS */
double avg_fps; /* Average FPS */
int frame_count; /* Total frames rendered */
int triangles; /* Triangles/voxels rendered */
} FrameStats;
/**
* @brief Initialize the renderer
* @return 0 on success, -1 on failure
*/
int renderer_init(void);
/**
* @brief Cleanup renderer resources
*/
void renderer_cleanup(void);
/**
* @brief Clear the screen and depth buffers
*/
void renderer_clear(void);
/**
* @brief Render a 3D text string
* @param text Text to render (A-Z, 0-9)
* @param rotation Current rotation state
*/
void renderer_draw_text(const char *text, const RotationState *rotation);
/**
* @brief Render with full settings control
* @param text Text to render
* @param rotation Rotation state
* @param settings Render settings
*/
void renderer_draw_text_ex(const char *text, const RotationState *rotation,
const RenderSettings *settings);
/**
* @brief Display the rendered frame to terminal
*/
void renderer_present(void);
/**
* @brief Present with color support
* @param settings Render settings for color mode
*/
void renderer_present_color(const RenderSettings *settings);
/**
* @brief Update rotation angles based on time delta
* @param rotation Rotation state to update
* @param delta_time Time since last update in seconds
*/
void renderer_update_rotation(RotationState *rotation, double delta_time);
/**
* @brief Create default rotation state
* @return Default rotation state with Y-axis rotation enabled
*/
RotationState renderer_default_rotation(void);
/**
* @brief Create default render settings
* @return Default settings
*/
RenderSettings renderer_default_settings(void);
/**
* @brief Set the render mode
* @param mode New render mode
*/
void renderer_set_mode(RenderMode mode);
/**
* @brief Set the color mode
* @param mode New color mode
*/
void renderer_set_color_mode(ColorMode mode);
/**
* @brief Get current frame statistics
* @return Frame stats structure
*/
FrameStats renderer_get_stats(void);
/**
* @brief Set the shading palette
* @param palette_index 0=standard, 1=extended, 2=block, 3=minimal
*/
void renderer_set_palette(int palette_index);
/**
* @brief Get the lighting system for modification
* @return Pointer to lighting system
*/
LightingSystem *renderer_get_lighting(void);
/**
* @brief Hide terminal cursor
*/
void renderer_hide_cursor(void);
/**
* @brief Show terminal cursor
*/
void renderer_show_cursor(void);
/**
* @brief Clear terminal screen
*/
void renderer_clear_terminal(void);
/**
* @brief Set terminal to alternate screen buffer
*/
void renderer_enter_alternate_screen(void);
/**
* @brief Restore terminal to main screen buffer
*/
void renderer_exit_alternate_screen(void);
#ifdef __cplusplus
}
#endif
#endif /* ASCII3D_RENDERER_H */

38
include/timing.h Normal file
View File

@@ -0,0 +1,38 @@
/**
* @file timing.h
* @brief High-precision timing utilities
* @author ASCII3D Project
* @version 1.0.0
*/
#ifndef ASCII3D_TIMING_H
#define ASCII3D_TIMING_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Get current time in seconds (monotonic clock)
* @return Time in seconds with nanosecond precision
*/
double timing_get_seconds(void);
/**
* @brief Sleep for specified microseconds
* @param microseconds Duration to sleep
*/
void timing_sleep_us(unsigned int microseconds);
/**
* @brief Frame rate limiter - sleeps to maintain target FPS
* @param frame_start_time Time when frame started (from timing_get_seconds)
* @param target_fps Target frames per second
*/
void timing_limit_fps(double frame_start_time, int target_fps);
#ifdef __cplusplus
}
#endif
#endif /* ASCII3D_TIMING_H */

115
include/vec3.h Normal file
View File

@@ -0,0 +1,115 @@
/**
* @file vec3.h
* @brief 3D Vector mathematics for ASCII 3D Renderer
* @author ASCII3D Project
* @version 1.0.0
*/
#ifndef ASCII3D_VEC3_H
#define ASCII3D_VEC3_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief 3D Vector structure
*/
typedef struct Vec3 {
float x;
float y;
float z;
} Vec3;
/**
* @brief Create a new Vec3
* @param x X component
* @param y Y component
* @param z Z component
* @return New Vec3 instance
*/
Vec3 vec3_create(float x, float y, float z);
/**
* @brief Add two vectors
* @param a First vector
* @param b Second vector
* @return Result of a + b
*/
Vec3 vec3_add(Vec3 a, Vec3 b);
/**
* @brief Subtract two vectors
* @param a First vector
* @param b Second vector
* @return Result of a - b
*/
Vec3 vec3_sub(Vec3 a, Vec3 b);
/**
* @brief Scale a vector by a scalar
* @param v Vector to scale
* @param s Scalar value
* @return Scaled vector
*/
Vec3 vec3_scale(Vec3 v, float s);
/**
* @brief Calculate dot product of two vectors
* @param a First vector
* @param b Second vector
* @return Dot product
*/
float vec3_dot(Vec3 a, Vec3 b);
/**
* @brief Calculate cross product of two vectors
* @param a First vector
* @param b Second vector
* @return Cross product
*/
Vec3 vec3_cross(Vec3 a, Vec3 b);
/**
* @brief Calculate length of a vector
* @param v Vector
* @return Length
*/
float vec3_length(Vec3 v);
/**
* @brief Normalize a vector
* @param v Vector to normalize
* @return Normalized vector
*/
Vec3 vec3_normalize(Vec3 v);
/**
* @brief Rotate vector around X axis
* @param v Vector to rotate
* @param angle Angle in radians
* @return Rotated vector
*/
Vec3 vec3_rotate_x(Vec3 v, float angle);
/**
* @brief Rotate vector around Y axis
* @param v Vector to rotate
* @param angle Angle in radians
* @return Rotated vector
*/
Vec3 vec3_rotate_y(Vec3 v, float angle);
/**
* @brief Rotate vector around Z axis
* @param v Vector to rotate
* @param angle Angle in radians
* @return Rotated vector
*/
Vec3 vec3_rotate_z(Vec3 v, float angle);
#ifdef __cplusplus
}
#endif
#endif /* ASCII3D_VEC3_H */