Skip to content

Commit 60fcf22

Browse files
committed
Update to WebGL2
1 parent 56e77c5 commit 60fcf22

32 files changed

+259
-320
lines changed

docs/VPythonDocs/compound.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,8 @@
7777
<p class="Normal"><strong><font color="#0000a0">Current restrictions</font></strong></p>
7878
<p class="Normal">Currently objects in a compound can have their own values of <strong>color</strong>, <strong>opacity</strong>, <strong>shininess</strong>, and <strong>emissive</strong>, but they cannot have individual textures or bumpmaps, which can only be specified for the combined object, and which affect all of the compounded objects.</p>
7979
<p class="Normal">Currently label objects, lights, and objects based on curve objects (curve, helix) cannot be compounded. However, <a href="triangle.html">triangles</a>, <a href="triangle.html">quads</a>, and even other compounds can be compounded. Also, a compound object can be <a href="clone.html">cloned</a> to make additional copies.</p>
80-
<p class="Normal"><strong>Limitation on the number of objects to compound:</strong> In the WebGL 3D graphics library that is used by VPython, a compound cannot have more than 65536 (2**16) vertexes (pos, normal, color), due to WebGL using 16-bit integers to reference the vertexes. For example, a box object has 8*3 = 24 vertex objects, because each corner requires three vertex objects, all with the same positions but with three different normals. That means that a list of boxes to compound must not contain more than int(65536/24) = 2730 boxes. A cylinder, in order to have smooth sides, requires 206 vertexes, so a list of cylinders to compound must not contain more than int(65536/206) = 318 cylinders. If you need to compound more than 2730 boxes, a way to deal with the limitation is to make several compounds, each with less than 2730 boxes (and it may be convenient to store these compounds in a list). You can compound a list of compound objects, but the total number of vertexes cannot exceed the limit.</p>
81-
<p class="Normal">Currently here are the number of vertexes in each of the basic objects: box 24, cylinder 206, sphere 961, simple_sphere 81, pyramid 16, cone 604, ring 1281. Consequently, at present the limits on the number of objects that can be compounded are these (if all the objects are boxes, or cylinders, etc.): box 2730, cylinder 318, sphere 68, simple_sphere 809, pyramid 4096, cone 108, ring 51.</p>
82-
<p class="Normal">An informative error message is given if the vertex limit is exceeded.</p>
83-
<!-- InstanceEndEditable -->
80+
<p>&nbsp;</p>
81+
<!-- InstanceEndEditable -->
8482
</div>
8583
</div>
8684
</body>

lib/glow/WebGLRenderer.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
function WebGLRenderer(cvs, canvasElement, overlay) {
2424

2525
var renderer = this
26-
var gl = WebGLUtils.setupWebGL(canvasElement) // main canvas
26+
// var gl = WebGLUtils.setupWebGL(canvasElement) // main canvas
27+
var gl = canvasElement.getContext("webgl2");
2728
if (!gl) throw new Error("Can't create canvas: WebGL not supported")
2829
var points_pixel_first = true // set to false after world-to-pixel conversion established
2930

@@ -177,7 +178,7 @@
177178
this.emissive = new Float32Array(mesh.emissive)
178179
this.texpos = new Float32Array(mesh.texpos)
179180
this.bumpaxis = new Float32Array(mesh.bumpaxis)
180-
this.index = new Uint16Array(mesh.index)
181+
this.index = new Uint32Array(mesh.index)
181182

182183
this.posBuffer = gl.createBuffer()
183184
gl.bindBuffer(gl.ARRAY_BUFFER, this.posBuffer)
@@ -1107,7 +1108,7 @@
11071108
}
11081109
}
11091110
gl.uniform4fv(program.uniforms.segmentData, data) // point data for this curve
1110-
gl.drawElements(elements, model_length, gl.UNSIGNED_SHORT, 0)
1111+
gl.drawElements(elements, model_length, gl.UNSIGNED_INT, 0)
11111112
if (mode == PICK) {
11121113
for (var i=0; i<8; i++) data[8+i] = save[i]
11131114
}
@@ -1127,7 +1128,7 @@
11271128
model_arrays = {}
11281129
model_arrays.pos = new Float32Array(pickdata.pos)
11291130
model_arrays.color = new Float32Array(pickdata.color)
1130-
model_index = new Uint16Array(pickdata.index)
1131+
model_index = new Uint32Array(pickdata.index)
11311132
model_length = model_index.length
11321133

