Skip to content

Fix vertex ordering for beginShape(QUADS) in WebGL mode #5771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/webgl/p5.RendererGL.Immediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,20 @@ p5.RendererGL.prototype.vertex = function(x, y) {
if (this.immediateMode.shapeMode === constants.QUADS) {
// A finished quad turned into triangles should leave 6 vertices in the
// buffer:
// 0--2 0--2 3
// | | --> | / / |
// 1--3 1 4--5
// 0--3 0 3--5
// | | --> | \ \ |
// 1--2 1--2 4
// When vertex index 3 is being added, add the necessary duplicates.
if (this.immediateMode.geometry.vertices.length % 6 === 3) {
for (const key in immediateBufferStrides) {
const stride = immediateBufferStrides[key];
const buffer = this.immediateMode.geometry[key];
buffer.push(
...buffer.slice(buffer.length - stride, buffer.length),
...buffer.slice(buffer.length - 2 * stride, buffer.length - stride)
...buffer.slice(
buffer.length - 3 * stride,
buffer.length - 2 * stride
),
...buffer.slice(buffer.length - stride, buffer.length)
);
}
}
Expand Down Expand Up @@ -266,14 +269,14 @@ p5.RendererGL.prototype._calculateEdges = function(
break;
case constants.QUADS:
// Quads have been broken up into two triangles by `vertex()`:
// 0---2 3
// | / / |
// 1 4---5
// 0 3--5
// | \ \ |
// 1--2 4
for (i = 0; i < verts.length - 5; i += 6) {
res.push([i, i + 1]);
res.push([i, i + 2]);
res.push([i + 4, i + 5]);
res.push([i + 1, i + 2]);
res.push([i + 3, i + 5]);
res.push([i + 4, i + 5]);
}
break;
case constants.QUAD_STRIP:
Expand Down
18 changes: 17 additions & 1 deletion test/manual-test-examples/webgl/geometryImmediate/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,23 @@ function drawStrip(mode) {
beginShape(mode);
let vertexIndex = 0;
for (let y = 0; y <= 500; y += 100) {
for (const side of [-1, 1]) {
let sides = [-1, 1];
if (mode === QUADS && y % 200 !== 0) {
// QUAD_STRIP and TRIANGLE_STRIP need the vertices of each shared side
// ordered in the same way:
// 0--2--4--6
// | | | | ⬇️
// 1--3--5--7
//
// ...but QUADS orders vertices in a consisten CCW or CW manner around
// each quad, meaning each side will be in the reverse order of the
// previous:
// 0--3 4--7
// | | | | 🔄
// 1--2 5--6
sides.reverse();
}
for (const side of sides) {
fill(...stripColors[vertexIndex]);
vertex(side * 40, y);
vertexIndex++;
Expand Down
16 changes: 8 additions & 8 deletions test/unit/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,16 +560,16 @@ suite('p5.RendererGL', function() {
[0, 1, 1],
[1, 0, 2],

[0, 0, 0],
[1, 0, 2],
[0, 1, 1],
[1, 1, 3],

[2, 0, 4],
[2, 1, 5],
[3, 0, 6],

[2, 0, 4],
[3, 0, 6],
[2, 1, 5],
[3, 1, 7]
];
assert.equal(
Expand All @@ -587,16 +587,16 @@ suite('p5.RendererGL', function() {
[0, 1],
[1, 0],

[0, 0],
[1, 0],
[0, 1],
[1, 1],

[0, 0],
[0, 1],
[1, 0],

[0, 0],
[1, 0],
[0, 1],
[1, 1]
].flat();
assert.deepEqual(renderer.immediateMode.geometry.uvs, expectedUVs);
Expand All @@ -606,16 +606,16 @@ suite('p5.RendererGL', function() {
[0, 1, 0, 1],
[0, 0, 1, 1],

[1, 0, 0, 1],
[0, 0, 1, 1],
[0, 1, 0, 1],
[1, 0, 1, 1],

[1, 0, 0, 1],
[0, 1, 0, 1],
[0, 0, 1, 1],

[1, 0, 0, 1],
[0, 0, 1, 1],
[0, 1, 0, 1],
[1, 0, 1, 1]
].flat();
assert.deepEqual(
Expand All @@ -628,16 +628,16 @@ suite('p5.RendererGL', function() {
[3, 4, 5],
[6, 7, 8],

[0, 1, 2],
[6, 7, 8],
[3, 4, 5],
[9, 10, 11],

[12, 13, 14],
[15, 16, 17],
[18, 19, 20],

[12, 13, 14],
[18, 19, 20],
[15, 16, 17],
[21, 22, 23]
];
assert.equal(
Expand Down