streakRender.cpp 53 KB

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