11331134
gl.bindBuffer(gl.ARRAY_BUFFER, model.posBuffer)
@@ -1142,7 +1143,7 @@
11421143
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, model_index, gl.DYNAMIC_DRAW)
11431144
gl.vertexAttribPointer(program.attributes.color, 4, gl.FLOAT, false, 0, 0)
11441145

1145-
gl.drawElements(elements, model_length, gl.UNSIGNED_SHORT, 0)
1146+
gl.drawElements(elements, model_length, gl.UNSIGNED_INT, 0)
11461147

11471148
// Restore standard pos and color data:
11481149
gl.bindBuffer(gl.ARRAY_BUFFER, model.posBuffer)
@@ -1256,7 +1257,7 @@
12561257
var indices = sort[op][sort_type][sort_list]
12571258

12581259
var tbobj = indices[0] // representative object is given as first element of list
1259-
var model_index = new Uint16Array(indices.slice(1))
1260+
var model_index = new Uint32Array(indices.slice(1))
12601261
var model_length = model_index.length
12611262

12621263
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, model.indexBuffer)
@@ -1301,7 +1302,7 @@
13011302

13021303
gl.uniform1f(program.uniforms.T, Tdata)
13031304
gl.uniform1f(program.uniforms.B, Bdata)
1304-
gl.drawElements(elements, model_length, gl.UNSIGNED_SHORT, 0)
1305+
gl.drawElements(elements, model_length, gl.UNSIGNED_INT, 0)
13051306
}
13061307
}
13071308
}
@@ -1322,7 +1323,7 @@
13221323
gl.bindBuffer(gl.ARRAY_BUFFER, model.posBuffer)
13231324
gl.vertexAttribPointer(program.attributes.pos, 3, gl.FLOAT, false, 0, 0)
13241325
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, model.indexBuffer)
1325-
gl.drawElements(elements, model_length, gl.UNSIGNED_SHORT, 0)
1326+
gl.drawElements(elements, model_length, gl.UNSIGNED_INT, 0)
13261327

13271328
}
13281329

@@ -1517,7 +1518,7 @@
15171518

15181519
// This stuff needs to happen for each individual object
15191520
gl.uniform4fv(program.uniforms.objectData, data)
1520-
gl.drawElements(elements, model_length, gl.UNSIGNED_SHORT, 0)
1521+
gl.drawElements(elements, model_length, gl.UNSIGNED_INT, 0)
15211522

