dglMatrix.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "math/mMatrix.h"
  23. #include "graphics/dgl.h"
  24. #include "console/console.h"
  25. void dglLoadMatrix(const MatrixF *m)
  26. {
  27. //F32 mat[16];
  28. //m->transposeTo(mat);
  29. const_cast<MatrixF*>(m)->transpose();
  30. glLoadMatrixf(*m);
  31. const_cast<MatrixF*>(m)->transpose();
  32. }
  33. void dglMultMatrix(const MatrixF *m)
  34. {
  35. //F32 mat[16];
  36. //m->transposeTo(mat);
  37. // const F32* mp = *m;
  38. // Con::errorf(ConsoleLogEntry::General, "Mult: %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g",
  39. // mp[0],
  40. // mp[1],
  41. // mp[2],
  42. // mp[3],
  43. // mp[4],
  44. // mp[5],
  45. // mp[6],
  46. // mp[7],
  47. // mp[8],
  48. // mp[9],
  49. // mp[10],
  50. // mp[11],
  51. // mp[12],
  52. // mp[13],
  53. // mp[14],
  54. // mp[15]);
  55. const_cast<MatrixF*>(m)->transpose();
  56. glMultMatrixf(*m);
  57. const_cast<MatrixF*>(m)->transpose();
  58. }
  59. void dglGetModelview(MatrixF *m)
  60. {
  61. glGetFloatv(GL_MODELVIEW_MATRIX, *m);
  62. m->transpose();
  63. }
  64. void dglGetProjection(MatrixF *m)
  65. {
  66. glGetFloatv(GL_PROJECTION_MATRIX, *m);
  67. m->transpose();
  68. }
  69. static F64 frustLeft = 0, frustRight = 1, frustBottom, frustTop, frustNear, frustFar;
  70. static RectI viewPort;
  71. static F32 pixelScale;
  72. static F32 worldToScreenScale;
  73. static bool isOrtho;
  74. void dglSetFrustum(F64 left, F64 right, F64 bottom, F64 top, F64 nearPlane, F64 farPlane, bool ortho)
  75. {
  76. // this converts from a coord system looking down the pos-y axis
  77. // to ogl's down neg z axis.
  78. // it's stored in OGL matrix form
  79. static F32 darkToOGLCoord[16] = { 1, 0, 0, 0,
  80. 0, 0, -1, 0,
  81. 0, 1, 0, 0,
  82. 0, 0, 0, 1 };
  83. frustLeft = left;
  84. frustRight = right;
  85. frustBottom = bottom;
  86. frustTop = top;
  87. frustNear = nearPlane;
  88. frustFar = farPlane;
  89. isOrtho = ortho;
  90. if (ortho)
  91. {
  92. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID)
  93. glOrthof(left, right, bottom, top, nearPlane, farPlane);
  94. #else
  95. glOrtho(left, right, bottom, top, nearPlane, farPlane);
  96. #endif
  97. worldToScreenScale = F32(viewPort.extent.x / (frustRight - frustLeft));
  98. }
  99. else
  100. {
  101. glFrustum(left, right, bottom, top, nearPlane, farPlane);
  102. worldToScreenScale = F32((frustNear * viewPort.extent.x) / frustRight - frustLeft);
  103. }
  104. glMultMatrixf(darkToOGLCoord);
  105. }
  106. void dglGetFrustum(F64 *left, F64 *right, F64 *bottom, F64 *top, F64 *nearPlane, F64 *farPlane)
  107. {
  108. *left = frustLeft;
  109. *right = frustRight;
  110. *bottom = frustBottom;
  111. *top = frustTop;
  112. *nearPlane = frustNear;
  113. *farPlane = frustFar;
  114. }
  115. bool dglIsOrtho()
  116. {
  117. return isOrtho;
  118. }
  119. void dglSetViewport(const RectI &aViewPort)
  120. {
  121. viewPort = aViewPort;
  122. U32 screenHeight = Platform::getWindowSize().y;
  123. //glViewport(viewPort.point.x, viewPort.point.y + viewPort.extent.y,
  124. // viewPort.extent.x, -viewPort.extent.y);
  125. glViewport(viewPort.point.x, screenHeight - (viewPort.point.y + viewPort.extent.y),
  126. viewPort.extent.x, viewPort.extent.y);
  127. pixelScale = viewPort.extent.x / (F32)MIN_RESOLUTION_X;
  128. worldToScreenScale = F32((frustNear * viewPort.extent.x) / (frustRight - frustLeft));
  129. }
  130. void dglGetViewport(RectI* outViewport)
  131. {
  132. AssertFatal(outViewport != NULL, "Error, bad point in GetViewport");
  133. *outViewport = viewPort;
  134. }
  135. F32 dglGetPixelScale()
  136. {
  137. return pixelScale;
  138. }
  139. F32 dglGetWorldToScreenScale()
  140. {
  141. return worldToScreenScale;
  142. }
  143. F32 dglProjectRadius(F32 dist, F32 radius)
  144. {
  145. return (radius / dist) * worldToScreenScale;
  146. }