update pixelgl.VertexSlice API similarly to Triangles

This commit is contained in:
faiface 2017-01-28 20:14:20 +01:00
parent 63339235b7
commit 0771f07888
2 changed files with 20 additions and 32 deletions

View File

@ -119,12 +119,7 @@ func (gt *glTriangles) submitData() {
mainthread.CallNonBlock(func() { mainthread.CallNonBlock(func() {
gt.vs.Begin() gt.vs.Begin()
dataLen := len(data) / gt.vs.Stride() dataLen := len(data) / gt.vs.Stride()
if dataLen > gt.vs.Len() { gt.vs.SetLen(dataLen)
gt.vs.Append(make([]float32, (dataLen-gt.vs.Len())*gt.vs.Stride()))
}
if dataLen < gt.vs.Len() {
gt.vs = gt.vs.Slice(0, dataLen)
}
gt.vs.SetVertexData(gt.data) gt.vs.SetVertexData(gt.data)
gt.vs.End() gt.vs.End()
}) })

View File

@ -50,7 +50,7 @@ func (vs *VertexSlice) Stride() int {
return vs.va.stride / 4 return vs.va.stride / 4
} }
// Len returns the length of the VertexSlice. // Len returns the length of the VertexSlice (number of vertices).
func (vs *VertexSlice) Len() int { func (vs *VertexSlice) Len() int {
return vs.j - vs.i return vs.j - vs.i
} }
@ -60,20 +60,11 @@ func (vs *VertexSlice) Cap() int {
return vs.va.cap - vs.i return vs.va.cap - vs.i
} }
// Slice returns a sub-slice of this VertexSlice covering the range [i, j) (relative to this // SetLen resizes the VertexSlice to length len.
// VertexSlice). func (vs *VertexSlice) SetLen(len int) {
// vs.End() // vs must have been Begin-ed before calling this method
// Note, that the returned VertexSlice shares an underlying vertex array with the original *vs = vs.grow(len)
// VertexSlice. Modifying the contents of one modifies corresponding contents of the other. vs.Begin()
func (vs *VertexSlice) Slice(i, j int) *VertexSlice {
if i < 0 || j < i || j > vs.va.cap {
panic("failed to slice vertex slice: index out of range")
}
return &VertexSlice{
va: vs.va,
i: vs.i + i,
j: vs.i + j,
}
} }
// grow returns supplied vs with length changed to len. Allocates new underlying vertex array if // grow returns supplied vs with length changed to len. Allocates new underlying vertex array if
@ -110,18 +101,20 @@ func (vs VertexSlice) grow(len int) VertexSlice {
return newVs return newVs
} }
// Append adds supplied vertices to the end of the VertexSlice. If the capacity of the VertexSlice // Slice returns a sub-slice of this VertexSlice covering the range [i, j) (relative to this
// is not sufficient, a new, larger underlying vertex array will be allocated. The content of the // VertexSlice).
// original VertexSlice will be copied to the new underlying vertex array.
// //
// The data is in the same format as with SetVertexData. // Note, that the returned VertexSlice shares an underlying vertex array with the original
// // VertexSlice. Modifying the contents of one modifies corresponding contents of the other.
// The VertexSlice is appended 'in-place', contrary Go's builtin slices. func (vs *VertexSlice) Slice(i, j int) *VertexSlice {
func (vs *VertexSlice) Append(data []float32) { if i < 0 || j < i || j > vs.va.cap {
vs.End() // vs must have been Begin-ed before calling this method panic("failed to slice vertex slice: index out of range")
*vs = vs.grow(vs.Len() + len(data)/vs.Stride()) }
vs.Begin() return &VertexSlice{
vs.Slice(vs.Len()-len(data)/vs.Stride(), vs.Len()).SetVertexData(data) va: vs.va,
i: vs.i + i,
j: vs.i + j,
}
} }
// SetVertexData sets the contents of the VertexSlice. // SetVertexData sets the contents of the VertexSlice.