From f2fc23232eb04f8dd68d9b534d290fc4e490af74 Mon Sep 17 00:00:00 2001 From: Raymond Hulha Date: Thu, 7 Aug 2014 23:56:48 +0200 Subject: [PATCH] added support for the OES_element_index_uint WebGL extension. Enable this with new ChronosGL('#webgl-canvas', useElementIndexUint: true); --- lib/chronosgl.dart | 12 +++++++++++- lib/src/mesh.dart | 11 +++++++++-- lib/src/mesh_data.dart | 6 +++++- lib/src/shapes/cube.dart | 2 +- lib/src/shapes/cylinder.dart | 2 +- lib/src/shapes/icosahedron.dart | 2 ++ lib/src/shapes/torusknot.dart | 2 +- lib/src/utils.dart | 12 ++++++------ pubspec.yaml | 2 +- 9 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lib/chronosgl.dart b/lib/chronosgl.dart index 7608260..12e866e 100644 --- a/lib/chronosgl.dart +++ b/lib/chronosgl.dart @@ -49,6 +49,9 @@ class ChronosGL { static RenderingContext globalGL; + static bool useElementIndexUint=false; + var elementIndexUintExt; + RenderingContext gl; Map programs = new Map(); @@ -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); @@ -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); diff --git a/lib/src/mesh.dart b/lib/src/mesh.dart index 2b3aee4..2a84ff7 100644 --- a/lib/src/mesh.dart +++ b/lib/src/mesh.dart @@ -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; @@ -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; } @@ -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) diff --git a/lib/src/mesh_data.dart b/lib/src/mesh_data.dart index eee28b9..ed0baa1 100644 --- a/lib/src/mesh_data.dart +++ b/lib/src/mesh_data.dart @@ -10,6 +10,7 @@ class MeshData { List 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}); @@ -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; } diff --git a/lib/src/shapes/cube.dart b/lib/src/shapes/cube.dart index 8f7c1c4..6e7fcbe 100644 --- a/lib/src/shapes/cube.dart +++ b/lib/src/shapes/cube.dart @@ -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); } \ No newline at end of file diff --git a/lib/src/shapes/cylinder.dart b/lib/src/shapes/cylinder.dart index b6b53e0..fe1834a 100644 --- a/lib/src/shapes/cylinder.dart +++ b/lib/src/shapes/cylinder.dart @@ -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); } diff --git a/lib/src/shapes/icosahedron.dart b/lib/src/shapes/icosahedron.dart index 50e5637..ffa4e41 100644 --- a/lib/src/shapes/icosahedron.dart +++ b/lib/src/shapes/icosahedron.dart @@ -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]; diff --git a/lib/src/shapes/torusknot.dart b/lib/src/shapes/torusknot.dart index f340d7f..063e46e 100644 --- a/lib/src/shapes/torusknot.dart +++ b/lib/src/shapes/torusknot.dart @@ -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); } diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 66318c5..12c23ee 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -73,24 +73,24 @@ class Utils Mesh getWall( Texture texture, int size) { - Float32List verts = new Float32List.fromList( [ + List 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 textureCoords = [ 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0 - ]); + ]; - Uint16List vertexIndices = new Uint16List.fromList( [ + List vertexIndices = [ 0, 1, 2, 0, 2, 3 - ]); + ]; return new Mesh( new MeshData(vertices:verts, textureCoords:textureCoords, vertexIndices:vertexIndices, texture:texture)); } diff --git a/pubspec.yaml b/pubspec.yaml index bb797a5..62cc3a5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: chronosgl -version: 1.1.0 +version: 1.2.1 author: rhulha description: A simple, minimal and elegant scene graph for WebGL written in Dart homepage: https://github.com/rhulha/ChronosGL