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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use crate::Shader;
use solstice::mesh::MultiMesh;
use solstice::{
    mesh::{MappedVertexMesh, VertexMesh},
    vertex::{AttributeType, Vertex, VertexFormat},
    Context,
};

const SHADER_SRC: &str = include_str!("lines.glsl");

#[repr(C)]
#[derive(bytemuck::Zeroable, bytemuck::Pod, Vertex, Copy, Clone, Debug)]
pub struct Position {
    point: [f32; 3],
}

#[repr(C)]
#[derive(bytemuck::Zeroable, bytemuck::Pod, Copy, Clone, Debug)]
pub struct LineVertex {
    pub position: [f32; 3],
    pub width: f32,
    pub color: [f32; 4],
}

impl Default for LineVertex {
    fn default() -> Self {
        Self {
            position: [0., 0., 0.],
            width: 1.0,
            color: [1., 1., 1., 1.],
        }
    }
}

impl Vertex for LineVertex {
    fn build_bindings() -> &'static [VertexFormat] {
        const OFFSET: usize = std::mem::size_of::<LineVertex>();
        &[
            VertexFormat {
                name: "position1",
                offset: 0,
                atype: AttributeType::F32F32F32,
                normalize: false,
            },
            VertexFormat {
                name: "width1",
                offset: std::mem::size_of::<[f32; 3]>(),
                atype: AttributeType::F32,
                normalize: false,
            },
            VertexFormat {
                name: "color1",
                offset: std::mem::size_of::<[f32; 3]>() + std::mem::size_of::<f32>(),
                atype: AttributeType::F32F32F32F32,
                normalize: false,
            },
            VertexFormat {
                name: "position2",
                offset: OFFSET,
                atype: AttributeType::F32F32F32,
                normalize: false,
            },
            VertexFormat {
                name: "width2",
                offset: OFFSET + std::mem::size_of::<[f32; 3]>(),
                atype: AttributeType::F32,
                normalize: false,
            },
            VertexFormat {
                name: "color2",
                offset: OFFSET + std::mem::size_of::<[f32; 3]>() + std::mem::size_of::<f32>(),
                atype: AttributeType::F32F32F32F32,
                normalize: false,
            },
        ]
    }
}

#[allow(unused)]
const SEGMENT_VERTS: [Position; 6] = [
    Position {
        point: [0.0, -0.5, 0.],
    },
    Position {
        point: [1.0, -0.5, 0.],
    },
    Position {
        point: [1.0, 0.5, 0.],
    },
    Position {
        point: [0.0, -0.5, 0.],
    },
    Position {
        point: [1.0, 0.5, 0.],
    },
    Position {
        point: [0.0, 0.5, 0.],
    },
];

fn round_cap_join_geometry(resolution: usize) -> Vec<Position> {
    let mut instance_round_round = vec![
        Position {
            point: [0., -0.5, 0.],
        },
        Position {
            point: [0., -0.5, 1.],
        },
        Position {
            point: [0., 0.5, 1.],
        },
        Position {
            point: [0., -0.5, 0.],
        },
        Position {
            point: [0., 0.5, 1.],
        },
        Position {
            point: [0., 0.5, 0.],
        },
    ];

    const PI: f32 = std::f32::consts::PI;

    // Add the left cap.
    for step in 0..resolution {
        let theta0 = PI / 2. + ((step + 0) as f32 * PI) / resolution as f32;
        let theta1 = PI / 2. + ((step + 1) as f32 * PI) / resolution as f32;
        instance_round_round.push(Position {
            point: [0., 0., 0.],
        });
        instance_round_round.push(Position {
            point: [0.5 * theta0.cos(), 0.5 * theta0.sin(), 0.],
        });
        instance_round_round.push(Position {
            point: [0.5 * theta1.cos(), 0.5 * theta1.sin(), 0.],
        });
    }
    // Add the right cap.
    for step in 0..resolution {
        let theta0 = (3. * PI) / 2. + ((step + 0) as f32 * PI) / resolution as f32;
        let theta1 = (3. * PI) / 2. + ((step + 1) as f32 * PI) / resolution as f32;
        instance_round_round.push(Position {
            point: [0., 0., 1.],
        });
        instance_round_round.push(Position {
            point: [0.5 * theta0.cos(), 0.5 * theta0.sin(), 1.],
        });
        instance_round_round.push(Position {
            point: [0.5 * theta1.cos(), 0.5 * theta1.sin(), 1.],
        });
    }

    instance_round_round
}

const BUFFER_SIZE: usize = 10000;

pub struct LineWorkspace {
    segment_geometry: VertexMesh<Position>,
    positions: MappedVertexMesh<LineVertex>,
    offset: usize,

    shader: Shader,
}

impl LineWorkspace {
    pub fn new(ctx: &mut Context) -> Result<Self, super::GraphicsError> {
        let segment_geometry = round_cap_join_geometry(50);
        let segment_geometry = VertexMesh::with_data(ctx, &segment_geometry)?;
        // let segment_geometry = VertexMesh::with_data(ctx, &SEGMENT_VERTS)?;
        let positions = MappedVertexMesh::new(ctx, BUFFER_SIZE)?;

        let shader = Shader::with(SHADER_SRC, ctx)?;

        Ok(Self {
            segment_geometry,
            positions,
            offset: 0,
            shader,
        })
    }

    pub fn can_buffer(&self, verts: &[LineVertex]) -> bool {
        self.offset + verts.len() < BUFFER_SIZE
    }

    pub fn add_points(&mut self, verts: &[LineVertex]) {
        self.positions.set_vertices(verts, self.offset);
        self.offset += verts.len()
    }

    pub fn shader(&self) -> &Shader {
        &self.shader
    }

    pub fn geometry(&mut self, ctx: &mut Context) -> solstice::Geometry<MultiMesh> {
        use solstice::mesh::*;

        let mesh = self.positions.unmap(ctx);
        let instance_count = self.offset as u32 - 1;
        self.offset = 0;

        let draw_range = 0..(self.segment_geometry.len());
        let attached = self.segment_geometry.attach_with_step(mesh, 1);
        solstice::Geometry {
            mesh: attached,
            draw_range,
            draw_mode: solstice::DrawMode::Triangles,
            instance_count,
        }
    }
}