summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 0f8a07e482e71bc233e2fcdaadbb49e6815b2b78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
mod perlin;
mod vec3;
mod image;

use std::{println, format};

use perlin::MixInImage;

use crate::perlin::Perlin;
use crate::vec3::{Vec3,Point3, Color};
use crate::image::Image;

fn hex_color(string: &str) -> Color {
    Color {
        x: i64::from_str_radix(&string[0..2], 16).unwrap() as f64 / 255.0,
        y: i64::from_str_radix(&string[2..4], 16).unwrap() as f64 / 255.0,
        z: i64::from_str_radix(&string[4..6], 16).unwrap() as f64 / 255.0,
    }
}

// maps [0.0, 1.0] -> Color[0.0, 1.0]
trait Palette {
    fn sample(&self, index: f64) -> Color;
}

struct IdenPalette {}

impl Palette for IdenPalette {
    fn sample(&self, index: f64) -> Color {
        Color{x: index, y: index, z: index}
    }
}

struct FirePalette {}

impl Palette for FirePalette { 
    fn sample(&self, index: f64) -> Color {
        Color {
            //x: index / 3.0,
            x: 0.46 - (index / 3.0),
            y: 1.0,
            z: ((index + 0.03) * 2.0).min(1.0),
        }.hsl_to_rgb()
    }
}

struct GradientPalette {
    start: Color,
    end: Color,
}

impl Palette for GradientPalette {
    fn sample(&self, index: f64) -> Color {
        Color {
            x: (index * self.start.x) + ((1.0 - index) * self.end.x),
            y: (index * self.start.y) + ((1.0 - index) * self.end.y),
            z: (index * self.start.z) + ((1.0 - index) * self.end.z),
        }
    }
}

// fn palette_func_pink(index: f64) -> Color {
//     Color {
//         x: 1.0 - (index / 3.0),
//         y: 1.0,
//         z: (index * 2.0).min(1.0),
//     }.hsl_to_rgb()
// }


// fn palette_func_quant(index: f64) -> Color {
//     Color {
//         x: (index * 8.0).round() / 8.0,
//         y: 1.0,
//         z: (index * 2.0).min(1.0),
//     }.hsl_to_rgb()
// }

fn sample(h: f64, l: f64) -> Color {
    let start_hue = 0.75;
    let end_hue = start_hue + 0.2;
    let lightness_scale = 0.5;
    Color {
        x: (h * (end_hue - start_hue)) + start_hue,
        y: 1.0,
        z: (l * lightness_scale) + (lightness_scale / 2.0),
    }.hsl_to_rgb()
}

fn main() {
    // params
    let height = 512;
    let width = 512;
    let scale = 1.0;
    let turb_depth = 15;
    let dsw_factor = 1.5;
    let mixin_factor = 0.015;
    let mixin_strength = 0.3;
    let mixin_image = MixInImage::from_bmp(&include_bytes!("../mixin.bmp").to_vec());
    //let palette = GradientPalette{start: hex_color("0000ff"), end: hex_color("ff0000")};
    //let palette = IdenPalette{};
    let palette = FirePalette{};
    
    let hue_perlin = Perlin::new();
    //let light_perlin = Perlin::new();
    let mut image = Image::new(width, height);
    for x in 0..width {
        for y in 0..height {
            let hue_noise = hue_perlin.turb_dsw_mixin(&Point3{x: (x as f64 / width as f64) * scale, y: (y as f64 / height as f64) * scale, z: 0.0}, turb_depth, dsw_factor, &mixin_image, mixin_factor, mixin_strength);
            //let light_noise = light_perlin.turb_dsw(&Point3{x: (x as f64 / width as f64) * scale, y: (y as f64 / height as f64) * scale, z: 0.0}, 7);
            //image.set(x, y, &sample(hue_noise, light_noise)).unwrap();
            //image.set(x, y, &Color{x: hue_noise, y: hue_noise, z: hue_noise}).unwrap();
            image.set(x, y, &palette.sample(hue_noise)).unwrap();
        }
    }
    image.write(&mut std::io::stdout());
}

// fn main() {
//     // params
//     let height = 512;
//     let width = 512;
//     let frames = 600;
//     let scale = 1.0;
//     let turb_depth = 20;
//     let dsw_factor = 1.5;
//     let palette = FirePalette{};
    
//     let hue_perlin = Perlin::new();
//     let mut image = Image::new(width, height);
//     for f in 0..frames {
//         for x in 0..width {
//             for y in 0..height {
//                 let hue_noise = hue_perlin.turb_dsw(&Point3{x: (x as f64 / width as f64) * scale, y: (y as f64 / height as f64) * scale, z: (f as f64 / frames as f64) * scale}, turb_depth, dsw_factor);
//                 image.set(x, y, &palette.sample(hue_noise)).unwrap();
//             }
//         }
//         let path = format!("./anim/{}.ppm", f);
//         image.write(&mut std::fs::File::create(path).unwrap());

//         println!("Finished frame {} of {}!", f, frames);
//     }
// }