Skip to content
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

Separate Model and View Matrices. #6761

Merged
merged 35 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
2d9b090
separate model and view matrices
deveshidwivedi Jan 18, 2024
6eab6f7
Merge branch 'processing:main' into model-view
deveshidwivedi Jan 18, 2024
7267e59
Update package.json
deveshidwivedi Jan 18, 2024
0520b09
Merge branch 'processing:main' into model-view
deveshidwivedi Jan 19, 2024
c1d972f
Update p5.Shader.js
deveshidwivedi Jan 21, 2024
ee1962a
Update p5.Camera.js
deveshidwivedi Jan 21, 2024
35b5133
Update p5.Framebuffer.js
deveshidwivedi Jan 21, 2024
639c8cf
Update p5.RendererGL.js
deveshidwivedi Jan 21, 2024
85fff06
Update p5.Shader.js
deveshidwivedi Jan 21, 2024
d1d429d
reset method created
deveshidwivedi Jan 21, 2024
25e0ac3
Merge branch 'processing:main' into model-view
deveshidwivedi Jan 21, 2024
5dc6ccf
use .set method for matrix updates
deveshidwivedi Jan 24, 2024
7605bd3
use .set method for matrix updates
deveshidwivedi Jan 24, 2024
1a42091
Update p5.RendererGL.Retained.js
deveshidwivedi Jan 24, 2024
03e3ca7
Merge branch 'processing:main' into model-view
deveshidwivedi Jan 25, 2024
84800e2
Revert "Update package.json"
deveshidwivedi Jan 25, 2024
6571b07
Update package.json
deveshidwivedi Jan 26, 2024
2d9d70e
updated files
deveshidwivedi Jan 26, 2024
164f22b
Merge branch 'model-view' of https://github.com/deveshidwivedi/p5.js …
deveshidwivedi Jan 26, 2024
87bcde2
updated this._renderer. to this.
deveshidwivedi Jan 26, 2024
2ceca4e
undo changes to package-lock.json
deveshidwivedi Jan 27, 2024
f5baf36
updated to separate model and view
deveshidwivedi Feb 1, 2024
f851186
updated files
deveshidwivedi Feb 2, 2024
3fd2f4c
Update p5.RendererGL.Retained.js
deveshidwivedi Feb 2, 2024
88a91b0
Merge branch 'processing:main' into model-view
deveshidwivedi Feb 2, 2024
f70241b
Merge branch 'main' of https://github.com/deveshidwivedi/p5.js into m…
deveshidwivedi Feb 3, 2024
ac8bd07
fix errors
deveshidwivedi Feb 7, 2024
e06ee71
Merge branch 'processing:main' into model-view
deveshidwivedi Feb 7, 2024
a7ae6be
Merge branch 'model-view' of https://github.com/deveshidwivedi/p5.js …
deveshidwivedi Feb 7, 2024
b9828a7
updated tests
deveshidwivedi Feb 15, 2024
c97433a
updated files for failing tests
deveshidwivedi Feb 16, 2024
aee44d2
Merge branch 'processing:main' into model-view
deveshidwivedi Mar 30, 2024
0de03bb
update to fix failing tests
deveshidwivedi Mar 30, 2024
a8ebc4a
Merge branch 'main' into model-view
davepagurek May 21, 2024
9f2cdd1
Fix test
davepagurek May 21, 2024
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions src/webgl/3d_primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ p5.RendererGL.prototype.triangle = function(args) {
// this matrix multiplication transforms those two unit vectors
// onto the required vector prior to rendering, and moves the
// origin appropriately.
const uMVMatrix = this.uMVMatrix.copy();
const uModelMatrix = this.uModelMatrix.copy();
try {
// triangle orientation.
const orientation = Math.sign(x1*y2-x2*y1 + x2*y3-x3*y2 + x3*y1-x1*y3);
Expand All @@ -1255,13 +1255,13 @@ p5.RendererGL.prototype.triangle = function(args) {
x3 - x1, y3 - y1, 0, 0, // the resulting unit Y-axis
0, 0, orientation, 0, // the resulting unit Z-axis (Reflect the specified order of vertices)
x1, y1, 0, 1 // the resulting origin
]).mult(this.uMVMatrix);
]).mult(this.uModelMatrix);

this.uMVMatrix = mult;
this.uModelMatrix = mult;

this.drawBuffers(gId);
} finally {
this.uMVMatrix = uMVMatrix;
this.uModelMatrix = uModelMatrix;
}

return this;
Expand Down Expand Up @@ -1383,15 +1383,15 @@ p5.RendererGL.prototype.arc = function(args) {
this.createBuffers(gId, arcGeom);
}

const uMVMatrix = this.uMVMatrix.copy();
const uModelMatrix = this.uModelMatrix.copy();

