Skip to content

Commit

Permalink
added support for the OES_element_index_uint WebGL extension. Enable …
Browse files Browse the repository at this point in the history
…this with new ChronosGL('#webgl-canvas', useElementIndexUint: true);
  • Loading branch information
rhulha committed Aug 7, 2014
1 parent b69fed9 commit f2fc232
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 14 deletions.
12 changes: 11 additions & 1 deletion lib/chronosgl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class ChronosGL
{

static RenderingContext globalGL;
static bool useElementIndexUint=false;
var elementIndexUintExt;

RenderingContext gl;

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

ChronosGL(String canvasID, {bool transparent:false, bool useFramebuffer:false, ShaderObject fxShader, this.near:0.1, this.far:1000.0})

ChronosGL(String canvasID, {bool transparent:false, bool useFramebuffer:false, ShaderObject fxShader, this.near:0.1, this.far:1000.0, bool useElementIndexUint:false})
{
_canvas = HTML.document.querySelector(canvasID);

Expand All @@ -87,6 +91,12 @@ class ChronosGL
}
ChronosGL.globalGL = gl;

if( useElementIndexUint) {
elementIndexUintExt = gl.getExtension("OES_element_index_uint");
if(elementIndexUintExt==null) { throw "Error: OES_element_index_uint is not supported"; }
ChronosGL.useElementIndexUint = useElementIndexUint;
}

//print( gl.getSupportedExtensions());

gl.clearColor(0.0, 0.0, 0.0, 1.0);
Expand Down
11 changes: 9 additions & 2 deletions lib/src/mesh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class Mesh extends Node {
int numItems;

Mesh( MeshData meshData, [this.drawPoints=false]) {

if( !meshData.isOptimized)
meshData.optimize();

this.texture = meshData.texture;
this.texture2 = meshData.texture2;

Expand Down Expand Up @@ -55,7 +59,10 @@ class Mesh extends Node {
numItems = meshData.vertexIndices.length;
vertexIndexBuffer = gl.createBuffer();
gl.bindBuffer(ELEMENT_ARRAY_BUFFER, vertexIndexBuffer);
gl.bufferDataTyped(ELEMENT_ARRAY_BUFFER, meshData.vertexIndices as Uint16List, STATIC_DRAW);
if(ChronosGL.useElementIndexUint)
gl.bufferDataTyped(ELEMENT_ARRAY_BUFFER, meshData.vertexIndices as Uint32List, STATIC_DRAW);
else
gl.bufferDataTyped(ELEMENT_ARRAY_BUFFER, meshData.vertexIndices as Uint16List, STATIC_DRAW);
} else {
numItems = meshData.vertices.length ~/ 3;
}
Expand Down Expand Up @@ -147,7 +154,7 @@ class Mesh extends Node {
gl.drawArrays(TRIANGLES, 0, numItems);
} else {
gl.bindBuffer(ELEMENT_ARRAY_BUFFER, vertexIndexBuffer);
gl.drawElements(TRIANGLES, numItems, UNSIGNED_SHORT, 0);
gl.drawElements(TRIANGLES, numItems, ChronosGL.useElementIndexUint ? UNSIGNED_INT : UNSIGNED_SHORT, 0);
}

if( debug)
Expand Down
6 changes: 5 additions & 1 deletion lib/src/mesh_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MeshData {
List<int> vertexIndices;
Texture texture;
Texture texture2;
bool isOptimized=false;

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

Expand All @@ -33,7 +34,10 @@ class MeshData {

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

if (!(vertexIndices is Uint16List)) vertexIndices = new Uint16List.fromList(vertexIndices);
if ( vertexIndices != null && !(vertexIndices is TypedData)) {
vertexIndices = ChronosGL.useElementIndexUint ? new Uint32List.fromList(vertexIndices) : new Uint16List.fromList(vertexIndices);
}
isOptimized=true;
}


Expand Down
2 changes: 1 addition & 1 deletion lib/src/shapes/cube.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,6 @@ MeshData createCubeInternal( [Texture texture]) {
20, 21, 22, 20, 22, 23 // Left face
];

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);
return new MeshData(vertices : vertices, normals : normals, textureCoords : uvs, vertexIndices : vertIndices, texture : texture);

}
2 changes: 1 addition & 1 deletion lib/src/shapes/cylinder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ MeshData createCylinder( double radius, double height, int radialSubdivisions, [
}


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

}

2 changes: 2 additions & 0 deletions lib/src/shapes/icosahedron.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ class Icosahedron extends MeshData {

vertexIndices = tempMeshData.vertexIndices;
vertices = tempMeshData.vertices;

optimize();

// for ( int i = 0, l = vertexIndices.length; i < l; i ++ ) {
// int index = vertexIndices[i];
Expand Down
2 changes: 1 addition & 1 deletion lib/src/shapes/torusknot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ MeshData createTorusKnotInternal( {double radius:20.0, double tube:4.0, int seg
}
}

return new MeshData(vertices : new Float32List.fromList(vertices), textureCoords : new Float32List.fromList(uvs), vertexIndices : new Uint16List.fromList(indices), texture : texture);
return new MeshData(vertices : vertices, textureCoords : uvs, vertexIndices : indices, texture : texture);
}


Expand Down
12 changes: 6 additions & 6 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,24 @@ class Utils

Mesh getWall( Texture texture, int size)
{
Float32List verts = new Float32List.fromList( [
List<double> verts = [
-1.0*size, -1.0*size, 0.0,
1.0*size, -1.0*size, 0.0,
1.0*size, 1.0*size, 0.0,
-1.0*size, 1.0*size, 0.0
]);
];

Float32List textureCoords = new Float32List.fromList( [
List<double> textureCoords = [
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0
]);
];

Uint16List vertexIndices = new Uint16List.fromList( [
List<int> vertexIndices = [
0, 1, 2,
0, 2, 3
]);
];
return new Mesh( new MeshData(vertices:verts, textureCoords:textureCoords, vertexIndices:vertexIndices, texture:texture));
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: chronosgl
version: 1.1.0
version: 1.2.1
author: rhulha <java4life@gmail.com>
description: A simple, minimal and elegant scene graph for WebGL written in Dart
homepage: https://github.com/rhulha/ChronosGL
Expand Down

0 comments on commit f2fc232

Please sign in to comment.