Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
line3d.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 : G *
24 * *
25 * $Archive:: /Commando/Code/ww3d2/line3d.cpp $*
26 * *
27 * $Author:: Jani_p $*
28 * *
29 * $Modtime:: 7/05/01 4:15p $*
30 * *
31 * $Revision:: 11 $*
32 * *
33 *-------------------------------------------------------------------------*
34 * Functions: *
35 * Line3DClass::Line3DClass -- Constructor *
36 * Line3DClass::Line3DClass -- Copy constructor. *
37 * Line3DClass::operator = -- assignment operator *
38 * Line3DClass::~Line3DClass -- Destructor. *
39 * Line3DClass::Clone -- Creates a clone of this Line3D *
40 * Line3DClass::Scale -- Scale object *
41 * Line3DClass::Scale -- Scale object *
42 * Line3DClass::Update_Cached_Bounding_Volumes -- update bounding vols *
43 * Line3DClass::Reset -- Reset line start and end points. *
44 * Line3DClass::Reset -- Reset line start and end points, and line width.*
45 * Re_Color -- Reset the line color. *
46 * Set_Opacity -- Reset the line opacity. *
47 * Line3DClass::Render -- render the 3d line *
48 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
49
50#include "line3d.h"
51#include "vertmaterial.h"
52#include "shader.h"
53#include "wwdebug.h"
54#include "ww3d.h"
55#include "rinfo.h"
56#include "dx8wrapper.h"
57#include "dx8vertexbuffer.h"
58#include "dx8indexbuffer.h"
59#include "dx8fvf.h"
60
61// 12 Triangles for index buffer
62const unsigned short Indices[]=
63{
64 3,5,1,
65 7,5,3,
66 1,5,0,
67 5,4,0,
68 4,2,0,
69 4,6,2,
70 7,3,2,
71 6,7,2,
72 7,6,5,
73 5,6,4,
74 2,3,1,
75 2,1,0
76};
77
78
79/**************************************************************************
80 * Line3DClass::Line3DClass -- Constructor *
81 * *
82 * INPUT: Vector3 start, end - start, end points of line (world coords).*
83 * float width - width of line (in world units). *
84 * float r, g, b - R, G, B components of line color. *
85 * float opacity - opacity of line. *
86 * *
87 * OUTPUT: none. *
88 * *
89 * WARNINGS: *
90 * *
91 * HISTORY: *
92 * 01/15/1998 NH : Created. *
93 * 04/21/1998 NH : Ported to SR 1.3. *
94 * 02/16/2001 HY : Ported to DX8 *
95 *========================================================================*/
96Line3DClass::Line3DClass (const Vector3 & start, const Vector3 & end,
97 float width, float r, float g, float b, float opacity)
98{
99 Length = (end - start).Length();
100 Width = width;
101
102 // Create box model with origin at start point (X is major axis).
103
104 // 8 Vertices
105 float halfw = Width * 0.5f;
106
107 vert[0].X = 0.0f;
108 vert[0].Y = -halfw;
109 vert[0].Z = -halfw;
110 vert[1].X = 0.0f;
111 vert[1].Y = halfw;
112 vert[1].Z = -halfw;
113 vert[2].X = 0.0f;
114 vert[2].Y = -halfw;
115 vert[2].Z = halfw;
116 vert[3].X = 0.0f;
117 vert[3].Y = halfw;
118 vert[3].Z = halfw;
119 vert[4].X = Length;
120 vert[4].Y = -halfw;
121 vert[4].Z = -halfw;
122 vert[5].X = Length;
123 vert[5].Y = halfw;
124 vert[5].Z = -halfw;
125 vert[6].X = Length;
126 vert[6].Y = -halfw;
127 vert[6].Z = halfw;
128 vert[7].X = Length;
129 vert[7].Y = halfw;
130 vert[7].Z = halfw;
131
132 Color.X=r;
133 Color.Y=g;
134 Color.Z=b;
135 Set_Opacity(opacity);
136
137 // Set box transform so that the origin is at the start point and it
138 // 'looks towards' the endpoint.
139 Matrix3D transform(true);
140 transform.Obj_Look_At(start, end, 0.0);
141 Set_Transform(transform);
142}
143
144
145/**************************************************************************
146 * Line3DClass::Line3DClass -- Copy constructor. *
147 * *
148 * INPUT: const Line3DClass & src - source to copy from. *
149 * *
150 * OUTPUT: none. *
151 * *
152 * WARNINGS: *
153 * *
154 * HISTORY: *
155 * 01/15/1998 NH : Created. *
156 * 04/21/1998 NH : Ported to SR 1.3. *
157 * 02/16/2001 HY : Ported to DX8 *
158 *========================================================================*/
160 RenderObjClass(src),
161 Length(src.Length),
162 Width(src.Width),
163 Shader(src.Shader),
164 Color(src.Color)
165{
166 for (int i=0; i<8; i++) vert[i]=src.vert[i];
167}
168
169
170/**************************************************************************
171 * Line3DClass::operator = -- assignment operator *
172 * *
173 * INPUT: const Line3DClass & that - source to copy from. *
174 * *
175 * OUTPUT: Line3DClass & - result of assignment. *
176 * *
177 * WARNINGS: *
178 * *
179 * HISTORY: *
180 * 01/15/1998 NH : Created. *
181 * 04/21/1998 NH : Ported to SR 1.3. *
182 * 02/16/2001 HY : Ported to DX8 *
183 *========================================================================*/
185{
186 // Naty: need to add MatInfo and remapper to do this Byon
187 WWASSERT(0);
188
190
191 if (this != &that) {
192 Length = that.Length;
193 Width = that.Width;
194 Shader=that.Shader;
195 Color=that.Color;
196 for (int i=0; i<8; i++)
197 vert[i]=that.vert[i];
198 }
199
200 return * this;
201}
202
203
204/**************************************************************************
205 * Line3DClass::~Line3DClass -- Destructor. *
206 * *
207 * INPUT: none. *
208 * *
209 * OUTPUT: none. *
210 * *
211 * WARNINGS: *
212 * *
213 * HISTORY: *
214 * 01/15/1998 NH : Created. *
215 * 04/21/1998 NH : Ported to SR 1.3. *
216 * 02/16/2001 HY : Ported to DX8 *
217 *========================================================================*/
221
222
223/**************************************************************************
224 * Line3DClass::Clone -- Creates a clone of this Line3D *
225 * *
226 * INPUT: none. *
227 * *
228 * OUTPUT: RenderObjClass * - pointer to cloned object. *
229 * *
230 * WARNINGS: *
231 * *
232 * HISTORY: *
233 * 01/15/1998 NH : Created. *
234 *========================================================================*/
236{
237 return NEW_REF( Line3DClass, (*this));
238}
239
240/***********************************************************************************************
241 * Line3DClass::Render -- render the 3d line *
242 * *
243 * INPUT: *
244 * *
245 * OUTPUT: *
246 * *
247 * WARNINGS: *
248 * *
249 * HISTORY: *
250 * 12/8/98 GTH : Created. *
251 * 02/16/2001 HY : Ported to DX8 *
252 *=============================================================================================*/
253
255{
256 if (Is_Not_Hidden_At_All() == false) {
257 return;
258 }
259
260 // If static sort lists are enabled and this mesh has a sort level, put it on the list instead
261 // of rendering it.
262 unsigned int sort_level = (unsigned int)Get_Sort_Level();
263
265 {
266 WW3D::Add_To_Static_Sort_List(this, sort_level);
267 return;
268 }
269
274 REF_PTR_RELEASE(vm);
275
277
279 {
281 const FVFInfoClass &fi=vb.FVF_Info();
282 unsigned char *vb=(unsigned char*)Lock.Get_Formatted_Vertex_Array();
283 int i;
284 unsigned int color=DX8Wrapper::Convert_Color(Color);
285
286 for (i=0; i<8; i++)
287 {
288 *(Vector3*)(vb+fi.Get_Location_Offset())=vert[i];
289 *(unsigned int*)(vb+fi.Get_Diffuse_Offset())=color;
290 vb+=fi.Get_FVF_Size();
291 }
292 }
293
295 {
297 unsigned short *mem=Lock.Get_Index_Array();
298 try {
299 for (int i=0; i<36; i++)
300 mem[i]=Indices[i];
302 } catch(...) {
304 }
305 }
306
309 DX8Wrapper::Draw_Triangles(0,36/3,0,8);
310}
311
312/**************************************************************************
313 * Line3DClass::Scale -- Scale object *
314 * *
315 * INPUT: float scale - uniform scale factor. *
316 * *
317 * OUTPUT: none. *
318 * *
319 * WARNINGS: *
320 * *
321 * HISTORY: *
322 * 01/27/1998 NH : Created. *
323 * 04/21/1998 NH : Ported to SR 1.3. *
324 * 02/16/2001 HY : Ported to DX8 *
325 *========================================================================*/
327{
328 for (int i=0; i<8; i++) vert[i]*=scale;
329 Length *= scale;
330 Width *= scale;
331
333
334 // Now update the object space bounding volumes of this object's container:
335 RenderObjClass *container = Get_Container();
336 if (container) container->Update_Obj_Space_Bounding_Volumes();
337}
338
339
340/**************************************************************************
341 * Line3DClass::Scale -- Scale object *
342 * *
343 * INPUT: float scalex, scaley, scalez - axis scale factors. *
344 * *
345 * OUTPUT: none. *
346 * *
347 * WARNINGS: *
348 * *
349 * HISTORY: *
350 * 01/27/1998 NH : Created. *
351 * 04/21/1998 NH : Ported to SR 1.3. *
352 * 02/16/2001 HY : Ported to DX8 *
353 *========================================================================*/
354void Line3DClass::Scale(float scalex, float scaley, float scalez)
355{
356 // The line width is always the same in the y and z axes (the line
357 // approximates a cylinder).
358 Vector3 scale(scalex,scaley,scalez);
359 for (int i=0; i<8; i++) vert[i].Scale(scale);
360 Length *= scalex;
361 Width *= scaley;
362
364
365 // Now update the object space bounding volumes of this object's container:
366 RenderObjClass *container = Get_Container();
367 if (container) container->Update_Obj_Space_Bounding_Volumes();
368}
369
370
372{
373 float half_l = Length * 0.5f;
374 sphere.Center.Set(half_l, 0.0f, 0.0f);
375 sphere.Radius = half_l;
376}
377
378
380{
381 float half_l = Length * 0.5f;
382 box.Center.Set(half_l, 0.0f, 0.0f);
383 box.Extent.Set(half_l, 0.0f, 0.0f);
384}
385
386/**************************************************************************
387 * Line3DClass::Reset -- Reset line start and end points. *
388 * *
389 * INPUT: *
390 * *
391 * OUTPUT: *
392 * *
393 * WARNINGS: *
394 * *
395 * HISTORY: *
396 * 01/19/1998 NH : Created. *
397 * 04/21/1998 NH : Ported to SR 1.3. *
398 *========================================================================*/
399void Line3DClass::Reset(const Vector3 & new_start, const Vector3 & new_end)
400{
401 // Adjust length of line:
402 float new_length = (new_end - new_start).Length();
403 if (new_length == 0) {
404 new_length = 0.001f; // make sure we don't have a zero length BMG
405 }
406 Scale((new_length / Length), 1.0f, 1.0f);
407 Length = new_length;
408
409 // Adjust transform of line:
410 Matrix3D transform(true);
411 transform.Obj_Look_At(new_start, new_end, 0.0);
412 Set_Transform(transform);
413
415
416 // Now update the object space bounding volumes of this object's container:
417 RenderObjClass *container = Get_Container();
418 if (container) container->Update_Obj_Space_Bounding_Volumes();
419}
420
421
422/**************************************************************************
423 * Line3DClass::Reset -- Reset line start and end points, and line width. *
424 * *
425 * INPUT: *
426 * *
427 * OUTPUT: *
428 * *
429 * WARNINGS: *
430 * *
431 * HISTORY: *
432 * 01/19/1998 NH : Created. *
433 * 04/21/1998 NH : Ported to SR 1.3. *
434 *========================================================================*/
435void Line3DClass::Reset(const Vector3 & new_start, const Vector3 & new_end, float new_width)
436{
437 // Adjust length and width of line:
438 float new_length = (new_end - new_start).Length();
439 if (new_length == 0) {
440 new_length = 0.001f; // make sure we don't have a zero length BMG
441 }
442 float width_scale = new_width / Width;
443 Scale((new_length / Length), width_scale, width_scale);
444 Length = new_length;
445 Width = new_width;
446
447 // Adjust transform of line:
448 Matrix3D transform(true);
449 transform.Obj_Look_At(new_start, new_end, 0.0);
450 Set_Transform(transform);
451 Matrix3D inv;
452 transform.Get_Orthogonal_Inverse(inv);
453#ifdef ALLOW_TEMPORARIES
454// Vector3 test = inv * Vector3(new_end);
455#else
456// Vector3 test;
457// inv.mulVector3(new_end, test);
458#endif
459
461
462 // Now update the object space bounding volumes of this object's container:
463 RenderObjClass *container = Get_Container();
464 if (container) container->Update_Obj_Space_Bounding_Volumes();
465}
466
467
468/**************************************************************************
469 * Re_Color -- Reset the line color. *
470 * *
471 * INPUT: float r, g, b - components of the new color. *
472 * *
473 * OUTPUT: none. *
474 * *
475 * WARNINGS: *
476 * *
477 * HISTORY: *
478 * 01/26/1998 NH : Created. *
479 * 04/21/1998 NH : Ported to SR 1.3. *
480 *========================================================================*/
481void Line3DClass::Re_Color(float r, float g, float b)
482{
483 Color=Vector4(r,g,b,Color.W);
484}
485
486
487/**************************************************************************
488 * Set_Opacity -- Reset the line opacity. *
489 * *
490 * INPUT: float opacity - new opacity. *
491 * *
492 * OUTPUT: none. *
493 * *
494 * WARNINGS: *
495 * *
496 * HISTORY: *
497 * 11/03/1998 NH : Created. *
498 *========================================================================*/
499void Line3DClass::Set_Opacity(float opacity)
500{
501 if (opacity < 1.0f)
504 }
505 else
508 }
509 Color.W=opacity;
510}
511
512/*
513**
514*/
516{
517 return 12;
518}
519
#define NULL
Definition BaseType.h:92
Color scale(const Color &a, const Color &b)
Definition GameMtl.cpp:722
#define WWASSERT
#define SORT_LEVEL_NONE
Definition w3d_file.h:1195
Vector3 Center
Definition aabox.h:123
Vector3 Extent
Definition aabox.h:124
static void Set_Vertex_Buffer(const VertexBufferClass *vb, unsigned stream=0)
static void Set_Texture(unsigned stage, TextureBaseClass *texture)
static void Set_Index_Buffer(const IndexBufferClass *ib, unsigned short index_base_offset)
static Vector4 Convert_Color(unsigned color)
Definition dx8wrapper.h:958
static void Draw_Triangles(unsigned buffer_type, unsigned short start_index, unsigned short polygon_count, unsigned short min_vertex_index, unsigned short vertex_count)
static void Set_Material(const VertexMaterialClass *material)
static void Set_Shader(const ShaderClass &shader)
static void Set_Transform(D3DTRANSFORMSTATETYPE transform, const Matrix4x4 &m)
VertexFormatXYZNDUV2 * Get_Formatted_Vertex_Array()
const FVFInfoClass & FVF_Info() const
unsigned Get_FVF_Size() const
Definition dx8fvf.h:280
unsigned Get_Diffuse_Offset() const
Definition dx8fvf.h:277
unsigned Get_Location_Offset() const
Definition dx8fvf.h:269
virtual RenderObjClass * Clone(void) const
Definition line3d.cpp:235
virtual void Get_Obj_Space_Bounding_Box(AABoxClass &box) const
Definition line3d.cpp:379
virtual int Get_Num_Polys(void) const
Definition line3d.cpp:515
virtual void Scale(float scale)
Definition line3d.cpp:326
Vector3 vert[8]
Definition line3d.h:118
void Reset(const Vector3 &new_start, const Vector3 &new_end)
Definition line3d.cpp:399
void Set_Opacity(float opacity)
Definition line3d.cpp:499
virtual void Render(RenderInfoClass &rfinfo)
Definition line3d.cpp:254
void Re_Color(float r, float g, float b)
Definition line3d.cpp:481
virtual ~Line3DClass(void)
Definition line3d.cpp:218
virtual void Get_Obj_Space_Bounding_Sphere(SphereClass &sphere) const
Definition line3d.cpp:371
Vector4 Color
Definition line3d.h:120
void Set_Sort_Level(int level)
Definition line3d.h:104
float Length
Definition line3d.h:110
float Width
Definition line3d.h:113
Line3DClass(const Vector3 &start, const Vector3 &end, float width, float r, float g, float b, float opacity=1.0f)
Definition line3d.cpp:96
ShaderClass Shader
Definition line3d.h:116
Line3DClass & operator=(const Line3DClass &that)
Definition line3d.cpp:184
int Get_Sort_Level(void) const
Definition line3d.h:105
void Get_Orthogonal_Inverse(Matrix3D &set_inverse) const
Definition matrix3d.cpp:570
void Obj_Look_At(const Vector3 &p, const Vector3 &t, float roll)
Definition matrix3d.cpp:458
virtual void Set_Transform(const Matrix3D &m)
Definition rendobj.cpp:423
virtual int Is_Not_Hidden_At_All(void)
Definition rendobj.h:463
RenderObjClass(void)
Definition rendobj.cpp:170
virtual void Update_Obj_Space_Bounding_Volumes(void)
Definition rendobj.h:394
void Invalidate_Cached_Bounding_Volumes(void) const
Definition rendobj.h:523
RenderObjClass & operator=(const RenderObjClass &)
Definition rendobj.cpp:232
RenderObjClass * Get_Container(void) const
Definition rendobj.h:291
Matrix3D Transform
Definition rendobj.h:549
static ShaderClass _PresetAlphaSolidShader
Definition shader.h:417
static ShaderClass _PresetOpaqueSolidShader
Definition shader.h:407
float Radius
Definition sphere.h:91
Vector3 Center
Definition sphere.h:90
WWINLINE void Set(float x, float y, float z)
Definition vector3.h:103
static VertexMaterialClass * Get_Preset(PresetType type)
static void Add_To_Static_Sort_List(RenderObjClass *robj, unsigned int sort_level)
Definition ww3d.cpp:1984
static bool Are_Static_Sort_Lists_Enabled(void)
Definition ww3d.h:282
int IndexBufferExceptionFunc(void)
const unsigned dynamic_fvf_type
@ BUFFER_TYPE_DYNAMIC_DX8
Definition dx8wrapper.h:90
const unsigned short Indices[]
Definition line3d.cpp:62
#define REF_PTR_RELEASE(x)
Definition refcount.h:80
#define NEW_REF(C, P)
Definition refcount.h:62