bmp2d.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***************************************************************************
  21. * *
  22. * Project Name : Commando/G *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/bmp2d.cpp $*
  25. * *
  26. * $Author:: Naty_h $*
  27. * *
  28. * $Modtime:: 4/13/01 1:37p $*
  29. * *
  30. * $Revision:: 10 $*
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "bmp2d.h"
  36. #include "pot.h"
  37. #include "ww3d.h"
  38. #include "texture.h"
  39. #include "surfaceclass.h"
  40. Bitmap2DObjClass::Bitmap2DObjClass
  41. (
  42. const char *filename,
  43. float screen_x,
  44. float screen_y,
  45. bool center,
  46. bool additive,
  47. bool colorizable,
  48. int usable_width,
  49. int usable_height,
  50. bool ignore_alpha
  51. )
  52. : DynamicScreenMeshClass(2, 4)
  53. {
  54. int resw, resh, resbits;
  55. bool windowed;
  56. // find the resolution (for centering and pixel to pixel translation)
  57. WW3D::Get_Device_Resolution(resw, resh, resbits, windowed);
  58. // This should be the correct way to do things
  59. // but other code expects an aspect ratio of 1.0
  60. // Hector Yee 2/22/01
  61. // Set_Aspect(resh/(float)resw);
  62. // load up the surfaces file name
  63. SurfaceClass *surface=NEW_REF(SurfaceClass,(filename));
  64. SurfaceClass::SurfaceDescription sd;
  65. surface->Get_Description(sd);
  66. if (usable_width == -1)
  67. usable_width = sd.Width;
  68. if (usable_height == -1)
  69. usable_height = sd.Height;
  70. // if we requested the image to be centered around a point adjust the
  71. // coordinates accordingly.
  72. if (center) {
  73. screen_x -= ((float)usable_width / resw) / 2;
  74. screen_y -= ((float)usable_height / resh) / 2;
  75. }
  76. // The image will be broken down into square textures. The size of these
  77. // textures will be the smallest POT (power of two) which is equal or
  78. // greater than the smaller dimension of the image. Also, the pieces can
  79. // never be larger than 256 texels because some rendering devices don't
  80. // support textures larger than that.
  81. int surf_w = usable_width;
  82. int surf_h = usable_height;
  83. int piece = Find_POT(MIN(surf_w, surf_h));
  84. piece = MIN(piece, 256);
  85. // now take the image in question and break it down into
  86. // "piece"x"piece"-pixel polygons and calculate the number of textures
  87. // based from those calculations.
  88. int mw = (surf_w & (piece - 1)) ? (surf_w / piece)+1 : (surf_w /piece);
  89. int mh = (surf_h & (piece - 1)) ? (surf_h / piece)+1 : (surf_h /piece);
  90. // for every square texture it takes four vertexes to express the two
  91. // polygons.
  92. Resize(mw * mh *2, mw * mh * 4);
  93. // Set shader to additive if requested, else alpha or opaque depending on
  94. // whether the texture has an alpha channel. Sorting is always set so that
  95. // sortbias can be used to determine occlusion between the various 2D
  96. // elements.
  97. ShaderClass shader;
  98. if (additive) {
  99. shader = ShaderClass::_PresetAdditive2DShader;
  100. } else {
  101. if (ignore_alpha == false && Has_Alpha(sd.Format)) {
  102. shader = ShaderClass::_PresetAlpha2DShader;
  103. } else {
  104. shader = ShaderClass::_PresetOpaque2DShader;
  105. }
  106. }
  107. Enable_Sort();
  108. // If we want to be able to colorize this bitmap later (by setting
  109. // emissive color for the vertex material, or via a vertex emissive color
  110. // array) we need to enable the primary gradient in the shader (it is
  111. // disabled in the 2D presets), and set an appropriate vertex material.
  112. if (colorizable) {
  113. shader.Set_Primary_Gradient(ShaderClass::GRADIENT_MODULATE);
  114. VertexMaterialClass *vertex_material = NEW_REF( VertexMaterialClass, ());
  115. vertex_material->Set_Ambient(0.0f, 0.0f, 0.0f);
  116. vertex_material->Set_Diffuse(0.0f, 0.0f, 0.0f);
  117. vertex_material->Set_Specular(0.0f, 0.0f, 0.0f);
  118. vertex_material->Set_Emissive(1.0f, 1.0f, 1.0f);
  119. Set_Vertex_Material(vertex_material, true);
  120. vertex_material->Release_Ref();
  121. }
  122. // Set desired shader.
  123. Set_Shader(shader);
  124. // loop through the rows and columns of the image and make valid
  125. // textures from the pieces.
  126. for (int lpy = 0, tlpy=0; lpy < mh; lpy++, tlpy += piece) {
  127. for (int lpx = 0, tlpx = 0; lpx < mw; lpx++, tlpx += piece) {
  128. // figure the desired width and height of the texture (max "piece")
  129. int iw = MIN(piece, usable_width - (tlpx));
  130. int ih = MIN(piece, usable_height - (tlpy));
  131. int pot = MAX(Find_POT(iw), Find_POT(ih));
  132. // create the texture and turn MIP-mapping off.
  133. SurfaceClass *piece_surface=NEW_REF(SurfaceClass,(pot,pot,sd.Format));
  134. piece_surface->Copy(0,0,tlpx,tlpy,pot,pot,surface);
  135. TextureClass *piece_texture =NEW_REF(TextureClass,(piece_surface,TextureClass::MIP_LEVELS_1));
  136. piece_texture->Set_U_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  137. piece_texture->Set_V_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  138. REF_PTR_RELEASE(piece_surface);
  139. // calculate our actual texture coordinates based on the difference between
  140. // the width and height of the texture and the width and height the font
  141. // occupys.
  142. float tw = (float)iw / (float)pot;
  143. float th = (float)ih / (float)pot;
  144. // convert image width and image height to normalized values
  145. float vw = (float)iw / (float)resw;
  146. float vh = (float)ih / (float)resh;
  147. // figure out the screen space x and y positions of the object in question.
  148. float x = screen_x + (((float)tlpx) / (float)resw);
  149. float y = screen_y + (((float)tlpy) / (float)resh);
  150. Set_Texture(piece_texture);
  151. Begin_Tri_Strip();
  152. Vertex( x, y, 0, 0, 0);
  153. Vertex( x + vw, y, 0, tw, 0);
  154. Vertex( x, y + vh, 0, 0, th);
  155. Vertex( x + vw, y + vh, 0, tw, th);
  156. End_Tri_Strip();
  157. // release our reference to the texture
  158. REF_PTR_RELEASE(piece_texture);
  159. }
  160. }
  161. REF_PTR_RELEASE(surface);
  162. Set_Dirty();
  163. }
  164. Bitmap2DObjClass::Bitmap2DObjClass
  165. (
  166. TextureClass *texture,
  167. float screen_x,
  168. float screen_y,
  169. bool center,
  170. bool additive,
  171. bool colorizable,
  172. bool ignore_alpha
  173. )
  174. : DynamicScreenMeshClass(2, 4)
  175. {
  176. int resw, resh, resbits;
  177. bool windowed;
  178. // find the resolution (for centering and pixel to pixel translation)
  179. WW3D::Get_Device_Resolution(resw, resh, resbits, windowed);
  180. // This should be the correct way to do things
  181. // but other code expects an aspect ratio of 1.0
  182. // Hector Yee 2/22/01
  183. //Set_Aspect(resh/(float)resw);
  184. // Find the dimensions of the texture:
  185. SurfaceClass::SurfaceDescription sd;
  186. texture->Get_Level_Description(sd);
  187. // convert image width and image height to normalized values
  188. float vw = (float) sd.Width / (float)resw;
  189. float vh = (float) sd.Height / (float)resh;
  190. // if we requested the image to be centered around a point adjust the
  191. // coordinates accordingly.
  192. if (center) {
  193. screen_x -= vw / 2;
  194. screen_y -= vh / 2;
  195. }
  196. // Set shader to additive if requested, else alpha or opaque depending on whether the texture
  197. // has an alpha channel. Sorting is never set - if you wish to sort these types of objects you
  198. // should use static sort levels (note that static sort levels are not implemented for these
  199. // objects yet, but it should be very simple to do).
  200. ShaderClass shader;
  201. if (additive) {
  202. shader = ShaderClass::_PresetAdditive2DShader;
  203. } else {
  204. if (ignore_alpha == false && Has_Alpha(sd.Format)) {
  205. shader = ShaderClass::_PresetAlpha2DShader;
  206. } else {
  207. shader = ShaderClass::_PresetOpaque2DShader;
  208. }
  209. }
  210. Disable_Sort();
  211. // If we want to be able to colorize this bitmap later (by setting
  212. // emissive color for the vertex material, or via a vertex emissive color
  213. // array) we need to enable the primary gradient in the shader (it is
  214. // disabled in the 2D presets), and set an appropriate vertex material.
  215. if (colorizable) {
  216. shader.Set_Primary_Gradient(ShaderClass::GRADIENT_MODULATE);
  217. VertexMaterialClass *vertex_material = NEW_REF( VertexMaterialClass, ());
  218. vertex_material->Set_Ambient(0.0f, 0.0f, 0.0f);
  219. vertex_material->Set_Diffuse(0.0f, 0.0f, 0.0f);
  220. vertex_material->Set_Specular(0.0f, 0.0f, 0.0f);
  221. vertex_material->Set_Emissive(1.0f, 1.0f, 1.0f);
  222. Set_Vertex_Material(vertex_material, true);
  223. vertex_material->Release_Ref();
  224. }
  225. // Set desired shader.
  226. Set_Shader(shader);
  227. // Set texture to requested texture:
  228. Set_Texture(texture);
  229. Begin_Tri_Strip();
  230. Vertex( screen_x, screen_y, 0, 0, 0);
  231. Vertex( screen_x + vw, screen_y, 0, 1.0, 0);
  232. Vertex( screen_x, screen_y + vh, 0, 0, 1.0);
  233. Vertex( screen_x + vw, screen_y + vh, 0, 1.0, 1.0);
  234. End_Tri_Strip();
  235. Set_Dirty();
  236. }
  237. RenderObjClass * Bitmap2DObjClass::Clone(void) const
  238. {
  239. return NEW_REF( Bitmap2DObjClass, (*this));
  240. }