Skip to content

Commit

Permalink
fix torusknot camera example - #25
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmuth committed Jan 1, 2017
1 parent 3cbe0d4 commit bb8f01a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 53 deletions.
2 changes: 1 addition & 1 deletion example/torusknot_camera/torusknot_camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main() {
Material mat = new Material.Transparent("torus", new BlendEquation.Standard())
..SetUniform(uTextureSampler, generatedTexture)
..SetUniform(uColor, new VM.Vector3.zero());
Node m = new Node("torus", ShapeTorusKnot(chronosGL.gl), mat);
Node m = new Node("torus", ShapeTorusKnot(chronosGL.gl, useQuads:false), mat);

programBasic.add(m);

Expand Down
75 changes: 35 additions & 40 deletions lib/src/shapes/torusknot.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
part of shapes;


VM.Vector3 p1 = new VM.Vector3.zero();
VM.Vector3 p2 = new VM.Vector3.zero();

Expand All @@ -11,8 +10,8 @@ GeometryBuilder ShapeTorusKnotGeometry(
int segmentsT: 16,
int p: 2,
int q: 3,
double heightScale: 1.0}) {

double heightScale: 1.0,
useQuads: true}) {
List<VM.Vector3> vertices = [];

VM.Vector3 tang = new VM.Vector3.zero();
Expand Down Expand Up @@ -54,44 +53,40 @@ GeometryBuilder ShapeTorusKnotGeometry(
GeometryBuilder gb = new GeometryBuilder();
gb.EnableAttribute(aTextureCoordinates);

for (var i = 0; i < segmentsR; ++i) {
for (var j = 0; j < segmentsT; ++j) {
int ip = (i + 1) % segmentsR;
int jp = (j + 1) % segmentsT;
int a = grid[i][j];
int b = grid[ip][j];
int c = grid[ip][jp];
int d = grid[i][jp];

// TODO(rhulha): I change the texture compuation because the original
// did not seem correct but it got worse that way
VM.Vector2 uva = new VM.Vector2(i / segmentsR, j / segmentsT);
VM.Vector2 uvb = new VM.Vector2((i + 1) / segmentsR, j / segmentsT);
VM.Vector2 uvc = new VM.Vector2((i + 1) / segmentsR, (j + 1) / segmentsT);
VM.Vector2 uvd = new VM.Vector2(i / segmentsR, (j + 1) / segmentsT);

//uva[0] = 0; uva[1] = 0;
//uvb[0] = 1; uvb[1] = 0;
//uvc[0] = 1; uvc[1] = 1;
//uvd[0] = 0; uvd[1] = 1;
/*
uva[0] = 0.0;
uvb[0] = 1.0;
uvc[0] = 1.0;
uvd[0] = 0.0;
*/
gb.AddFaces4(1);
gb.AddVertices([vertices[a], vertices[b], vertices[c], vertices[d]]);
gb.AddAttributesVector2(aTextureCoordinates, [uva, uvb, uvc, uvd]);
for (int i = 0; i < segmentsR; ++i) {
for (int j = 0; j < segmentsT; ++j) {
final int ip = (i + 1) % segmentsR;
final int jp = (j + 1) % segmentsT;
final int a = grid[i][j];
final int b = grid[ip][j];
final int c = grid[ip][jp];
final int d = grid[i][jp];
final ip1 = i + 1;
final jp1 = j + 1;

/*
uvs.addAll(uva);
uvs.addAll(uvb);
uvs.addAll(uvc);
uvs.addAll(uvd);
uvs.addAll(uvc);
uvs.addAll(uvb);
*/
VM.Vector2 uva = new VM.Vector2(i / segmentsR, j / segmentsT);
VM.Vector2 uvb = new VM.Vector2(ip1 / segmentsR, j / segmentsT);
VM.Vector2 uvc = new VM.Vector2(ip1 / segmentsR, jp1 / segmentsT);
VM.Vector2 uvd = new VM.Vector2(i / segmentsR, jp1 / segmentsT);
*/
VM.Vector2 uva = new VM.Vector2(0.0, j / segmentsT);
VM.Vector2 uvb = new VM.Vector2(1.0, j / segmentsT);
VM.Vector2 uvc = new VM.Vector2(1.0, jp1 / segmentsT);
VM.Vector2 uvd = new VM.Vector2(0.0, jp1 / segmentsT);
if (useQuads) {
gb.AddFaces4(1);
gb.AddVertices([vertices[a], vertices[b], vertices[c], vertices[d]]);
gb.AddAttributesVector2(aTextureCoordinates, [uva, uvb, uvc, uvd]);
} else {
gb.AddFaces3(2);
gb.AddVertices([vertices[a], vertices[b], vertices[c]]);
gb.AddVertices([vertices[a], vertices[c], vertices[d]]);
// TODO: explain why this choice of uvs is more appealing
gb.AddAttributesVector2(aTextureCoordinates, [uva, uvb, uvc]);
gb.AddAttributesVector2(aTextureCoordinates, [uvd, uvc, uvb]);
//gb.AddAttributesVector2(aTextureCoordinates, [uva, uvc, uvd]);
}
}
}
return gb;
Expand Down
30 changes: 18 additions & 12 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Utils {
static HTML.CanvasElement createCanvas(
HTML.CanvasElement canvas, callback(HTML.CanvasRenderingContext2D ctx),
[int size = 512]) {
if (canvas == null) canvas =
new HTML.CanvasElement(width: size, height: size);
if (canvas == null)
canvas = new HTML.CanvasElement(width: size, height: size);
HTML.CanvasRenderingContext2D context = canvas.getContext('2d');
callback(context);
return canvas;
Expand Down Expand Up @@ -79,8 +79,9 @@ class Utils {
}

static Node MakeSkycube(gl, Texture cubeTexture) {
Material mat = new Material("skycube")..SetUniform(uTextureCubeSampler, cubeTexture);
MeshData md = ShapeCube(gl, x:512.0, y:512.0, z:512.0);
Material mat = new Material("skycube")
..SetUniform(uTextureCubeSampler, cubeTexture);
MeshData md = ShapeCube(gl, x: 512.0, y: 512.0, z: 512.0);
return new Node("skycube", md, mat);
}

Expand All @@ -90,7 +91,7 @@ class Utils {
Material mm(String middle) {
return new Material()..SetUniform(uTextureSampler, textureCache.getTW(prefix + middle + suffix).texture);
}
Mesh skybox_nx = createQuad(mm(nx), 1004);
skybox_nx.setPos(-2.0, 2.0, -1000.0);
chronosGL.programBasic.addFollowCameraObject(skybox_nx);
Expand Down Expand Up @@ -161,16 +162,19 @@ class Utils {
p: p,
q: q,
heightScale: heightScale), mat);
}
}
*/

static Node MakeParticles(WEBGL.RenderingContext gl, int numPoints, [int dimension = 100]) {
return MakePointSprites(gl, numPoints, createParticleTexture(gl), dimension);
static Node MakeParticles(WEBGL.RenderingContext gl, int numPoints,
[int dimension = 100]) {
return MakePointSprites(
gl, numPoints, createParticleTexture(gl), dimension);
}

static int id = 1;

static Node MakePointSprites(WEBGL.RenderingContext gl, int numPoints, Texture texture,
static Node MakePointSprites(
WEBGL.RenderingContext gl, int numPoints, Texture texture,
[int dimension = 500]) {
// TODO: make this asynchronous (async/await?)
Math.Random rand = new Math.Random();
Expand Down Expand Up @@ -277,19 +281,21 @@ MeshData ShapeTorusKnot(WEBGL.RenderingContext gl,
int segmentsT: 16,
int p: 2,
int q: 3,
double heightScale: 1.0}) {
double heightScale: 1.0,
useQuads: true}) {
GeometryBuilder gb = ShapeTorusKnotGeometry(
radius: radius,
tube: tube,
segmentsR: segmentsR,
segmentsT: segmentsT,
p: p,
q: q,
heightScale: heightScale);
heightScale: heightScale,
useQuads: useQuads);
return GeometryBuilderToMeshData("torusknot", gl, gb);
}

MeshData ShapeQuad(WEBGL.RenderingContext gl, int size) {
GeometryBuilder gb = QuadGeometry(size);
return GeometryBuilderToMeshData("quad", gl, gb);
}
}

0 comments on commit bb8f01a

Please sign in to comment.