matrix.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. Copyright (c) 2008, Luke Benstead.
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. Redistributions of source code must retain the above copyright notice,
  10. this list of conditions and the following disclaimer.
  11. Redistributions in binary form must reproduce the above copyright notice,
  12. this list of conditions and the following disclaimer in the documentation
  13. and/or other materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. cc.KM_GL_MODELVIEW = 0x1700;
  26. cc.KM_GL_PROJECTION = 0x1701;
  27. cc.KM_GL_TEXTURE = 0x1702;
  28. cc.modelview_matrix_stack = new cc.km_mat4_stack();
  29. cc.projection_matrix_stack = new cc.km_mat4_stack();
  30. cc.texture_matrix_stack = new cc.km_mat4_stack();
  31. cc.current_stack = null;
  32. cc.initialized = false;
  33. cc.lazyInitialize = function () {
  34. if (!cc.initialized) {
  35. var identity = new cc.kmMat4(); //Temporary identity matrix
  36. //Initialize all 3 stacks
  37. cc.km_mat4_stack_initialize(cc.modelview_matrix_stack);
  38. cc.km_mat4_stack_initialize(cc.projection_matrix_stack);
  39. cc.km_mat4_stack_initialize(cc.texture_matrix_stack);
  40. cc.current_stack = cc.modelview_matrix_stack;
  41. cc.initialized = true;
  42. cc.kmMat4Identity(identity);
  43. //Make sure that each stack has the identity matrix
  44. cc.km_mat4_stack_push(cc.modelview_matrix_stack, identity);
  45. cc.km_mat4_stack_push(cc.projection_matrix_stack, identity);
  46. cc.km_mat4_stack_push(cc.texture_matrix_stack, identity);
  47. }
  48. };
  49. cc.lazyInitialize();
  50. cc.kmGLFreeAll = function () {
  51. //Clear the matrix stacks
  52. cc.km_mat4_stack_release(cc.modelview_matrix_stack);
  53. cc.km_mat4_stack_release(cc.projection_matrix_stack);
  54. cc.km_mat4_stack_release(cc.texture_matrix_stack);
  55. //Delete the matrices
  56. cc.initialized = false; //Set to uninitialized
  57. cc.current_stack = null; //Set the current stack to point nowhere
  58. };
  59. cc.kmGLPushMatrix = function () {
  60. cc.km_mat4_stack_push(cc.current_stack, cc.current_stack.top);
  61. };
  62. cc.kmGLPushMatrixWitMat4 = function (saveMat) {
  63. cc.current_stack.stack.push(cc.current_stack.top);
  64. cc.kmMat4Assign(saveMat, cc.current_stack.top);
  65. cc.current_stack.top = saveMat;
  66. };
  67. cc.kmGLPopMatrix = function () {
  68. //No need to lazy initialize, you shouldnt be popping first anyway!
  69. //cc.km_mat4_stack_pop(cc.current_stack, null);
  70. cc.current_stack.top = cc.current_stack.stack.pop();
  71. };
  72. cc.kmGLMatrixMode = function (mode) {
  73. //cc.lazyInitialize();
  74. switch (mode) {
  75. case cc.KM_GL_MODELVIEW:
  76. cc.current_stack = cc.modelview_matrix_stack;
  77. break;
  78. case cc.KM_GL_PROJECTION:
  79. cc.current_stack = cc.projection_matrix_stack;
  80. break;
  81. case cc.KM_GL_TEXTURE:
  82. cc.current_stack = cc.texture_matrix_stack;
  83. break;
  84. default:
  85. throw "Invalid matrix mode specified"; //TODO: Proper error handling
  86. break;
  87. }
  88. };
  89. cc.kmGLLoadIdentity = function () {
  90. //cc.lazyInitialize();
  91. cc.kmMat4Identity(cc.current_stack.top); //Replace the top matrix with the identity matrix
  92. };
  93. cc.kmGLLoadMatrix = function (pIn) {
  94. //cc.lazyInitialize();
  95. cc.kmMat4Assign(cc.current_stack.top, pIn);
  96. };
  97. cc.kmGLMultMatrix = function (pIn) {
  98. //cc.lazyInitialize();
  99. cc.kmMat4Multiply(cc.current_stack.top, cc.current_stack.top, pIn);
  100. };
  101. cc.kmGLTranslatef = function (x, y, z) {
  102. var translation = new cc.kmMat4();
  103. //Create a rotation matrix using the axis and the angle
  104. cc.kmMat4Translation(translation, x, y, z);
  105. //Multiply the rotation matrix by the current matrix
  106. cc.kmMat4Multiply(cc.current_stack.top, cc.current_stack.top, translation);
  107. };
  108. cc.kmGLRotatef = function (angle, x, y, z) {
  109. var axis = new cc.kmVec3(x, y, z);
  110. var rotation = new cc.kmMat4();
  111. //Create a rotation matrix using the axis and the angle
  112. cc.kmMat4RotationAxisAngle(rotation, axis, cc.kmDegreesToRadians(angle));
  113. //Multiply the rotation matrix by the current matrix
  114. cc.kmMat4Multiply(cc.current_stack.top, cc.current_stack.top, rotation);
  115. };
  116. cc.kmGLScalef = function (x, y, z) {
  117. var scaling = new cc.kmMat4();
  118. cc.kmMat4Scaling(scaling, x, y, z);
  119. cc.kmMat4Multiply(cc.current_stack.top, cc.current_stack.top, scaling);
  120. };
  121. cc.kmGLGetMatrix = function (mode, pOut) {
  122. //cc.lazyInitialize();
  123. switch (mode) {
  124. case cc.KM_GL_MODELVIEW:
  125. cc.kmMat4Assign(pOut, cc.modelview_matrix_stack.top);
  126. break;
  127. case cc.KM_GL_PROJECTION:
  128. cc.kmMat4Assign(pOut, cc.projection_matrix_stack.top);
  129. break;
  130. case cc.KM_GL_TEXTURE:
  131. cc.kmMat4Assign(pOut, cc.texture_matrix_stack.top);
  132. break;
  133. default:
  134. throw "Invalid matrix mode specified"; //TODO: Proper error handling
  135. break;
  136. }
  137. };