AndroidOutlineGL.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // The debug render modes are only built in a debug build,
  23. // partially because a release build should not need them
  24. // and partially because using aglMacro.h or cglMacro.h would prevent us from
  25. // playing this little function-pointer-hijacking trick
  26. #if defined(TORQUE_DEBUG)
  27. #define NO_REDEFINE_GL_FUNCS
  28. #include "platformAndroid/platformGL.h"
  29. #include "console/console.h"
  30. bool gOutlineEnabled = false;
  31. void (* glDrawElementsProcPtr) (GLenum mode, GLsizei count, GLenum type, const GLvoid *indices) = glDrawElements;
  32. void (* glDrawArraysProcPtr) (GLenum mode, GLint first, GLsizei count) = glDrawArrays;
  33. void (* glNormDrawElements) (GLenum mode, GLsizei count, GLenum type, const GLvoid *indices) = glDrawElements;
  34. void (* glNormDrawArrays) (GLenum mode, GLint first, GLsizei count) = glDrawArrays;
  35. /// A utility for the outline drawing routines
  36. static U32 getIndex(GLenum type, const void *indices, U32 i)
  37. {
  38. if(type == GL_UNSIGNED_BYTE)
  39. return ((U8 *) indices)[i];
  40. else if(type == GL_UNSIGNED_SHORT)
  41. return ((U16 *) indices)[i];
  42. else
  43. return ((U32 *) indices)[i];
  44. }
  45. void glOutlineDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
  46. {
  47. if(mode == GL_POLYGON)
  48. mode = GL_LINE_LOOP;
  49. if(mode == GL_POINTS || mode == GL_LINE_STRIP || mode == GL_LINE_LOOP || mode == GL_LINES)
  50. glDrawElements( mode, count, type, indices );
  51. else
  52. {
  53. glBegin(GL_LINES);
  54. if(mode == GL_TRIANGLE_STRIP)
  55. {
  56. U32 i;
  57. for(i = 0; i < count - 1; i++)
  58. {
  59. glArrayElement(getIndex(type, indices, i));
  60. glArrayElement(getIndex(type, indices, i + 1));
  61. if(i + 2 != count)
  62. {
  63. glArrayElement(getIndex(type, indices, i));
  64. glArrayElement(getIndex(type, indices, i + 2));
  65. }
  66. }
  67. }
  68. else if(mode == GL_TRIANGLE_FAN)
  69. {
  70. for(U32 i = 1; i < count; i ++)
  71. {
  72. glArrayElement(getIndex(type, indices, 0));
  73. glArrayElement(getIndex(type, indices, i));
  74. if(i != count - 1)
  75. {
  76. glArrayElement(getIndex(type, indices, i));
  77. glArrayElement(getIndex(type, indices, i + 1));
  78. }
  79. }
  80. }
  81. else if(mode == GL_TRIANGLES)
  82. {
  83. for(U32 i = 3; i <= count; i += 3)
  84. {
  85. glArrayElement(getIndex(type, indices, i - 3));
  86. glArrayElement(getIndex(type, indices, i - 2));
  87. glArrayElement(getIndex(type, indices, i - 2));
  88. glArrayElement(getIndex(type, indices, i - 1));
  89. glArrayElement(getIndex(type, indices, i - 3));
  90. glArrayElement(getIndex(type, indices, i - 1));
  91. }
  92. }
  93. else if(mode == GL_QUADS)
  94. {
  95. for(U32 i = 4; i <= count; i += 4)
  96. {
  97. glArrayElement(getIndex(type, indices, i - 4));
  98. glArrayElement(getIndex(type, indices, i - 3));
  99. glArrayElement(getIndex(type, indices, i - 3));
  100. glArrayElement(getIndex(type, indices, i - 2));
  101. glArrayElement(getIndex(type, indices, i - 2));
  102. glArrayElement(getIndex(type, indices, i - 1));
  103. glArrayElement(getIndex(type, indices, i - 4));
  104. glArrayElement(getIndex(type, indices, i - 1));
  105. }
  106. }
  107. else if(mode == GL_QUAD_STRIP)
  108. {
  109. if(count < 4)
  110. return;
  111. glArrayElement(getIndex(type, indices, 0));
  112. glArrayElement(getIndex(type, indices, 1));
  113. for(U32 i = 4; i <= count; i += 2)
  114. {
  115. glArrayElement(getIndex(type, indices, i - 4));
  116. glArrayElement(getIndex(type, indices, i - 2));
  117. glArrayElement(getIndex(type, indices, i - 3));
  118. glArrayElement(getIndex(type, indices, i - 1));
  119. glArrayElement(getIndex(type, indices, i - 2));
  120. glArrayElement(getIndex(type, indices, i - 1));
  121. }
  122. }
  123. glEnd();
  124. }
  125. }
  126. void glOutlineDrawArrays(GLenum mode, GLint first, GLsizei count)
  127. {
  128. if(mode == GL_POLYGON)
  129. mode = GL_LINE_LOOP;
  130. if(mode == GL_POINTS || mode == GL_LINE_STRIP || mode == GL_LINE_LOOP || mode == GL_LINES)
  131. glDrawArrays( mode, first, count );
  132. else
  133. {
  134. glBegin(GL_LINES);
  135. if(mode == GL_TRIANGLE_STRIP)
  136. {
  137. U32 i;
  138. for(i = 0; i < count - 1; i++)
  139. {
  140. glArrayElement(first + i);
  141. glArrayElement(first + i + 1);
  142. if(i + 2 != count)
  143. {
  144. glArrayElement(first + i);
  145. glArrayElement(first + i + 2);
  146. }
  147. }
  148. }
  149. else if(mode == GL_TRIANGLE_FAN)
  150. {
  151. for(U32 i = 1; i < count; i ++)
  152. {
  153. glArrayElement(first);
  154. glArrayElement(first + i);
  155. if(i != count - 1)
  156. {
  157. glArrayElement(first + i);
  158. glArrayElement(first + i + 1);
  159. }
  160. }
  161. }
  162. else if(mode == GL_TRIANGLES)
  163. {
  164. for(U32 i = 3; i <= count; i += 3)
  165. {
  166. glArrayElement(first + i - 3);
  167. glArrayElement(first + i - 2);
  168. glArrayElement(first + i - 2);
  169. glArrayElement(first + i - 1);
  170. glArrayElement(first + i - 3);
  171. glArrayElement(first + i - 1);
  172. }
  173. }
  174. else if(mode == GL_QUADS)
  175. {
  176. for(U32 i = 4; i <= count; i += 4)
  177. {
  178. glArrayElement(first + i - 4);
  179. glArrayElement(first + i - 3);
  180. glArrayElement(first + i - 3);
  181. glArrayElement(first + i - 2);
  182. glArrayElement(first + i - 2);
  183. glArrayElement(first + i - 1);
  184. glArrayElement(first + i - 4);
  185. glArrayElement(first + i - 1);
  186. }
  187. }
  188. else if(mode == GL_QUAD_STRIP)
  189. {
  190. if(count < 4)
  191. return;
  192. glArrayElement(first + 0);
  193. glArrayElement(first + 1);
  194. for(U32 i = 4; i <= count; i += 2)
  195. {
  196. glArrayElement(first + i - 4);
  197. glArrayElement(first + i - 2);
  198. glArrayElement(first + i - 3);
  199. glArrayElement(first + i - 1);
  200. glArrayElement(first + i - 2);
  201. glArrayElement(first + i - 1);
  202. }
  203. }
  204. glEnd();
  205. }
  206. }
  207. ConsoleFunction(GLEnableOutline,void,2,2,"GLEnableOutline( true | false ) - sets whether to render wireframe")
  208. {
  209. gOutlineEnabled = dAtob(argv[1]);
  210. if(gOutlineEnabled)
  211. {
  212. glDrawElementsProcPtr = glOutlineDrawElements;
  213. glDrawArraysProcPtr = glOutlineDrawArrays;
  214. Con::printf("swapped to outline mode funcs");
  215. } else {
  216. glDrawElementsProcPtr = glDrawElements;
  217. glDrawArraysProcPtr = glDrawArrays;
  218. Con::printf("swapped to normal funcs");
  219. }
  220. }
  221. #endif