matrix4x4.h (3464B)
1 /* 2 * Copyright (c) 2009, Mozilla Corp 3 * Copyright (c) 2012, Google, Inc. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * * Neither the name of the <organization> nor the 14 * names of its contributors may be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * Based on sample code from the OpenGL(R) ES 2.0 Programming Guide, which carriers 31 * the following header: 32 * 33 * Book: OpenGL(R) ES 2.0 Programming Guide 34 * Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner 35 * ISBN-10: 0321502795 36 * ISBN-13: 9780321502797 37 * Publisher: Addison-Wesley Professional 38 * URLs: http://safari.informit.com/9780321563835 39 * http://www.opengles-book.com 40 */ 41 42 /* 43 * Ported from JavaScript to C by Behdad Esfahbod, 2012. 44 * Added MultMatrix. Converting from fixed-function OpenGL matrix 45 * operations to these functions should be as simple as renaming the 46 * 'gl' prefix to 'm4' and adding the matrix argument to the call. 47 * 48 * The C version lives at http://code.google.com/p/matrix4x4-c/ 49 */ 50 51 /* 52 * A simple 4x4 matrix utility implementation 53 */ 54 55 #ifndef MATRIX4x4_H 56 #define MATRIX4x4_H 57 58 /* Copies other matrix into mat */ 59 float * 60 m4Copy (float *mat, const float *other); 61 62 float * 63 m4Multiply (float *mat, const float *right); 64 65 float * 66 m4MultMatrix (float *mat, const float *left); 67 68 float 69 m4Get (float *mat, unsigned int row, unsigned int col); 70 71 float * 72 m4Scale (float *mat, float sx, float sy, float sz); 73 74 float * 75 m4Translate (float *mat, float tx, float ty, float tz); 76 77 float * 78 m4Rotate (float *mat, float angle, float x, float y, float z); 79 80 float * 81 m4Frustum (float *mat, float left, float right, float bottom, float top, float nearZ, float farZ); 82 83 float * 84 m4Perspective (float *mat, float fovy, float aspect, float nearZ, float farZ); 85 86 float * 87 m4Ortho (float *mat, float left, float right, float bottom, float top, float nearZ, float farZ); 88 89 /* In-place inversion */ 90 float * 91 m4Invert (float *mat); 92 93 /* Puts the inverse of other matrix into mat */ 94 float * 95 m4Inverse (float *mat, const float *other); 96 97 /* In-place transpose */ 98 float * 99 m4Transpose (float *mat); 100 101 float * 102 m4LoadIdentity (float *mat); 103 104 #endif