Giter Club home page Giter Club logo

Comments (11)

toji avatar toji commented on August 22, 2024

I was thinking about this on my way in to work this morning. :) Completely agree on all points. I'm sure someone somewhere will gripe about needing to type a bit more, but it would bring a lot of consistency to the use of the library, further enforce best practices, and be faster to boot! Can't really see a downside!

from gl-matrix.

jrus avatar jrus commented on August 22, 2024

One way to avoid too much extra typing would be to make the names like vec3, mat4, etc. directly into the creation functions for the relevant objects. Since in Javascript, functions can still have arbitrary properties assigned, nothing else would change. So:

eye4 = mat4.identity(mat4());

w = vec3.add(u, v, vec3());
// ... or alternately
vec3.add(u, v, u);

from gl-matrix.

jrus avatar jrus commented on August 22, 2024

If you do make the change so that dest is always required, I would also change it so that the creation function never takes an argument. So under the scheme suggested above, replace..

v = mat3.create(u);

with

v = mat3.set(u, mat3());

or to keep the current creator function name

v = mat3.set(u, mat3.create());

or maybe that change would just seem unnecessarily obnoxious for library consumers.

from gl-matrix.

sinisterchipmunk avatar sinisterchipmunk commented on August 22, 2024

I don't see a problem with mat3() as a shortcut to mat3.create. I'd lean towards keeping one as an alias for the other, rather than replacing one with the other.

As for constructor arguments, I like being able to initialize it with a value. I do this in dozens of places, particularly during app initialization: position = vec3.create([0, 0, -5]);. The create function is one (the only?) function I wouldn't mind keeping the optional argument to, although I'd be just as happy to initialize it without having to build a junk array in the process, like so: position = vec3.create(0, 0, -5);.

from gl-matrix.

sinisterchipmunk avatar sinisterchipmunk commented on August 22, 2024

For the record, there'd be no performance loss for turning each namespace into a function. http://jsperf.com/access-function-properties-vs-object-properties

from gl-matrix.

jrus avatar jrus commented on August 22, 2024

I'd lean towards keeping one as an alias for the other

Oh absolutely. To do otherwise would be a really mean break in backwards-compat.

Actually, maybe make functions like mat3() take no arguments, and then mat3.create(foo) could be used for initializing with a value.

Or as you say, the “bare” functions could be instead aliases for the current createFrom functions, but with arguments optional. So you could have: position = vec3(0, 0, -5), or zeros = mat3().

from gl-matrix.

jrus avatar jrus commented on August 22, 2024

Here’s what the homepage examples could look like

// creating a perspective matrix
var persp = mat4.perspective(45, 4/3, 1, 100, mat4());

// performing multiple transforms on a matrix
var modelView = mat4.identity(mat4());                   // Start with identity

mat4.translate(modelView, [0, 0, -10], modelView);       // Translate back 10 units
mat4.rotate(modelView, Math.PI/2, [0, 1, 0], modelView); // Rotate 90 degrees around the Y axis
mat4.scale(modelView, [2, 2, 2], modelView);             // Scale by 200%

// matrices can be passed directly to WebGL
gl.uniformMatrix4fv(modelViewUniform, false, modelView);

// updating a destination matrix  ... modelViewPersp = modelView * persp 
var modelViewPersp = mat4.multiply(modelView, persp, mat4());

// transforming a point
var cameraPos = vec3(0, 0, 0);

mat4.multiplyVec3(modelView, cameraPos, cameraPos); // result written into cameraPos
var newPos = mat4.multiplyVec3(modelView, cameraPos, vec3());

from gl-matrix.

jrus avatar jrus commented on August 22, 2024

By the way, the last couple lines of example on the current front page save the output of mat4.multiplyVec3(...) into the two regular Javascript arrays, instead of into whatever type is specified to be a MatrixArray. Is that intentional?

from gl-matrix.

toji avatar toji commented on August 22, 2024

Yes, that was absolutely intentional. glMatrix works with any array-like object that contains enough elements to represent the given type. So a JS array of 16 values can be used as a 4x4 matrix just as easily as one that you get from mat4.create();

from gl-matrix.

jrus avatar jrus commented on August 22, 2024

Okay. Probably worth saying so explicitly in the text around the example. So (still using this proposed new API with required destination), something like:

// Transforming a point. Note that regular arrays of Javascript numbers
// can be used as the "destination" for all functions in this API.
var cameraPos = [0, 0, 0],
    newPos = [0, 0, 0];

mat4.multiplyVec3(modelView, cameraPos, cameraPos);  // result written into cameraPos
mat4.multiplyVec3(modelView, cameraPos, newPos);     // result written into newPos

from gl-matrix.

toji avatar toji commented on August 22, 2024

This request is being addressed in the 2.0 revision, described in #43.

from gl-matrix.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.