15221523
if (mode == PICK) {
15231524
for (var i=0; i<4; i++) data[16+i] = save[i]

lib/glow/canvas.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,21 @@
161161
this.__points_objects = [] // list of points objects
162162
this.__opaque_objects = {}
163163
this.__transparent_objects = {}
164+
164165
this.vertex_id = 1
165166
var N = 100 // the number of additional vertices to allocate each time we need more storage
166167
this.__vertices = {
167168
Nalloc:N, pos:new Float32Array(3*N), normal:new Float32Array(3*N),
168169
color:new Float32Array(3*N), opacity:new Float32Array(N),
169170
shininess:new Float32Array(N), emissive:new Float32Array(N),
170171
texpos:new Float32Array(2*N), bumpaxis:new Float32Array(3*N),
171-
index: new Uint16Array(N), // not actually used
172-
model_transparent: false, // not actually used
172+
//index: new Uint32Array(N), // not currently used
173+
//model_transparent: false, // not currently used
173174
object_info:{}, // vertex_object:[list of triangles/quads using this vertex]
174175
available:[] // push no longer used id onto available stack; pop to get an available vertex id
175176
}
176177
this.__vertices.normal[2] = 1 // to avoid possible problems with the unused 0th vertex at render time
178+
177179
// Sort triangles/quads into renderable categories. For example, an entry in opaque:textures
178180
// has the form texture_name:[indices of all the opaque triangles that share this texture].
179181
// In the case of plain, there's only one entry -- all:[indices...].

lib/glow/mesh.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
var xmin = null, xmax = null, ymin = null, ymax = null, zmin = null, zmax = null
2121
var offset = this.pos.length / 3
2222
if (object instanceof vertex) {
23-
if ((offset + 1) >= 65536) return null
23+
if (offset >= 4.29e9) return null
2424
if (bias < 0) this.index.push(offset + bias)
2525
else {
2626
if (xmin === null || object.__pos.x < xmin) xmin = object.__pos.x
@@ -41,7 +41,7 @@
4141
this.index.push(offset)
4242
}
4343
} else {
44-
if (offset + otherMesh.pos.length/3 >= 65536) return null
44+
if (offset + otherMesh.pos.length/3 >= 4.29e9) return null
4545
var c = [object.__color.x, object.__color.y, object.__color.z]
4646
for (var j = 0; j < otherMesh.pos.length; j++) {
4747
if (j%3 === 0) {

lib/glow/primitives.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,11 +1158,8 @@
11581158
})
11591159

11601160
function vertex(args) {
1161-
// Comment by David Scherer: In WebGL indices are required to be Uint16Array, so less than 65536.
1162-
// To handle more than this many index values, we need more lists.
1163-
// Moreover, a triangle or quad might use vertex objects from more than one list, which requires some
1164-
// duplication. As a temporary measure to get going, just give an error if one tries to create more
1165-
// than 65536 vertex objects.
1161+
// In WebGL 1, indices were required to be Uint16Array, so less than 65536.
1162+
// In WebGL 2, indices are 32 bits, so up to 4.29e9.
11661163
// We keep info on what triangles or quads use a vertex, and if the
11671164
// count goes to zero, the slot can be reused.
11681165
if (!(this instanceof vertex)) { return new vertex(args) } // so vertex() is like new vertex()
@@ -1180,13 +1177,13 @@
11801177
}
11811178
if (this.opacity === undefined) this.opacity = 1
11821179
if (this.__texpos.z !== 0) throw new Error('In a vertex the z component of texpos must be zero.')
1183-
if (this.canvas.vertex_id >= 65536) throw new Error('Currently the number of vertices is limited to 65536.')
1180+
if (this.canvas.vertex_id >= 4.29e9) throw new Error('Currently the number of vertices is limited to 4.29e9.')
11841181
var lengths = {pos:3, normal:3, color:3, opacity:1, shininess:1, emissive:1, texpos:2, bumpaxis:3}
11851182
this.__id = this.canvas.__vertices.available.pop() // try to use a no-longer-used vertex slot
11861183
if (this.__id === undefined) {
11871184
this.__id = this.canvas.vertex_id
11881185
var c = this.canvas.__vertices
1189-
if (this.canvas.vertex_id % c.Nalloc === 0) { // need to extend arrays
1186+
if (this.__id % c.Nalloc === 0) { // need to extend arrays
11901187
var temp
11911188
var L = this.canvas.vertex_id + c.Nalloc
11921189
for (var t in lengths) {
@@ -1524,17 +1521,17 @@
15241521
if (k == 3) {
15251522
// Bias the index to point to already existing data:
15261523
temp = mesh.merge(o.v0, o.v0, -3)
1527-
if (temp === null) compound_error(i)
1524+
// if (temp === null) compound_error(i)
15281525
update_extent( self, temp )
15291526
temp = mesh.merge(o.v2, o.v2, -1)
1530-
if (temp === null) compound_error(i)
1527+
// if (temp === null) compound_error(i)
15311528
update_extent( self, temp )
15321529
temp = mesh.merge(o.v3, o.v3, 0)
1533-
if (temp === null) compound_error(i)
1530+
// if (temp === null) compound_error(i)
15341531
update_extent( self, temp )
15351532
} else {
15361533
temp = mesh.merge(q[k], q[k], 0)
1537-
if (temp === null) compound_error(i)
1534+
// if (temp === null) compound_error(i)
15381535
update_extent( self, temp )
15391536
}
15401537

@@ -1548,20 +1545,20 @@
15481545
} else if (o instanceof text) {
15491546
var comp = o.__comp
15501547
temp = mesh.merge(comp.getTransformedMesh(), comp, 0)
1551-
if (temp === null) compound_error(i)
1548+
// if (temp === null) compound_error(i)
15521549
update_extent( self, temp )
15531550
} else if (o.__components !== undefined) { // currently, only arrow has components
15541551
var c = o.__components
15551552
o.__update() // ensure that all components are up to date at time of compounding
15561553
for (var ci=0; ci<c.length; ci++) {
15571554
var comp = c[ci]
15581555
temp = mesh.merge(comp.getTransformedMesh(), comp, 0)
1559-
if (temp === null) compound_error(i)
1556+
// if (temp === null) compound_error(i)
15601557
update_extent( self, temp )
15611558
}
15621559
} else {
15631560
temp = mesh.merge(o.getTransformedMesh(), o, 0)
1564-
if (temp === null) compound_error(i)
1561+
// if (temp === null) compound_error(i)
15651562
update_extent( self, temp )
15661563
}
15671564
o.visible = false

0 commit comments

Comments
 (0)