Skip to content

Commit 3e06359

Browse files
committed
Merged MeshData2 into MeshData.
Started to work on some clean up methods.
1 parent d02fea0 commit 3e06359

11 files changed

Lines changed: 65 additions & 60 deletions

File tree

example/test_obj.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void main() {
99
FlyingCamera fc = new FlyingCamera(camera); // W,A,S,D keys fly
1010
chronosGL.addAnimatable('flyingCamera', fc);
1111

12-
loadObj( "ct_logo.obj").then((MeshData2 md) {
12+
loadObj( "ct_logo.obj").then((MeshData md) {
1313

1414
Mesh mesh = md.createMesh();
1515
mesh.rotX(3.14/2);

example/test_sobel.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void main() {
99
FlyingCamera fc = new FlyingCamera(camera); // W,A,S,D keys fly
1010
chronosGL.addAnimatable('flyingCamera', fc);
1111

12-
loadObj( "ct_logo.obj").then((MeshData2 md) {
12+
loadObj( "ct_logo.obj").then((MeshData md) {
1313

1414
Mesh mesh = md.createMesh();
1515
mesh.rotX(3.14/2);

example/test_ssao.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void main() {
99
FlyingCamera fc = new FlyingCamera(camera); // W,A,S,D keys fly
1010
chronosGL.addAnimatable('flyingCamera', fc);
1111

12-
loadObj( "ct_logo.obj").then((MeshData2 md) {
12+
loadObj( "ct_logo.obj").then((MeshData md) {
1313

1414
Mesh mesh = md.createMesh();
1515
mesh.rotX(3.14/2);

lib/src/load_obj.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ part of chronosgl;
22

33
// only loads fully triangulated OBJ files
44

5-
Future<MeshData2> loadObj(String url)
5+
Future<MeshData> loadObj(String url)
66
{
77
Completer c = new Completer();
88
HTML.HttpRequest hr = new HTML.HttpRequest();
99
//hr.responseType = "arraybuffer";
1010
hr.open("GET", url);
1111
hr.onLoadEnd.listen( (e) {
1212

13-
MeshData2 result = doLoadObj( hr.response);
13+
MeshData result = doLoadObj( hr.response);
1414
c.complete(result);
1515

1616

@@ -19,10 +19,10 @@ Future<MeshData2> loadObj(String url)
1919
return c.future;
2020
}
2121

22-
MeshData2 doLoadObj(String text)
22+
MeshData doLoadObj(String text)
2323
{
24-
MeshData2 endResult = new MeshData2.empty();
25-
MeshData2 rawData = new MeshData2.empty();
24+
MeshData endResult = new MeshData.empty();
25+
MeshData rawData = new MeshData.empty();
2626

2727
Map<String, int> facemap = new Map<String, int>();
2828
int index = 0;

lib/src/mesh.dart

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,43 @@ class Mesh extends Node {
2525

2626
verticesBuffer = gl.createBuffer();
2727
gl.bindBuffer(ARRAY_BUFFER, verticesBuffer);
28-
gl.bufferDataTyped(ARRAY_BUFFER, meshData.vertices, STATIC_DRAW);
28+
gl.bufferDataTyped(ARRAY_BUFFER, meshData.vertices as Float32List, STATIC_DRAW);
2929

3030
if( meshData.textureCoords != null ) {
3131
textureCoordBuffer = gl.createBuffer();
3232
gl.bindBuffer(ARRAY_BUFFER, textureCoordBuffer);
33-
gl.bufferDataTyped(ARRAY_BUFFER, meshData.textureCoords, STATIC_DRAW);
33+
gl.bufferDataTyped(ARRAY_BUFFER, meshData.textureCoords as Float32List, STATIC_DRAW);
3434
}
3535

3636
if( meshData.normals != null ) {
3737
normalsBuffer = gl.createBuffer();
3838
gl.bindBuffer(ARRAY_BUFFER, normalsBuffer);
39-
gl.bufferDataTyped(ARRAY_BUFFER, meshData.normals, STATIC_DRAW);
39+
gl.bufferDataTyped(ARRAY_BUFFER, meshData.normals as Float32List, STATIC_DRAW);
4040
}
4141

4242
if( meshData.vertexIndices != null ) {
4343
numItems = meshData.vertexIndices.length;
4444
vertexIndexBuffer = gl.createBuffer();
4545
gl.bindBuffer(ELEMENT_ARRAY_BUFFER, vertexIndexBuffer);
46-
gl.bufferDataTyped(ELEMENT_ARRAY_BUFFER, meshData.vertexIndices, STATIC_DRAW);
46+
gl.bufferDataTyped(ELEMENT_ARRAY_BUFFER, meshData.vertexIndices as Uint16List, STATIC_DRAW);
4747
} else {
4848
numItems = meshData.vertices.length ~/ 3;
4949
}
5050

5151
}
52+
53+
void clearData() {
54+
gl.deleteBuffer( verticesBuffer);
55+
if( textureCoordBuffer != null ) {
56+
gl.deleteBuffer( textureCoordBuffer);
57+
}
58+
if( normalsBuffer != null ) {
59+
gl.deleteBuffer( normalsBuffer);
60+
}
61+
if( vertexIndexBuffer != null ) {
62+
gl.deleteBuffer( vertexIndexBuffer);
63+
}
64+
}
5265

5366
// this gets called by Node.draw()
5467
void draw2( ShaderProgram program) {

lib/src/mesh_data.dart

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,36 @@
11
part of chronosgl;
22

33
class MeshData {
4-
5-
Float32List vertices;
6-
Float32List textureCoords;
7-
Float32List normals;
8-
Uint16List vertexIndices;
9-
Texture texture;
10-
Texture texture2;
11-
12-
MeshData({ this.vertices, this.textureCoords, this.normals, this.vertexIndices, this.texture, this.texture2});
13-
14-
MeshData.empty(){
15-
vertices = new List<double>();
16-
textureCoords = new List<double>();
17-
normals = new List<double>();
18-
vertexIndices = new List<int>();
19-
}
20-
21-
Mesh createMesh() {
22-
return new Mesh( this);
23-
}
24-
25-
}
264

27-
class MeshData2 {
28-
295
List<double> vertices;
306
List<double> normals;
317
List<double> textureCoords;
328
List<int> vertexIndices;
339
Texture texture;
3410
Texture texture2;
35-
36-
MeshData2({ this.vertices, this.textureCoords, this.normals, this.vertexIndices, this.texture, this.texture2});
37-
38-
MeshData2.empty(){
11+
12+
MeshData({this.vertices, this.textureCoords, this.normals, this.vertexIndices, this.texture, this.texture2});
13+
14+
MeshData.empty() {
3915
vertices = new List<double>();
4016
textureCoords = new List<double>();
4117
normals = new List<double>();
4218
vertexIndices = new List<int>();
4319
}
44-
45-
MeshData createMeshData() {
46-
return new MeshData(
47-
vertices : new Float32List.fromList(vertices),
48-
textureCoords : new Float32List.fromList(textureCoords),
49-
normals : new Float32List.fromList(normals),
50-
vertexIndices : new Uint16List.fromList(vertexIndices),
51-
texture : texture,
52-
texture2 : texture2
53-
);
20+
21+
void optimize() {
22+
if (!(vertices is Float32List)) vertices = new Float32List.fromList(vertices);
23+
if ( textureCoords == null) textureCoords = new List<double>();
24+
if (!(textureCoords is Float32List)) textureCoords = new Float32List.fromList(textureCoords);
25+
if ( normals == null) normals = new List<double>();
26+
if (!(normals is Float32List)) normals = new Float32List.fromList(normals);
27+
if (!(vertexIndices is Uint16List)) vertexIndices = new Uint16List.fromList(vertexIndices);
5428
}
55-
29+
30+
5631
Mesh createMesh() {
57-
return new Mesh( createMeshData() );
32+
optimize();
33+
return new Mesh(this);
5834
}
59-
35+
6036
}

lib/src/node.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,20 @@ class Node extends Spatial {
1515
Matrix4 tempMatrix = new Matrix4();
1616

1717
Node( [Node child]) {
18-
1918
if( child!=null)
2019
children.add( child);
20+
}
21+
22+
void clear() {
23+
enabled=false;
24+
for ( Node node in children)
25+
{
26+
node.clear();
27+
}
28+
clearData();
29+
}
30+
31+
void clearData() {
2132

2233
}
2334

lib/src/shader/plasma_shader.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ part of chronosgl;
22

33
// http://www.bidouille.org/prog/plasma
44
// see also: https://www.shadertoy.com/view/XdX3Wn
5+
// and http://iq12.com/2013/04/fifty-shaders-of-grey/
56

67
ShaderObject createPlasmaShader() {
78
ShaderObject shaderObject = new ShaderObject("Plasma");

lib/src/shader_program.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ class ShaderProgram implements Drawable {
103103
return obj;
104104
}
105105

106+
bool remove( Node obj) {
107+
return objects.remove(obj);
108+
}
109+
106110
Node addFollowCameraObject( Node obj){
107111
followCameraObjects.add(obj);
108112
return obj;

lib/src/shapes/icosahedron.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
part of chronosgl;
22

3-
class Icosahedron extends MeshData2 {
3+
class Icosahedron extends MeshData {
44

55

66

@@ -25,7 +25,7 @@ class Icosahedron extends MeshData2 {
2525
return 0.5 - y / 2;
2626
}
2727

28-
void f3( int a, int b, int c, MeshData2 scope ) {
28+
void f3( int a, int b, int c, MeshData scope ) {
2929

3030
List<double> v1 = vertices.sublist(a*3, a*3+3);
3131
List<double> v2 = vertices.sublist(b*3, b*3+3);
@@ -59,7 +59,7 @@ class Icosahedron extends MeshData2 {
5959

6060
Icosahedron( [int subdivisions=0] ) : super.empty()
6161
{
62-
MeshData2 tempMeshData = new MeshData2.empty();
62+
MeshData tempMeshData = new MeshData.empty();
6363

6464
// create 12 vertices of a Icosahedron
6565

@@ -116,7 +116,7 @@ class Icosahedron extends MeshData2 {
116116

117117
for ( int i = 0; i < subdivisions; i ++ ) {
118118

119-
MeshData2 tempMeshData2 = new MeshData2.empty();
119+
MeshData tempMeshData2 = new MeshData.empty();
120120

121121
for ( int i = 0, l = tempMeshData.vertexIndices.length~/3; i < l; i ++ ) {
122122

@@ -143,7 +143,7 @@ class Icosahedron extends MeshData2 {
143143
vertexIndices = tempMeshData.vertexIndices;
144144
textureCoords = tempMeshData.textureCoords;
145145

146-
tempMeshData = new MeshData2.empty();
146+
tempMeshData = new MeshData.empty();
147147

148148
// we need to point vertexIndices to unique vertex and textureCoord combinations... if only OpenGL supported the obj file format natively...
149149
for ( int i = 0, l = vertexIndices.length; i < l; i ++ ) {

0 commit comments

Comments
 (0)