Advertisement
Guest User

AppleSucks.c

a guest
Mar 15th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.89 KB | None | 0 0
  1. /*
  2.  
  3.  File: matrix.c
  4.  
  5.  Abstract: simple 4x4 matrix computations
  6.  
  7.  Version: 1.0
  8.  
  9.  Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Inc.
  10.  ("Apple") in consideration of your agreement to the following terms, and your
  11.  use, installation, modification or redistribution of this Apple software
  12.  constitutes acceptance of these terms.  If you do not agree with these terms,
  13.  please do not use, install, modify or redistribute this Apple software.
  14.  
  15.  In consideration of your agreement to abide by the following terms, and subject
  16.  to these terms, Apple grants you a personal, non-exclusive license, under
  17.  Apple's copyrights in this original Apple software (the "Apple Software"), to
  18.  use, reproduce, modify and redistribute the Apple Software, with or without
  19.  modifications, in source and/or binary forms; provided that if you redistribute
  20.  the Apple Software in its entirety and without modifications, you must retain
  21.  this notice and the following text and disclaimers in all such redistributions
  22.  of the Apple Software.
  23.  Neither the name, trademarks, service marks or logos of Apple Inc. may be used
  24.  to endorse or promote products derived from the Apple Software without specific
  25.  prior written permission from Apple.  Except as expressly stated in this notice,
  26.  no other rights or licenses, express or implied, are granted by Apple herein,
  27.  including but not limited to any patent rights that may be infringed by your
  28.  derivative works or by other works in which the Apple Software may be
  29.  incorporated.
  30.  
  31.  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  32.  WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  33.  WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  35.  COMBINATION WITH YOUR PRODUCTS.
  36.  
  37.  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  38.  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  39.  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40.  ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
  41.  DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
  42.  CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
  43.  APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.  
  45.  Copyright (C) 2009 Apple Inc. All Rights Reserved.
  46.  
  47. */
  48.  
  49. #include <string.h>
  50. #include <math.h>
  51. #include "matrix.h"
  52.  
  53. /*
  54.  NOTE: These functions are created for your convenience but the matrix algorithms
  55.  are not optimized. You are encouraged to do additional research on your own to
  56.  implement a more robust numerical algorithm.
  57. */
  58.  
  59. void mat4f_LoadIdentity(float* m)
  60. {
  61.     m[0] = 1.0f;
  62.     m[1] = 0.0f;
  63.     m[2] = 0.0f;
  64.     m[3] = 0.0f;
  65.    
  66.     m[4] = 0.0f;
  67.     m[5] = 1.0f;
  68.     m[6] = 0.0f;
  69.     m[7] = 0.0f;
  70.    
  71.     m[8] = 0.0f;
  72.     m[9] = 0.0f;
  73.     m[10] = 1.0f;
  74.     m[11] = 0.0f;  
  75.  
  76.     m[12] = 0.0f;
  77.     m[13] = 0.0f;
  78.     m[14] = 0.0f;
  79.     m[15] = 1.0f;
  80. }
  81.  
  82. // s is a 3D vector
  83. void mat4f_LoadScale(float* s, float* m)
  84. {
  85.     m[0] = s[0];
  86.     m[1] = 0.0f;
  87.     m[2] = 0.0f;
  88.     m[3] = 0.0f;
  89.    
  90.     m[4] = 0.0f;
  91.     m[5] = s[1];
  92.     m[6] = 0.0f;
  93.     m[7] = 0.0f;
  94.    
  95.     m[8] = 0.0f;
  96.     m[9] = 0.0f;
  97.     m[10] = s[2];
  98.     m[11] = 0.0f;  
  99.    
  100.     m[12] = 0.0f;
  101.     m[13] = 0.0f;
  102.     m[14] = 0.0f;
  103.     m[15] = 1.0f;
  104. }
  105.  
  106. void mat4f_LoadXRotation(float radians, float* m)
  107. {
  108.     float cosrad = cosf(radians);
  109.     float sinrad = sinf(radians);
  110.    
  111.     m[0] = 1.0f;
  112.     m[1] = 0.0f;
  113.     m[2] = 0.0f;
  114.     m[3] = 0.0f;
  115.    
  116.     m[4] = 0.0f;
  117.     m[5] = cosrad;
  118.     m[6] = sinrad;
  119.     m[7] = 0.0f;
  120.    
  121.     m[8] = 0.0f;
  122.     m[9] = -sinrad;
  123.     m[10] = cosrad;
  124.     m[11] = 0.0f;  
  125.    
  126.     m[12] = 0.0f;
  127.     m[13] = 0.0f;
  128.     m[14] = 0.0f;
  129.     m[15] = 1.0f;
  130. }
  131.  
  132. void mat4f_LoadYRotation(float radians, float* mout)
  133. {
  134.     float cosrad = cosf(radians);
  135.     float sinrad = sinf(radians);
  136.    
  137.     mout[0] = cosrad;
  138.     mout[1] = 0.0f;
  139.     mout[2] = -sinrad;
  140.     mout[3] = 0.0f;
  141.    
  142.     mout[4] = 0.0f;
  143.     mout[5] = 1.0f;
  144.     mout[6] = 0.0f;
  145.     mout[7] = 0.0f;
  146.    
  147.     mout[8] = sinrad;
  148.     mout[9] = 0.0f;
  149.     mout[10] = cosrad;
  150.     mout[11] = 0.0f;    
  151.    
  152.     mout[12] = 0.0f;
  153.     mout[13] = 0.0f;
  154.     mout[14] = 0.0f;
  155.     mout[15] = 1.0f;
  156. }
  157.  
  158. void mat4f_LoadZRotation(float radians, float* mout)
  159. {
  160.     float cosrad = cosf(radians);
  161.     float sinrad = sinf(radians);
  162.    
  163.     mout[0] = cosrad;
  164.     mout[1] = sinrad;
  165.     mout[2] = 0.0f;
  166.     mout[3] = 0.0f;
  167.    
  168.     mout[4] = -sinrad;
  169.     mout[5] = cosrad;
  170.     mout[6] = 0.0f;
  171.     mout[7] = 0.0f;
  172.    
  173.     mout[8] = 0.0f;
  174.     mout[9] = 0.0f;
  175.     mout[10] = 1.0f;
  176.     mout[11] = 0.0f;    
  177.    
  178.     mout[12] = 0.0f;
  179.     mout[13] = 0.0f;
  180.     mout[14] = 0.0f;
  181.     mout[15] = 1.0f;
  182. }
  183.  
  184. // v is a 3D vector
  185. void mat4f_LoadTranslation(float* v, float* mout)
  186. {
  187.     mout[0] = 1.0f;
  188.     mout[1] = 0.0f;
  189.     mout[2] = 0.0f;
  190.     mout[3] = 0.0f;
  191.    
  192.     mout[4] = 0.0f;
  193.     mout[5] = 1.0f;
  194.     mout[6] = 0.0f;
  195.     mout[7] = 0.0f;
  196.    
  197.     mout[8] = 0.0f;
  198.     mout[9] = 0.0f;
  199.     mout[10] = 1.0f;
  200.     mout[11] = 0.0f;    
  201.    
  202.     mout[12] = v[0];
  203.     mout[13] = v[1];
  204.     mout[14] = v[2];
  205.     mout[15] = 1.0f;
  206. }
  207.  
  208. void mat4f_LoadPerspective(float fov_radians, float aspect, float zNear, float zFar, float* mout)
  209. {
  210.     float f = 1.0f / tanf(fov_radians/2.0f);
  211.    
  212.     mout[0] = f / aspect;
  213.     mout[1] = 0.0f;
  214.     mout[2] = 0.0f;
  215.     mout[3] = 0.0f;
  216.    
  217.     mout[4] = 0.0f;
  218.     mout[5] = f;
  219.     mout[6] = 0.0f;
  220.     mout[7] = 0.0f;
  221.    
  222.     mout[8] = 0.0f;
  223.     mout[9] = 0.0f;
  224.     mout[10] = (zFar+zNear) / (zNear-zFar);
  225.     mout[11] = -1.0f;
  226.    
  227.     mout[12] = 0.0f;
  228.     mout[13] = 0.0f;
  229.     mout[14] = 2 * zFar * zNear /  (zNear-zFar);
  230.     mout[15] = 0.0f;
  231. }
  232.  
  233. void mat4f_LoadOrtho(float left, float right, float bottom, float top, float near, float far, float* mout)
  234. {
  235.     float r_l = right - left;
  236.     float t_b = top - bottom;
  237.     float f_n = far - near;
  238.     float tx = - (right + left) / (right - left);
  239.     float ty = - (top + bottom) / (top - bottom);
  240.     float tz = - (far + near) / (far - near);
  241.  
  242.     mout[0] = 2.0f / r_l;
  243.     mout[1] = 0.0f;
  244.     mout[2] = 0.0f;
  245.     mout[3] = 0.0f;
  246.    
  247.     mout[4] = 0.0f;
  248.     mout[5] = 2.0f / t_b;
  249.     mout[6] = 0.0f;
  250.     mout[7] = 0.0f;
  251.    
  252.     mout[8] = 0.0f;
  253.     mout[9] = 0.0f;
  254.     mout[10] = -2.0f / f_n;
  255.     mout[11] = 0.0f;
  256.    
  257.     mout[12] = tx;
  258.     mout[13] = ty;
  259.     mout[14] = tz;
  260.     mout[15] = 1.0f;
  261. }
  262.  
  263. void mat4f_MultiplyMat4f(const float* a, const float* b, float* mout)
  264. {
  265.     mout[0]  = a[0] * b[0]  + a[4] * b[1]  + a[8] * b[2]   + a[12] * b[3];
  266.     mout[1]  = a[1] * b[0]  + a[5] * b[1]  + a[9] * b[2]   + a[13] * b[3];
  267.     mout[2]  = a[2] * b[0]  + a[6] * b[1]  + a[10] * b[2]  + a[14] * b[3];
  268.     mout[3]  = a[3] * b[0]  + a[7] * b[1]  + a[11] * b[2]  + a[15] * b[3];
  269.  
  270.     mout[4]  = a[0] * b[4]  + a[4] * b[5]  + a[8] * b[6]   + a[12] * b[7];
  271.     mout[5]  = a[1] * b[4]  + a[5] * b[5]  + a[9] * b[6]   + a[13] * b[7];
  272.     mout[6]  = a[2] * b[4]  + a[6] * b[5]  + a[10] * b[6]  + a[14] * b[7];
  273.     mout[7]  = a[3] * b[4]  + a[7] * b[5]  + a[11] * b[6]  + a[15] * b[7];
  274.  
  275.     mout[8]  = a[0] * b[8]  + a[4] * b[9]  + a[8] * b[10]  + a[12] * b[11];
  276.     mout[9]  = a[1] * b[8]  + a[5] * b[9]  + a[9] * b[10]  + a[13] * b[11];
  277.     mout[10] = a[2] * b[8]  + a[6] * b[9]  + a[10] * b[10] + a[14] * b[11];
  278.     mout[11] = a[3] * b[8]  + a[7] * b[9]  + a[11] * b[10] + a[15] * b[11];
  279.  
  280.     mout[12] = a[0] * b[12] + a[4] * b[13] + a[8] * b[14]  + a[12] * b[15];
  281.     mout[13] = a[1] * b[12] + a[5] * b[13] + a[9] * b[14]  + a[13] * b[15];
  282.     mout[14] = a[2] * b[12] + a[6] * b[13] + a[10] * b[14] + a[14] * b[15];
  283.     mout[15] = a[3] * b[12] + a[7] * b[13] + a[11] * b[14] + a[15] * b[15];
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement