Code Monkey home page Code Monkey logo

gl-matrix's Introduction

glMatrix

With the the increasing popularity of WebGL comes the need for javascript libraries that handle matrix and vector operations. glMatrix is designed to handle those operations at stupidly fast speeds!

General Matrix Operations

Most matrix operations share a similar format:

mat4.operation(srcMatrix, otherOperands, destMatrix (optional));

For all functions following this format the operation will be applied to the values in srcMatrix and the result will be written into destMatrix, which will also be returned. If destMatrix is not specified the result will be written into srcMatrix, if destMatrix is specified srcMatrix will not be altered.

Any 4x4 matrix functions expect sequences at least 16 elements in length as inputs when taking a matrix.

Function Documentation

Documentation for the individual functions can be found here

Examples

Creating a perspective matrix

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

Performing multiple transforms on a matrix

var modelView = mat4.create();

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

Matricies created with glMatrix don't need to undergo any transformation to be used with WebGL. They can be passed directly

gl.uniformMatrix4fv(modelViewUniform, false, modelView);

Updating a destination matrix

var modelViewPersp = mat4.create();

mat4.multiply(modelView, persp, modelViewPersp); // Sets modelViewPersp to modelView * persp 

Tranforming a point

var cameraPos = [0, 0, 0];
var newPos = [0, 0, 0];

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

gl-matrix's People

Contributors

sinisterchipmunk avatar toji avatar abberg avatar jwagner avatar kos avatar coreh avatar

Watchers

Eric Karl avatar

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.