forked from google/gxui
/
index_buffer.go
79 lines (66 loc) · 1.78 KB
/
index_buffer.go
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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gl
import (
"fmt"
"reflect"
"github.com/goxjs/gl"
)
type indexBuffer struct {
data []byte
ty primitiveType
}
func newIndexBuffer(ty primitiveType, data16 []uint16) *indexBuffer {
switch ty {
case ptUbyte, ptUshort, ptUint:
if !ty.isArrayOfType(data16) {
panic(fmt.Errorf("Index data is not of type %v", ty))
}
default:
panic(fmt.Errorf("Index type must be either UBYTE, USHORT or UINT. Got: %v", ty))
}
// HACK: Hardcode support for only ptUshort.
data := make([]byte, len(data16)*2)
for i, v := range data16 {
data[2*i+0] = byte(v >> 0)
data[2*i+1] = byte(v >> 8)
}
ib := &indexBuffer{
data: data,
ty: ty,
}
return ib
}
func (b *indexBuffer) newContext() *indexBufferContext {
dataVal := reflect.ValueOf(b.data)
length := dataVal.Len() / 2 // HACK: Hardcode support for only ptUshort.
buffer := gl.CreateBuffer()
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, b.data, gl.STATIC_DRAW)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl.Buffer{})
checkError()
globalStats.indexBufferContextCount.inc()
return &indexBufferContext{
buffer: buffer,
ty: b.ty,
length: length,
}
}
type indexBufferContext struct {
contextResource
buffer gl.Buffer
ty primitiveType
length int
}
func (c *indexBufferContext) destroy() {
globalStats.indexBufferContextCount.dec()
gl.DeleteBuffer(c.buffer)
c.buffer = gl.Buffer{}
}
func (c *indexBufferContext) render(drawMode drawMode) {
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, c.buffer)
gl.DrawElements(gl.Enum(drawMode), c.length, gl.Enum(c.ty), 0)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl.Buffer{})
checkError()
}