Skip to content

Commit f2fc232

Browse files
committed
added support for the OES_element_index_uint WebGL extension. Enable this with new ChronosGL('#webgl-canvas', useElementIndexUint: true);
1 parent b69fed9 commit f2fc232

File tree

9 files changed

+37
-14
lines changed

9 files changed

+37
-14
lines changed

lib/chronosgl.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class ChronosGL
4949
{
5050

5151
static RenderingContext globalGL;
52+
static bool useElementIndexUint=false;
53+
var elementIndexUintExt;
54+
5255
RenderingContext gl;
5356

5457
Map<String, ShaderProgram> programs = new Map<String, ShaderProgram>();
@@ -71,7 +74,8 @@ class ChronosGL
7174
num near=0.1;
7275
num far=1000;
7376

74-
ChronosGL(String canvasID, {bool transparent:false, bool useFramebuffer:false, ShaderObject fxShader, this.near:0.1, this.far:1000.0})
77+
78+
ChronosGL(String canvasID, {bool transparent:false, bool useFramebuffer:false, ShaderObject fxShader, this.near:0.1, this.far:1000.0, bool useElementIndexUint:false})
7579
{
7680
_canvas = HTML.document.querySelector(canvasID);
7781

@@ -87,6 +91,12 @@ class ChronosGL
8791
}
8892
ChronosGL.globalGL = gl;
8993

94+
if( useElementIndexUint) {
95+
elementIndexUintExt = gl.getExtension("OES_element_index_uint");
96+
if(elementIndexUintExt==null) { throw "Error: OES_element_index_uint is not supported"; }
97+
ChronosGL.useElementIndexUint = useElementIndexUint;
98+
}
99+
90100
//print( gl.getSupportedExtensions());
91101

92102
gl.clearColor(0.0, 0.0, 0.0, 1.0);

lib/src/mesh.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class Mesh extends Node {
1818
int numItems;
1919

2020
Mesh( MeshData meshData, [this.drawPoints=false]) {
21+
22+
if( !meshData.isOptimized)
23+
meshData.optimize();
24+
2125
this.texture = meshData.texture;
2226
this.texture2 = meshData.texture2;
2327

@@ -55,7 +59,10 @@ class Mesh extends Node {
5559
numItems = meshData.vertexIndices.length;
5660
vertexIndexBuffer = gl.createBuffer();
5761
gl.bindBuffer(ELEMENT_ARRAY_BUFFER, vertexIndexBuffer);
58-
gl.bufferDataTyped(ELEMENT_ARRAY_BUFFER, meshData.vertexIndices as Uint16List, STATIC_DRAW);
62+
if(ChronosGL.useElementIndexUint)
63+
gl.bufferDataTyped(ELEMENT_ARRAY_BUFFER, meshData.vertexIndices as Uint32List, STATIC_DRAW);
64+
else
65+
gl.bufferDataTyped(ELEMENT_ARRAY_BUFFER, meshData.vertexIndices as Uint16List, STATIC_DRAW);
5966
} else {
6067
numItems = meshData.vertices.length ~/ 3;
6168
}
@@ -147,7 +154,7 @@ class Mesh extends Node {
147154
gl.drawArrays(TRIANGLES, 0, numItems);
148155
} else {
149156
gl.bindBuffer(ELEMENT_ARRAY_BUFFER, vertexIndexBuffer);
150-
gl.drawElements(TRIANGLES, numItems, UNSIGNED_SHORT, 0);
157+
gl.drawElements(TRIANGLES, numItems, ChronosGL.useElementIndexUint ? UNSIGNED_INT : UNSIGNED_SHORT, 0);
151158
}
152159

153160
if( debug)

lib/src/mesh_data.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class MeshData {
1010
List<int> vertexIndices;
1111
Texture texture;
1212
Texture texture2;
13+
bool isOptimized=false;
1314

1415
MeshData({this.vertices, this.colors, this.textureCoords, this.normals, this.binormals, this.vertexIndices, this.texture, this.texture2});
1516

@@ -33,7 +34,10 @@ class MeshData {
3334

3435
if ( binormals != null && !(binormals is Float32List)) binormals = new Float32List.fromList(binormals);
3536

36-
if (!(vertexIndices is Uint16List)) vertexIndices = new Uint16List.fromList(vertexIndices);
37+
if ( vertexIndices != null && !(vertexIndices is TypedData)) {
38+
vertexIndices = ChronosGL.useElementIndexUint ? new Uint32List.fromList(vertexIndices) : new Uint16List.fromList(vertexIndices);
39+
}
40+
isOptimized=true;
3741
}
3842

3943

lib/src/shapes/cube.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,6 @@ MeshData createCubeInternal( [Texture texture]) {
116116
20, 21, 22, 20, 22, 23 // Left face
117117
];
118118

119-
return new MeshData(vertices : new Float32List.fromList(vertices), normals : new Float32List.fromList(normals), textureCoords : new Float32List.fromList(uvs), vertexIndices : new Uint16List.fromList(vertIndices), texture : texture);
119+
return new MeshData(vertices : vertices, normals : normals, textureCoords : uvs, vertexIndices : vertIndices, texture : texture);
120120

121121
}

lib/src/shapes/cylinder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ MeshData createCylinder( double radius, double height, int radialSubdivisions, [
6060
}
6161

6262

63-
return new MeshData(vertices : new Float32List.fromList(vertices), textureCoords : new Float32List.fromList(uvs), vertexIndices : new Uint16List.fromList(vertIndices), texture : texture);
63+
return new MeshData(vertices : vertices, textureCoords : uvs, vertexIndices : vertIndices, texture : texture);
6464

6565
}
6666

lib/src/shapes/icosahedron.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ class Icosahedron extends MeshData {
155155

156156
vertexIndices = tempMeshData.vertexIndices;
157157
vertices = tempMeshData.vertices;
158+
159+
optimize();
158160

159161
// for ( int i = 0, l = vertexIndices.length; i < l; i ++ ) {
160162
// int index = vertexIndices[i];

lib/src/shapes/torusknot.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ MeshData createTorusKnotInternal( {double radius:20.0, double tube:4.0, int seg
8282
}
8383
}
8484

85-
return new MeshData(vertices : new Float32List.fromList(vertices), textureCoords : new Float32List.fromList(uvs), vertexIndices : new Uint16List.fromList(indices), texture : texture);
85+
return new MeshData(vertices : vertices, textureCoords : uvs, vertexIndices : indices, texture : texture);
8686
}
8787

8888

lib/src/utils.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,24 @@ class Utils
7373

7474
Mesh getWall( Texture texture, int size)
7575
{
76-
Float32List verts = new Float32List.fromList( [
76+
List<double> verts = [
7777
-1.0*size, -1.0*size, 0.0,
7878
1.0*size, -1.0*size, 0.0,
7979
1.0*size, 1.0*size, 0.0,
8080
-1.0*size, 1.0*size, 0.0
81-
]);
81+
];
8282

83-
Float32List textureCoords = new Float32List.fromList( [
83+
List<double> textureCoords = [
8484
0.0, 0.0,
8585
1.0, 0.0,
8686
1.0, 1.0,
8787
0.0, 1.0
88-
]);
88+
];
8989

90-
Uint16List vertexIndices = new Uint16List.fromList( [
90+
List<int> vertexIndices = [
9191
0, 1, 2,
9292
0, 2, 3
93-
]);
93+
];
9494
return new Mesh( new MeshData(vertices:verts, textureCoords:textureCoords, vertexIndices:vertexIndices, texture:texture));
9595
}
9696

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: chronosgl
2-
version: 1.1.0
2+
version: 1.2.1
33
author: rhulha <java4life@gmail.com>
44
description: A simple, minimal and elegant scene graph for WebGL written in Dart
55
homepage: https://github.com/rhulha/ChronosGL

0 commit comments

Comments
 (0)