Skip to content

Commit

Permalink
Fix error in TesselatedSvgNode::join - offset indices correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed Aug 16, 2021
1 parent d8dc2c8 commit fa57877
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
5 changes: 3 additions & 2 deletions azul-core/src/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,9 @@ uniform vec2 vBboxSize;
attribute vec2 vAttrXY;
void main() {
gl_Position = vec4(vAttrXY / vBboxSize - vec2(1.0), 1.0, 1.0);
gl_PointSize = 1.0;
vec2 vCalc = vAttrXY / vBboxSize;
vec2 vCalc1 = vCalc - vec2(1.0, 1.0);
gl_Position = vec4(vCalc1, 0.0, 1.0);
}";

static SVG_FRAGMENT_SHADER: &[u8] = b"#version 150
Expand Down
35 changes: 34 additions & 1 deletion azulc/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,21 @@ pub fn join_tessellated_nodes(nodes: &[TessellatedSvgNode]) -> TessellatedSvgNod

use rayon::iter::IntoParallelRefIterator;
use rayon::iter::ParallelIterator;
use rayon::iter::IndexedParallelIterator;
use rayon::iter::IntoParallelRefMutIterator;

let mut index_offset = 0;

// note: can not be parallelized!
let all_index_offsets = nodes
.as_ref()
.iter()
.map(|t| {
let i = index_offset;
index_offset += t.vertices.len();
i
})
.collect::<Vec<_>>();

let all_vertices = nodes
.as_ref()
Expand All @@ -464,9 +479,27 @@ pub fn join_tessellated_nodes(nodes: &[TessellatedSvgNode]) -> TessellatedSvgNod
let all_indices = nodes
.as_ref()
.par_iter()
.flat_map(|t| {
.enumerate()
.flat_map(|(buffer_index, t)| {

// since the vertex buffers are now joined,
// offset the indices by the vertex buffers lengths
// encountered so far
let vertex_buffer_offset: u32 = all_index_offsets
.get(buffer_index)
.copied()
.unwrap_or(0)
.min(core::u32::MAX as usize) as u32;

let mut indices = t.indices.clone().into_library_owned_vec();
if vertex_buffer_offset != 0 {
indices
.par_iter_mut()
.for_each(|i| { *i += vertex_buffer_offset; });
}

indices.push(GL_RESTART_INDEX);

indices
})
.collect::<Vec<_>>();
Expand Down
9 changes: 7 additions & 2 deletions examples/rust/opengl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,17 @@ fn parse_multipolygons(data: &str) -> Vec<SvgMultiPolygon> {

let mut current = SvgPoint { x: i[0], y: i[1] };
current.x -= 13.804493;
current.x *= 10000.0;
current.y -= 51.05264;
current.y *= 10000.0;
current.x *= 50000.0;
current.y *= 50000.0;
current.x += 500.0;
current.y += 500.0;
current.x *= 2.0;
current.y *= 2.0;

last = Some(current);
let last_point = last_point?;

Some(SvgPathElement::Line(SvgLine { start: last_point, end: current }))
}).collect::<Vec<_>>().into(),
}
Expand Down

0 comments on commit fa57877

Please sign in to comment.