Mesh: Fix vertex_count, vertex_array in newMesh. Fix add_polygonal_line()

This commit is contained in:
Alberto Torres 2024-11-30 00:48:05 +01:00
parent 893208f4c2
commit 2caf78c88a

View file

@ -278,26 +278,15 @@ proc initMesh(self: Mesh, engine: MyouEngine, name: string, scene: Scene = nil,
self.layout.add Attribute(name: "normal", dtype: Byte, count: 4)
if uv in common_attributes:
self.layout.add Attribute(name: "uv_0", dtype: Float, count: 2)
# self.stride = if stride != 0: stride else: self.layout.stride
let stride = self.layout.stride
if vertex_count != 0:
if vertex_array.len != 0:
var va = newArrRef(vertex_array)
var ia = newArrRef(index_array)
self.skip_upload = skip_upload
self.load_from_va_ia(@[va], @[ia])
elif vertex_count != 0:
self.ensure_capacity(vertex_count)
# var va = newArrRef(vertex_array)
# if vertex_count != 0:
# va.set_len max(va.len, vertex_count * self.stride div 4)
# if va.len != 0:
# var ia = newArrRef(index_array)
# self.skip_upload = skip_upload
# self.load_from_va_ia(@[va], @[ia])
# self.data.num_indices ?= @[0.int32]
# if ?vertex_array:
# if not ?ia:
# self.data.num_indices[0] = vertex_count
# assert((self.data.varray.bytelen div self.data.stride) >= vertex_count,
# &"Array is {self.data.varray.bytelen} bytes long but expected at least {vertex_count * self.data.stride}")
# else:
# self.data.num_indices[0] = 0
if scene != nil:
scene.add_object(self, name=name)
return self
@ -736,12 +725,20 @@ proc debug_print_vertices*(self: Mesh, starts: int = 0, ends: int = 10) =
else: ""
echo &"{attr.name} {data}"
proc add_polygonal_line*(self: Mesh, orig, dest: Vec3, width: float) =
proc add_polygonal_line*(self: Mesh, orig, dest: Vec3, width: float,
color = vec4(1), update = true) =
## Adds a line made of a quad, from `orig` to `dest` with a given width. The
## quad will face up the Z axis. If the line starts where the previous line
## ended, it will change both ends to match (unless the angle is under ~37°).
##
## Optionally you can supply a color.
##
## If you're going to add many lines, set `update` to false, then call
## `update_varray` at the end.
##
## It requires the mesh to have draw method TriangleStrip.
when not defined(release):
assert self.draw_method == TriangleStrip, "Error: mesh draw_method should be TriangleStrip"
let last = self.last_polyline_point
let last_left = self.last_polyline_left
let has_last = self.data.num_indices[0] != 0
@ -757,20 +754,17 @@ proc add_polygonal_line*(self: Mesh, orig, dest: Vec3, width: float) =
# may be too big, ignore it
inleft = left
if has_last and not (last ~= orig):
## NaN polygons
self.add_vertex(vec3(), vec4(0,0,0,1))
self.add_vertex(vec3(), vec4(0,0,0,1))
# ## degen tris
# self.add_vertex(last, vec4(0,0,0,1))
# self.add_vertex(last, vec4(0,0,0,1))
# self.add_vertex(orig, vec4(0,0,0,1))
# self.add_vertex(orig, vec4(0,0,0,1))
## degen tris
self.add_vertex(last - last_left, color)
self.add_vertex(orig + inleft, color)
# self.data.num_indices[0] -= 2
self.add_vertex(orig + inleft, vec4(0,0,0,1))
self.add_vertex(orig - inleft, vec4(0,0,0,1))
self.add_vertex(dest + left, vec4(0,0,0,1))
self.add_vertex(dest - left, vec4(0,0,0,1))
self.add_vertex(orig + inleft, color)
self.add_vertex(orig - inleft, color)
self.add_vertex(dest + left, color)
self.add_vertex(dest - left, color)
self.last_polyline_point = dest
self.last_polyline_left = left
self.data.update_varray()
if update:
self.data.update_varray()