maierfelix / glmw

WebAssembly based Matrix and Vector library
https://maierfelix.github.io/glmw/
MIT License
200 stars 7 forks source link

Library not working as a drop-in replacement for glMatrix #6

Open DrPlantabyte opened 3 years ago

DrPlantabyte commented 3 years ago

Hi there!

I have a WebGL test page that I created using the glMatrix library, source code here: https://github.com/DrPlantabyte/WebGL-Play/blob/master/index.html

When I modified it to use GLMW instead, the mat4.translate and mat4.rotate functions don't seem to be working, source code here: https://github.com/DrPlantabyte/WebGL-Play/blob/with-wasm/index.html

All I did to change the code was wrap the mat4 object in mat4.view(obj) and add mat4.free(obj) afterwards (as you can see in this changeset ).

Am I missing something or using the API wrong?

Any help is appreciated and I hope this library is a success! Thanks!

DrPlantabyte commented 3 years ago

Here's a simpler example which uses both libraries to do the same perspective, translate, and rotate operations:

<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>
<script src="lib/gl-matrix-min.js" ></script>
<script src="lib/glmw-browser.js" ></script>
<script>
glmw.init().then(ready => {

const pi = Math.PI;
const fieldOfView = 45 * pi / 180;   //// in radians
const aspect = 600/400;
const zNear = 0.1; // depth clipping
const zFar = 100.0;// depth clipping
const time_ms = 101;
const period_ms = 4000;
const rotation = ((time_ms) % period_ms) / period_ms * pi;

// gl-matrix
// first set the camera perspective
console.log("Using glMatrix:")
var projectionMatrix = glMatrix.mat4.create();
console.log("New matrix: "+(projectionMatrix));
glMatrix.mat4.perspective(projectionMatrix, fieldOfView, aspect, zNear,zFar);
console.log("perspective: "+(projectionMatrix));

// now position the 3D model
var modelViewMatrix = glMatrix.mat4.create();
console.log("New matrix: "+(modelViewMatrix));
glMatrix.mat4.translate(modelViewMatrix, modelViewMatrix, [0.0, 0.0, -4]);
console.log("translate: "+(modelViewMatrix));
glMatrix.mat4.rotate(modelViewMatrix, modelViewMatrix, rotation,[0, 0, 1] );
console.log("rotate: "+(modelViewMatrix));

console.log("----------")
// glmw
// first set the camera perspective
console.log("Using GLMW:")
var projectionMatrix = glmw.mat4.create();
console.log("New matrix: "+glmw.mat4.str(projectionMatrix));
glmw.mat4.perspective(projectionMatrix, fieldOfView, aspect, zNear,zFar);
console.log("perspective: "+glmw.mat4.str(projectionMatrix));

// now position the 3D model
var modelViewMatrix = glmw.mat4.create();
console.log("New matrix: "+glmw.mat4.str(modelViewMatrix));
glmw.mat4.translate(modelViewMatrix, modelViewMatrix, [0.0, 0.0, -4]);
console.log("translate: "+glmw.mat4.str(modelViewMatrix));
glmw.mat4.rotate(modelViewMatrix, modelViewMatrix, rotation,[0, 0, 1] );
console.log("rotate: "+glmw.mat4.str(modelViewMatrix));

});
</script>
</body></html>

The output is as follows:

Using glMatrix:
New matrix: 1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1
perspective: 1.6094757318496704,0,0,0,0,2.4142136573791504,0,0,0,0,-1.0020020008087158,-1,0,0,-0.20020020008087158,0
New matrix: 1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1
translate: 1,0,0,0,0,1,0,0,0,0,1,0,0,0,-4,1
rotate: 0.9968553781509399,0.0792420506477356,0,0,-0.0792420506477356,0.9968553781509399,0,0,0,0,1,0,0,0,-4,1
----------
Using GLMW:
New matrix: mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)
perspective: mat4(1.6094756126403809, 0, 0, 0, 0, 2.4142134189605713, 0, 0, 0, 0, -1.0020020008087158, -1, 0, 0, -0.20020020008087158, 0)
New matrix: mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)
translate: mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1.8811030585096344e-41, 0, 1)
rotate: mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1.8811030585096344e-41, 0, 1)

As you can see, mat4.perspective works the same in both libraries, but neither mat4.translate nor mat4.rotate are storing the correct values in the matrix