bmp2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. * $Org Author:: Jani_p $*
  27. * *
  28. * $Author:: Kenny_m $*
  29. * *
  30. * $Modtime:: 08/05/02 10:44a $*
  31. * *
  32. * $Revision:: 12 $*
  33. * *
  34. * 08/05/02 KM Texture class redesign
  35. *-------------------------------------------------------------------------*
  36. * Functions: *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "bmp2d.h"
  39. #include "pot.h"
  40. #include "ww3d.h"
  41. #include "texture.h"
  42. #include "surfaceclass.h"
  43. #include "assetmgr.h"
  44. #include "textureloader.h"
  45. #include "ww3dformat.h"
  46. Bitmap2DObjClass::Bitmap2DObjClass
  47. (
  48. const char *filename,
  49. float screen_x,
  50. float screen_y,
  51. bool center,
  52. bool additive,
  53. bool colorizable,
  54. int usable_width,
  55. int usable_height,
  56. bool ignore_alpha
  57. )
  58. : DynamicScreenMeshClass(2, 4)
  59. {
  60. int resw, resh, resbits;
  61. bool windowed;
  62. // find the resolution (for centering and pixel to pixel translation)
  63. WW3D::Get_Device_Resolution(resw, resh, resbits, windowed);
  64. // This should be the correct way to do things
  65. // but other code expects an aspect ratio of 1.0
  66. // Hector Yee 2/22/01
  67. // Set_Aspect(resh/(float)resw);
  68. // load up the surfaces file name
  69. TextureClass *tex = WW3DAssetManager::Get_Instance()->Get_Texture(filename, MIP_LEVELS_1);
  70. if (!tex->Is_Initialized())
  71. TextureLoader::Request_Foreground_Loading(tex);
  72. SurfaceClass *surface = tex->Get_Surface_Level(0);
  73. if (!surface) {
  74. surface = NEW_REF(SurfaceClass, (32, 32, Get_Valid_Texture_Format(WW3D_FORMAT_R8G8B8,true)));
  75. }
  76. SurfaceClass::SurfaceDescription sd;
  77. surface->Get_Description(sd);
  78. if (usable_width == -1)
  79. usable_width = sd.Width;
  80. if (usable_height == -1)
  81. usable_height = sd.Height;
  82. // if we requested the image to be centered around a point adjust the
  83. // coordinates accordingly.
  84. if (center) {
  85. screen_x -= ((float)usable_width / resw) / 2;
  86. screen_y -= ((float)usable_height / resh) / 2;
  87. }
  88. // The image will be broken down into square textures. The size of these
  89. // textures will be the smallest POT (power of two) which is equal or
  90. // greater than the smaller dimension of the image. Also, the pieces can
  91. // never be larger than 256 texels because some rendering devices don't
  92. // support textures larger than that.
  93. int surf_w = usable_width;
  94. int surf_h = usable_height;
  95. int piece = Find_POT(MIN(surf_w, surf_h));
  96. piece = MIN(piece, 256);
  97. // now take the image in question and break it down into
  98. // "piece"x"piece"-pixel polygons and calculate the number of textures
  99. // based from those calculations.
  100. int mw = (surf_w & (piece - 1)) ? (surf_w / piece)+1 : (surf_w /piece);
  101. int mh = (surf_h & (piece - 1)) ? (surf_h / piece)+1 : (surf_h /piece);
  102. // for every square texture it takes four vertexes to express the two
  103. // polygons.
  104. Resize(mw * mh *2, mw * mh * 4);
  105. // Set shader to additive if requested, else alpha or opaque depending on
  106. // whether the texture has an alpha channel. Sorting is always set so that
  107. // sortbias can be used to determine occlusion between the various 2D
  108. // elements.
  109. ShaderClass shader;
  110. if (additive) {
  111. shader = ShaderClass::_PresetAdditive2DShader;
  112. } else {
  113. if (ignore_alpha == false && Has_Alpha(sd.Format)) {
  114. shader = ShaderClass::_PresetAlpha2DShader;
  115. } else {
  116. shader = ShaderClass::_PresetOpaque2DShader;
  117. }
  118. }
  119. Enable_Sort();
  120. // If we want to be able to colorize this bitmap later (by setting
  121. // emissive color for the vertex material, or via a vertex emissive color
  122. // array) we need to enable the primary gradient in the shader (it is
  123. // disabled in the 2D presets), and set an appropriate vertex material.
  124. if (colorizable) {
  125. shader.Set_Primary_Gradient(ShaderClass::GRADIENT_MODULATE);
  126. VertexMaterialClass *vertex_material = NEW_REF( VertexMaterialClass, ());
  127. vertex_material->Set_Ambient(0.0f, 0.0f, 0.0f);
  128. vertex_material->Set_Diffuse(0.0f, 0.0f, 0.0f);
  129. vertex_material->Set_Specular(0.0f, 0.0f, 0.0f);
  130. vertex_material->Set_Emissive(1.0f, 1.0f, 1.0f);
  131. Set_Vertex_Material(vertex_material, true);
  132. vertex_material->Release_Ref();
  133. }
  134. // Set desired shader.
  135. Set_Shader(shader);
  136. // loop through the rows and columns of the image and make valid
  137. // textures from the pieces.
  138. for (int lpy = 0, tlpy=0; lpy < mh; lpy++, tlpy += piece) {
  139. for (int lpx = 0, tlpx = 0; lpx < mw; lpx++, tlpx += piece) {
  140. // figure the desired width and height of the texture (max "piece")
  141. int iw = MIN(piece, usable_width - (tlpx));
  142. int ih = MIN(piece, usable_height - (tlpy));
  143. int pot = MAX(Find_POT(iw), Find_POT(ih));
  144. // create the texture and turn MIP-mapping off.
  145. SurfaceClass *piece_surface=NEW_REF(SurfaceClass,(pot,pot,sd.Format));
  146. piece_surface->Copy(0,0,tlpx,tlpy,pot,pot,surface);
  147. TextureClass *piece_texture =NEW_REF(TextureClass,(piece_surface,MIP_LEVELS_1));
  148. piece_texture->Get_Filter().Set_U_Addr_Mode(TextureFilterClass::TEXTURE_ADDRESS_CLAMP);
  149. piece_texture->Get_Filter().Set_V_Addr_Mode(TextureFilterClass::TEXTURE_ADDRESS_CLAMP);
  150. REF_PTR_RELEASE(piece_surface);
  151. // calculate our actual texture coordinates based on the difference between
  152. // the width and height of the texture and the width and height the font
  153. // occupys.
  154. float tw = (float)iw / (float)pot;
  155. float th = (float)ih / (float)pot;
  156. // convert image width and image height to normalized values
  157. float vw = (float)iw / (float)resw;
  158. float vh = (float)ih / (float)resh;
  159. // figure out the screen space x and y positions of the object in question.
  160. float x = screen_x + (((float)tlpx) / (float)resw);
  161. float y = screen_y + (((float)tlpy) / (float)resh);
  162. Set_Texture(piece_texture);
  163. Begin_Tri_Strip();
  164. Vertex( x, y, 0, 0, 0);
  165. Vertex( x + vw, y, 0, tw, 0);
  166. Vertex( x, y + vh, 0, 0, th);
  167. Vertex( x + vw, y + vh, 0, tw, th);
  168. End_Tri_Strip();
  169. // release our reference to the texture
  170. REF_PTR_RELEASE(piece_texture);
  171. }
  172. }
  173. REF_PTR_RELEASE(tex);
  174. REF_PTR_RELEASE(surface);
  175. Set_Dirty();
  176. }
  177. Bitmap2DObjClass::Bitmap2DObjClass
  178. (
  179. TextureClass *texture,
  180. float screen_x,
  181. float screen_y,
  182. bool center,
  183. bool additive,
  184. bool colorizable,
  185. bool ignore_alpha
  186. )
  187. : DynamicScreenMeshClass(2, 4)
  188. {
  189. int resw, resh, resbits;
  190. bool windowed;
  191. // find the resolution (for centering and pixel to pixel translation)
  192. WW3D::Get_Device_Resolution(resw, resh, resbits, windowed);
  193. // This should be the correct way to do things
  194. // but other code expects an aspect ratio of 1.0
  195. // Hector Yee 2/22/01
  196. //Set_Aspect(resh/(float)resw);
  197. // Find the dimensions of the texture:
  198. // SurfaceClass::SurfaceDescription sd;
  199. // texture->Get_Level_Description(sd);
  200. if (!texture->Is_Initialized())
  201. TextureLoader::Request_Foreground_Loading(texture);
  202. // convert image width and image height to normalized values
  203. float vw = (float) texture->Get_Width() / (float)resw;
  204. float vh = (float) texture->Get_Height() / (float)resh;
  205. // if we requested the image to be centered around a point adjust the
  206. // coordinates accordingly.
  207. if (center) {
  208. screen_x -= vw / 2;
  209. screen_y -= vh / 2;
  210. }
  211. // Set shader to additive if requested, else alpha or opaque depending on whether the texture
  212. // has an alpha channel. Sorting is never set - if you wish to sort these types of objects you
  213. // should use static sort levels (note that static sort levels are not implemented for these
  214. // objects yet, but it should be very simple to do).
  215. ShaderClass shader;
  216. if (additive) {
  217. shader = ShaderClass::_PresetAdditive2DShader;
  218. } else {
  219. if (ignore_alpha == false && Has_Alpha(texture->Get_Texture_Format())) {
  220. shader = ShaderClass::_PresetAlpha2DShader;
  221. } else {
  222. shader = ShaderClass::_PresetOpaque2DShader;
  223. }
  224. }
  225. Disable_Sort();
  226. // If we want to be able to colorize this bitmap later (by setting
  227. // emissive color for the vertex material, or via a vertex emissive color
  228. // array) we need to enable the primary gradient in the shader (it is
  229. // disabled in the 2D presets), and set an appropriate vertex material.
  230. if (colorizable) {
  231. shader.Set_Primary_Gradient(ShaderClass::GRADIENT_MODULATE);
  232. VertexMaterialClass *vertex_material = NEW_REF( VertexMaterialClass, ());
  233. vertex_material->Set_Ambient(0.0f, 0.0f, 0.0f);
  234. vertex_material->Set_Diffuse(0.0f, 0.0f, 0.0f);
  235. vertex_material->Set_Specular(0.0f, 0.0f, 0.0f);
  236. vertex_material->Set_Emissive(1.0f, 1.0f, 1.0f);
  237. Set_Vertex_Material(vertex_material, true);
  238. vertex_material->Release_Ref();
  239. }
  240. // Set desired shader.
  241. Set_Shader(shader);
  242. // Set texture to requested texture:
  243. Set_Texture(texture);
  244. Begin_Tri_Strip();
  245. Vertex( screen_x, screen_y, 0, 0, 0);
  246. Vertex( screen_x + vw, screen_y, 0, 1.0, 0);
  247. Vertex( screen_x, screen_y + vh, 0, 0, 1.0);
  248. Vertex( screen_x + vw, screen_y + vh, 0, 1.0, 1.0);
  249. End_Tri_Strip();
  250. Set_Dirty();
  251. }
  252. RenderObjClass * Bitmap2DObjClass::Clone(void) const
  253. {
  254. return NEW_REF( Bitmap2DObjClass, (*this));
  255. }