try {
this.uMVMatrix.translate([x, y, 0]);
this.uMVMatrix.scale(width, height, 1);
this.uModelMatrix.translate([x, y, 0]);
this.uModelMatrix.scale(width, height, 1);

this.drawBuffers(gId);
} finally {
this.uMVMatrix = uMVMatrix;
this.uModelMatrix = uModelMatrix;
}

return this;
Expand Down Expand Up @@ -1443,14 +1443,14 @@ p5.RendererGL.prototype.rect = function(args) {
// opposite corners at (0,0) & (1,1).
//
// before rendering, this square is scaled & moved to the required location.
const uMVMatrix = this.uMVMatrix.copy();
const uModelMatrix = this.uModelMatrix.copy();
try {
this.uMVMatrix.translate([x, y, 0]);
this.uMVMatrix.scale(width, height, 1);
this.uModelMatrix.translate([x, y, 0]);
this.uModelMatrix.scale(width, height, 1);

this.drawBuffers(gId);
} finally {
this.uMVMatrix = uMVMatrix;
this.uModelMatrix = uModelMatrix;
}
} else {
// Use Immediate mode to round the rectangle corner,
Expand Down
8 changes: 3 additions & 5 deletions src/webgl/GeometryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GeometryBuilder {
this.renderer = renderer;
renderer._pInst.push();
this.identityMatrix = new p5.Matrix();
renderer.uMVMatrix = new p5.Matrix();
renderer.uModelMatrix = new p5.Matrix();
this.geometry = new p5.Geometry();
this.geometry.gid = `_p5_GeometryBuilder_${GeometryBuilder.nextGeometryId}`;
GeometryBuilder.nextGeometryId++;
Expand All @@ -25,7 +25,7 @@ class GeometryBuilder {
transformVertices(vertices) {
if (!this.hasTransform) return vertices;

return vertices.map(v => this.renderer.uMVMatrix.multiplyPoint(v));
return vertices.map(v => this.renderer.uModelMatrix.multiplyPoint(v));
}

/**
Expand All @@ -46,11 +46,9 @@ class GeometryBuilder {
* transformations.
*/
addGeometry(input) {
this.hasTransform = !this.renderer.uMVMatrix.mat4
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
.every((v, i) => v === this.identityMatrix.mat4[i]);

if (this.hasTransform) {
this.renderer.uNMatrix.inverseTranspose(this.renderer.uMVMatrix);
this.renderer.uNMatrix.inverseTranspose(this.renderer.uModelMatrix);
}

let startIdx = this.geometry.vertices.length;
Expand Down
4 changes: 2 additions & 2 deletions src/webgl/interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ p5.prototype._grid = function(size, numDivs, xOff, yOff, zOff) {
this._renderer.curStrokeColor[1] * 255,
this._renderer.curStrokeColor[2] * 255
);
this._renderer.uMVMatrix.set(this._renderer._curCamera.cameraMatrix);
this._renderer.uModelMatrix.reset();

// Lines along X axis
for (let q = 0; q <= numDivs; q++) {
Expand Down Expand Up @@ -733,7 +733,7 @@ p5.prototype._axesIcon = function(size, xOff, yOff, zOff) {

return function() {
this.push();
this._renderer.uMVMatrix.set(this._renderer._curCamera.cameraMatrix);
this._renderer.uModelMatrix.reset();

// X axis
this.strokeWeight(2);
Expand Down
10 changes: 5 additions & 5 deletions src/webgl/p5.Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,8 @@ p5.Camera = class Camera {
this.cameraMatrix.translate([tx, ty, tz]);

if (this._isActive()) {
this._renderer.uMVMatrix.set(this.cameraMatrix);
this._renderer.uModelMatrix.set(this.modelMatrix);
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
this._renderer.uViewMatrix.set(this.viewMatrix);
}
return this;
}
Expand Down Expand Up @@ -1599,13 +1600,12 @@ p5.Camera = class Camera {
this.cameraMatrix = cam.cameraMatrix.copy();
this.projMatrix = cam.projMatrix.copy();

// If the target camera is active, update uMVMatrix and uPMatrix.
if (this._isActive()) {
this._renderer.uMVMatrix.mat4 = this.cameraMatrix.mat4.slice();
this._renderer.uPMatrix.mat4 = this.projMatrix.mat4.slice();
this._renderer.uModelMatrix.reset();
this._renderer.uViewMatrix.set(this.cameraMatrix);
this._renderer.uPMatrix.set(this.projMatrix);
}
}

/**
* For the cameras cam0 and cam1 with the given arguments, their view are combined
* with the parameter amt that represents the quantity, and the obtained view is applied.
Expand Down
7 changes: 4 additions & 3 deletions src/webgl/p5.Framebuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,9 +842,10 @@ class Framebuffer {
// RendererGL.reset() does, but this does not try to clear any buffers;
// it only sets the camera.
this.target.setCamera(this.defaultCamera);
this.target._renderer.uMVMatrix.set(
this.target._renderer._curCamera.cameraMatrix
);
this.target.resetMatrix();
this.target._renderer.uViewMatrix
.set(this.target._renderer._curCamera.cameraMatrix);
this.target._renderer.uModelMatrix.set(this.target.modelMatrix);
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/webgl/p5.Matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ p5.Matrix = class {
return this;
}

reset() {
if (this.mat3) {
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
this.mat3.set([1, 0, 0, 0, 1, 0, 0, 0, 1]);
} else if (this.mat4) {
this.mat4.set([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
}
return this;
}

/**
* Replace the entire contents of a 4x4 matrix.
* If providing an array or a p5.Matrix, the values will be copied without
Expand Down
9 changes: 5 additions & 4 deletions src/webgl/p5.RendererGL.Retained.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,16 @@ p5.RendererGL.prototype.drawBuffersScaled = function(
scaleY,
scaleZ
) {
const uMVMatrix = this.uMVMatrix.copy();
let originalModelMatrix = this.uModelMatrix.copy();
try {
this.uMVMatrix.scale(scaleX, scaleY, scaleZ);
this.uModelMatrix.scale(scaleX, scaleY, scaleZ);

this.drawBuffers(gId);
} finally {
this.uMVMatrix = uMVMatrix;

this.uModelMatrix = originalModelMatrix;
}
};

p5.RendererGL.prototype._drawArrays = function(drawMode, gId) {
this.GL.drawArrays(
drawMode,
Expand Down
23 changes: 15 additions & 8 deletions src/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
* model view, projection, & normal
* matrices
*/
this.uModelMatrix = new p5.Matrix();
this.uViewMatrix = new p5.Matrix();
this.uMVMatrix = new p5.Matrix();
this.uPMatrix = new p5.Matrix();
this.uNMatrix = new p5.Matrix('mat3');
Expand Down Expand Up @@ -855,7 +857,8 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
_update() {
// reset model view and apply initial camera transform
// (containing only look at info; no projection).
this.uMVMatrix.set(this._curCamera.cameraMatrix);
this.uModelMatrix.reset();
this.uViewMatrix.set(this._curCamera.cameraMatrix);
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved

// reset light data for new frame.

Expand Down Expand Up @@ -1488,9 +1491,9 @@ p5.RendererGL = class RendererGL extends p5.Renderer {

applyMatrix(a, b, c, d, e, f) {
if (arguments.length === 16) {
p5.Matrix.prototype.apply.apply(this.uMVMatrix, arguments);
p5.Matrix.prototype.apply.apply(this.uModelMatrix, arguments);
} else {
this.uMVMatrix.apply([
this.uModelMatrix.apply([
a, b, 0, 0,
c, d, 0, 0,
0, 0, 1, 0,
Expand All @@ -1514,7 +1517,7 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
y = x.y;
x = x.x;
}
this.uMVMatrix.translate([x, y, z]);
this.uModelMatrix.translate([x, y, z]);
return this;
}

Expand All @@ -1527,15 +1530,15 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
* @chainable
*/
scale(x, y, z) {
this.uMVMatrix.scale(x, y, z);
this.uModelMatrix.scale(x, y, z);
return this;
}

rotate(rad, axis) {
if (typeof axis === 'undefined') {
return this.rotateZ(rad);
}
p5.Matrix.prototype.rotate.apply(this.uMVMatrix, arguments);
p5.Matrix.prototype.rotate.apply(this.uModelMatrix, arguments);
return this;
}

Expand All @@ -1561,10 +1564,13 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
// add webgl-specific style properties
const properties = style.properties;

properties.uMVMatrix = this.uMVMatrix.copy();
properties.uModelMatrix = this.uModelMatrix.copy();
properties.uViewMatrix = this.uViewMatrix.copy();
properties.uPMatrix = this.uPMatrix.copy();
properties._curCamera = this._curCamera;

this.uViewMatrix.set(this._curCamera.cameraMatrix);
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved

// make a copy of the current camera for the push state
// this preserves any references stored using 'createCamera'
this._curCamera = this._curCamera.copy();
Expand Down Expand Up @@ -1648,7 +1654,8 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
}
}
resetMatrix() {
this.uMVMatrix.set(this._curCamera.cameraMatrix);
this.uModelMatrix.reset();
this.uViewMatrix.set(this._curCamera.cameraMatrix);
return this;
}

Expand Down
4 changes: 3 additions & 1 deletion src/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,10 @@ p5.Shader = class {
}

_setMatrixUniforms() {
const modelMatrix = this._renderer.uModelMatrix;
const viewMatrix = this._renderer._curCamera.cameraMatrix;
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
const projectionMatrix = this._renderer.uPMatrix;
const modelViewMatrix = this._renderer.uMVMatrix;
const modelViewMatrix = (modelMatrix.copy()).mult(viewMatrix);
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved

const modelViewProjectionMatrix = modelViewMatrix.copy();
modelViewProjectionMatrix.mult(projectionMatrix);
Expand All @@ -334,6 +335,7 @@ p5.Shader = class {
}
this.setUniform('uViewMatrix', viewMatrix.mat4);
this.setUniform('uProjectionMatrix', projectionMatrix.mat4);
this.setUniform('uModelMatrix', modelMatrix.mat4);
this.setUniform('uModelViewMatrix', modelViewMatrix.mat4);
this.setUniform(
'uModelViewProjectionMatrix',
Expand Down
6 changes: 3 additions & 3 deletions test/unit/webgl/p5.Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,13 @@ suite('p5.Camera', function() {

// Confirmation that the argument camera and the matrix of the camera
// that received set() match
assert.deepEqual(copyCam.cameraMatrix.mat4, myCam.cameraMatrix.mat4);
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
assert.deepEqual(copyCam.modelMatrix.mat4, myCam.modelMatrix.mat4);
assert.deepEqual(copyCam.projMatrix.mat4, myCam.projMatrix.mat4);
// If the set()ed camera is active,
// the renderer's matrix will also change.
assert.deepEqual(
copyCam.cameraMatrix.mat4,
myp5._renderer.uMVMatrix.mat4
copyCam.modelMatrix.mat4,
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
myp5._renderer.uModelMatrix.mat4
);
assert.deepEqual(
copyCam.projMatrix.mat4,
Expand Down
16 changes: 13 additions & 3 deletions test/unit/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,23 @@ suite('p5.RendererGL', function() {
suite('push() and pop() work in WEBGL Mode', function() {
test('push/pop and translation works as expected in WEBGL Mode', function(done) {
myp5.createCanvas(100, 100, myp5.WEBGL);
var modelView = myp5._renderer.uMVMatrix.copy();
var modelMatrixBefore = myp5._renderer.uModelMatrix.copy();
var viewMatrixBefore = myp5._renderer.uViewMatrix.copy();

myp5.push();
myp5.rotateX(Math.random(0, 100));
myp5.translate(20, 100, 5);
assert.notEqual(modelView.mat4, myp5._renderer.uMVMatrix.mat4);
// Check if the model matrix has changed
assert.notDeepEqual(modelMatrixBefore.mat4,
myp5._renderer.uModelMatrix.mat4);
// Check if the view matrix has changed
assert.deepEqual(viewMatrixBefore.mat4,
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
myp5._renderer.uViewMatrix.mat4);
myp5.pop();
assert.deepEqual(modelView.mat4, myp5._renderer.uMVMatrix.mat4);
// Check if both the model and view matrices are restored after popping
assert.deepEqual(modelMatrixBefore.mat4,
myp5._renderer.uModelMatrix.mat4);
assert.deepEqual(viewMatrixBefore.mat4, myp5._renderer.uViewMatrix.mat4);
done();
});

Expand Down
12 changes: 8 additions & 4 deletions test/unit/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ suite('p5.Shader', function() {
var expectedAttributes = ['aPosition', 'aNormal', 'aTexCoord'];

var expectedUniforms = [
'uModelViewMatrix',
'uModelMatrix',
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
'uViewMatrix',
'uProjectionMatrix',
'uNormalMatrix',
'uAmbientLightCount',
Expand Down Expand Up @@ -108,7 +109,8 @@ suite('p5.Shader', function() {
var expectedAttributes = ['aPosition'];

var expectedUniforms = [
'uModelViewMatrix',
'uModelMatrix',
'uViewMatrix',
'uProjectionMatrix',
'uMaterialColor'
];
Expand All @@ -124,7 +126,8 @@ suite('p5.Shader', function() {
var expectedAttributes = ['aPosition', 'aVertexColor'];

var expectedUniforms = [
'uModelViewMatrix',
'uModelMatrix',
'uViewMatrix',
'uProjectionMatrix',
/*'uResolution',*/
'uPointSize'
Expand All @@ -141,7 +144,8 @@ suite('p5.Shader', function() {
var expectedAttributes = ['aPosition', 'aNormal'];

var expectedUniforms = [
'uModelViewMatrix',
'uModelMatrix',
'uViewMatrix',
'uProjectionMatrix',
'uNormalMatrix'
];
Expand Down
Loading