Skip to content

Commit

Permalink
Merge pull request regl-project#331 from regl-project/elements-count
Browse files Browse the repository at this point in the history
Infer element count/length for empty constructor
  • Loading branch information
mikolalysenko authored Sep 11, 2016
2 parents 21ed15b + e7f03cc commit cfabd67
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 38 deletions.
78 changes: 40 additions & 38 deletions lib/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,29 @@ module.exports = function wrapElementsState (gl, extensions, bufferState, stats)
count,
byteLength,
type) {
var predictedType = type
if (!type && (
!isTypedArray(data) ||
(isNDArrayLike(data) && !isTypedArray(data.data)))) {
predictedType = extensions.oes_element_index_uint
? GL_UNSIGNED_INT
: GL_UNSIGNED_SHORT
}
elements.buffer.bind()
bufferState._initBuffer(
elements.buffer,
data,
usage,
predictedType,
3)
if (data) {
var predictedType = type
if (!type && (
!isTypedArray(data) ||
(isNDArrayLike(data) && !isTypedArray(data.data)))) {
predictedType = extensions.oes_element_index_uint
? GL_UNSIGNED_INT
: GL_UNSIGNED_SHORT
}
bufferState._initBuffer(
elements.buffer,
data,
usage,
predictedType,
3)
} else {
gl.bufferData(GL_ELEMENT_ARRAY_BUFFER, byteLength, usage)
elements.buffer.dtype = dtype || GL_UNSIGNED_BYTE
elements.buffer.usage = usage
elements.buffer.dimension = 3
elements.buffer.byteLength = byteLength
}

var dtype = type
if (!type) {
Expand Down Expand Up @@ -212,38 +220,32 @@ module.exports = function wrapElementsState (gl, extensions, bufferState, stats)
'invalid vertex count for elements')
vertCount = options.count | 0
}
if ('length' in options) {
byteLength = options.length | 0
}
if ('type' in options) {
check.parameter(
options.type,
elementTypes,
'invalid buffer type')
dtype = elementTypes[options.type]
}
if ('length' in options) {
byteLength = options.length | 0
} else {
byteLength = vertCount
if (dtype === GL_UNSIGNED_SHORT || dtype === GL_SHORT) {
byteLength *= 2
} else if (dtype === GL_UNSIGNED_INT || dtype === GL_INT) {
byteLength *= 4
}
}
}
if (data) {
initElements(
elements,
data,
usage,
primType,
vertCount,
byteLength,
dtype)
} else {
var _buffer = elements.buffer
_buffer.bind()
gl.bufferData(GL_ELEMENT_ARRAY_BUFFER, byteLength, usage)
_buffer.dtype = dtype || GL_UNSIGNED_BYTE
_buffer.usage = usage
_buffer.dimension = 3
_buffer.byteLength = byteLength
elements.primType = primType < 0 ? GL_TRIANGLES : primType
elements.vertCount = vertCount < 0 ? 0 : vertCount
elements.type = _buffer.dtype
}
initElements(
elements,
data,
usage,
primType,
vertCount,
byteLength,
dtype)
}

return reglElements
Expand Down
32 changes: 32 additions & 0 deletions test/elements-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var tape = require('tape')
var createContext = require('./util/create-context')
var createREGL = require('../regl')

tape('elements - length', function (t) {
var gl = createContext(5, 5)
var regl = createREGL(gl)

var N = 5

var cellsBuffer = regl.elements({
type: 'uint16',
usage: 'dynamic',
primitive: 'triangles',
count: N * 3
})

t.equals(cellsBuffer._elements.buffer.byteLength, N * 3 * 2, 'count')

var cells2Buffer = regl.elements({
type: 'uint16',
usage: 'dynamic',
primitive: 'triangles',
length: N * 2 * 3
})

t.equals(cells2Buffer._elements.vertCount, N * 3, 'count')

regl.destroy()
createContext.destroy(gl)
t.end()
})
1 change: 1 addition & 0 deletions test/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ require('../attribute-constants')
require('../attributes-nested')
require('../buffer-update')
require('../elements-update')
require('../elements-length')

0 comments on commit cfabd67

Please sign in to comment.