opengl_texture.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. Copyright (C) 2004 Michael Liebscher
  3. Copyright (C) 1997-2001 Id Software, Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. /*
  17. * opengl_texture.c: OpenGL Texture Manager.
  18. *
  19. * Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
  20. * Date: 2004
  21. *
  22. * Acknowledgement:
  23. * Portion of this code was derived from Quake II, and was originally
  24. * written by Id Software, Inc.
  25. *
  26. */
  27. #include "../wolfiphone.h"
  28. int currentTextures[ 4 ];
  29. int currenttmu;
  30. int glMaxTexSize;
  31. // ***************************************************************************
  32. // ***************************************************************************
  33. /*
  34. -----------------------------------------------------------------------------
  35. Function:
  36. Parameters:
  37. Returns:
  38. Notes:
  39. -----------------------------------------------------------------------------
  40. */
  41. PUBLIC void R_DeleteTexture( unsigned int texnum )
  42. {
  43. pfglDeleteTextures( 1, &texnum );
  44. }
  45. /*
  46. -----------------------------------------------------------------------------
  47. Function:
  48. Parameters:
  49. Returns:
  50. Notes:
  51. -----------------------------------------------------------------------------
  52. */
  53. PUBLIC void R_TexEnv( GLenum mode )
  54. {
  55. static int lastmodes[ 4 ] = { -1, -1, -1, -1 };
  56. if ( mode != lastmodes[ currenttmu ] )
  57. {
  58. pfglTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode );
  59. lastmodes[ currenttmu ] = mode;
  60. }
  61. }
  62. /*
  63. -----------------------------------------------------------------------------
  64. Function:
  65. Parameters:
  66. Returns:
  67. Notes:
  68. -----------------------------------------------------------------------------
  69. */
  70. PUBLIC void R_SelectTexture( GLenum texture )
  71. {
  72. int tmu;
  73. #ifndef IPHONE
  74. if( ! pfglActiveTextureARB )
  75. {
  76. return;
  77. }
  78. #endif
  79. if( texture == GL_TEXTURE0 )
  80. {
  81. tmu = 0;
  82. }
  83. else if( texture == GL_TEXTURE1 )
  84. {
  85. tmu = 1;
  86. }
  87. else if( texture == GL_TEXTURE2 )
  88. {
  89. tmu = 2;
  90. }
  91. else
  92. {
  93. tmu = 3;
  94. }
  95. if( tmu == currenttmu )
  96. {
  97. return;
  98. }
  99. currenttmu = tmu;
  100. pfglActiveTextureARB( texture );
  101. pfglClientActiveTextureARB( texture );
  102. }
  103. /*
  104. -----------------------------------------------------------------------------
  105. Function:
  106. Parameters:
  107. Returns:
  108. Notes:
  109. -----------------------------------------------------------------------------
  110. */
  111. PUBLIC void R_Bind( int texnum )
  112. {
  113. // Is this texture already bound
  114. if( currentTextures[ currenttmu ] == texnum )
  115. {
  116. return;
  117. }
  118. currentTextures[ currenttmu ] = texnum;
  119. pfglBindTexture( GL_TEXTURE_2D, texnum );
  120. }
  121. /*
  122. -----------------------------------------------------------------------------
  123. Function:
  124. Parameters:
  125. Returns:
  126. Notes:
  127. -----------------------------------------------------------------------------
  128. */
  129. PUBLIC void R_MBind( GLenum target, int texnum )
  130. {
  131. R_SelectTexture( target );
  132. if( target == GL_TEXTURE0 )
  133. {
  134. if ( currentTextures[ 0 ] == texnum )
  135. {
  136. return;
  137. }
  138. }
  139. else if( target == GL_TEXTURE1 )
  140. {
  141. if( currentTextures[ 1 ] == texnum )
  142. {
  143. return;
  144. }
  145. }
  146. else if( target == GL_TEXTURE2 )
  147. {
  148. if( currentTextures[ 2 ] == texnum )
  149. {
  150. return;
  151. }
  152. }
  153. else
  154. {
  155. if( currentTextures[ 3 ] == texnum )
  156. {
  157. return;
  158. }
  159. }
  160. R_Bind( texnum );
  161. }
  162. /*
  163. -----------------------------------------------------------------------------
  164. Function:
  165. Parameters:
  166. Returns:
  167. Notes:
  168. -----------------------------------------------------------------------------
  169. */
  170. PUBLIC void R_EnableMultitexture( _boolean enable )
  171. {
  172. #ifndef IPHONE
  173. if( ! pfglActiveTextureARB )
  174. {
  175. return;
  176. }
  177. #endif
  178. if( enable )
  179. {
  180. R_SelectTexture( GL_TEXTURE1 );
  181. pfglEnable( GL_TEXTURE_2D );
  182. R_TexEnv( GL_REPLACE );
  183. }
  184. else
  185. {
  186. R_SelectTexture( GL_TEXTURE1 );
  187. pfglDisable( GL_TEXTURE_2D );
  188. R_TexEnv( GL_REPLACE );
  189. }
  190. R_SelectTexture( GL_TEXTURE0 );
  191. R_TexEnv( GL_REPLACE );
  192. }