Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
bmp2d.cpp
Go to the documentation of this file.
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/***************************************************************************
20 *** 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 ***
21 ***************************************************************************
22 * *
23 * Project Name : Commando/G *
24 * *
25 * $Archive:: /Commando/Code/ww3d2/bmp2d.cpp $*
26 * *
27 * $Org Author:: Jani_p $*
28 * *
29 * $Author:: Kenny_m $*
30 * *
31 * $Modtime:: 08/05/02 10:44a $*
32 * *
33 * $Revision:: 12 $*
34 * *
35 * 08/05/02 KM Texture class redesign
36 *-------------------------------------------------------------------------*
37 * Functions: *
38 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
39
40#include "bmp2d.h"
41#include "pot.h"
42#include "ww3d.h"
43#include "texture.h"
44#include "surfaceclass.h"
45#include "assetmgr.h"
46#include "textureloader.h"
47#include "ww3dformat.h"
48
50(
51 const char *filename,
52 float screen_x,
53 float screen_y,
54 bool center,
55 bool additive,
56 bool colorizable,
57 int usable_width,
58 int usable_height,
59 bool ignore_alpha
60)
62{
63
64 int resw, resh, resbits;
65 bool windowed;
66
67 // find the resolution (for centering and pixel to pixel translation)
68 WW3D::Get_Device_Resolution(resw, resh, resbits, windowed);
69 // This should be the correct way to do things
70 // but other code expects an aspect ratio of 1.0
71 // Hector Yee 2/22/01
72 // Set_Aspect(resh/(float)resw);
73
74
75 // load up the surfaces file name
77 if (!tex->Is_Initialized())
79
80 SurfaceClass *surface = tex->Get_Surface_Level(0);
81
82 if (!surface) {
84 }
85
87 surface->Get_Description(sd);
88
89 if (usable_width == -1)
90 usable_width = sd.Width;
91 if (usable_height == -1)
92 usable_height = sd.Height;
93
94 // if we requested the image to be centered around a point adjust the
95 // coordinates accordingly.
96 if (center) {
97 screen_x -= ((float)usable_width / resw) / 2;
98 screen_y -= ((float)usable_height / resh) / 2;
99 }
100
101 // The image will be broken down into square textures. The size of these
102 // textures will be the smallest POT (power of two) which is equal or
103 // greater than the smaller dimension of the image. Also, the pieces can
104 // never be larger than 256 texels because some rendering devices don't
105 // support textures larger than that.
106 int surf_w = usable_width;
107 int surf_h = usable_height;
108 int piece = Find_POT(MIN(surf_w, surf_h));
109 piece = MIN(piece, 256);
110
111 // now take the image in question and break it down into
112 // "piece"x"piece"-pixel polygons and calculate the number of textures
113 // based from those calculations.
114 int mw = (surf_w & (piece - 1)) ? (surf_w / piece)+1 : (surf_w /piece);
115 int mh = (surf_h & (piece - 1)) ? (surf_h / piece)+1 : (surf_h /piece);
116
117 // for every square texture it takes four vertexes to express the two
118 // polygons.
119 Resize(mw * mh *2, mw * mh * 4);
120
121 // Set shader to additive if requested, else alpha or opaque depending on
122 // whether the texture has an alpha channel. Sorting is always set so that
123 // sortbias can be used to determine occlusion between the various 2D
124 // elements.
125 ShaderClass shader;
126
127 if (additive) {
129 } else {
130 if (ignore_alpha == false && Has_Alpha(sd.Format)) {
132 } else {
134 }
135 }
136
137 Enable_Sort();
138
139
140 // If we want to be able to colorize this bitmap later (by setting
141 // emissive color for the vertex material, or via a vertex emissive color
142 // array) we need to enable the primary gradient in the shader (it is
143 // disabled in the 2D presets), and set an appropriate vertex material.
144 if (colorizable) {
146 VertexMaterialClass *vertex_material = NEW_REF( VertexMaterialClass, ());
147 vertex_material->Set_Ambient(0.0f, 0.0f, 0.0f);
148 vertex_material->Set_Diffuse(0.0f, 0.0f, 0.0f);
149 vertex_material->Set_Specular(0.0f, 0.0f, 0.0f);
150 vertex_material->Set_Emissive(1.0f, 1.0f, 1.0f);
151 Set_Vertex_Material(vertex_material, true);
152 vertex_material->Release_Ref();
153 }
154
155 // Set desired shader.
156 Set_Shader(shader);
157
158 // loop through the rows and columns of the image and make valid
159 // textures from the pieces.
160 for (int lpy = 0, tlpy=0; lpy < mh; lpy++, tlpy += piece) {
161 for (int lpx = 0, tlpx = 0; lpx < mw; lpx++, tlpx += piece) {
162
163 // figure the desired width and height of the texture (max "piece")
164 int iw = MIN(piece, usable_width - (tlpx));
165 int ih = MIN(piece, usable_height - (tlpy));
166 int pot = MAX(Find_POT(iw), Find_POT(ih));
167
168 // create the texture and turn MIP-mapping off.
169 SurfaceClass *piece_surface=NEW_REF(SurfaceClass,(pot,pot,sd.Format));
170 piece_surface->Copy(0,0,tlpx,tlpy,pot,pot,surface);
171 TextureClass *piece_texture =NEW_REF(TextureClass,(piece_surface,MIP_LEVELS_1));
174 REF_PTR_RELEASE(piece_surface);
175
176 // calculate our actual texture coordinates based on the difference between
177 // the width and height of the texture and the width and height the font
178 // occupys.
179 float tw = (float)iw / (float)pot;
180 float th = (float)ih / (float)pot;
181
182 // convert image width and image height to normalized values
183 float vw = (float)iw / (float)resw;
184 float vh = (float)ih / (float)resh;
185
186 // figure out the screen space x and y positions of the object in question.
187 float x = screen_x + (((float)tlpx) / (float)resw);
188 float y = screen_y + (((float)tlpy) / (float)resh);
189
190 Set_Texture(piece_texture);
192 Vertex( x, y, 0, 0, 0);
193 Vertex( x + vw, y, 0, tw, 0);
194 Vertex( x, y + vh, 0, 0, th);
195 Vertex( x + vw, y + vh, 0, tw, th);
197
198 // release our reference to the texture
199 REF_PTR_RELEASE(piece_texture);
200 }
201 }
202 REF_PTR_RELEASE(tex);
203 REF_PTR_RELEASE(surface);
204
205 Set_Dirty();
206}
207
209(
210 TextureClass *texture,
211 float screen_x,
212 float screen_y,
213 bool center,
214 bool additive,
215 bool colorizable,
216 bool ignore_alpha
217)
219{
220 int resw, resh, resbits;
221 bool windowed;
222
223 // find the resolution (for centering and pixel to pixel translation)
224 WW3D::Get_Device_Resolution(resw, resh, resbits, windowed);
225 // This should be the correct way to do things
226 // but other code expects an aspect ratio of 1.0
227 // Hector Yee 2/22/01
228 //Set_Aspect(resh/(float)resw);
229
230 // Find the dimensions of the texture:
231// SurfaceClass::SurfaceDescription sd;
232// texture->Get_Level_Description(sd);
233
234 if (!texture->Is_Initialized())
236
237 // convert image width and image height to normalized values
238 float vw = (float) texture->Get_Width() / (float)resw;
239 float vh = (float) texture->Get_Height() / (float)resh;
240
241 // if we requested the image to be centered around a point adjust the
242 // coordinates accordingly.
243 if (center) {
244 screen_x -= vw / 2;
245 screen_y -= vh / 2;
246 }
247
248 // Set shader to additive if requested, else alpha or opaque depending on whether the texture
249 // has an alpha channel. Sorting is never set - if you wish to sort these types of objects you
250 // should use static sort levels (note that static sort levels are not implemented for these
251 // objects yet, but it should be very simple to do).
252 ShaderClass shader;
253
254 if (additive) {
256 } else {
257 if (ignore_alpha == false && Has_Alpha(texture->Get_Texture_Format())) {
259 } else {
261 }
262 }
263 Disable_Sort();
264
265 // If we want to be able to colorize this bitmap later (by setting
266 // emissive color for the vertex material, or via a vertex emissive color
267 // array) we need to enable the primary gradient in the shader (it is
268 // disabled in the 2D presets), and set an appropriate vertex material.
269 if (colorizable) {
271 VertexMaterialClass *vertex_material = NEW_REF( VertexMaterialClass, ());
272 vertex_material->Set_Ambient(0.0f, 0.0f, 0.0f);
273 vertex_material->Set_Diffuse(0.0f, 0.0f, 0.0f);
274 vertex_material->Set_Specular(0.0f, 0.0f, 0.0f);
275 vertex_material->Set_Emissive(1.0f, 1.0f, 1.0f);
276 Set_Vertex_Material(vertex_material, true);
277 vertex_material->Release_Ref();
278 }
279
280 // Set desired shader.
281 Set_Shader(shader);
282
283 // Set texture to requested texture:
284 Set_Texture(texture);
285
287 Vertex( screen_x, screen_y, 0, 0, 0);
288 Vertex( screen_x + vw, screen_y, 0, 1.0, 0);
289 Vertex( screen_x, screen_y + vh, 0, 0, 1.0);
290 Vertex( screen_x + vw, screen_y + vh, 0, 1.0, 1.0);
292
293 Set_Dirty();
294}
295
297{
298 return NEW_REF( Bitmap2DObjClass, (*this));
299}
#define MIN(a, b)
Definition always.h:189
#define MAX(a, b)
Definition always.h:185
int Find_POT(int val)
Definition pot.cpp:53
virtual RenderObjClass * Clone(void) const
Definition bmp2d.cpp:296
Bitmap2DObjClass(const char *filename, float norm_x, float norm_y, bool center, bool additive, bool colorizable=false, int width=-1, int height=-1, bool ignore_alpha=false)
Definition bmp2d.cpp:50
bool Vertex(float x, float y, float z, float u, float v)
Definition dynamesh.h:325
void Disable_Sort(void)
Definition dynamesh.h:386
void Resize(int max_polys, int max_verts)
Definition dynamesh.cpp:603
void Set_Dirty(void)
Definition dynamesh.h:399
void Enable_Sort(void)
Definition dynamesh.h:387
int Set_Shader(const ShaderClass &shader, int pass=0)
Definition dynamesh.h:183
int Set_Texture(int idx, int pass=0)
Definition dynamesh.cpp:743
int Set_Vertex_Material(int idx, int pass=0)
Definition dynamesh.cpp:681
void Begin_Tri_Strip(void)
Definition dynamesh.h:240
void End_Tri_Strip(void)
Definition dynamesh.h:340
DynamicScreenMeshClass(int max_poly, int max_vert, float aspect=1.0f)
Definition dynamesh.h:548
WWINLINE void Release_Ref(void) const
Definition refcount.h:146
RenderObjClass(void)
Definition rendobj.cpp:170
@ GRADIENT_MODULATE
Definition shader.h:193
static ShaderClass _PresetAdditive2DShader
Definition shader.h:389
static ShaderClass _PresetAlpha2DShader
Definition shader.h:393
void Set_Primary_Gradient(PriGradientType x)
Definition shader.h:332
static ShaderClass _PresetOpaque2DShader
Definition shader.h:381
void Get_Description(SurfaceDescription &surface_desc)
void Copy(unsigned int dstx, unsigned int dsty, unsigned int srcx, unsigned int srcy, unsigned int width, unsigned int height, const SurfaceClass *other)
int Get_Width() const
Definition texture.h:124
int Get_Height() const
Definition texture.h:128
bool Is_Initialized() const
Definition texture.h:145
WW3DFormat Get_Texture_Format() const
Definition texture.h:338
SurfaceClass * Get_Surface_Level(unsigned int level=0)
Get surface from mip level.
Definition texture.cpp:986
TextureFilterClass & Get_Filter()
Definition texture.h:336
void Set_V_Addr_Mode(TxtAddrMode mode)
void Set_U_Addr_Mode(TxtAddrMode mode)
static void Request_Foreground_Loading(TextureBaseClass *tc)
void Set_Specular(const Vector3 &color)
void Set_Diffuse(const Vector3 &color)
void Set_Ambient(const Vector3 &color)
void Set_Emissive(const Vector3 &color)
virtual TextureClass * Get_Texture(const char *filename, MipCountType mip_level_count=MIP_LEVELS_ALL, WW3DFormat texture_format=WW3D_FORMAT_UNKNOWN, bool allow_compression=true, TextureBaseClass::TexAssetType type=TextureBaseClass::TEX_REGULAR, bool allow_reduction=true)
static WW3DAssetManager * Get_Instance(void)
Definition assetmgr.h:205
static void Get_Device_Resolution(int &set_w, int &set_h, int &get_bits, bool &get_windowed)
Definition ww3d.cpp:670
#define REF_PTR_RELEASE(x)
Definition refcount.h:80
#define NEW_REF(C, P)
Definition refcount.h:62
@ MIP_LEVELS_1
WW3DFormat Get_Valid_Texture_Format(WW3DFormat format, bool is_compression_allowed)
bool Has_Alpha(WW3DFormat format)
Definition ww3dformat.h:126
@ WW3D_FORMAT_R8G8B8
Definition ww3dformat.h:77