seglinerenderer.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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 : ww3d *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/seglinerenderer.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Ian_l $*
  29. * *
  30. * $Modtime:: 7/12/01 3:20p $*
  31. * *
  32. * $Revision:: 3 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #if 1
  38. #include "seglinerenderer.h"
  39. #include "ww3d.h"
  40. #include "rinfo.h"
  41. #include "dx8wrapper.h"
  42. #include "sortingrenderer.h"
  43. #include "vp.h"
  44. #include "vector3i.h"
  45. #include "random.h"
  46. #include "v3_rnd.h"
  47. /* We have chunking logic which handles N segments at a time. To simplify the subdivision logic,
  48. ** we will ensure that N is a power of two and that N >= 2^MAX_SEGLINE_SUBDIV_LEVELS, so that the
  49. ** subdivision logic can be inside the chunking loop.
  50. */
  51. #if MAX_SEGLINE_SUBDIV_LEVELS > 7
  52. #define SEGLINE_CHUNK_SIZE (1 << MAX_SEGLINE_SUBDIV_LEVELS)
  53. #else
  54. #define SEGLINE_CHUNK_SIZE (128)
  55. #endif
  56. #define MAX_SEGLINE_POINT_BUFFER_SIZE (1 + SEGLINE_CHUNK_SIZE)
  57. // This macro depends on the assumption that each line segment is two polys.
  58. #define MAX_SEGLINE_POLY_BUFFER_SIZE (SEGLINE_CHUNK_SIZE * 2)
  59. SegLineRendererClass::SegLineRendererClass(void) :
  60. Texture(NULL),
  61. Shader(ShaderClass::_PresetAdditiveSpriteShader),
  62. Width(0.0f),
  63. Color(Vector3(1,1,1)),
  64. Opacity(1.0f),
  65. SubdivisionLevel(0),
  66. NoiseAmplitude(0.0f),
  67. MergeAbortFactor(1.5f),
  68. TextureTileFactor(1.0f),
  69. LastUsedSyncTime(WW3D::Get_Sync_Time()),
  70. CurrentUVOffset(0.0f,0.0f),
  71. UVOffsetDeltaPerMS(0.0f, 0.0f),
  72. Bits(DEFAULT_BITS)
  73. {
  74. }
  75. SegLineRendererClass::SegLineRendererClass(const SegLineRendererClass & that) :
  76. Texture(NULL),
  77. Shader(ShaderClass::_PresetAdditiveSpriteShader),
  78. Width(0.0f),
  79. Color(Vector3(1,1,1)),
  80. Opacity(1.0f),
  81. SubdivisionLevel(0),
  82. NoiseAmplitude(0.0f),
  83. MergeAbortFactor(1.5f),
  84. TextureTileFactor(1.0f),
  85. LastUsedSyncTime(that.LastUsedSyncTime),
  86. CurrentUVOffset(0.0f,0.0f),
  87. UVOffsetDeltaPerMS(0.0f, 0.0f),
  88. Bits(DEFAULT_BITS)
  89. {
  90. *this = that;
  91. }
  92. SegLineRendererClass & SegLineRendererClass::operator = (const SegLineRendererClass & that)
  93. {
  94. if (this != &that) {
  95. REF_PTR_SET(Texture,that.Texture);
  96. Shader = that.Shader;
  97. Width = that.Width;
  98. Color = that.Color;
  99. Opacity = that.Opacity;
  100. SubdivisionLevel = that.SubdivisionLevel;
  101. NoiseAmplitude = that.NoiseAmplitude;
  102. MergeAbortFactor = that.MergeAbortFactor;
  103. TextureTileFactor = that.TextureTileFactor;
  104. LastUsedSyncTime = that.LastUsedSyncTime;
  105. CurrentUVOffset = that.CurrentUVOffset;
  106. UVOffsetDeltaPerMS = that.UVOffsetDeltaPerMS;
  107. Bits = that.Bits;
  108. }
  109. return *this;
  110. }
  111. SegLineRendererClass::~SegLineRendererClass(void)
  112. {
  113. REF_PTR_RELEASE(Texture);
  114. }
  115. void SegLineRendererClass::Init(const W3dEmitterLinePropertiesStruct & props)
  116. {
  117. // translate the flags
  118. Set_Merge_Intersections(props.Flags & W3D_ELINE_MERGE_INTERSECTIONS);
  119. Set_Freeze_Random(props.Flags & W3D_ELINE_FREEZE_RANDOM);
  120. Set_Disable_Sorting(props.Flags & W3D_ELINE_DISABLE_SORTING);
  121. Set_End_Caps(props.Flags & W3D_ELINE_END_CAPS);
  122. int texture_mode = ((props.Flags & W3D_ELINE_TEXTURE_MAP_MODE_MASK) >> W3D_ELINE_TEXTURE_MAP_MODE_OFFSET);
  123. switch (texture_mode)
  124. {
  125. case W3D_ELINE_UNIFORM_WIDTH_TEXTURE_MAP:
  126. Set_Texture_Mapping_Mode(UNIFORM_WIDTH_TEXTURE_MAP);
  127. break;
  128. case W3D_ELINE_UNIFORM_LENGTH_TEXTURE_MAP:
  129. Set_Texture_Mapping_Mode(UNIFORM_LENGTH_TEXTURE_MAP);
  130. break;
  131. case W3D_ELINE_TILED_TEXTURE_MAP:
  132. Set_Texture_Mapping_Mode(TILED_TEXTURE_MAP);
  133. break;
  134. };
  135. // install all other settings
  136. Set_Current_Subdivision_Level(props.SubdivisionLevel);
  137. Set_Noise_Amplitude(props.NoiseAmplitude);
  138. Set_Merge_Abort_Factor(props.MergeAbortFactor);
  139. Set_Texture_Tile_Factor(props.TextureTileFactor);
  140. Set_UV_Offset_Rate(Vector2(props.UPerSec,props.VPerSec));
  141. }
  142. void SegLineRendererClass::Set_Texture(TextureClass *texture)
  143. {
  144. REF_PTR_SET(Texture,texture);
  145. }
  146. TextureClass * SegLineRendererClass::Get_Texture(void) const
  147. {
  148. if (Texture != NULL) {
  149. Texture->Add_Ref();
  150. }
  151. return Texture;
  152. }
  153. void SegLineRendererClass::Set_Current_UV_Offset(const Vector2 & offset)
  154. {
  155. CurrentUVOffset = offset;
  156. }
  157. void SegLineRendererClass::Set_Texture_Tile_Factor(float factor)
  158. {
  159. if (factor > 50.0f) { ///@todo: I raised this number and didn't see much difference on our min-spec. -MW
  160. factor = 50.0f;
  161. WWDEBUG_SAY(("Texture Tile Factor too large in SegLineRendererClass!\r\n"));
  162. } else {
  163. factor = MAX(factor, 0.0f);
  164. }
  165. TextureTileFactor = factor;
  166. }
  167. void SegLineRendererClass::Reset_Line(void)
  168. {
  169. LastUsedSyncTime = WW3D::Get_Sync_Time();
  170. CurrentUVOffset.Set(0.0f,0.0f);
  171. }
  172. void SegLineRendererClass::Render
  173. (
  174. RenderInfoClass & rinfo,
  175. const Matrix3D & transform,
  176. unsigned int num_points,
  177. Vector3 * points,
  178. const SphereClass & obj_sphere
  179. )
  180. {
  181. Matrix4 view;
  182. DX8Wrapper::Get_Transform(D3DTS_VIEW,view);
  183. Matrix4 identity(true);
  184. DX8Wrapper::Set_Transform(D3DTS_WORLD,identity);
  185. DX8Wrapper::Set_Transform(D3DTS_VIEW,identity);
  186. /*
  187. ** Handle texture UV offset animation (done once for entire line).
  188. */
  189. unsigned int delta = WW3D::Get_Sync_Time() - LastUsedSyncTime;
  190. float del = (float)delta;
  191. Vector2 uv_offset = CurrentUVOffset + UVOffsetDeltaPerMS * del;
  192. // ensure offsets are in [0, 1] range:
  193. uv_offset.X = uv_offset.X - floorf(uv_offset.X);
  194. uv_offset.Y = uv_offset.Y - floorf(uv_offset.Y);
  195. // Update state
  196. CurrentUVOffset = uv_offset;
  197. LastUsedSyncTime = WW3D::Get_Sync_Time();
  198. // Used later
  199. TextureMapMode map_mode = Get_Texture_Mapping_Mode();
  200. /*
  201. ** Process line geometry:
  202. */
  203. // This has been tweaked to produce empirically good results.
  204. const float parallel_factor = 0.9f;
  205. // We reduce the chunk size to take account of subdivision levels (so that the # of points
  206. // after subdivision will be no higher than the allowed maximum). We know this will not reduce
  207. // the chunk size below 2, since the chunk size must be at least two to the power of the
  208. // maximum allowable number of subdivisions. The plus 1 is because #points = #segments + 1.
  209. unsigned int chunk_size = (SEGLINE_CHUNK_SIZE >> SubdivisionLevel) + 1;
  210. if (chunk_size > num_points) chunk_size = num_points;
  211. // Chunk through the points (we increment by chunk_size - 1 because the last point of this
  212. // chunk must be reused as the first point of the next chunk. This is also the reason we stop
  213. // when chidx = NumPoints - 1: the last point has already been processed in the previous
  214. // iteration so we don't need another one).
  215. for (unsigned int chidx = 0; chidx < num_points - 1; chidx += (chunk_size - 1)) {
  216. unsigned int point_cnt = num_points - chidx;
  217. point_cnt = MIN(point_cnt, chunk_size);
  218. // We use these different loop indices (which loop INSIDE a chunk) to improve readability:
  219. unsigned int pidx; // Point index
  220. unsigned int sidx; // Segment index
  221. unsigned int iidx; // Intersection index
  222. /*
  223. ** Transform points in chunk from objectspace to eyespace:
  224. */
  225. Vector3 xformed_pts[MAX_SEGLINE_POINT_BUFFER_SIZE];
  226. Matrix3D view2( view[0].X,view[0].Y,view[0].Z,view[0].W,
  227. view[1].X,view[1].Y,view[1].Z,view[1].W,
  228. view[2].X,view[2].Y,view[2].Z,view[2].W);
  229. #ifdef ALLOW_TEMPORARIES
  230. Matrix3D modelview=view2*transform;
  231. #else
  232. Matrix3D modelview;
  233. modelview.mul(view2, transform);
  234. #endif
  235. VectorProcessorClass::Transform(&xformed_pts[0],
  236. &points[chidx], modelview, point_cnt);
  237. /*
  238. ** Prepare v parameter per point - used for texture mapping (esp. tiled mapping mode)
  239. */
  240. float base_tex_v[MAX_SEGLINE_POINT_BUFFER_SIZE];
  241. float u_values[2];
  242. switch (map_mode) {
  243. case UNIFORM_WIDTH_TEXTURE_MAP:
  244. for (pidx = 0; pidx < point_cnt; pidx++) {
  245. // All 0
  246. base_tex_v[pidx] = 0.0f;
  247. }
  248. u_values[0] = 0.0f;
  249. u_values[1] = 1.0f;
  250. break;
  251. case UNIFORM_LENGTH_TEXTURE_MAP:
  252. for (pidx = 0; pidx < point_cnt; pidx++) {
  253. // Increasing V
  254. base_tex_v[pidx] = (float)(pidx + chidx) * TextureTileFactor;
  255. }
  256. u_values[0] = 0.0f;
  257. u_values[1] = 0.0f;
  258. break;
  259. case TILED_TEXTURE_MAP:
  260. for (pidx = 0; pidx < point_cnt; pidx++) {
  261. // Increasing V
  262. base_tex_v[pidx] = (float)(pidx + chidx) * TextureTileFactor;
  263. }
  264. u_values[0] = 0.0f;
  265. u_values[1] = 1.0f;
  266. break;
  267. }
  268. /*
  269. ** Fractal noise recursive subdivision:
  270. ** We find the midpoint for each section, apply a random offset, and recurse. We also find
  271. ** the average V coordinate of the endpoints which is the midpoint V (for tiled texture
  272. ** mapping).
  273. */
  274. Vector3 xformed_subdiv_pts[MAX_SEGLINE_POINT_BUFFER_SIZE];
  275. float subdiv_tex_v[MAX_SEGLINE_POINT_BUFFER_SIZE];
  276. unsigned int sub_point_cnt;
  277. subdivision_util(point_cnt, xformed_pts, base_tex_v, &sub_point_cnt, xformed_subdiv_pts, subdiv_tex_v);
  278. // Start using subdivided points from now on
  279. Vector3 *points = xformed_subdiv_pts;
  280. float *tex_v = subdiv_tex_v;
  281. point_cnt = sub_point_cnt;
  282. /*
  283. ** Calculate line segment edge planes:
  284. */
  285. // For each line segment find the two silhouette planes from eyepoint to the line segment
  286. // cylinder. To simplify we do not find the tangent planes but intersect the cylinder with a
  287. // plane passing through its axis and perpendicular to the eye vector, find the edges of the
  288. // resulting rectangle, and create planes through these edges and the eyepoint.
  289. // Note that these planes are represented as a single normal rather than a normal and a
  290. // distance; this is because they pass through the origin (eyepoint) so their distance is
  291. // always zero.
  292. // Since the line has thickness, each segment has two edges. We name these 'top' and
  293. // 'bottom' - note however that the top/bottom distinction does not relate to screen
  294. // up/down and remains consistent throughout the segmented line.
  295. enum SegmentEdge {
  296. FIRST_EDGE = 0, // For loop conditions
  297. TOP_EDGE = 0, // Top Edge
  298. BOTTOM_EDGE = 1, // Bottom Edge
  299. MAX_EDGE = 1, // For loop conditions
  300. NUM_EDGES = 2 // For array allocations
  301. };
  302. bool switch_edges = false;
  303. // We have dummy segments for "before the first point" and "after the last point" - in these
  304. // segments the top and bottom edge are the same - they are a perpendicular plane defined by
  305. // the endpoint vertices. This is so we can merge intersections properly for the first and
  306. // last points.
  307. struct LineSegment {
  308. Vector3 StartPlane;
  309. Vector3 EdgePlane[NUM_EDGES];
  310. };
  311. // # segments = numpoints + 1 (numpoints - 1, plus two dummy segments)
  312. LineSegment segment[MAX_SEGLINE_POINT_BUFFER_SIZE + 1];
  313. // Intersections. This has data for two edges (top or bottom) intersecting.
  314. struct LineSegmentIntersection {
  315. unsigned int PointCount; // How many points does this intersection represent
  316. unsigned int NextSegmentID; // ID of segment after this intersection
  317. Vector3 Direction; // Calculated intersection direction line
  318. Vector3 Point; // Averaged 3D point on the line which this represents
  319. float TexV; // Averaged texture V coordinate of points
  320. bool Fold; // Does the line fold over at this intersection?
  321. bool Parallel; // Edges at this intersection are parallel (or almost-)
  322. };
  323. // Used to calculate the edge planes
  324. float radius = Width * 0.5f;
  325. // The number of intersections is the number of points minus 2. However, we store
  326. // intersection records for the first and last point, even though they are not really
  327. // intersections. The reason we do this is for the intersection merging - the vertices for
  328. // the first and last points can get merged just like any other intersection. Also, we have
  329. // a dummy intersection record before the first point - this is because we want "previous
  330. // segments" for the first point and each intersection only has an index for the next
  331. // segment.
  332. LineSegmentIntersection intersection[MAX_SEGLINE_POINT_BUFFER_SIZE + 1][NUM_EDGES];
  333. for (sidx = 1; sidx < point_cnt; sidx++) { // #segments = #points - 1 (+ 2 dummy segments)
  334. Vector3 &curr_point = points[sidx - 1];
  335. Vector3 &next_point = points[sidx];
  336. // We temporarily store the segment direction in the segment's StartPlane (since it is
  337. // used to calculate the StartPlane later).
  338. Vector3 &segdir = segment[sidx].StartPlane;
  339. segdir = next_point - curr_point;
  340. segdir.Normalize();
  341. // Find nearest point on infinite line to eye (origin)
  342. Vector3 nearest = curr_point + segdir * -Vector3::Dot_Product(segdir, curr_point);
  343. // Find top and bottom points on cylinder
  344. Vector3 offset;
  345. Vector3::Cross_Product(segdir, nearest, &offset);
  346. offset.Normalize();
  347. Vector3 top = curr_point + offset * radius;
  348. Vector3 bottom = curr_point + offset * -radius;
  349. // Find planes through top/bottom points and eyepoint. In addition to the two points, we
  350. // know that the planes are parallel to the line segment.
  351. Vector3 top_normal;
  352. Vector3::Cross_Product(top, segdir, &top_normal);
  353. top_normal.Normalize();
  354. segment[sidx].EdgePlane[TOP_EDGE] = top_normal;
  355. Vector3 bottom_normal;
  356. Vector3::Cross_Product(segdir, bottom, &bottom_normal);
  357. bottom_normal.Normalize();
  358. segment[sidx].EdgePlane[BOTTOM_EDGE] = bottom_normal;
  359. // If the visual angle between the previous and current line segments (we use the angle
  360. // between the planes defined by each line segment and the eyepoint) is less than 90
  361. // degrees, switch the top and bottom edges for the current and subsequent segments and
  362. // mark the intersection as having a fold
  363. if (sidx > 1) {
  364. Vector3 prev_plane;
  365. Vector3::Cross_Product(points[sidx - 2], curr_point, &prev_plane);
  366. prev_plane.Normalize();
  367. Vector3 curr_plane;
  368. Vector3::Cross_Product(curr_point, next_point, &curr_plane);
  369. curr_plane.Normalize();
  370. if (Vector3::Dot_Product(prev_plane, curr_plane) < 0.0f) {
  371. switch_edges = !switch_edges;
  372. intersection[sidx][TOP_EDGE].Fold = true;
  373. intersection[sidx][BOTTOM_EDGE].Fold = true;
  374. } else {
  375. intersection[sidx][TOP_EDGE].Fold = false;
  376. intersection[sidx][BOTTOM_EDGE].Fold = false;
  377. }
  378. }
  379. if (switch_edges) {
  380. // We switch signs so the normals will always point inwards
  381. segment[sidx].EdgePlane[TOP_EDGE] = -bottom_normal;
  382. segment[sidx].EdgePlane[BOTTOM_EDGE] = -top_normal;
  383. }
  384. }
  385. // The two dummy segments for the clipping edges of the first and last real segments will be
  386. // defined later, with the first and last intersections.
  387. /*
  388. ** Calculate segment edge intersections:
  389. */
  390. unsigned int numsegs = point_cnt - 1; // Doesn't include the two dummy segments
  391. unsigned int num_intersections[NUM_EDGES];
  392. // These include the 1st, last point "intersections", not the pre-first dummy intersection
  393. num_intersections[TOP_EDGE] = point_cnt;
  394. num_intersections[BOTTOM_EDGE] = point_cnt;
  395. // Initialize pre-first point dummy intersection record (only NextSegmentID will be used).
  396. intersection[0][TOP_EDGE].PointCount = 0; // Should never be used
  397. intersection[0][TOP_EDGE].NextSegmentID = 0; // Points to first dummy segment
  398. intersection[0][TOP_EDGE].Direction.Set(1,0,0); // Should never be used
  399. intersection[0][TOP_EDGE].Point.Set(0,0,0); // Should never be used
  400. intersection[0][TOP_EDGE].TexV = 0.0f; // Should never be used
  401. intersection[0][TOP_EDGE].Fold = true; // Should never be used
  402. intersection[0][TOP_EDGE].Parallel = false; // Should never be used
  403. intersection[0][BOTTOM_EDGE].PointCount = 0; // Should never be used
  404. intersection[0][BOTTOM_EDGE].NextSegmentID = 0; // Points to first dummy segment
  405. intersection[0][BOTTOM_EDGE].Point.Set(0,0,0); // Should never be used
  406. intersection[0][BOTTOM_EDGE].TexV = 0.0f; // Should never be used
  407. intersection[0][BOTTOM_EDGE].Direction.Set(1,0,0); // Should never be used
  408. intersection[0][BOTTOM_EDGE].Fold = true; // Should never be used
  409. intersection[0][BOTTOM_EDGE].Parallel = false; // Should never be used
  410. // Initialize first point "intersection" record.
  411. intersection[1][TOP_EDGE].PointCount = 1;
  412. intersection[1][TOP_EDGE].NextSegmentID = 1;
  413. intersection[1][TOP_EDGE].Point = points[0];
  414. intersection[1][TOP_EDGE].TexV = tex_v[0];
  415. intersection[1][TOP_EDGE].Fold = true;
  416. intersection[1][TOP_EDGE].Parallel = false;
  417. intersection[1][BOTTOM_EDGE].PointCount = 1;
  418. intersection[1][BOTTOM_EDGE].NextSegmentID = 1;
  419. intersection[1][BOTTOM_EDGE].Point = points[0];
  420. intersection[1][BOTTOM_EDGE].TexV = tex_v[0];
  421. intersection[1][BOTTOM_EDGE].Fold = true;
  422. intersection[1][BOTTOM_EDGE].Parallel = false;
  423. // Find closest point to 1st top/bottom segment edge plane, and convert to direction vector
  424. // and dummy segment edge plane.
  425. Vector3 top;
  426. Vector3 bottom;
  427. Vector3 &first_point = points[0];
  428. Vector3 *first_plane = &(segment[1].EdgePlane[0]);
  429. top = first_point - first_plane[TOP_EDGE] * Vector3::Dot_Product(first_plane[TOP_EDGE], first_point);
  430. top.Normalize();
  431. intersection[1][TOP_EDGE].Direction = top;
  432. bottom = first_point - first_plane[BOTTOM_EDGE] * Vector3::Dot_Product(first_plane[BOTTOM_EDGE], first_point);
  433. bottom.Normalize();
  434. intersection[1][BOTTOM_EDGE].Direction = bottom;
  435. Vector3 segdir = points[1] - points[0];
  436. segdir.Normalize(); // Is this needed? Probably not - remove later when all works
  437. Vector3 start_pl;
  438. Vector3::Cross_Product(top, bottom, &start_pl);
  439. start_pl.Normalize();
  440. float dp = Vector3::Dot_Product(segdir, start_pl);
  441. if (dp > 0.0f) {
  442. segment[0].StartPlane = segment[0].EdgePlane[TOP_EDGE] = segment[0].EdgePlane[BOTTOM_EDGE] = start_pl;
  443. } else {
  444. segment[0].StartPlane = segment[0].EdgePlane[TOP_EDGE] = segment[0].EdgePlane[BOTTOM_EDGE] = -start_pl;
  445. }
  446. // Initialize StartPlane for the first "real" segment
  447. segment[1].StartPlane = segment[0].StartPlane;
  448. // Initialize last point "intersection" record.
  449. unsigned int last_isec = num_intersections[TOP_EDGE]; // Same # top, bottom intersections
  450. intersection[last_isec][TOP_EDGE].PointCount = 1;
  451. intersection[last_isec][TOP_EDGE].NextSegmentID = numsegs + 1; // Last dummy segment
  452. intersection[last_isec][TOP_EDGE].Point = points[point_cnt - 1];
  453. intersection[last_isec][TOP_EDGE].TexV = tex_v[point_cnt - 1];
  454. intersection[last_isec][TOP_EDGE].Fold = true;
  455. intersection[last_isec][TOP_EDGE].Parallel = false;
  456. intersection[last_isec][BOTTOM_EDGE].PointCount = 1;
  457. intersection[last_isec][BOTTOM_EDGE].NextSegmentID = numsegs + 1;// Last dummy segment
  458. intersection[last_isec][BOTTOM_EDGE].Point = points[point_cnt - 1];
  459. intersection[last_isec][BOTTOM_EDGE].TexV = tex_v[point_cnt - 1];
  460. intersection[last_isec][BOTTOM_EDGE].Fold = true;
  461. intersection[last_isec][BOTTOM_EDGE].Parallel = false;
  462. // Find closest point to last top/bottom segment edge plane, and convert to direction vector
  463. // and dummy segment edge vector
  464. Vector3 &last_point = points[point_cnt - 1];
  465. Vector3 *last_plane = &(segment[numsegs].EdgePlane[0]);
  466. top = last_point - last_plane[TOP_EDGE] * Vector3::Dot_Product(last_plane[TOP_EDGE], last_point);
  467. top.Normalize();
  468. intersection[last_isec][TOP_EDGE].Direction = top;
  469. bottom = last_point - last_plane[BOTTOM_EDGE] * Vector3::Dot_Product(last_plane[BOTTOM_EDGE], last_point);
  470. bottom.Normalize();
  471. intersection[last_isec][BOTTOM_EDGE].Direction = bottom;
  472. segdir = points[point_cnt - 1] - points[point_cnt - 2];
  473. segdir.Normalize(); // Is this needed? Probably not - remove later when all works
  474. Vector3::Cross_Product(top, bottom, &start_pl);
  475. start_pl.Normalize();
  476. dp = Vector3::Dot_Product(segdir, start_pl);
  477. if (dp > 0.0f) {
  478. segment[numsegs + 1].StartPlane = segment[numsegs + 1].EdgePlane[TOP_EDGE] =
  479. segment[numsegs + 1].EdgePlane[BOTTOM_EDGE] = start_pl;
  480. } else {
  481. segment[numsegs + 1].StartPlane = segment[numsegs + 1].EdgePlane[TOP_EDGE] =
  482. segment[numsegs + 1].EdgePlane[BOTTOM_EDGE] = -start_pl;
  483. }
  484. // Calculate midpoint segment intersections. There are 2 segment intersections for each
  485. // point: top and bottom (due to the fact that the segments have width, so they have a top
  486. // edge and a bottom edge). Note that the top/bottom distinction does not relate to screen
  487. // up/down. Since each segment edge is represented by a plane passing through the origin
  488. // (eyepoint), the intersection of two such is a line passing through the origin, which is
  489. // represented as a normalized direction vector.
  490. // We use both segment intersections to define the startplane for the segment which begins
  491. // at that intersection.
  492. float vdp;
  493. for (iidx = 2; iidx < num_intersections[TOP_EDGE]; iidx++) {
  494. // Relevant midpoint:
  495. Vector3 &midpoint = points[iidx - 1];
  496. float mid_tex_v = tex_v[iidx - 1];
  497. // Initialize misc. fields
  498. intersection[iidx][TOP_EDGE].PointCount = 1;
  499. intersection[iidx][TOP_EDGE].NextSegmentID = iidx;
  500. intersection[iidx][TOP_EDGE].Point = midpoint;
  501. intersection[iidx][TOP_EDGE].TexV = mid_tex_v;
  502. intersection[iidx][BOTTOM_EDGE].PointCount = 1;
  503. intersection[iidx][BOTTOM_EDGE].NextSegmentID = iidx;
  504. intersection[iidx][BOTTOM_EDGE].Point = midpoint;
  505. intersection[iidx][BOTTOM_EDGE].TexV = mid_tex_v;
  506. // Intersection calculation: if the top/bottom planes of both adjoining segments are not
  507. // very close to being parallel, intersect them to get top/bottom intersection lines. If
  508. // the planes are almost parallel, pick one, find the point on the plane closest to the
  509. // midpoint, and convert that point to a line direction vector.
  510. // Top:
  511. vdp = Vector3::Dot_Product(segment[iidx - 1].EdgePlane[TOP_EDGE], segment[iidx].EdgePlane[TOP_EDGE]);
  512. if (fabs(vdp) < parallel_factor) {
  513. // Not parallel - intersect planes to get line (get vector, normalize it, ensure it is
  514. // pointing towards the midpoint)
  515. Vector3::Cross_Product(segment[iidx - 1].EdgePlane[TOP_EDGE], segment[iidx].EdgePlane[TOP_EDGE],
  516. &(intersection[iidx][TOP_EDGE].Direction));
  517. intersection[iidx][TOP_EDGE].Direction.Normalize();
  518. if (Vector3::Dot_Product(intersection[iidx][TOP_EDGE].Direction, midpoint) < 0.0f) {
  519. intersection[iidx][TOP_EDGE].Direction = -intersection[iidx][TOP_EDGE].Direction;
  520. }
  521. intersection[iidx][TOP_EDGE].Parallel = false;
  522. } else {
  523. // Parallel (or almost): find point on av. plane closest to midpoint, convert to line
  524. // Ensure average calculation is numerically stable:
  525. Vector3 pl;
  526. if (vdp > 0.0f) {
  527. pl = segment[iidx - 1].EdgePlane[TOP_EDGE] + segment[iidx].EdgePlane[TOP_EDGE];
  528. } else {
  529. pl = segment[iidx - 1].EdgePlane[TOP_EDGE] - segment[iidx].EdgePlane[TOP_EDGE];
  530. }
  531. pl.Normalize();
  532. intersection[iidx][TOP_EDGE].Direction = midpoint - pl * Vector3::Dot_Product(pl, midpoint);
  533. intersection[iidx][TOP_EDGE].Direction.Normalize();
  534. intersection[iidx][TOP_EDGE].Parallel = true;
  535. }
  536. // Bottom:
  537. vdp = Vector3::Dot_Product(segment[iidx - 1].EdgePlane[BOTTOM_EDGE], segment[iidx].EdgePlane[BOTTOM_EDGE]);
  538. if (fabs(vdp) < parallel_factor) {
  539. // Not parallel - intersect planes to get line (get vector, normalize it, ensure it is
  540. // pointing towards the midpoint)
  541. Vector3::Cross_Product(segment[iidx - 1].EdgePlane[BOTTOM_EDGE], segment[iidx].EdgePlane[BOTTOM_EDGE],
  542. &(intersection[iidx][BOTTOM_EDGE].Direction));
  543. intersection[iidx][BOTTOM_EDGE].Direction.Normalize();
  544. if (Vector3::Dot_Product(intersection[iidx][BOTTOM_EDGE].Direction, midpoint) < 0.0f) {
  545. intersection[iidx][BOTTOM_EDGE].Direction = -intersection[iidx][BOTTOM_EDGE].Direction;
  546. }
  547. intersection[iidx][BOTTOM_EDGE].Parallel = false;
  548. } else {
  549. // Parallel (or almost): find point on av. plane closest to midpoint, convert to line
  550. // Ensure average calculation is numerically stable:
  551. Vector3 pl;
  552. if (vdp > 0.0f) {
  553. pl = segment[iidx - 1].EdgePlane[BOTTOM_EDGE] + segment[iidx].EdgePlane[BOTTOM_EDGE];
  554. } else {
  555. pl = segment[iidx - 1].EdgePlane[BOTTOM_EDGE] - segment[iidx].EdgePlane[BOTTOM_EDGE];
  556. }
  557. pl.Normalize();
  558. intersection[iidx][BOTTOM_EDGE].Direction = midpoint - pl * Vector3::Dot_Product(pl, midpoint);
  559. intersection[iidx][BOTTOM_EDGE].Direction.Normalize();
  560. intersection[iidx][BOTTOM_EDGE].Parallel = true;
  561. }
  562. // Find StartPlane:
  563. Vector3::Cross_Product(intersection[iidx][TOP_EDGE].Direction, intersection[iidx][BOTTOM_EDGE].Direction, &start_pl);
  564. start_pl.Normalize();
  565. dp = Vector3::Dot_Product(segment[iidx].StartPlane, start_pl);
  566. if (dp > 0.0f) {
  567. segment[iidx].StartPlane = start_pl;
  568. } else {
  569. segment[iidx].StartPlane = -start_pl;
  570. }
  571. } // for iidx
  572. /*
  573. ** Intersection merging: when an intersection is inside an adjacent segment and certain
  574. ** other conditions hold true, we need to merge intersections to avoid visual glitches
  575. ** caused by the polys folding over on themselves.
  576. */
  577. if (Is_Merge_Intersections()) {
  578. // Since we are merging the intersections in-place, we have two index variables, a "read
  579. // index" and a "write index".
  580. unsigned int iidx_r;
  581. unsigned int iidx_w;
  582. // The merges will be repeated in multiple passes until none are performed. The reason
  583. // for this is that one merge may cause the need for another merge elsewhere.
  584. bool merged = true;
  585. while (merged) {
  586. merged = false;
  587. SegmentEdge edge;
  588. for (edge = FIRST_EDGE; edge <= MAX_EDGE; edge = (SegmentEdge)((int)edge + 1)) {
  589. // Merge top and bottom edge intersections: loop through the intersections from the
  590. // first intersection to the penultimate intersection, for each intersection check
  591. // if it needs to be merged with the next one (which is why the loop doesn't go all
  592. // the way to the last intersection). We start at 1 because 0 is the dummy
  593. // "pre-first-point" intersection.
  594. unsigned int num_isects = num_intersections[edge]; // Capture here because will change inside loop
  595. for (iidx_r = 1, iidx_w = 1; iidx_r < num_isects; iidx_r++, iidx_w++) {
  596. // Check for either of two possible reasons to merge this intersection with the
  597. // next: either the segment on the far side of the next intersection overlaps
  598. // this intersection, or the previous segment overlaps the next intersection.
  599. // Note that some other conditions need to be true as well.
  600. // Note: iidx_r is used for anything at or after the current position, iidx_w is
  601. // used for anything before the current position (previous positions have
  602. // potentially already been merged).
  603. // Note: iidx_r is used for anything at or after the current position, iidx_w is
  604. // used for anything before the current position (previous positions have
  605. // potentially already been merged).
  606. LineSegmentIntersection *curr_int = &(intersection[iidx_r][edge]);
  607. LineSegmentIntersection *next_int = &(intersection[iidx_r + 1][edge]);
  608. LineSegmentIntersection *write_int = &(intersection[iidx_w][edge]);
  609. LineSegmentIntersection *prev_int = &(intersection[iidx_w - 1][edge]);
  610. LineSegment *next_seg = &(segment[next_int->NextSegmentID]);
  611. LineSegment *curr_seg = &(segment[curr_int->NextSegmentID]);
  612. LineSegment *prev_seg = &(segment[prev_int->NextSegmentID]);
  613. // If this intersection is inside both the start plane and the segment edge
  614. // plane of the segment after the next intersection, merge this edge
  615. // intersection and the next. We repeat merging until no longer needed.
  616. // NOTE - we do not merge across a fold.
  617. while ( (!next_int->Fold &&
  618. (Vector3::Dot_Product(curr_int->Direction, next_seg->StartPlane) > 0.0f) &&
  619. (Vector3::Dot_Product(curr_int->Direction, next_seg->EdgePlane[edge]) > 0.0f )) ||
  620. (!curr_int->Fold &&
  621. (Vector3::Dot_Product(next_int->Direction, -curr_seg->StartPlane) > 0.0f) &&
  622. (Vector3::Dot_Product(next_int->Direction, prev_seg->EdgePlane[edge]) > 0.0f )) ) {
  623. // First calculate location of merged intersection - this is so we can abort
  624. // the merge if it yields funky results.
  625. // Find mean point (weighted so all points have same weighting)
  626. unsigned int new_count = curr_int->PointCount + next_int->PointCount;
  627. float oo_new_count = 1.0f / (float)new_count;
  628. float curr_factor = oo_new_count * (float)curr_int->PointCount;
  629. float next_factor = oo_new_count * (float)curr_int->PointCount;
  630. Vector3 new_point = curr_int->Point * curr_factor + next_int->Point * next_factor;
  631. float new_tex_v = curr_int->TexV * curr_factor + next_int->TexV * next_factor;
  632. // Calculate new intersection direction by intersecting prev_seg with next_seg
  633. bool new_parallel;
  634. Vector3 new_direction;
  635. vdp = Vector3::Dot_Product(prev_seg->EdgePlane[edge], next_seg->EdgePlane[edge]);
  636. if (fabs(vdp) < parallel_factor) {
  637. // Not parallel - intersect planes to get line (get vector, normalize it,
  638. // ensure it is pointing towards the current point)
  639. Vector3::Cross_Product(prev_seg->EdgePlane[edge], next_seg->EdgePlane[edge], &new_direction);
  640. new_direction.Normalize();
  641. if (Vector3::Dot_Product(new_direction, new_point) < 0.0f) {
  642. new_direction = -new_direction;
  643. }
  644. new_parallel = false;
  645. } else {
  646. // Parallel (or almost). If the current intersection is not parallel, take
  647. // the average plane and intersect it with the skipped plane. If the
  648. // current intersection is parallel, find the average plane, and find the
  649. // direction vector on it closest to the current intersections direction
  650. // vector.
  651. // Ensure average calculation is numerically stable:
  652. Vector3 pl;
  653. if (vdp > 0.0f) {
  654. pl = prev_seg->EdgePlane[edge] + next_seg->EdgePlane[edge];
  655. } else {
  656. pl = prev_seg->EdgePlane[edge] - next_seg->EdgePlane[edge];
  657. }
  658. pl.Normalize();
  659. if (curr_int->Parallel) {
  660. new_direction = new_direction - pl * Vector3::Dot_Product(pl, new_direction);
  661. new_direction.Normalize();
  662. } else {
  663. Vector3::Cross_Product(curr_seg->EdgePlane[edge], pl, &new_direction);
  664. new_direction.Normalize();
  665. }
  666. new_parallel = true;
  667. }
  668. // Now check to see if the merge caused any funky results - if so abort it.
  669. // Currently we check to see if the distance of the direction from the two
  670. // points is larger than the radius times the merge_abort factor.
  671. if (MergeAbortFactor > 0.0f) {
  672. float abort_dist = radius * MergeAbortFactor;
  673. float abort_dist2 = abort_dist * abort_dist;
  674. Vector3 diff_curr = curr_int->Point -
  675. new_direction * Vector3::Dot_Product(curr_int->Point, new_direction);
  676. if (diff_curr.Length2() > abort_dist2) break;
  677. Vector3 next_curr = next_int->Point -
  678. new_direction * Vector3::Dot_Product(next_int->Point, new_direction);
  679. if (next_curr.Length2() > abort_dist2) break;
  680. }
  681. // Merge edge intersections (curr_int and next_int) into curr_int
  682. merged = true;
  683. curr_int->Direction = new_direction;
  684. curr_int->Parallel = new_parallel;
  685. curr_int->Point = new_point;
  686. curr_int->TexV = new_tex_v;
  687. curr_int->PointCount = new_count;
  688. curr_int->NextSegmentID = next_int->NextSegmentID;
  689. curr_int->Fold = curr_int->Fold || next_int->Fold;
  690. // Decrement number of edge intersections
  691. num_intersections[edge]--;
  692. // Advance iidx_r to shift subsequent entries backwards in result.
  693. iidx_r++;
  694. // If we are at the end then break:
  695. if (iidx_r == num_isects) {
  696. break;
  697. }
  698. // Advance next_int and next_seg.
  699. next_int = &(intersection[iidx_r + 1][edge]);
  700. next_seg = &(segment[next_int->NextSegmentID]);
  701. } // while <merging needed>
  702. // Copy from "read index" to "write index"
  703. write_int->PointCount = curr_int->PointCount;
  704. write_int->NextSegmentID = curr_int->NextSegmentID;
  705. write_int->Point = curr_int->Point;
  706. write_int->TexV = curr_int->TexV;
  707. write_int->Direction = curr_int->Direction;
  708. write_int->Fold = curr_int->Fold;
  709. } // for iidx
  710. // If iidx_r is exactly equal to num_isects (rather than being larger by one) at this
  711. // point, this means that the last intersection was not merged with the previous one. In
  712. // this case, we need to do one last copy:
  713. if (iidx_r == num_isects) {
  714. LineSegmentIntersection *write_int = &(intersection[iidx_w][edge]);
  715. LineSegmentIntersection *curr_int = &(intersection[iidx_r][edge]);
  716. write_int->PointCount = curr_int->PointCount;
  717. write_int->NextSegmentID = curr_int->NextSegmentID;
  718. write_int->Point = curr_int->Point;
  719. write_int->TexV = curr_int->TexV;
  720. write_int->Direction = curr_int->Direction;
  721. write_int->Fold = curr_int->Fold;
  722. }
  723. #ifdef ENABLE_WWDEBUGGING
  724. // Testing code - ensure total PointCount fits the number of points
  725. unsigned int total_cnt = 0;
  726. for (unsigned int nidx = 0; nidx <= num_intersections[edge]; nidx++) {
  727. total_cnt += intersection[nidx][edge].PointCount;
  728. }
  729. assert(total_cnt == point_cnt);
  730. #endif
  731. } // for edge
  732. } // while (merged)
  733. } // if (Is_Merge_Intersections())
  734. /*
  735. ** Find vertex positions, generate vertices and triangles:
  736. ** Since we can have top/bottom intersections merged, we need to skip points if both the top
  737. ** and bottom intersections are merged, generate triangle fans if one of the sides is merged
  738. ** and the other isnt, and generate triangle strips otherwise.
  739. */
  740. // Configure vertex array and setup renderer.
  741. unsigned int vnum = num_intersections[TOP_EDGE] + num_intersections[BOTTOM_EDGE];
  742. VertexFormatXYZDUV1 *vArray=W3DNEWARRAY VertexFormatXYZDUV1[vnum];
  743. Vector3i v_index_array[MAX_SEGLINE_POLY_BUFFER_SIZE];
  744. // Vertex and triangle indices
  745. unsigned int vidx = 0;
  746. unsigned int tidx = 0;
  747. // GENERALIZE FOR WHEN NO TEXTURE (DO NOT SET UV IN THESE CASES? NEED TO GENERALIZE FOR DIFFERENT TEXTURING MODES ANYWAY).
  748. // "Prime the pump" with two vertices (pick nearest point on each direction line):
  749. Vector3 &top_dir = intersection[1][TOP_EDGE].Direction;
  750. top = top_dir * Vector3::Dot_Product(points[0], top_dir);
  751. Vector3 &bottom_dir = intersection[1][BOTTOM_EDGE].Direction;
  752. bottom = bottom_dir * Vector3::Dot_Product(points[0], bottom_dir);
  753. vArray[vidx].x = top.X;
  754. vArray[vidx].y = top.Y;
  755. vArray[vidx].z = top.Z;
  756. vArray[vidx].u1 = u_values[0] + uv_offset.X;
  757. vArray[vidx].v1 = intersection[1][TOP_EDGE].TexV + uv_offset.Y;
  758. vidx++;
  759. vArray[vidx].x = bottom.X;
  760. vArray[vidx].y = bottom.Y;
  761. vArray[vidx].z = bottom.Z;
  762. vArray[vidx].u1 = u_values[1] + uv_offset.X;
  763. vArray[vidx].v1 = intersection[1][BOTTOM_EDGE].TexV + uv_offset.Y;
  764. vidx++;
  765. unsigned int last_top_vidx = 0;
  766. unsigned int last_bottom_vidx = 1;
  767. // Loop over intersections, create new vertices and triangles.
  768. unsigned int top_int_idx = 1; // Skip "pre-first-point" dummy intersection
  769. unsigned int bottom_int_idx = 1; // Skip "pre-first-point" dummy intersection
  770. pidx = 0;
  771. unsigned int residual_top_points = intersection[1][TOP_EDGE].PointCount;
  772. unsigned int residual_bottom_points = intersection[1][BOTTOM_EDGE].PointCount;
  773. // Reduce both pointcounts by the same amount so the smaller one is 1 (skip points)
  774. unsigned int delta = MIN(residual_top_points, residual_bottom_points) - 1;
  775. residual_top_points -= delta;
  776. residual_bottom_points -= delta;
  777. pidx += delta;
  778. for (; ; ) {
  779. if (residual_top_points == 1 && residual_bottom_points == 1) {
  780. // Advance both intersections, creating a tristrip segment
  781. v_index_array[tidx].I = last_top_vidx;
  782. v_index_array[tidx].J = last_bottom_vidx;
  783. v_index_array[tidx].K = vidx;
  784. tidx++;
  785. v_index_array[tidx].I = last_bottom_vidx;
  786. v_index_array[tidx].J = vidx + 1;
  787. v_index_array[tidx].K = vidx;
  788. tidx++;
  789. last_top_vidx = vidx;
  790. last_bottom_vidx = vidx + 1;
  791. // Advance both intersections.
  792. top_int_idx++;
  793. bottom_int_idx++;
  794. residual_top_points = intersection[top_int_idx][TOP_EDGE].PointCount;
  795. residual_bottom_points = intersection[bottom_int_idx][BOTTOM_EDGE].PointCount;
  796. // Advance point index (must do here because the new point index is used below):
  797. pidx++;
  798. // Generate two vertices for next point by picking nearest point on each direction line
  799. Vector3 &top_dir = intersection[top_int_idx][TOP_EDGE].Direction;
  800. top = top_dir * Vector3::Dot_Product(points[pidx], top_dir);
  801. Vector3 &bottom_dir = intersection[bottom_int_idx][BOTTOM_EDGE].Direction;
  802. bottom = bottom_dir * Vector3::Dot_Product(points[pidx], bottom_dir);
  803. vArray[vidx].x = top.X;
  804. vArray[vidx].y = top.Y;
  805. vArray[vidx].z = top.Z;
  806. vArray[vidx].u1 = u_values[0] + uv_offset.X;
  807. vArray[vidx].v1 = intersection[top_int_idx][TOP_EDGE].TexV + uv_offset.Y;
  808. vidx++;
  809. vArray[vidx].x = bottom.X;
  810. vArray[vidx].y = bottom.Y;
  811. vArray[vidx].z = bottom.Z;
  812. vArray[vidx].u1 = u_values[1] + uv_offset.X;
  813. vArray[vidx].v1 = intersection[bottom_int_idx][BOTTOM_EDGE].TexV + uv_offset.Y;
  814. vidx++;
  815. } else {
  816. // Exactly one of the pointcounts is greater than one - advance it and draw one triangle
  817. if (residual_top_points > 1) {
  818. // Draw one triangle (fan segment)
  819. v_index_array[tidx].I = last_top_vidx;
  820. v_index_array[tidx].J = last_bottom_vidx;
  821. v_index_array[tidx].K = vidx;
  822. tidx++;
  823. last_bottom_vidx = vidx;
  824. // Advance bottom intersection only
  825. residual_top_points--;
  826. bottom_int_idx++;
  827. residual_bottom_points = intersection[bottom_int_idx][BOTTOM_EDGE].PointCount;
  828. // Advance point index (must do here because the new point index is used below):
  829. pidx++;
  830. // Generate bottom vertex by picking nearest point on bottom direction line
  831. Vector3 &bottom_dir = intersection[bottom_int_idx][BOTTOM_EDGE].Direction;
  832. bottom = bottom_dir * Vector3::Dot_Product(points[pidx], bottom_dir);
  833. vArray[vidx].x = bottom.X;
  834. vArray[vidx].y = bottom.Y;
  835. vArray[vidx].z = bottom.Z;
  836. vArray[vidx].u1 = u_values[1] + uv_offset.X;
  837. vArray[vidx].v1 = intersection[bottom_int_idx][BOTTOM_EDGE].TexV + uv_offset.Y;
  838. vidx++;
  839. } else {
  840. // residual_bottom_points > 1
  841. // Draw one triangle (fan segment)
  842. v_index_array[tidx].I = last_top_vidx;
  843. v_index_array[tidx].J = last_bottom_vidx;
  844. v_index_array[tidx].K = vidx;
  845. tidx++;
  846. last_top_vidx = vidx;
  847. // Advance top intersection only
  848. residual_bottom_points--;
  849. top_int_idx++;
  850. residual_top_points = intersection[top_int_idx][TOP_EDGE].PointCount;
  851. // Advance point index (must do here because the new point index is used below):
  852. pidx++;
  853. // Generate top vertex by picking nearest point on top direction line
  854. Vector3 &top_dir = intersection[top_int_idx][TOP_EDGE].Direction;
  855. top = top_dir * Vector3::Dot_Product(points[pidx], top_dir);
  856. vArray[vidx].x = top.X;
  857. vArray[vidx].y = top.Y;
  858. vArray[vidx].z = top.Z;
  859. vArray[vidx].u1 = u_values[0] + uv_offset.X;
  860. vArray[vidx].v1 = intersection[top_int_idx][TOP_EDGE].TexV + uv_offset.Y;
  861. vidx++;
  862. }
  863. }
  864. // Reduce both pointcounts by the same amount so the smaller one is 1 (skip points)
  865. delta = MIN(residual_top_points, residual_bottom_points) - 1;
  866. residual_top_points -= delta;
  867. residual_bottom_points -= delta;
  868. pidx += delta;
  869. // Exit conditions
  870. if ( (top_int_idx >= num_intersections[TOP_EDGE] && residual_top_points == 1) ||
  871. (bottom_int_idx >= num_intersections[BOTTOM_EDGE] && residual_bottom_points == 1)) {
  872. // Debugging check - if either intersection index is before end, both of them should be
  873. // and the points should be before the end.
  874. assert(top_int_idx == num_intersections[TOP_EDGE]);
  875. assert(bottom_int_idx == num_intersections[BOTTOM_EDGE]);
  876. assert(pidx == point_cnt - 1);
  877. break;
  878. }
  879. }
  880. /*
  881. ** Set color, opacity, vertex flags:
  882. */
  883. // If color is not white or opacity not 100%, enable gradient in shader and in renderer - otherwise disable.
  884. unsigned int rgba;
  885. rgba=DX8Wrapper::Convert_Color(Color,Opacity);
  886. bool rgba_all=(rgba==0xFFFFFFFF);
  887. // Enable sorting if sorting has not been disabled and line is translucent and alpha testing is not enabled.
  888. bool sorting = (!Is_Sorting_Disabled()) && (Shader.Get_Dst_Blend_Func() != ShaderClass::DSTBLEND_ZERO && Shader.Get_Alpha_Test() == ShaderClass::ALPHATEST_DISABLE);
  889. ShaderClass shader = Shader;
  890. shader.Set_Cull_Mode(ShaderClass::CULL_MODE_DISABLE);
  891. VertexMaterialClass *mat;
  892. if (!rgba_all) {
  893. shader.Set_Primary_Gradient(ShaderClass::GRADIENT_MODULATE);
  894. for (vidx = 0; vidx < vnum; vidx++) vArray[vidx].diffuse=rgba;
  895. mat=VertexMaterialClass::Get_Preset(VertexMaterialClass::PRELIT_DIFFUSE);
  896. } else {
  897. shader.Set_Primary_Gradient(ShaderClass::GRADIENT_DISABLE);
  898. mat=VertexMaterialClass::Get_Preset(VertexMaterialClass::PRELIT_NODIFFUSE);
  899. }
  900. // If Texture is non-NULL enable texturing in shader - otherwise disable.
  901. if (Texture) {
  902. shader.Set_Texturing(ShaderClass::TEXTURING_ENABLE);
  903. } else {
  904. shader.Set_Texturing(ShaderClass::TEXTURING_DISABLE);
  905. }
  906. /*
  907. ** Render
  908. */
  909. DynamicVBAccessClass Verts((sorting?BUFFER_TYPE_DYNAMIC_SORTING:BUFFER_TYPE_DYNAMIC_DX8),dynamic_fvf_type,vnum);
  910. // Copy in the data to the VB
  911. {
  912. DynamicVBAccessClass::WriteLockClass Lock(&Verts);
  913. unsigned int i;
  914. unsigned char *vb=(unsigned char*)Lock.Get_Formatted_Vertex_Array();
  915. const FVFInfoClass& fvfinfo=Verts.FVF_Info();
  916. for (i=0; i<vnum; i++)
  917. {
  918. // Copy Locations
  919. ((Vector3*)(vb+fvfinfo.Get_Location_Offset()))->X=vArray[i].x;
  920. ((Vector3*)(vb+fvfinfo.Get_Location_Offset()))->Y=vArray[i].y;
  921. ((Vector3*)(vb+fvfinfo.Get_Location_Offset()))->Z=vArray[i].z;
  922. *(unsigned int*)(vb+fvfinfo.Get_Diffuse_Offset())=vArray[i].diffuse;
  923. ((Vector2*)(vb+fvfinfo.Get_Tex_Offset(0)))->U=vArray[i].u1;
  924. ((Vector2*)(vb+fvfinfo.Get_Tex_Offset(0)))->V=vArray[i].v1;
  925. vb+=fvfinfo.Get_FVF_Size();
  926. }
  927. } // copy
  928. DynamicIBAccessClass ib_access((sorting?BUFFER_TYPE_DYNAMIC_SORTING:BUFFER_TYPE_DYNAMIC_DX8),tidx*3);
  929. {
  930. unsigned int i;
  931. DynamicIBAccessClass::WriteLockClass lock(&ib_access);
  932. unsigned short* inds=lock.Get_Index_Array();
  933. for (i=0; i<tidx; i++)
  934. {
  935. *inds++=v_index_array[i].I;
  936. *inds++=v_index_array[i].J;
  937. *inds++=v_index_array[i].K;
  938. }
  939. }
  940. DX8Wrapper::Set_Index_Buffer(ib_access,0);
  941. DX8Wrapper::Set_Vertex_Buffer(Verts);
  942. DX8Wrapper::Set_Material(mat);
  943. DX8Wrapper::Set_Texture(0,Texture);
  944. DX8Wrapper::Set_Shader(shader);
  945. if (sorting) {
  946. SortingRendererClass::Insert_Triangles(obj_sphere,0,tidx,0,vnum);
  947. } else {
  948. DX8Wrapper::Draw_Triangles(0,tidx,0,vnum);
  949. }
  950. REF_PTR_RELEASE(mat);
  951. delete [] vArray;
  952. } // Chunking loop
  953. DX8Wrapper::Set_Transform(D3DTS_VIEW,view);
  954. }
  955. void SegLineRendererClass::subdivision_util(unsigned int point_cnt, const Vector3 *xformed_pts,
  956. const float *base_tex_v, unsigned int *p_sub_point_cnt, Vector3 *xformed_subdiv_pts,
  957. float *subdiv_tex_v)
  958. {
  959. // CAUTION: freezing the random offsets will make it more readily apparent that the offsets
  960. // are in camera space rather than worldspace.
  961. int freeze_random = Is_Freeze_Random();
  962. Random3Class randomize;
  963. const float oo_int_max = 1.0f / (float)INT_MAX;
  964. Vector3SolidBoxRandomizer randomizer(Vector3(1,1,1));
  965. Vector3 randvec(0,0,0);
  966. unsigned int sub_pidx = 0;
  967. struct SegLineSubdivision {
  968. Vector3 StartPos;
  969. Vector3 EndPos;
  970. float StartTexV; // V texture coordinate of start point
  971. float EndTexV; // V texture coordinate of end point
  972. float Rand;
  973. unsigned int Level; // Subdivision level
  974. };
  975. SegLineSubdivision stack[2 * MAX_SEGLINE_SUBDIV_LEVELS]; // Maximum number needed
  976. int tos = 0;
  977. for (unsigned int pidx = 0; pidx < point_cnt - 1; pidx++) {
  978. // Subdivide the (pidx, pidx + 1) segment. Produce pidx and all subdivided points up to
  979. // (not including) pidx + 1.
  980. tos = 0;
  981. stack[0].StartPos = xformed_pts[pidx];
  982. stack[0].EndPos = xformed_pts[pidx + 1];
  983. stack[0].StartTexV = base_tex_v[pidx];
  984. stack[0].EndTexV = base_tex_v[pidx + 1];
  985. stack[0].Rand = NoiseAmplitude;
  986. stack[0].Level = 0;
  987. for (; tos >= 0;) {
  988. if (stack[tos].Level == SubdivisionLevel) {
  989. // Generate point location and texture V coordinate
  990. xformed_subdiv_pts[sub_pidx] = stack[tos].StartPos;
  991. subdiv_tex_v[sub_pidx++] = stack[tos].StartTexV;
  992. // Pop
  993. tos--;
  994. } else {
  995. // Recurse down: pop existing entry and push two subdivided ones.
  996. if (freeze_random) {
  997. randvec.Set(randomize * oo_int_max, randomize * oo_int_max, randomize * oo_int_max);
  998. } else {
  999. randomizer.Get_Vector(randvec);
  1000. }
  1001. stack[tos + 1].StartPos = stack[tos].StartPos;
  1002. stack[tos + 1].EndPos = (stack[tos].StartPos + stack[tos].EndPos) * 0.5f + randvec * stack[tos].Rand;
  1003. stack[tos + 1].StartTexV = stack[tos].StartTexV;
  1004. stack[tos + 1].EndTexV = (stack[tos].StartTexV + stack[tos].EndTexV) * 0.5f;
  1005. stack[tos + 1].Rand = stack[tos].Rand * 0.5f;
  1006. stack[tos + 1].Level = stack[tos].Level + 1;
  1007. stack[tos].StartPos = stack[tos + 1].EndPos;
  1008. // stack[tos].EndPos already has the right value
  1009. stack[tos].StartTexV = stack[tos + 1].EndTexV;
  1010. // stack[tos].EndTexV already has the right value
  1011. stack[tos].Rand = stack[tos + 1].Rand;
  1012. stack[tos].Level = stack[tos + 1].Level;
  1013. tos++;
  1014. }
  1015. }
  1016. }
  1017. // Last point
  1018. xformed_subdiv_pts[sub_pidx] = xformed_pts[point_cnt - 1];
  1019. subdiv_tex_v[sub_pidx++] = base_tex_v[point_cnt - 1];
  1020. // Output:
  1021. *p_sub_point_cnt = sub_pidx;
  1022. }
  1023. #endif //0