matrix.js 5.6 KB

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