| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320 |
- //
- // This unit is part of the GLScene Engine, http://glscene.org
- //
- unit GLGeomObjects;
- (* Geometric objects. *)
- interface
- {$I GLScene.inc}
- uses
- Winapi.OpenGL,
- System.Math,
- System.Classes,
-
- OpenGLTokens,
- OpenGLAdapter,
- GLScene,
- Scene.PersistentClasses,
- Scene.VectorGeometry,
- Scene.VectorLists,
- GLContext,
- GLObjects,
- Scene.Silhouette,
- Scene.VectorTypes,
- GLGeometryBB,
- GLRenderContextInfo,
- XOpenGL,
- Scene.Polynomials;
- type
- (* A Disk object that may not be complete, it can have a hole (controlled by the
- InnerRadius property) and can only be a slice (controlled by the StartAngle
- and SweepAngle properties). *)
- TGLDisk = class(TGLQuadricObject)
- private
- FStartAngle, FSweepAngle, FOuterRadius, FInnerRadius: Single;
- FSlices, FLoops: Integer;
- procedure SetOuterRadius(const aValue: Single);
- procedure SetInnerRadius(const aValue: Single);
- procedure SetSlices(aValue: Integer);
- procedure SetLoops(aValue: Integer);
- procedure SetStartAngle(const aValue: Single);
- procedure SetSweepAngle(const aValue: Single);
- public
- constructor Create(AOwner: TComponent); override;
- procedure BuildList(var rci: TGLRenderContextInfo); override;
- procedure Assign(Source: TPersistent); override;
- function AxisAlignedDimensionsUnscaled: TVector; override;
- function RayCastIntersect(const rayStart, rayVector: TVector;
- intersectPoint: PVector = nil; intersectNormal: PVector = nil) : Boolean; override;
- published
- // Allows defining a "hole" in the disk
- property InnerRadius: Single read FInnerRadius write SetInnerRadius;
- // Number of radial mesh subdivisions
- property Loops: Integer read FLoops write SetLoops default 2;
- // Outer radius for the disk. If you leave InnerRadius at 0, this is the disk radius
- property OuterRadius: Single read FOuterRadius write SetOuterRadius;
- // Number of mesh slices. For instance, if Slices=6, your disk will look like an hexagon
- property Slices: Integer read FSlices write SetSlices default 16;
- property StartAngle: Single read FStartAngle write SetStartAngle;
- property SweepAngle: Single read FSweepAngle write SetSweepAngle;
- end;
- (* Base class to cylinder-like objects that introduces the basic cylinder description properties.
- Be aware teh default slices and stacks make up for a high-poly cylinder,
- unless you're after high-quality lighting it is recommended to reduce the
- Stacks property to 1. *)
- TGLCylinderBase = class(TGLQuadricObject)
- private
- FBottomRadius: Single;
- FSlices, FStacks, FLoops: Integer;
- FHeight: Single;
- protected
- procedure SetBottomRadius(const aValue: Single);
- procedure SetHeight(const aValue: Single);
- procedure SetSlices(aValue: Integer);
- procedure SetStacks(aValue: Integer);
- procedure SetLoops(aValue: Integer);
- function GetTopRadius: Single; virtual;
- public
- constructor Create(AOwner: TComponent); override;
- procedure Assign(Source: TPersistent); override;
- function GenerateSilhouette(const silhouetteParameters
- : TGLSilhouetteParameters): TGLSilhouette; override;
- published
- property BottomRadius: Single read FBottomRadius write SetBottomRadius;
- property Height: Single read FHeight write SetHeight;
- property Slices: Integer read FSlices write SetSlices default 16;
- property Stacks: Integer read FStacks write SetStacks default 4;
- // Number of concentric rings for top/bottom disk(s).
- property Loops: Integer read FLoops write SetLoops default 1;
- end;
- TConePart = (coSides, coBottom);
- TConeParts = set of TConePart;
- // A cone object
- TGLCone = class(TGLCylinderBase)
- private
- FParts: TConeParts;
- protected
- procedure SetParts(aValue: TConeParts);
- function GetTopRadius: Single; override;
- public
- constructor Create(AOwner: TComponent); override;
- procedure Assign(Source: TPersistent); override;
- procedure BuildList(var rci: TGLRenderContextInfo); override;
- function AxisAlignedDimensionsUnscaled: TVector; override;
- function RayCastIntersect(const rayStart, rayVector: TVector;
- intersectPoint: PVector = nil; intersectNormal: PVector = nil)
- : Boolean; override;
- published
- property Parts: TConeParts read FParts write SetParts
- default [coSides, coBottom];
- end;
- TCylinderPart = (cySides, cyBottom, cyTop);
- TCylinderParts = set of TCylinderPart;
- TCylinderAlignment = (caCenter, caTop, caBottom);
- // Cylinder object, can also be used to make truncated cones
- TGLCylinder = class(TGLCylinderBase)
- private
- FParts: TCylinderParts;
- FTopRadius: Single;
- FAlignment: TCylinderAlignment;
- protected
- procedure SetTopRadius(const aValue: Single);
- procedure SetParts(aValue: TCylinderParts);
- procedure SetAlignment(val: TCylinderAlignment);
- function GetTopRadius: Single; override;
- public
- constructor Create(AOwner: TComponent); override;
- procedure Assign(Source: TPersistent); override;
- procedure BuildList(var rci: TGLRenderContextInfo); override;
- function AxisAlignedDimensionsUnscaled: TVector; override;
- function RayCastIntersect(const rayStart, rayVector: TVector;
- intersectPoint: PVector = nil; intersectNormal: PVector = nil)
- : Boolean; override;
- procedure Align(const startPoint, endPoint: TVector); overload;
- procedure Align(const startObj, endObj: TGLBaseSceneObject); overload;
- procedure Align(const startPoint, endPoint: TAffineVector); overload;
- published
- property TopRadius: Single read FTopRadius write SetTopRadius;
- property Parts: TCylinderParts read FParts write SetParts
- default [cySides, cyBottom, cyTop];
- property Alignment: TCylinderAlignment read FAlignment write SetAlignment
- default caCenter;
- end;
- // Capsule object, can also be used to make truncated cones
- TGLCapsule = class(TGLSceneObject)
- private
- FParts: TCylinderParts;
- FRadius: Single;
- FSlices: Integer;
- FStacks: Integer;
- FHeight: Single;
- FAlignment: TCylinderAlignment;
- protected
- procedure SetHeight(const aValue: Single);
- procedure SetRadius(const aValue: Single);
- procedure SetSlices(const aValue: integer);
- procedure SetStacks(const aValue: integer);
- procedure SetParts(aValue: TCylinderParts);
- procedure SetAlignment(val: TCylinderAlignment);
- public
- constructor Create(AOwner: TComponent); override;
- procedure Assign(Source: TPersistent); override;
- procedure BuildList(var rci: TGLRenderContextInfo); override;
- function AxisAlignedDimensionsUnscaled: TVector; override;
- function RayCastIntersect(const rayStart, rayVector: TVector;
- intersectPoint: PVector = nil; intersectNormal: PVector = nil)
- : Boolean; override;
- procedure Align(const startPoint, endPoint: TVector); overload;
- procedure Align(const startObj, endObj: TGLBaseSceneObject); overload;
- procedure Align(const startPoint, endPoint: TAffineVector); overload;
- published
- property Height: Single read FHeight write SetHeight;
- property Slices: Integer read FSlices write SetSlices;
- property Stacks: Integer read FStacks write SetStacks;
- property Radius: Single read FRadius write SetRadius;
- property Parts: TCylinderParts read FParts write SetParts
- default [cySides, cyBottom, cyTop];
- property Alignment: TCylinderAlignment read FAlignment write SetAlignment
- default caCenter;
- end;
- TAnnulusPart = (anInnerSides, anOuterSides, anBottom, anTop);
- TAnnulusParts = set of TAnnulusPart;
- // An annulus is a cylinder that can be made hollow (pipe-like)
- TGLAnnulus = class(TGLCylinderBase)
- private
- FParts: TAnnulusParts;
- FBottomInnerRadius: Single;
- FTopInnerRadius: Single;
- FTopRadius: Single;
- protected
- procedure SetTopRadius(const aValue: Single);
- procedure SetTopInnerRadius(const aValue: Single);
- procedure SetBottomInnerRadius(const aValue: Single);
- procedure SetParts(aValue: TAnnulusParts);
- public
- constructor Create(AOwner: TComponent); override;
- procedure Assign(Source: TPersistent); override;
- procedure BuildList(var rci: TGLRenderContextInfo); override;
- function AxisAlignedDimensionsUnscaled: TVector; override;
- function RayCastIntersect(const rayStart, rayVector: TVector;
- intersectPoint: PVector = nil; intersectNormal: PVector = nil)
- : Boolean; override;
- published
- property BottomInnerRadius: Single read FBottomInnerRadius
- write SetBottomInnerRadius;
- property TopInnerRadius: Single read FTopInnerRadius
- write SetTopInnerRadius;
- property TopRadius: Single read FTopRadius write SetTopRadius;
- property Parts: TAnnulusParts read FParts write SetParts
- default [anInnerSides, anOuterSides, anBottom, anTop];
- end;
- TTorusPart = (toSides, toStartDisk, toStopDisk);
- TTorusParts = set of TTorusPart;
- // A Torus object
- TGLTorus = class(TGLSceneObject)
- private
- FParts: TTorusParts;
- FRings, FSides: Cardinal;
- FStartAngle, FStopAngle: Single;
- FMinorRadius, FMajorRadius: Single;
- FMesh: array of array of TVertexRec;
- protected
- procedure SetMajorRadius(const aValue: Single);
- procedure SetMinorRadius(const aValue: Single);
- procedure SetRings(aValue: Cardinal);
- procedure SetSides(aValue: Cardinal);
- procedure SetStartAngle(const aValue: Single);
- procedure SetStopAngle(const aValue: Single);
- procedure SetParts(aValue: TTorusParts);
- public
- constructor Create(AOwner: TComponent); override;
- procedure BuildList(var rci: TGLRenderContextInfo); override;
- function AxisAlignedDimensionsUnscaled: TVector; override;
- function RayCastIntersect(const rayStart, rayVector: TVector;
- intersectPoint: PVector = nil; intersectNormal: PVector = nil): Boolean; override;
- published
- property MajorRadius: Single read FMajorRadius write SetMajorRadius;
- property MinorRadius: Single read FMinorRadius write SetMinorRadius;
- property Rings: Cardinal read FRings write SetRings default 25;
- property Sides: Cardinal read FSides write SetSides default 15;
- property StartAngle: Single read FStartAngle write SetStartAngle;
- property StopAngle: Single read FStopAngle write SetStopAngle;
- property Parts: TTorusParts read FParts write SetParts default [toSides];
- end;
- TArrowLinePart = (alLine, alTopArrow, alBottomArrow);
- TArrowLineParts = set of TArrowLinePart;
- TGLArrowHeadStyle = (ahssStacked, ahssCentered, ahssIncluded);
- (* Draws an arrowhead (cylinder + cone).
- The arrow head is a cone that shares the attributes of the cylinder
- (ie stacks/slices, materials etc). Seems to work ok.
- This is useful for displaying a vector based field (eg velocity) or
- other arrows that might be required.
- By default the bottom arrow is off *)
- TGLArrowLine = class(TGLCylinderBase)
- private
- FParts: TArrowLineParts;
- FTopRadius: Single;
- fTopArrowHeadHeight: Single;
- fTopArrowHeadRadius: Single;
- fBottomArrowHeadHeight: Single;
- fBottomArrowHeadRadius: Single;
- FHeadStackingStyle: TGLArrowHeadStyle;
- protected
- procedure SetTopRadius(const aValue: Single);
- procedure SetTopArrowHeadHeight(const aValue: Single);
- procedure SetTopArrowHeadRadius(const aValue: Single);
- procedure SetBottomArrowHeadHeight(const aValue: Single);
- procedure SetBottomArrowHeadRadius(const aValue: Single);
- procedure SetParts(aValue: TArrowLineParts);
- procedure SetHeadStackingStyle(const val: TGLArrowHeadStyle);
- public
- constructor Create(AOwner: TComponent); override;
- procedure BuildList(var rci: TGLRenderContextInfo); override;
- procedure Assign(Source: TPersistent); override;
- published
- property TopRadius: Single read FTopRadius write SetTopRadius;
- property HeadStackingStyle: TGLArrowHeadStyle read FHeadStackingStyle
- write SetHeadStackingStyle default ahssStacked;
- property Parts: TArrowLineParts read FParts write SetParts
- default [alLine, alTopArrow];
- property TopArrowHeadHeight: Single read fTopArrowHeadHeight
- write SetTopArrowHeadHeight;
- property TopArrowHeadRadius: Single read fTopArrowHeadRadius
- write SetTopArrowHeadRadius;
- property BottomArrowHeadHeight: Single read fBottomArrowHeadHeight
- write SetBottomArrowHeadHeight;
- property BottomArrowHeadRadius: Single read fBottomArrowHeadRadius
- write SetBottomArrowHeadRadius;
- end;
- TArrowArcPart = (aaArc, aaTopArrow, aaBottomArrow);
- TGLArrowArcPart = set of TArrowArcPart;
- (* Draws an arrowhead (Sliced Torus + cone).
- The arrow head is a cone that shares the attributes of the Torus
- (ie stacks/slices, materials etc).
- This is useful for displaying a movement (eg twist) or
- other arc arrows that might be required.
- By default the bottom arrow is off *)
- TGLArrowArc = class(TGLCylinderBase)
- private
- fArcRadius: Single;
- FStartAngle: Single;
- FStopAngle: Single;
- FParts: TGLArrowArcPart;
- FTopRadius: Single;
- fTopArrowHeadHeight: Single;
- fTopArrowHeadRadius: Single;
- fBottomArrowHeadHeight: Single;
- fBottomArrowHeadRadius: Single;
- FHeadStackingStyle: TGLArrowHeadStyle;
- FMesh: array of array of TVertexRec;
- protected
- procedure SetArcRadius(const aValue: Single);
- procedure SetStartAngle(const aValue: Single);
- procedure SetStopAngle(const aValue: Single);
- procedure SetTopRadius(const aValue: Single);
- procedure SetTopArrowHeadHeight(const aValue: Single);
- procedure SetTopArrowHeadRadius(const aValue: Single);
- procedure SetBottomArrowHeadHeight(const aValue: Single);
- procedure SetBottomArrowHeadRadius(const aValue: Single);
- procedure SetParts(aValue: TGLArrowArcPart);
- procedure SetHeadStackingStyle(const val: TGLArrowHeadStyle);
- public
- constructor Create(AOwner: TComponent); override;
- procedure BuildList(var rci: TGLRenderContextInfo); override;
- procedure Assign(Source: TPersistent); override;
- published
- property ArcRadius: Single read fArcRadius write SetArcRadius;
- property StartAngle: Single read FStartAngle write SetStartAngle;
- property StopAngle: Single read FStopAngle write SetStopAngle;
- property TopRadius: Single read FTopRadius write SetTopRadius;
- property HeadStackingStyle: TGLArrowHeadStyle read FHeadStackingStyle
- write SetHeadStackingStyle default ahssStacked;
- property Parts: TGLArrowArcPart read FParts write SetParts
- default [aaArc, aaTopArrow];
- property TopArrowHeadHeight: Single read fTopArrowHeadHeight
- write SetTopArrowHeadHeight;
- property TopArrowHeadRadius: Single read fTopArrowHeadRadius
- write SetTopArrowHeadRadius;
- property BottomArrowHeadHeight: Single read fBottomArrowHeadHeight
- write SetBottomArrowHeadHeight;
- property BottomArrowHeadRadius: Single read fBottomArrowHeadRadius
- write SetBottomArrowHeadRadius;
- end;
- TPolygonPart = (ppTop, ppBottom);
- TGLPolygonParts = set of TPolygonPart;
- (* A basic polygon object.
- The curve is described by the Nodes and SplineMode properties, should be
- planar and is automatically tessellated.
- Texture coordinates are deduced from X and Y coordinates only.
- This object allows only for polygons described by a single curve, if you
- need "complex polygons" with holes, patches and cutouts, see GLMultiPolygon. *)
- TGLPolygon = class(TGLPolygonBase)
- private
- FParts: TGLPolygonParts;
- protected
- procedure SetParts(const val: TGLPolygonParts);
- public
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure BuildList(var rci: TGLRenderContextInfo); override;
- published
- (* Parts of polygon.
- The 'top' of the polygon is the position were the curve describing
- the polygon spin counter-clockwise (i.e. right handed convention). *)
- property Parts: TGLPolygonParts read FParts write SetParts default [ppTop, ppBottom];
- end;
- TFrustrumPart = (fpTop, fpBottom, fpFront, fpBack, fpLeft, fpRight);
- TFrustrumParts = set of TFrustrumPart;
- const
- cAllFrustrumParts = [fpTop, fpBottom, fpFront, fpBack, fpLeft, fpRight];
- type
- (* A frustrum is a pyramid with the top chopped off.
- The height of the imaginary pyramid is ApexHeight, the height of the
- frustrum is Height. If ApexHeight and Height are the same, the frustrum
- degenerates into a pyramid.
- Height cannot be greater than ApexHeight. *)
- TGLFrustrum = class(TGLSceneObject)
- private
- FApexHeight, FBaseDepth, FBaseWidth, FHeight: Single;
- FParts: TFrustrumParts;
- FNormalDirection: TGLNormalDirection;
- procedure SetApexHeight(const aValue: Single);
- procedure SetBaseDepth(const aValue: Single);
- procedure SetBaseWidth(const aValue: Single);
- procedure SetHeight(const aValue: Single);
- procedure SetParts(aValue: TFrustrumParts);
- procedure SetNormalDirection(aValue: TGLNormalDirection);
- protected
- procedure DefineProperties(Filer: TFiler); override;
- procedure ReadData(Stream: TStream);
- procedure WriteData(Stream: TStream);
- public
- constructor Create(AOwner: TComponent); override;
- procedure BuildList(var rci: TGLRenderContextInfo); override;
- procedure Assign(Source: TPersistent); override;
- function TopDepth: Single;
- function TopWidth: Single;
- function AxisAlignedBoundingBoxUnscaled: TAABB;
- function AxisAlignedDimensionsUnscaled: TVector; override;
- published
- property ApexHeight: Single read FApexHeight write SetApexHeight stored False;
- property BaseDepth: Single read FBaseDepth write SetBaseDepth stored False;
- property BaseWidth: Single read FBaseWidth write SetBaseWidth stored False;
- property Height: Single read FHeight write SetHeight stored False;
- property NormalDirection: TGLNormalDirection read FNormalDirection
- write SetNormalDirection default ndOutside;
- property Parts: TFrustrumParts read FParts write SetParts default cAllFrustrumParts;
- end;
- // -------------------------------------------------------------
- implementation
- // -------------------------------------------------------------
- // ------------------
- // ------------------ TGLDisk ------------------
- // ------------------
- constructor TGLDisk.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FOuterRadius := 0.5;
- FInnerRadius := 0;
- FSlices := 16;
- FLoops := 2;
- FStartAngle := 0;
- FSweepAngle := 360;
- end;
- procedure TGLDisk.BuildList(var rci: TGLRenderContextInfo);
- var
- quadric: PGLUquadric;
- begin
- quadric := gluNewQuadric();
- SetupQuadricParams(quadric);
- gluPartialDisk(quadric, FInnerRadius, FOuterRadius, FSlices, FLoops,
- FStartAngle, FSweepAngle);
- gluDeleteQuadric(quadric);
- end;
- procedure TGLDisk.SetOuterRadius(const aValue: Single);
- begin
- if aValue <> FOuterRadius then
- begin
- FOuterRadius := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLDisk.SetInnerRadius(const aValue: Single);
- begin
- if aValue <> FInnerRadius then
- begin
- FInnerRadius := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLDisk.SetSlices(aValue: integer);
- begin
- if aValue <> FSlices then
- begin
- FSlices := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLDisk.SetLoops(aValue: integer);
- begin
- if aValue <> FLoops then
- begin
- FLoops := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLDisk.SetStartAngle(const aValue: Single);
- begin
- if aValue <> FStartAngle then
- begin
- FStartAngle := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLDisk.SetSweepAngle(const aValue: Single);
- begin
- if aValue <> FSweepAngle then
- begin
- FSweepAngle := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLDisk.Assign(Source: TPersistent);
- begin
- if Assigned(Source) and (Source is TGLDisk) then
- begin
- FOuterRadius := TGLDisk(Source).FOuterRadius;
- FInnerRadius := TGLDisk(Source).FInnerRadius;
- FSlices := TGLDisk(Source).FSlices;
- FLoops := TGLDisk(Source).FLoops;
- FStartAngle := TGLDisk(Source).FStartAngle;
- FSweepAngle := TGLDisk(Source).FSweepAngle;
- end;
- inherited Assign(Source);
- end;
- function TGLDisk.AxisAlignedDimensionsUnscaled: TVector;
- var
- r: Single;
- begin
- r := Abs(FOuterRadius);
- Result := VectorMake(r, r, 0);
- end;
- function TGLDisk.RayCastIntersect(const rayStart, rayVector: TVector;
- intersectPoint: PVector = nil; intersectNormal: PVector = nil): Boolean;
- var
- ip: TVector;
- d: Single;
- angle, beginAngle, endAngle: Single;
- localIntPoint: TVector;
- begin
- Result := False;
- if SweepAngle > 0 then
- if RayCastPlaneIntersect(rayStart, rayVector, AbsolutePosition,
- AbsoluteDirection, @ip) then
- begin
- if Assigned(intersectPoint) then
- SetVector(intersectPoint^, ip);
- localIntPoint := AbsoluteToLocal(ip);
- d := VectorNorm(localIntPoint);
- if (d >= Sqr(InnerRadius)) and (d <= Sqr(OuterRadius)) then
- begin
- if SweepAngle >= 360 then
- Result := true
- else
- begin
- // arctan2 returns results between -pi and +pi, we want between 0 and 360
- angle := 180 / pi * ArcTan2(localIntPoint.X, localIntPoint.Y);
- if angle < 0 then
- angle := angle + 360;
- // we also want StartAngle and StartAngle+SweepAngle to be in this range
- beginAngle := Trunc(StartAngle) mod 360;
- endAngle := Trunc(StartAngle + SweepAngle) mod 360;
- // If beginAngle>endAngle then area crosses the boundary from 360=>0 degrees
- // therefore have 2 valid regions (beginAngle to 360) & (0 to endAngle)
- // otherwise just 1 valid region (beginAngle to endAngle)
- if beginAngle > endAngle then
- begin
- if (angle > beginAngle) or (angle < endAngle) then
- Result := true;
- end
- else if (angle > beginAngle) and (angle < endAngle) then
- Result := true;
- end;
- end;
- end;
- if Result = true then
- if Assigned(intersectNormal) then
- SetVector(intersectNormal^, AbsoluteUp);
- end;
- // ------------------
- // ------------------ TGLCylinderBase ------------------
- // ------------------
- constructor TGLCylinderBase.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FBottomRadius := 0.5;
- FHeight := 1;
- FSlices := 16;
- FStacks := 4;
- FLoops := 1;
- end;
- procedure TGLCylinderBase.SetBottomRadius(const aValue: Single);
- begin
- if aValue <> FBottomRadius then
- begin
- FBottomRadius := aValue;
- StructureChanged;
- end;
- end;
- function TGLCylinderBase.GetTopRadius: Single;
- begin
- Result := FBottomRadius;
- end;
- procedure TGLCylinderBase.SetHeight(const aValue: Single);
- begin
- if aValue <> FHeight then
- begin
- FHeight := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLCylinderBase.SetSlices(aValue: Integer);
- begin
- if aValue <> FSlices then
- begin
- FSlices := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLCylinderBase.SetStacks(aValue: Integer);
- begin
- if aValue <> FStacks then
- begin
- FStacks := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLCylinderBase.SetLoops(aValue: Integer);
- begin
- if (aValue >= 1) and (aValue <> FLoops) then
- begin
- FLoops := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLCylinderBase.Assign(Source: TPersistent);
- begin
- if Assigned(Source) and (Source is TGLCylinderBase) then
- begin
- FBottomRadius := TGLCylinderBase(Source).FBottomRadius;
- FSlices := TGLCylinderBase(Source).FSlices;
- FStacks := TGLCylinderBase(Source).FStacks;
- FLoops := TGLCylinderBase(Source).FLoops;
- FHeight := TGLCylinderBase(Source).FHeight;
- end;
- inherited Assign(Source);
- end;
- function TGLCylinderBase.GenerateSilhouette(const silhouetteParameters
- : TGLSilhouetteParameters): TGLSilhouette;
- var
- connectivity: TConnectivity;
- sil: TGLSilhouette;
- ShadowSlices: integer;
- i: integer;
- p: array [0 .. 3] of TVector3f;
- PiDivSlices: Single;
- a1, a2: Single;
- c1, c2: TVector3f;
- cosa1, cosa2, sina1, sina2: Single;
- HalfHeight: Single;
- ShadowTopRadius: Single;
- begin
- connectivity := TConnectivity.Create(true);
- ShadowSlices := FSlices div 1;
- if FSlices < 5 then
- FSlices := 5;
- PiDivSlices := 2 * pi / ShadowSlices;
- a1 := 0;
- // Is this a speed improvement or just a waste of code?
- HalfHeight := FHeight / 2;
- MakeVector(c1, 0, -HalfHeight, 0);
- MakeVector(c2, 0, HalfHeight, 0);
- ShadowTopRadius := GetTopRadius;
- for i := 0 to ShadowSlices - 1 do
- begin
- a2 := a1 + PiDivSlices;
- // Is this a speed improvement or just a waste of code?
- cosa1 := cos(a1);
- cosa2 := cos(a2);
- sina1 := sin(a1);
- sina2 := sin(a2);
- // Generate the four "corners";
- // Bottom corners
- MakeVector(p[0], FBottomRadius * sina2, -HalfHeight, FBottomRadius * cosa2);
- MakeVector(p[1], FBottomRadius * sina1, -HalfHeight, FBottomRadius * cosa1);
- // Top corners
- MakeVector(p[2], ShadowTopRadius * sina1, HalfHeight,
- ShadowTopRadius * cosa1);
- MakeVector(p[3], ShadowTopRadius * sina2, HalfHeight,
- ShadowTopRadius * cosa2); // }
- // This should be optimized to use AddIndexedFace, because this method
- // searches for each of the vertices and adds them or re-uses them.
- // Skin
- connectivity.AddFace(p[2], p[1], p[0]);
- connectivity.AddFace(p[3], p[2], p[0]);
- // Sides / caps
- connectivity.AddFace(c1, p[0], p[1]);
- connectivity.AddFace(p[2], p[3], c2);
- a1 := a1 + PiDivSlices;
- end;
- sil := nil;
- connectivity.CreateSilhouette(silhouetteParameters, sil, False);
- Result := sil;
- connectivity.Free;
- end;
- // ------------------
- // ------------------ TGLCone ------------------
- // ------------------
- constructor TGLCone.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FParts := [coSides, coBottom];
- end;
- procedure TGLCone.BuildList(var rci: TGLRenderContextInfo);
- var
- quadric: PGLUquadricObj;
- begin
- gl.PushMatrix;
- quadric := gluNewQuadric();
- SetupQuadricParams(quadric);
- gl.Rotated(-90, 1, 0, 0);
- gl.Translatef(0, 0, -FHeight * 0.5);
- if coSides in FParts then
- gluCylinder(quadric, BottomRadius, 0, Height, Slices, Stacks);
- if coBottom in FParts then
- begin
- // top of a disk is defined as outside
- SetInvertedQuadricOrientation(quadric);
- gluDisk(quadric, 0, BottomRadius, Slices, FLoops);
- end;
- gluDeleteQuadric(quadric);
- gl.PopMatrix;
- end;
- procedure TGLCone.SetParts(aValue: TConeParts);
- begin
- if aValue <> FParts then
- begin
- FParts := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLCone.Assign(Source: TPersistent);
- begin
- if Assigned(Source) and (Source is TGLCone) then
- begin
- FParts := TGLCone(Source).FParts;
- end;
- inherited Assign(Source);
- end;
- function TGLCone.AxisAlignedDimensionsUnscaled: TVector;
- var
- r: Single;
- begin
- r := Abs(FBottomRadius);
- Result := VectorMake(r { *Scale.DirectX } , 0.5 * FHeight { *Scale.DirectY } ,
- r { *Scale.DirectZ } );
- end;
- function TGLCone.GetTopRadius: Single;
- begin
- Result := 0;
- end;
- function TGLCone.RayCastIntersect(const rayStart, rayVector: TVector;
- intersectPoint: PVector = nil; intersectNormal: PVector = nil): Boolean;
- var
- ip, localRayStart, localRayVector: TVector;
- poly: array [0 .. 2] of Double;
- roots: TDoubleArray;
- minRoot: Double;
- d, t, hconst: Single;
- begin
- Result := False;
- localRayStart := AbsoluteToLocal(rayStart);
- localRayVector := VectorNormalize(AbsoluteToLocal(rayVector));
- if coBottom in Parts then
- begin
- // bottom can only be raycast from beneath
- if localRayStart.Y < -FHeight * 0.5 then
- begin
- if RayCastPlaneIntersect(localRayStart, localRayVector,
- PointMake(0, -FHeight * 0.5, 0), YHmgVector, @ip) then
- begin
- d := VectorNorm(ip.X, ip.Z);
- if (d <= Sqr(BottomRadius)) then
- begin
- Result := true;
- if Assigned(intersectPoint) then
- SetVector(intersectPoint^, LocalToAbsolute(ip));
- if Assigned(intersectNormal) then
- SetVector(intersectNormal^, VectorNegate(AbsoluteUp));
- Exit;
- end;
- end;
- end;
- end;
- if coSides in Parts then
- begin
- hconst := -Sqr(BottomRadius) / Sqr(Height);
- // intersect against infinite cones (in positive and negative direction)
- poly[0] := Sqr(localRayStart.X) + hconst *
- Sqr(localRayStart.Y - 0.5 * FHeight) +
- Sqr(localRayStart.Z);
- poly[1] := 2 * (localRayStart.X * localRayVector.X + hconst *
- (localRayStart.Y - 0.5 * FHeight) * localRayVector.Y +
- localRayStart.Z* localRayVector.Z);
- poly[2] := Sqr(localRayVector.X) + hconst * Sqr(localRayVector.Y) +
- Sqr(localRayVector.Z);
- SetLength(roots, 0);
- roots := SolveQuadric(@poly);
- if MinPositiveCoef(roots, minRoot) then
- begin
- t := minRoot;
- ip := VectorCombine(localRayStart, localRayVector, 1, t);
- // check that intersection with infinite cone is within the range we want
- if (ip.Y > -FHeight * 0.5) and (ip.Y < FHeight * 0.5) then
- begin
- Result := true;
- if Assigned(intersectPoint) then
- intersectPoint^ := LocalToAbsolute(ip);
- if Assigned(intersectNormal) then
- begin
- ip.Y := hconst * (ip.Y - 0.5 * Height);
- ip.W := 0;
- NormalizeVector(ip);
- intersectNormal^ := LocalToAbsolute(ip);
- end;
- end;
- end;
- end;
- end;
- // ------------------
- // ------------------ TGLCylinder ------------------
- // ------------------
- constructor TGLCylinder.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FTopRadius := 0.5;
- FParts := [cySides, cyBottom, cyTop];
- FAlignment := caCenter;
- end;
- procedure TGLCylinder.BuildList(var rci: TGLRenderContextInfo);
- var
- quadric: PGLUquadricObj;
- begin
- gl.PushMatrix;
- quadric := gluNewQuadric;
- SetupQuadricParams(quadric);
- gl.Rotatef(-90, 1, 0, 0);
- case Alignment of
- caTop:
- gl.Translatef(0, 0, -FHeight);
- caBottom:
- ;
- else // caCenter
- gl.Translatef(0, 0, -FHeight * 0.5);
- end;
- if cySides in FParts then
- gluCylinder(quadric, FBottomRadius, FTopRadius, FHeight, FSlices, FStacks);
- if cyTop in FParts then
- begin
- gl.PushMatrix;
- gl.Translatef(0, 0, FHeight);
- gluDisk(Quadric, 0, FTopRadius, FSlices, FLoops);
- gl.PopMatrix;
- end;
- if cyBottom in FParts then
- begin
- // swap quadric orientation because top of a disk is defined as outside
- SetInvertedQuadricOrientation(quadric);
- gluDisk(quadric, 0, FBottomRadius, FSlices, FLoops);
- end;
- gluDeleteQuadric(Quadric);
- gl.PopMatrix;
- end;
- procedure TGLCylinder.SetTopRadius(const aValue: Single);
- begin
- if aValue <> FTopRadius then
- begin
- FTopRadius := aValue;
- StructureChanged;
- end;
- end;
- function TGLCylinder.GetTopRadius: Single;
- begin
- Result := FTopRadius;
- end;
- procedure TGLCylinder.SetParts(aValue: TCylinderParts);
- begin
- if aValue <> FParts then
- begin
- FParts := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLCylinder.SetAlignment(val: TCylinderAlignment);
- begin
- if val <> FAlignment then
- begin
- FAlignment := val;
- StructureChanged;
- end;
- end;
- procedure TGLCylinder.Assign(Source: TPersistent);
- begin
- if Assigned(Source) and (Source is TGLCylinder) then
- begin
- FParts := TGLCylinder(Source).FParts;
- FTopRadius := TGLCylinder(Source).FTopRadius;
- end;
- inherited Assign(Source);
- end;
- function TGLCylinder.AxisAlignedDimensionsUnscaled: TVector;
- var
- r, r1: Single;
- begin
- r := Abs(FBottomRadius);
- r1 := Abs(FTopRadius);
- if r1 > r then
- r := r1;
- Result := VectorMake(r, 0.5 * FHeight, r);
- // ScaleVector(Result, Scale.AsVector);
- end;
- function TGLCylinder.RayCastIntersect(const rayStart, rayVector: TVector;
- intersectPoint: PVector = nil; intersectNormal: PVector = nil): Boolean;
- const
- cOne: Single = 1;
- var
- locRayStart, locRayVector, ip: TVector;
- poly: array [0 .. 2] of Double;
- roots: TDoubleArray;
- minRoot: Double;
- t, tr2, invRayVector1, hTop, hBottom: Single;
- tPlaneMin, tPlaneMax: Single;
- begin
- Result := False;
- locRayStart := AbsoluteToLocal(rayStart);
- locRayVector := AbsoluteToLocal(rayVector);
- case Alignment of
- caTop:
- begin
- hTop := 0;
- hBottom := -Height;
- end;
- caBottom:
- begin
- hTop := Height;
- hBottom := 0;
- end;
- else
- // caCenter
- hTop := Height * 0.5;
- hBottom := -hTop;
- end;
- if locRayVector.Y = 0 then
- begin
- // intersect if ray shot through the top/bottom planes
- if (locRayStart.X > hTop) or (locRayStart.X < hBottom) then
- Exit;
- tPlaneMin := -1E99;
- tPlaneMax := 1E99;
- end
- else
- begin
- invRayVector1 := cOne / locRayVector.Y;
- tr2 := Sqr(TopRadius);
- // compute intersection with topPlane
- t := (hTop - locRayStart.Y) * invRayVector1;
- if (t > 0) and (cyTop in Parts) then
- begin
- ip.X := locRayStart.X + t * locRayVector.X;
- ip.Z := locRayStart.Z + t * locRayVector.Z;
- if Sqr(ip.X) + Sqr(ip.Z) <= tr2 then
- begin
- // intersect with top plane
- if Assigned(intersectPoint) then
- intersectPoint^ := LocalToAbsolute(VectorMake(ip.X, hTop, ip.Z, 1));
- if Assigned(intersectNormal) then
- intersectNormal^ := LocalToAbsolute(YHmgVector);
- Result := true;
- end;
- end;
- tPlaneMin := t;
- tPlaneMax := t;
- // compute intersection with bottomPlane
- t := (hBottom - locRayStart.Y) * invRayVector1;
- if (t > 0) and (cyBottom in Parts) then
- begin
- ip.X := locRayStart.X + t * locRayVector.X;
- ip.Z := locRayStart.Z + t * locRayVector.Z;
- if (t < tPlaneMin) or (not(cyTop in Parts)) then
- begin
- if Sqr(ip.X) + Sqr(ip.Z) <= tr2 then
- begin
- // intersect with top plane
- if Assigned(intersectPoint) then
- intersectPoint^ := LocalToAbsolute(VectorMake(ip.X, hBottom,
- ip.Z, 1));
- if Assigned(intersectNormal) then
- intersectNormal^ := LocalToAbsolute(VectorNegate(YHmgVector));
- Result := true;
- end;
- end;
- end;
- if t < tPlaneMin then
- tPlaneMin := t;
- if t > tPlaneMax then
- tPlaneMax := t;
- end;
- if cySides in Parts then
- begin
- // intersect against cylinder infinite cylinder
- poly[0] := Sqr(locRayStart.X) + Sqr(locRayStart.Z) - Sqr(TopRadius);
- poly[1] := 2 * (locRayStart.X * locRayVector.X + locRayStart.Z *
- locRayVector.Z);
- poly[2] := Sqr(locRayVector.X) + Sqr(locRayVector.Z);
- roots := SolveQuadric(@poly);
- if MinPositiveCoef(roots, minRoot) then
- begin
- t := minRoot;
- if (t >= tPlaneMin) and (t < tPlaneMax) then
- begin
- if Assigned(intersectPoint) or Assigned(intersectNormal) then
- begin
- ip := VectorCombine(locRayStart, locRayVector, 1, t);
- if Assigned(intersectPoint) then
- intersectPoint^ := LocalToAbsolute(ip);
- if Assigned(intersectNormal) then
- begin
- ip.Y := 0;
- ip.W := 0;
- intersectNormal^ := LocalToAbsolute(ip);
- end;
- end;
- Result := true;
- end;
- end;
- end
- else
- SetLength(roots, 0);
- end;
- procedure TGLCylinder.Align(const startPoint, endPoint: TVector);
- var
- dir: TAffineVector;
- begin
- AbsolutePosition := startPoint;
- VectorSubtract(endPoint, startPoint, dir);
- if Parent <> nil then
- dir := Parent.AbsoluteToLocal(dir);
- Up.AsAffineVector := dir;
- Height := VectorLength(dir);
- Lift(Height * 0.5);
- Alignment := caCenter;
- end;
- procedure TGLCylinder.Align(const startObj, endObj: TGLBaseSceneObject);
- begin
- Align(startObj.AbsolutePosition, endObj.AbsolutePosition);
- end;
- procedure TGLCylinder.Align(const startPoint, endPoint: TAffineVector);
- begin
- Align(PointMake(startPoint), PointMake(endPoint));
- end;
- // ------------------
- // ------------------ TGLCapsule ------------------
- // ------------------
- constructor TGLCapsule.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FHeight := 1;
- FRadius := 0.5;
- FSlices := 4;
- FStacks := 4;
- FParts := [cySides, cyBottom, cyTop];
- FAlignment := caCenter;
- end;
- procedure TGLCapsule.BuildList(var rci: TGLRenderContextInfo);
- var
- i, j, n: integer;
- start_nx2: Single;
- start_ny2: Single;
- tmp, nx, ny, nz, start_nx, start_ny, a, ca, sa, l: Single;
- nx2, ny2, nz2: Single;
- begin
- gl.PushMatrix;
- gl.Rotatef(-90, 0, 0, 1);
- case Alignment of
- caTop:
- gl.Translatef(0, 0, FHeight + 1);
- caBottom:
- gl.Translatef(0, 0, -FHeight);
- else // caCenter
- gl.Translatef(0, 0, 0.5);
- end;
- n := FSlices * FStacks;
- l := FHeight;
- l := l * 0.5;
- a := (pi * 2.0) / n;
- sa := sin(a);
- ca := cos(a);
- ny := 0;
- nz := 1;
- if cySides in FParts then
- begin
- gl.Begin_(GL_TRIANGLE_STRIP);
- for i := 0 to n do
- begin
- gl.Normal3d(ny, nz, 0);
- gl.TexCoord2f(i / n, 1);
- gl.Vertex3d(ny * FRadius, nz * FRadius, l - 0.5);
- gl.Normal3d(ny, nz, 0);
- gl.TexCoord2f(i / n, 0);
- gl.Vertex3d(ny * FRadius, nz * FRadius, -l - 0.5);
- tmp := ca * ny - sa * nz;
- nz := sa * ny + ca * nz;
- ny := tmp;
- end;
- gl.End_();
- end;
- //
- if cyTop in FParts then
- begin
- start_nx := 0;
- start_ny := 1;
- for j := 0 to (n div FStacks) do
- begin
- start_nx2 := ca * start_nx + sa * start_ny;
- start_ny2 := -sa * start_nx + ca * start_ny;
- nx := start_nx;
- ny := start_ny;
- nz := 0;
- nx2 := start_nx2;
- ny2 := start_ny2;
- nz2 := 0;
- gl.PushMatrix;
- gl.Translatef(0, 0, -0.5);
- gl.Begin_(GL_TRIANGLE_STRIP);
- for i := 0 to n do
- begin
- gl.Normal3d(ny2, nz2, nx2);
- gl.TexCoord2f(i / n, j / n);
- gl.Vertex3d(ny2 * FRadius, nz2 * FRadius, l + nx2 * FRadius);
- gl.Normal3d(ny, nz, nx);
- gl.TexCoord2f(i / n, (j - 1) / n);
- gl.Vertex3d(ny * FRadius, nz * FRadius, l + nx * FRadius);
- tmp := ca * ny - sa * nz;
- nz := sa * ny + ca * nz;
- ny := tmp;
- tmp := ca * ny2 - sa * nz2;
- nz2 := sa * ny2 + ca * nz2;
- ny2 := tmp;
- end;
- gl.End_();
- gl.PopMatrix;
- start_nx := start_nx2;
- start_ny := start_ny2;
- end;
- end;
- //
- if cyBottom in FParts then
- begin
- start_nx := 0;
- start_ny := 1;
- for j := 0 to (n div FStacks) do
- begin
- start_nx2 := ca * start_nx - sa * start_ny;
- start_ny2 := sa * start_nx + ca * start_ny;
- nx := start_nx;
- ny := start_ny;
- nz := 0;
- nx2 := start_nx2;
- ny2 := start_ny2;
- nz2 := 0;
- gl.PushMatrix;
- gl.Translatef(0, 0, -0.5);
- gl.Begin_(GL_TRIANGLE_STRIP);
- for i := 0 to n do
- begin
- gl.Normal3d(ny, nz, nx);
- gl.TexCoord2f(i / n, (j - 1) / n);
- gl.Vertex3d(ny * FRadius, nz * FRadius, -l + nx * FRadius);
- gl.Normal3d(ny2, nz2, nx2);
- gl.TexCoord2f(i / n, j / n);
- gl.Vertex3d(ny2 * FRadius, nz2 * FRadius, -l + nx2 * FRadius);
- tmp := ca * ny - sa * nz;
- nz := sa * ny + ca * nz;
- ny := tmp;
- tmp := ca * ny2 - sa * nz2;
- nz2 := sa * ny2 + ca * nz2;
- ny2 := tmp;
- end;
- gl.End_();
- gl.PopMatrix;
- start_nx := start_nx2;
- start_ny := start_ny2;
- end;
- end;
- gl.PopMatrix;
- end;
- procedure TGLCapsule.SetHeight(const aValue: Single);
- begin
- if aValue <> FHeight then
- begin
- FHeight := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLCapsule.SetRadius(const aValue: Single);
- begin
- if aValue <> FRadius then
- begin
- FRadius := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLCapsule.SetSlices(const aValue: integer);
- begin
- if aValue <> FSlices then
- begin
- FSlices := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLCapsule.SetStacks(const aValue: integer);
- begin
- if aValue <> FStacks then
- begin
- FStacks := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLCapsule.SetParts(aValue: TCylinderParts);
- begin
- if aValue <> FParts then
- begin
- FParts := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLCapsule.SetAlignment(val: TCylinderAlignment);
- begin
- if val <> FAlignment then
- begin
- FAlignment := val;
- StructureChanged;
- end;
- end;
- procedure TGLCapsule.Assign(Source: TPersistent);
- begin
- if Assigned(Source) and (Source is TGLCapsule) then
- begin
- FParts := TGLCapsule(Source).FParts;
- FRadius := TGLCapsule(Source).FRadius;
- end;
- inherited Assign(Source);
- end;
- function TGLCapsule.AxisAlignedDimensionsUnscaled: TVector;
- var
- r, r1: Single;
- begin
- r := Abs(FRadius);
- r1 := Abs(FRadius);
- if r1 > r then
- r := r1;
- Result := VectorMake(r, 0.5 * FHeight, r);
- // ScaleVector(Result, Scale.AsVector);
- end;
- function TGLCapsule.RayCastIntersect(const rayStart, rayVector: TVector;
- intersectPoint: PVector = nil; intersectNormal: PVector = nil): Boolean;
- const
- cOne: Single = 1;
- var
- locRayStart, locRayVector, ip: TVector;
- poly: array [0 .. 2] of Double;
- roots: TDoubleArray;
- minRoot: Double;
- t, tr2, invRayVector1, hTop, hBottom: Single;
- tPlaneMin, tPlaneMax: Single;
- begin
- Result := False;
- locRayStart := AbsoluteToLocal(rayStart);
- locRayVector := AbsoluteToLocal(rayVector);
- case Alignment of
- caTop:
- begin
- hTop := 0;
- hBottom := -FHeight;
- end;
- caBottom:
- begin
- hTop := FHeight;
- hBottom := 0;
- end;
- else
- // caCenter
- hTop := FHeight * 0.5;
- hBottom := -hTop;
- end;
- if locRayVector.Y = 0 then
- begin
- // intersect if ray shot through the top/bottom planes
- if (locRayStart.X > hTop) or (locRayStart.X < hBottom) then
- Exit;
- tPlaneMin := -1E99;
- tPlaneMax := 1E99;
- end
- else
- begin
- invRayVector1 := cOne / locRayVector.Y;
- tr2 := Sqr(Radius);
- // compute intersection with topPlane
- t := (hTop - locRayStart.Y) * invRayVector1;
- if (t > 0) and (cyTop in Parts) then
- begin
- ip.X := locRayStart.X + t * locRayVector.X;
- ip.Z := locRayStart.Z + t * locRayVector.Z;
- if Sqr(ip.X) + Sqr(ip.Z) <= tr2 then
- begin
- // intersect with top plane
- if Assigned(intersectPoint) then
- intersectPoint^ := LocalToAbsolute(VectorMake(ip.X, hTop, ip.Z, 1));
- if Assigned(intersectNormal) then
- intersectNormal^ := LocalToAbsolute(YHmgVector);
- Result := true;
- end;
- end;
- tPlaneMin := t;
- tPlaneMax := t;
- // compute intersection with bottomPlane
- t := (hBottom - locRayStart.Y) * invRayVector1;
- if (t > 0) and (cyBottom in Parts) then
- begin
- ip.X := locRayStart.X + t * locRayVector.X;
- ip.Z := locRayStart.Z + t * locRayVector.Z;
- if (t < tPlaneMin) or (not(cyTop in Parts)) then
- begin
- if Sqr(ip.X) + Sqr(ip.Z) <= tr2 then
- begin
- // intersect with top plane
- if Assigned(intersectPoint) then
- intersectPoint^ := LocalToAbsolute(VectorMake(ip.X, hBottom,
- ip.Z, 1));
- if Assigned(intersectNormal) then
- intersectNormal^ := LocalToAbsolute(VectorNegate(YHmgVector));
- Result := true;
- end;
- end;
- end;
- if t < tPlaneMin then
- tPlaneMin := t;
- if t > tPlaneMax then
- tPlaneMax := t;
- end;
- if cySides in Parts then
- begin
- // intersect against cylinder infinite cylinder
- poly[0] := Sqr(locRayStart.X) + Sqr(locRayStart.Z) - Sqr(Radius);
- poly[1] := 2 * (locRayStart.X * locRayVector.X +
- locRayStart.Z * locRayVector.Z);
- poly[2] := Sqr(locRayVector.X) + Sqr(locRayVector.Z);
- roots := SolveQuadric(@poly);
- if MinPositiveCoef(roots, minRoot) then
- begin
- t := minRoot;
- if (t >= tPlaneMin) and (t < tPlaneMax) then
- begin
- if Assigned(intersectPoint) or Assigned(intersectNormal) then
- begin
- ip := VectorCombine(locRayStart, locRayVector, 1, t);
- if Assigned(intersectPoint) then
- intersectPoint^ := LocalToAbsolute(ip);
- if Assigned(intersectNormal) then
- begin
- ip.Y := 0;
- ip.W := 0;
- intersectNormal^ := LocalToAbsolute(ip);
- end;
- end;
- Result := true;
- end;
- end;
- end
- else
- SetLength(roots, 0);
- end;
- procedure TGLCapsule.Align(const startPoint, endPoint: TVector);
- var
- dir: TAffineVector;
- begin
- AbsolutePosition := startPoint;
- VectorSubtract(endPoint, startPoint, dir);
- if Parent <> nil then
- dir := Parent.AbsoluteToLocal(dir);
- Up.AsAffineVector := dir;
- FHeight := VectorLength(dir);
- Lift(FHeight * 0.5);
- Alignment := caCenter;
- end;
- procedure TGLCapsule.Align(const startObj, endObj: TGLBaseSceneObject);
- begin
- Align(startObj.AbsolutePosition, endObj.AbsolutePosition);
- end;
- procedure TGLCapsule.Align(const startPoint, endPoint: TAffineVector);
- begin
- Align(PointMake(startPoint), PointMake(endPoint));
- end;
- // ------------------
- // ------------------ TGLAnnulus ------------------
- // ------------------
- constructor TGLAnnulus.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FBottomInnerRadius := 0.3;
- FTopInnerRadius := 0.3;
- FTopRadius := 0.5;
- FParts := [anInnerSides, anOuterSides, anBottom, anTop];
- end;
- procedure TGLAnnulus.SetBottomInnerRadius(const aValue: Single);
- begin
- if aValue <> FBottomInnerRadius then
- begin
- FBottomInnerRadius := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLAnnulus.SetTopRadius(const aValue: Single);
- begin
- if aValue <> FTopRadius then
- begin
- FTopRadius := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLAnnulus.SetTopInnerRadius(const aValue: Single);
- begin
- if aValue <> FTopInnerRadius then
- begin
- FTopInnerRadius := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLAnnulus.SetParts(aValue: TAnnulusParts);
- begin
- if aValue <> FParts then
- begin
- FParts := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLAnnulus.BuildList(var rci: TGLRenderContextInfo);
- var
- quadric: PGLUquadricObj;
- begin
- gl.PushMatrix;
- quadric := gluNewQuadric;
- SetupQuadricParams(quadric);
- gl.Rotatef(-90, 1, 0, 0);
- gl.Translatef(0, 0, -FHeight * 0.5);
- if anOuterSides in FParts then
- gluCylinder(quadric, FBottomRadius, FTopRadius, FHeight, FSlices, FStacks);
- if anTop in FParts then
- begin
- gl.PushMatrix;
- gl.Translatef(0, 0, FHeight);
- gluDisk(quadric, FTopInnerRadius, FTopRadius, FSlices, FLoops);
- gl.PopMatrix;
- end;
- if [anBottom, anInnerSides] * FParts <> [] then
- begin
- // swap quadric orientation because top of a disk is defined as outside
- SetInvertedQuadricOrientation(quadric);
- if anBottom in FParts then
- gluDisk(quadric, FBottomInnerRadius, FBottomRadius, FSlices, FLoops);
- if anInnerSides in FParts then
- gluCylinder(quadric, FBottomInnerRadius, FTopInnerRadius, FHeight,
- FSlices, FStacks);
- end;
- gluDeleteQuadric(quadric);
- gl.PopMatrix;
- end;
- procedure TGLAnnulus.Assign(Source: TPersistent);
- begin
- if Assigned(Source) and (Source is TGLAnnulus) then
- begin
- FParts := TGLAnnulus(Source).FParts;
- FTopRadius := TGLAnnulus(Source).FTopRadius;
- FTopInnerRadius := TGLAnnulus(Source).FTopInnerRadius;
- FBottomRadius := TGLAnnulus(Source).FBottomRadius;
- FBottomInnerRadius := TGLAnnulus(Source).FBottomInnerRadius;
- end;
- inherited Assign(Source);
- end;
- function TGLAnnulus.AxisAlignedDimensionsUnscaled: TVector;
- var
- r, r1: Single;
- begin
- r := Abs(FBottomRadius);
- r1 := Abs(FTopRadius);
- if r1 > r then
- r := r1;
- Result := VectorMake(r, 0.5 * FHeight, r);
- end;
- function TGLAnnulus.RayCastIntersect(const rayStart, rayVector: TVector;
- intersectPoint, intersectNormal: PVector): Boolean;
- const
- cOne: Single = 1;
- var
- locRayStart, locRayVector, ip: TVector;
- poly: array [0 .. 2] of Double;
- t, tr2, invRayVector1: Single;
- tPlaneMin, tPlaneMax: Single;
- tir2, d2: Single;
- Root: Double;
- roots, tmpRoots: TDoubleArray;
- FirstIntersected: Boolean;
- h1, h2, hTop, hBot: Single;
- Draw1, Draw2: Boolean;
- begin
- Result := False;
- FirstIntersected := False;
- SetLength(tmpRoots, 0);
- locRayStart := AbsoluteToLocal(rayStart);
- locRayVector := AbsoluteToLocal(rayVector);
- hTop := Height * 0.5;
- hBot := -hTop;
- if locRayVector.Y < 0 then
- begin // Sort the planes according to the direction of view
- h1 := hTop; // Height of the 1st plane
- h2 := hBot; // Height of the 2nd plane
- Draw1 := (anTop in Parts); // 1st "cap" Must be drawn?
- Draw2 := (anBottom in Parts);
- end
- else
- begin
- h1 := hBot;
- h2 := hTop;
- Draw1 := (anBottom in Parts);
- Draw2 := (anTop in Parts);
- end; // if
- if locRayVector.Y = 0 then
- begin
- // intersect if ray shot through the top/bottom planes
- if (locRayStart.X > hTop) or (locRayStart.X < hBot) then
- Exit;
- tPlaneMin := -1E99;
- tPlaneMax := 1E99;
- end
- else
- begin
- invRayVector1 := cOne / locRayVector.Y;
- tr2 := Sqr(TopRadius);
- tir2 := Sqr(TopInnerRadius);
- FirstIntersected := False;
- // compute intersection with first plane
- t := (h1 - locRayStart.Y) * invRayVector1;
- if (t > 0) and Draw1 then
- begin
- ip.X := locRayStart.X + t * locRayVector.X;
- ip.Z := locRayStart.Z + t * locRayVector.Z;
- d2 := Sqr(ip.X) + Sqr(ip.Z);
- if (d2 <= tr2) and (d2 >= tir2) then
- begin
- // intersect with top plane
- FirstIntersected := true;
- if Assigned(intersectPoint) then
- intersectPoint^ := LocalToAbsolute(VectorMake(ip.X, h1, ip.Z, 1));
- if Assigned(intersectNormal) then
- intersectNormal^ := LocalToAbsolute(YHmgVector);
- Result := true;
- end;
- end;
- tPlaneMin := t;
- tPlaneMax := t;
- // compute intersection with second plane
- t := (h2 - locRayStart.Y) * invRayVector1;
- if (t > 0) and Draw2 then
- begin
- ip.X := locRayStart.X + t * locRayVector.X;
- ip.Z := locRayStart.Z + t * locRayVector.Z;
- d2 := Sqr(ip.X) + Sqr(ip.Z);
- if (t < tPlaneMin) or (not FirstIntersected) then
- begin
- if (d2 <= tr2) and (d2 >= tir2) then
- begin
- // intersect with top plane
- if Assigned(intersectPoint) then
- intersectPoint^ := LocalToAbsolute(VectorMake(ip.X, h2, ip.Z, 1));
- if Assigned(intersectNormal) then
- intersectNormal^ := LocalToAbsolute(VectorNegate(YHmgVector));
- Result := true;
- end;
- end;
- end;
- if t < tPlaneMin then
- begin
- tPlaneMin := t;
- end; // if
- if t > tPlaneMax then
- tPlaneMax := t;
- end;
- try
- SetLength(roots, 4);
- roots[0] := -1;
- roots[1] := -1;
- roots[2] := -1;
- roots[3] := -1; // By default, side is behind rayStart
- // Compute roots for outer cylinder
- if anOuterSides in Parts then
- begin
- // intersect against infinite cylinder, will be cut by tPlaneMine and tPlaneMax
- poly[0] := Sqr(locRayStart.X) + Sqr(locRayStart.Z) - Sqr(TopRadius);
- poly[1] := 2 * (locRayStart.X * locRayVector.X + locRayStart.Z *
- locRayVector.Z);
- poly[2] := Sqr(locRayVector.X) + Sqr(locRayVector.Z);
- tmpRoots := SolveQuadric(@poly);
- // Intersect coordinates on rayVector (rayStart=0)
- if ( High(tmpRoots) >= 0) and // Does root exist?
- ((tmpRoots[0] > tPlaneMin) and not FirstIntersected) and
- // In the annulus and not masked by first cap
- ((tmpRoots[0] < tPlaneMax)) { // In the annulus } then
- roots[0] := tmpRoots[0];
- if ( High(tmpRoots) >= 1) and
- ((tmpRoots[1] > tPlaneMin) and not FirstIntersected) and
- ((tmpRoots[1] < tPlaneMax)) then
- roots[1] := tmpRoots[1];
- end; // if
- // Compute roots for inner cylinder
- if anInnerSides in Parts then
- begin
- // intersect against infinite cylinder
- poly[0] := Sqr(locRayStart.X) +
- Sqr(locRayStart.Z) - Sqr(TopInnerRadius);
- poly[1] := 2 * (locRayStart.X * locRayVector.X +
- locRayStart.Z * locRayVector.Z);
- poly[2] := Sqr(locRayVector.X) + Sqr(locRayVector.Z);
- tmpRoots := SolveQuadric(@poly);
- if ( High(tmpRoots) >= 0) and
- ((tmpRoots[0] > tPlaneMin) and not FirstIntersected) and
- ((tmpRoots[0] < tPlaneMax)) then
- roots[2] := tmpRoots[0];
- if ( High(tmpRoots) >= 1) and
- ((tmpRoots[1] > tPlaneMin) and not FirstIntersected) and
- ((tmpRoots[1] < tPlaneMax)) then
- roots[3] := tmpRoots[1];
- end; // if
- { Find the first intersection point and compute its coordinates and normal }
- if MinPositiveCoef(roots, Root) then
- begin
- t := Root;
- if (t >= tPlaneMin) and (t < tPlaneMax) then
- begin
- if Assigned(intersectPoint) or Assigned(intersectNormal) then
- begin
- ip := VectorCombine(locRayStart, locRayVector, 1, t);
- if Assigned(intersectPoint) then
- intersectPoint^ := LocalToAbsolute(ip);
- if Assigned(intersectNormal) then
- begin
- ip.Y := 0;
- ip.W := 0;
- intersectNormal^ := LocalToAbsolute(ip);
- end;
- end;
- Result := true;
- end;
- end;
- finally
- roots := nil;
- tmpRoots := nil;
- end; // finally
- end;
- // ------------------
- // ------------------ TGLTorus ------------------
- // ------------------
- constructor TGLTorus.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FRings := 25;
- FSides := 15;
- FMinorRadius := 0.1;
- FMajorRadius := 0.4;
- FStartAngle := 0.0;
- FStopAngle := 360.0;
- FParts := [toSides, toStartDisk, toStopDisk];
- end;
- procedure TGLTorus.BuildList(var rci: TGLRenderContextInfo);
- procedure EmitVertex(ptr: PVertexRec; L1, L2: integer);
- begin
- XGL.TexCoord2fv(@ptr^.TexCoord);
- begin
- gl.Normal3fv(@ptr^.Normal);
- if L1 > -1 then
- gl.VertexAttrib3fv(L1, @ptr.Tangent);
- if L2 > -1 then
- gl.VertexAttrib3fv(L2, @ptr.Binormal);
- gl.Vertex3fv(@ptr^.Position);
- end;
- end;
- var
- i, j: integer;
- Theta, Phi, Theta1, cosPhi, sinPhi, dist: Single;
- cosTheta1, sinTheta1: Single;
- ringDelta, sideDelta: Single;
- ringDir: TAffineVector;
- iFact, jFact: Single;
- pVertex: PVertexRec;
- TanLoc, BinLoc: Integer;
- MeshSize: integer;
- MeshIndex: integer;
- Vertex: TVertexRec;
- begin
- if FMesh = nil then
- begin
- MeshSize := 0;
- MeshIndex := 0;
- if toStartDisk in FParts then
- MeshSize := MeshSize + 1;
- if toStopDisk in FParts then
- MeshSize := MeshSize + 1;
- if toSides in FParts then
- MeshSize := MeshSize + Integer(FRings) + 1;
- SetLength(FMesh, MeshSize);
- // handle texture generation
- ringDelta := ((FStopAngle - FStartAngle) / 360) * c2PI / FRings;
- sideDelta := c2PI / FSides;
- iFact := 1 / FRings;
- jFact := 1 / FSides;
- if toSides in FParts then
- begin
- Theta := DegToRadian(FStartAngle) - ringDelta;
- for i := FRings downto 0 do
- begin
- SetLength(FMesh[i], FSides + 1);
- Theta1 := Theta + ringDelta;
- SinCosine(Theta1, sinTheta1, cosTheta1);
- Phi := 0;
- for j := FSides downto 0 do
- begin
- Phi := Phi + sideDelta;
- SinCosine(Phi, sinPhi, cosPhi);
- dist := FMajorRadius + FMinorRadius * cosPhi;
- FMesh[i][j].Position := Vector3fMake(cosTheta1 * dist,
- -sinTheta1 * dist, FMinorRadius * sinPhi);
- ringDir := FMesh[i][j].Position;
- ringDir.Z := 0.0;
- NormalizeVector(ringDir);
- FMesh[i][j].Normal := Vector3fMake(cosTheta1 * cosPhi,
- -sinTheta1 * cosPhi, sinPhi);
- FMesh[i][j].Tangent := VectorCrossProduct(ZVector, ringDir);
- FMesh[i][j].Binormal := VectorCrossProduct(FMesh[i][j].Normal,
- FMesh[i][j].Tangent);
- FMesh[i][j].TexCoord := Vector2fMake(i * iFact, j * jFact);
- end;
- Theta := Theta1;
- end;
- MeshIndex := FRings + 1;
- end;
- if toStartDisk in FParts then
- begin
- SetLength(FMesh[MeshIndex], FSides + 1);
- Theta1 := DegToRadian(FStartAngle);
- SinCosine(Theta1, sinTheta1, cosTheta1);
- if toSides in FParts then
- begin
- for j := FSides downto 0 do
- begin
- FMesh[MeshIndex][j].Position := FMesh[MeshIndex - 1][j].Position;
- FMesh[MeshIndex][j].Normal := FMesh[MeshIndex - 1][j].Tangent;
- FMesh[MeshIndex][j].Tangent := FMesh[MeshIndex - 1][j].Position;
- FMesh[MeshIndex][j].Tangent.Z := 0;
- FMesh[MeshIndex][j].Binormal := ZVector;
- FMesh[MeshIndex][j].TexCoord := FMesh[MeshIndex - 1][j].TexCoord;
- FMesh[MeshIndex][j].TexCoord.X := 0;
- end;
- end
- else
- begin
- Phi := 0;
- for j := FSides downto 0 do
- begin
- Phi := Phi + sideDelta;
- SinCosine(Phi, sinPhi, cosPhi);
- dist := FMajorRadius + FMinorRadius * cosPhi;
- FMesh[MeshIndex][j].Position := Vector3fMake(cosTheta1 * dist,
- -sinTheta1 * dist, FMinorRadius * sinPhi);
- ringDir := FMesh[MeshIndex][j].Position;
- ringDir.Z := 0.0;
- NormalizeVector(ringDir);
- FMesh[MeshIndex][j].Normal := VectorCrossProduct(ZVector, ringDir);
- FMesh[MeshIndex][j].Tangent := ringDir;
- FMesh[MeshIndex][j].Binormal := ZVector;
- FMesh[MeshIndex][j].TexCoord := Vector2fMake(0, j * jFact);
- end;
- end;
- Vertex.Position := Vector3fMake(cosTheta1 * FMajorRadius,
- -sinTheta1 * FMajorRadius, 0);
- Vertex.Normal := FMesh[MeshIndex][0].Normal;
- Vertex.Tangent := FMesh[MeshIndex][0].Tangent;
- Vertex.Binormal := FMesh[MeshIndex][0].Binormal;
- Vertex.TexCoord := Vector2fMake(1, 1);
- MeshIndex := MeshIndex + 1;
- end;
- if toStopDisk in FParts then
- begin
- SetLength(FMesh[MeshIndex], FSides + 1);
- Theta1 := DegToRadian(FStopAngle);
- SinCosine(Theta1, sinTheta1, cosTheta1);
- if toSides in FParts then
- begin
- for j := FSides downto 0 do
- begin
- FMesh[MeshIndex][j].Position := FMesh[0][j].Position;
- FMesh[MeshIndex][j].Normal := VectorNegate(FMesh[0][j].Tangent);
- FMesh[MeshIndex][j].Tangent := FMesh[0][j].Position;
- FMesh[MeshIndex][j].Tangent.Z := 0;
- FMesh[MeshIndex][j].Binormal := VectorNegate(ZVector);
- FMesh[MeshIndex][j].TexCoord := FMesh[0][j].TexCoord;
- FMesh[MeshIndex][j].TexCoord.X := 1;
- end;
- end
- else
- begin
- Phi := 0;
- for j := FSides downto 0 do
- begin
- Phi := Phi + sideDelta;
- SinCosine(Phi, sinPhi, cosPhi);
- dist := FMajorRadius + FMinorRadius * cosPhi;
- FMesh[MeshIndex][j].Position := Vector3fMake(cosTheta1 * dist,
- -sinTheta1 * dist, FMinorRadius * sinPhi);
- ringDir := FMesh[MeshIndex][j].Position;
- ringDir.Z := 0.0;
- NormalizeVector(ringDir);
- FMesh[MeshIndex][j].Normal := VectorCrossProduct(ringDir, ZVector);
- FMesh[MeshIndex][j].Tangent := ringDir;
- FMesh[MeshIndex][j].Binormal := VectorNegate(ZVector);
- FMesh[MeshIndex][j].TexCoord := Vector2fMake(1, j * jFact);
- end;
- end;
- Vertex.Position := Vector3fMake(cosTheta1 * FMajorRadius,
- -sinTheta1 * FMajorRadius, 0);
- Vertex.Normal := FMesh[MeshIndex][0].Normal;
- Vertex.Tangent := FMesh[MeshIndex][0].Tangent;
- Vertex.Binormal := FMesh[MeshIndex][0].Binormal;
- Vertex.TexCoord := Vector2fMake(0, 0);
- end;
- end;
- begin
- if GL.ARB_shader_objects and (rci.GLStates.CurrentProgram > 0) then
- begin
- TanLoc := gl.GetAttribLocation(rci.GLStates.CurrentProgram,
- PAnsiChar(TangentAttributeName));
- BinLoc := gl.GetAttribLocation(rci.GLStates.CurrentProgram,
- PAnsiChar(BinormalAttributeName));
- end
- else
- begin
- TanLoc := -1;
- BinLoc := TanLoc;
- end;
- MeshIndex := 0;
- if toSides in FParts then
- begin
- gl.Begin_(GL_TRIANGLES);
- for i := FRings - 1 downto 0 do
- for j := FSides - 1 downto 0 do
- begin
- pVertex := @FMesh[i][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[i][j + 1];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[i + 1][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[i + 1][j + 1];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[i + 1][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[i][j + 1];
- EmitVertex(pVertex, TanLoc, BinLoc);
- end;
- gl.End_;
- MeshIndex := FRings + 1;
- end;
- if toStartDisk in FParts then
- begin
- gl.Begin_(GL_TRIANGLE_FAN);
- pVertex := @Vertex;
- EmitVertex(pVertex, TanLoc, BinLoc);
- for j := 0 to FSides do
- begin
- pVertex := @FMesh[MeshIndex][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- end;
- gl.End_;
- MeshIndex := MeshIndex + 1;
- end;
- if toStopDisk in FParts then
- begin
- gl.Begin_(GL_TRIANGLE_FAN);
- pVertex := @Vertex;
- EmitVertex(pVertex, TanLoc, BinLoc);
- for j := FSides downto 0 do
- begin
- pVertex := @FMesh[MeshIndex][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- end;
- gl.End_;
- end;
- end;
- end;
- procedure TGLTorus.SetMajorRadius(const aValue: Single);
- begin
- if FMajorRadius <> aValue then
- begin
- FMajorRadius := aValue;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLTorus.SetMinorRadius(const aValue: Single);
- begin
- if FMinorRadius <> aValue then
- begin
- FMinorRadius := aValue;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLTorus.SetRings(aValue: Cardinal);
- begin
- if FRings <> aValue then
- begin
- FRings := aValue;
- if FRings < 2 then
- FRings := 2;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLTorus.SetSides(aValue: Cardinal);
- begin
- if FSides <> aValue then
- begin
- FSides := aValue;
- if FSides < 3 then
- FSides := 3;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLTorus.SetStartAngle(const aValue: Single);
- begin
- if FStartAngle <> aValue then
- begin
- FStartAngle := aValue;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLTorus.SetStopAngle(const aValue: Single);
- begin
- if FStopAngle <> aValue then
- begin
- FStopAngle := aValue;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLTorus.SetParts(aValue: TTorusParts);
- begin
- if aValue <> FParts then
- begin
- FParts := aValue;
- StructureChanged;
- end;
- end;
- function TGLTorus.AxisAlignedDimensionsUnscaled: TVector;
- var
- r, r1: Single;
- begin
- r := Abs(FMajorRadius);
- r1 := Abs(FMinorRadius);
- Result := VectorMake(r + r1, r + r1, r1); // Danb
- end;
- function TGLTorus.RayCastIntersect(const rayStart, rayVector: TVector;
- intersectPoint: PVector = nil; intersectNormal: PVector = nil): Boolean;
- var
- i: integer;
- fRo2, fRi2, fDE, fVal, r, nearest: Double;
- polynom: array [0 .. 4] of Double;
- polyRoots: TDoubleArray;
- localStart, localVector: TVector;
- vi, vc: TVector;
- begin
- // compute coefficients of quartic polynomial
- fRo2 := Sqr(MajorRadius);
- fRi2 := Sqr(MinorRadius);
- localStart := AbsoluteToLocal(rayStart);
- localVector := AbsoluteToLocal(rayVector);
- NormalizeVector(localVector);
- fDE := VectorDotProduct(localStart, localVector);
- fVal := VectorNorm(localStart) - (fRo2 + fRi2);
- polynom[0] := Sqr(fVal) - 4.0 * fRo2 * (fRi2 - Sqr(localStart.Z));
- polynom[1] := 4.0 * fDE * fVal + 8.0 * fRo2 * localVector.Z * localStart.Z;
- polynom[2] := 2.0 * fVal + 4.0 * Sqr(fDE) + 4.0 * fRo2 * Sqr(localVector.Z);
- polynom[3] := 4.0 * fDE;
- polynom[4] := 1;
- // solve the quartic
- polyRoots := SolveQuartic(@polynom[0]);
- // search for closest point
- Result := (Length(polyRoots) > 0);
- if Result then
- begin
- nearest := 1E20;
- for i := 0 to High(polyRoots) do
- begin
- r := polyRoots[i];
- if (r > 0) and (r < nearest) then
- begin
- nearest := r;
- Result := true;
- end;
- end;
- vi := VectorCombine(localStart, localVector, 1, nearest);
- if Assigned(intersectPoint) then
- SetVector(intersectPoint^, LocalToAbsolute(vi));
- if Assigned(intersectNormal) then
- begin
- // project vi on local torus plane
- vc.X := vi.X;
- vc.Y := vi.Y;
- vc.Z := 0;
- // project vc on MajorRadius circle
- ScaleVector(vc, MajorRadius / (VectorLength(vc) + 0.000001));
- // calculate circle to intersect vector (gives normal);
- SubtractVector(vi, vc);
- // return to absolute coordinates and normalize
- vi.W := 0;
- SetVector(intersectNormal^, LocalToAbsolute(vi));
- end;
- end;
- end;
- // ------------------
- // ------------------ TGLArrowLine ------------------
- // ------------------
- constructor TGLArrowLine.Create(AOwner: TComponent);
- begin
- inherited;
- FTopRadius := 0.1;
- BottomRadius := 0.1;
- fTopArrowHeadRadius := 0.2;
- fTopArrowHeadHeight := 0.5;
- fBottomArrowHeadRadius := 0.2;
- fBottomArrowHeadHeight := 0.5;
- FHeadStackingStyle := ahssStacked;
- { by default there is not much point having the top of the line (cylinder)
- showing as it is coincidental with the Toparrowhead bottom.
- Note I've defaulted to "vector" type arrows (arrow head on top only }
- FParts := [alLine, alTopArrow];
- end;
- procedure TGLArrowLine.SetTopRadius(const aValue: Single);
- begin
- if aValue <> FTopRadius then
- begin
- FTopRadius := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLArrowLine.SetTopArrowHeadHeight(const aValue: Single);
- begin
- if aValue <> fTopArrowHeadHeight then
- begin
- fTopArrowHeadHeight := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLArrowLine.SetTopArrowHeadRadius(const aValue: Single);
- begin
- if aValue <> fTopArrowHeadRadius then
- begin
- fTopArrowHeadRadius := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLArrowLine.SetBottomArrowHeadHeight(const aValue: Single);
- begin
- if aValue <> fBottomArrowHeadHeight then
- begin
- fBottomArrowHeadHeight := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLArrowLine.SetBottomArrowHeadRadius(const aValue: Single);
- begin
- if aValue <> fBottomArrowHeadRadius then
- begin
- fBottomArrowHeadRadius := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLArrowLine.SetParts(aValue: TArrowLineParts);
- begin
- if aValue <> FParts then
- begin
- FParts := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLArrowLine.SetHeadStackingStyle(const val: TGLArrowHeadStyle);
- begin
- if val <> FHeadStackingStyle then
- begin
- FHeadStackingStyle := val;
- StructureChanged;
- end;
- end;
- procedure TGLArrowLine.BuildList(var rci: TGLRenderContextInfo);
- var
- quadric: PGLUquadricObj;
- cylHeight, cylOffset, headInfluence: Single;
- begin
- case HeadStackingStyle of
- ahssCentered:
- headInfluence := 0.5;
- ahssIncluded:
- headInfluence := 1;
- else // ahssStacked
- headInfluence := 0;
- end;
- cylHeight := Height;
- cylOffset := -FHeight * 0.5;
- // create a new quadric
- quadric := gluNewQuadric;
- SetupQuadricParams(quadric);
- // does the top arrow part - the cone
- if alTopArrow in Parts then
- begin
- cylHeight := cylHeight - TopArrowHeadHeight * headInfluence;
- gl.PushMatrix;
- gl.Translatef(0, 0, Height * 0.5 - TopArrowHeadHeight * headInfluence);
- gluCylinder(quadric, fTopArrowHeadRadius, 0, fTopArrowHeadHeight,
- Slices, Stacks);
- // top of a disk is defined as outside
- SetInvertedQuadricOrientation(quadric);
- if alLine in Parts then
- gluDisk(quadric, FTopRadius, fTopArrowHeadRadius, Slices, FLoops)
- else
- gluDisk(quadric, 0, fTopArrowHeadRadius, Slices, FLoops);
- gl.PopMatrix;
- end;
- // does the bottom arrow part - another cone
- if alBottomArrow in Parts then
- begin
- cylHeight := cylHeight - BottomArrowHeadHeight * headInfluence;
- cylOffset := cylOffset + BottomArrowHeadHeight * headInfluence;
- gl.PushMatrix;
- // make the bottom arrow point in the other direction
- gl.Rotatef(180, 1, 0, 0);
- gl.Translatef(0, 0, Height * 0.5 - BottomArrowHeadHeight * headInfluence);
- SetNormalQuadricOrientation(quadric);
- gluCylinder(quadric, fBottomArrowHeadRadius, 0, fBottomArrowHeadHeight,
- Slices, Stacks);
- // top of a disk is defined as outside
- SetInvertedQuadricOrientation(quadric);
- if alLine in Parts then
- gluDisk(quadric, FBottomRadius, fBottomArrowHeadRadius, Slices, FLoops)
- else
- gluDisk(quadric, 0, fBottomArrowHeadRadius, Slices, FLoops);
- gl.PopMatrix;
- end;
- // does the cylinder that makes the line
- if (cylHeight > 0) and (alLine in Parts) then
- begin
- gl.PushMatrix;
- gl.Translatef(0, 0, cylOffset);
- SetNormalQuadricOrientation(quadric);
- gluCylinder(quadric, FBottomRadius, FTopRadius, cylHeight, FSlices,
- FStacks);
- if not(alTopArrow in Parts) then
- begin
- gl.PushMatrix;
- gl.Translatef(0, 0, cylHeight);
- gluDisk(quadric, 0, FTopRadius, FSlices, FLoops);
- gl.PopMatrix;
- end;
- if not(alBottomArrow in Parts) then
- begin
- // swap quadric orientation because top of a disk is defined as outside
- SetInvertedQuadricOrientation(quadric);
- gluDisk(quadric, 0, FBottomRadius, FSlices, FLoops);
- end;
- gl.PopMatrix;
- end;
- gluDeleteQuadric(quadric);
- end;
- procedure TGLArrowLine.Assign(Source: TPersistent);
- begin
- if Assigned(Source) and (Source is TGLArrowLine) then
- begin
- FParts := TGLArrowLine(Source).FParts;
- FTopRadius := TGLArrowLine(Source).FTopRadius;
- fTopArrowHeadHeight := TGLArrowLine(Source).fTopArrowHeadHeight;
- fTopArrowHeadRadius := TGLArrowLine(Source).fTopArrowHeadRadius;
- fBottomArrowHeadHeight := TGLArrowLine(Source).fBottomArrowHeadHeight;
- fBottomArrowHeadRadius := TGLArrowLine(Source).fBottomArrowHeadRadius;
- FHeadStackingStyle := TGLArrowLine(Source).FHeadStackingStyle;
- end;
- inherited Assign(Source);
- end;
- // ------------------
- // ------------------ TGLArrowArc ------------------
- // ------------------
- constructor TGLArrowArc.Create(AOwner: TComponent);
- begin
- inherited;
- FStacks := 16;
- fArcRadius := 0.5;
- FStartAngle := 0;
- FStopAngle := 360;
- FTopRadius := 0.1;
- BottomRadius := 0.1;
- fTopArrowHeadRadius := 0.2;
- fTopArrowHeadHeight := 0.5;
- fBottomArrowHeadRadius := 0.2;
- fBottomArrowHeadHeight := 0.5;
- FHeadStackingStyle := ahssStacked;
- FParts := [aaArc, aaTopArrow];
- end;
- procedure TGLArrowArc.SetArcRadius(const aValue: Single);
- begin
- if fArcRadius <> aValue then
- begin
- fArcRadius := aValue;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLArrowArc.SetStartAngle(const aValue: Single);
- begin
- if FStartAngle <> aValue then
- begin
- FStartAngle := aValue;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLArrowArc.SetStopAngle(const aValue: Single);
- begin
- if FStopAngle <> aValue then
- begin
- FStopAngle := aValue;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLArrowArc.SetTopRadius(const aValue: Single);
- begin
- if aValue <> FTopRadius then
- begin
- FTopRadius := aValue;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLArrowArc.SetTopArrowHeadHeight(const aValue: Single);
- begin
- if aValue <> fTopArrowHeadHeight then
- begin
- fTopArrowHeadHeight := aValue;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLArrowArc.SetTopArrowHeadRadius(const aValue: Single);
- begin
- if aValue <> fTopArrowHeadRadius then
- begin
- fTopArrowHeadRadius := aValue;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLArrowArc.SetBottomArrowHeadHeight(const aValue: Single);
- begin
- if aValue <> fBottomArrowHeadHeight then
- begin
- fBottomArrowHeadHeight := aValue;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLArrowArc.SetBottomArrowHeadRadius(const aValue: Single);
- begin
- if aValue <> fBottomArrowHeadRadius then
- begin
- fBottomArrowHeadRadius := aValue;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLArrowArc.SetParts(aValue: TGLArrowArcPart);
- begin
- if aValue <> FParts then
- begin
- FParts := aValue;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLArrowArc.SetHeadStackingStyle(const val: TGLArrowHeadStyle);
- begin
- if val <> FHeadStackingStyle then
- begin
- FHeadStackingStyle := val;
- FMesh := nil;
- StructureChanged;
- end;
- end;
- procedure TGLArrowArc.BuildList(var rci: TGLRenderContextInfo);
- procedure EmitVertex(ptr: PVertexRec; L1, L2: integer);
- begin
- XGL.TexCoord2fv(@ptr^.TexCoord);
- gl.Normal3fv(@ptr^.Normal);
- if L1 > -1 then
- gl.VertexAttrib3fv(L1, @ptr.Tangent);
- if L2 > -1 then
- gl.VertexAttrib3fv(L2, @ptr.Binormal);
- gl.Vertex3fv(@ptr^.Position);
- end;
- var
- i, j: integer;
- Theta, Phi, Theta1, cosPhi, sinPhi, dist: Single;
- cosTheta1, sinTheta1: Single;
- ringDelta, sideDelta: Single;
- ringDir: TAffineVector;
- iFact, jFact: Single;
- pVertex: PVertexRec;
- TanLoc, BinLoc: Integer;
- MeshSize: integer;
- MeshIndex: integer;
- ConeCenter: TVertexRec;
- StartOffset, StopOffset: Single;
- begin
- if FMesh = nil then
- begin
- MeshIndex := 0;
- MeshSize := 0;
- // Check Parts
- if aaArc in FParts then
- MeshSize := MeshSize + FStacks + 1;
- if aaTopArrow in FParts then
- MeshSize := MeshSize + 3
- else
- MeshSize := MeshSize + 1;
- if aaBottomArrow in FParts then
- MeshSize := MeshSize + 3
- else
- MeshSize := MeshSize + 1;
- // Allocate Mesh
- SetLength(FMesh, MeshSize);
- case FHeadStackingStyle of
- ahssStacked:
- begin
- StartOffset := 0;
- StopOffset := 0;
- end;
- ahssCentered:
- begin
- if aaBottomArrow in Parts then
- StartOffset :=
- RadToDeg(ArcTan(0.5 * fBottomArrowHeadHeight / fArcRadius))
- else
- StartOffset :=0;
- if aaTopArrow in Parts then
- StopOffset :=
- RadToDeg(ArcTan(0.5 * fTopArrowHeadHeight / fArcRadius))
- else
- StopOffset :=0;
- end ;
- ahssIncluded:
- begin
- if aaBottomArrow in Parts then
- StartOffset := RadToDeg(ArcTan(fBottomArrowHeadHeight / fArcRadius))
- else
- StartOffset :=0;
- if aaTopArrow in Parts then
- StopOffset := RadToDeg(ArcTan(fTopArrowHeadHeight / fArcRadius))
- else
- StopOffset :=0;
- end ;
- else
- StartOffset := 0;
- StopOffset := 0;
- end;
- // handle texture generation
- ringDelta := (((FStopAngle - StopOffset) - (FStartAngle + StartOffset)) /
- 360) * c2PI / FStacks;
- sideDelta := c2PI / FSlices;
- iFact := 1 / FStacks;
- jFact := 1 / FSlices;
- if aaArc in FParts then
- begin
- Theta := DegToRadian(FStartAngle + StartOffset) - ringDelta;
- for i := FStacks downto 0 do
- begin
- SetLength(FMesh[i], FSlices + 1);
- Theta1 := Theta + ringDelta;
- SinCosine(Theta1, sinTheta1, cosTheta1);
- Phi := 0;
- for j := FSlices downto 0 do
- begin
- Phi := Phi + sideDelta;
- SinCosine(Phi, sinPhi, cosPhi);
- dist := fArcRadius + Lerp(FTopRadius, FBottomRadius, i * iFact) * cosPhi;
- FMesh[i][j].Position := Vector3fMake(cosTheta1 * dist,
- -sinTheta1 * dist, Lerp(FTopRadius, FBottomRadius, i * iFact) * sinPhi);
- ringDir := FMesh[i][j].Position;
- ringDir.Z := 0.0;
- NormalizeVector(ringDir);
- FMesh[i][j].Normal := Vector3fMake(cosTheta1 * cosPhi,
- -sinTheta1 * cosPhi, sinPhi);
- FMesh[i][j].Tangent := VectorCrossProduct(ZVector, ringDir);
- FMesh[i][j].Binormal := VectorCrossProduct(FMesh[i][j].Normal,
- FMesh[i][j].Tangent);
- FMesh[i][j].TexCoord := Vector2fMake(i * iFact, j * jFact);
- end;
- Theta := Theta1;
- end;
- MeshIndex := FStacks + 1;
- begin
- if GL.ARB_shader_objects and (rci.GLStates.CurrentProgram > 0) then
- begin
- TanLoc := gl.GetAttribLocation(rci.GLStates.CurrentProgram,
- PAnsiChar(TangentAttributeName));
- BinLoc := gl.GetAttribLocation(rci.GLStates.CurrentProgram,
- PAnsiChar(BinormalAttributeName));
- end
- else
- begin
- TanLoc := -1;
- BinLoc := TanLoc;
- end;
- gl.Begin_(GL_TRIANGLES);
- for i := FStacks - 1 downto 0 do
- for j := FSlices - 1 downto 0 do
- begin
- pVertex := @FMesh[i][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[i][j + 1];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[i + 1][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[i + 1][j + 1];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[i + 1][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[i][j + 1];
- EmitVertex(pVertex, TanLoc, BinLoc);
- end;
- gl.End_;
- end;
- end;
- // Build Arrow or start cap
- if aaBottomArrow in FParts then
- begin
- SetLength(FMesh[MeshIndex], FSlices + 1);
- SetLength(FMesh[MeshIndex + 1], FSlices + 1);
- SetLength(FMesh[MeshIndex + 2], FSlices + 1);
- Theta1 := DegToRadian(FStartAngle + StartOffset);
- SinCosine(Theta1, sinTheta1, cosTheta1);
- ConeCenter.Position := Vector3fMake(cosTheta1 * fArcRadius,
- -sinTheta1 * fArcRadius, 0);
- Phi := 0;
- for j := FSlices downto 0 do
- begin
- Phi := Phi + sideDelta;
- SinCosine(Phi, sinPhi, cosPhi);
- dist := fArcRadius + fBottomArrowHeadRadius * cosPhi;
- // Cap
- FMesh[MeshIndex][J].Position := Vector3fMake(cosTheta1 * dist,
- -sinTheta1 * dist, fBottomArrowHeadRadius * sinPhi);
- ringDir := FMesh[MeshIndex][j].Position;
- ringDir.Z := 0.0;
- NormalizeVector(ringDir);
- FMesh[MeshIndex][j].Normal := VectorCrossProduct(ringDir, ZVector);
- FMesh[MeshIndex][j].Tangent := ringDir;
- FMesh[MeshIndex][j].Binormal := ZVector;
- FMesh[MeshIndex][j].TexCoord := Vector2fMake(1, j * jFact);
- // Cone
- FMesh[MeshIndex+1][j].Position := Vector3fMake(cosTheta1 * dist,
- -sinTheta1 * dist, fBottomArrowHeadRadius * sinPhi);
- FMesh[MeshIndex+2][j].Position := VectorAdd(ConeCenter.Position,
- Vector3fMake(sinTheta1 * fBottomArrowHeadHeight,
- cosTheta1 * fBottomArrowHeadHeight, 0));
- FMesh[MeshIndex + 1][j].Tangent :=
- VectorNormalize(VectorSubtract(FMesh[MeshIndex + 1][j].Position,
- FMesh[MeshIndex + 2][j].Position));
- FMesh[MeshIndex + 2][j].Tangent := FMesh[MeshIndex + 1][j].Tangent;
- FMesh[MeshIndex + 1][j].Binormal := Vector3fMake(cosTheta1 * -sinPhi,
- sinTheta1 * sinPhi, cosPhi);
- FMesh[MeshIndex + 2][j].Binormal := FMesh[MeshIndex + 1][j].Binormal;
- FMesh[MeshIndex + 1][j].Normal :=
- VectorCrossProduct(FMesh[MeshIndex + 1][j].Binormal,
- FMesh[MeshIndex + 1][j].Tangent);
- FMesh[MeshIndex + 2][j].Normal := FMesh[MeshIndex + 1][j].Normal;
- FMesh[MeshIndex + 1][j].TexCoord := Vector2fMake(0, j * jFact);
- FMesh[MeshIndex + 2][j].TexCoord := Vector2fMake(1, j * jFact);
- end;
- ConeCenter.Normal := FMesh[MeshIndex][0].Normal;
- ConeCenter.Tangent := FMesh[MeshIndex][0].Tangent;
- ConeCenter.Binormal := FMesh[MeshIndex][0].Binormal;
- ConeCenter.TexCoord := Vector2fMake(0, 0);
- begin
- if GL.ARB_shader_objects and (rci.GLStates.CurrentProgram > 0) then
- begin
- TanLoc := gl.GetAttribLocation(rci.GLStates.CurrentProgram,
- PAnsiChar(TangentAttributeName));
- BinLoc := gl.GetAttribLocation(rci.GLStates.CurrentProgram,
- PAnsiChar(BinormalAttributeName));
- end
- else
- begin
- TanLoc := -1;
- BinLoc := TanLoc;
- end;
- gl.Begin_(GL_TRIANGLE_FAN);
- pVertex := @ConeCenter;
- EmitVertex(pVertex, TanLoc, BinLoc);
- for j := FSlices downto 0 do
- begin
- pVertex := @FMesh[MeshIndex][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- end;
- gl.End_;
- gl.Begin_(GL_TRIANGLES);
- for j := FSlices - 1 downto 0 do
- begin
- pVertex := @FMesh[MeshIndex + 1][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[MeshIndex + 1][j + 1];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[MeshIndex + 2][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[MeshIndex + 2][j + 1];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[MeshIndex + 2][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[MeshIndex + 1][j + 1];
- EmitVertex(pVertex, TanLoc, BinLoc);
- end;
- gl.End_;
- end;
- MeshIndex := MeshIndex + 3;
- end
- else
- begin
- SetLength(FMesh[MeshIndex], FSlices + 1);
- Theta1 := DegToRadian(FStartAngle);
- SinCosine(Theta1, sinTheta1, cosTheta1);
- Phi := 0;
- for j := FSlices downto 0 do
- begin
- Phi := Phi + sideDelta;
- SinCosine(Phi, sinPhi, cosPhi);
- dist := fArcRadius + fBottomRadius * cosPhi;
- FMesh[MeshIndex][j].Position := Vector3fMake(cosTheta1 * dist,
- -sinTheta1 * dist, FBottomRadius * sinPhi);
- ringDir := FMesh[MeshIndex][j].Position;
- ringDir.Z := 0.0;
- NormalizeVector(ringDir);
- FMesh[MeshIndex][j].Normal := VectorCrossProduct(ZVector, ringDir);
- FMesh[MeshIndex][j].Tangent := ringDir;
- FMesh[MeshIndex][j].Binormal := ZVector;
- FMesh[MeshIndex][j].TexCoord := Vector2fMake(0, j * jFact);
- end;
- ConeCenter.Position := Vector3fMake(cosTheta1 * fArcRadius,
- -sinTheta1 * fArcRadius, 0);
- ConeCenter.Normal := FMesh[MeshIndex][0].Normal;
- ConeCenter.Tangent := FMesh[MeshIndex][0].Tangent;
- ConeCenter.Binormal := FMesh[MeshIndex][0].Binormal;
- ConeCenter.TexCoord := Vector2fMake(1, 1);
- begin
- if GL.ARB_shader_objects and (rci.GLStates.CurrentProgram > 0) then
- begin
- TanLoc := gl.GetAttribLocation(rci.GLStates.CurrentProgram,
- PAnsiChar(TangentAttributeName));
- BinLoc := gl.GetAttribLocation(rci.GLStates.CurrentProgram,
- PAnsiChar(BinormalAttributeName));
- end
- else
- begin
- TanLoc := -1;
- BinLoc := TanLoc;
- end;
- gl.Begin_(GL_TRIANGLE_FAN);
- pVertex := @ConeCenter;
- EmitVertex(pVertex, TanLoc, BinLoc);
- for j := 0 to FSlices do
- begin
- pVertex := @FMesh[MeshIndex][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- end;
- gl.End_;
- end;
- MeshIndex := MeshIndex + 1;
- end;
- if aaTopArrow in FParts then
- begin
- SetLength(FMesh[MeshIndex], FSlices + 1);
- SetLength(FMesh[MeshIndex + 1], FSlices + 1);
- SetLength(FMesh[MeshIndex + 2], FSlices + 1);
- Theta1 := DegToRadian(FStopAngle - StopOffset);
- SinCosine(Theta1, sinTheta1, cosTheta1);
- ConeCenter.Position := Vector3fMake(cosTheta1 * fArcRadius,
- -sinTheta1 * fArcRadius, 0);
- Phi := 0;
- for j := FSlices downto 0 do
- begin
- Phi := Phi + sideDelta;
- SinCosine(Phi, sinPhi, cosPhi);
- dist := fArcRadius + fTopArrowHeadRadius * cosPhi;
- // Cap
- FMesh[MeshIndex][j].Position := Vector3fMake(cosTheta1 * dist,
- -sinTheta1 * dist, fTopArrowHeadRadius * sinPhi);
- ringDir := FMesh[MeshIndex][j].Position;
- ringDir.Z := 0.0;
- NormalizeVector(ringDir);
- FMesh[MeshIndex][j].Normal := VectorCrossProduct(ZVector, ringDir);
- FMesh[MeshIndex][j].Tangent := ringDir;
- FMesh[MeshIndex][j].Binormal := ZVector;
- FMesh[MeshIndex][j].TexCoord := Vector2fMake(0, j * jFact);
- // Cone
- FMesh[MeshIndex + 1][j].Position := Vector3fMake(cosTheta1 * dist,
- -sinTheta1 * dist, fTopArrowHeadRadius * sinPhi);
- FMesh[MeshIndex + 2][j].Position := VectorSubtract(ConeCenter.Position,
- Vector3fMake(sinTheta1 * fTopArrowHeadHeight,
- cosTheta1 * fTopArrowHeadHeight, 0));
- FMesh[MeshIndex + 1][j].Tangent :=
- VectorNormalize(VectorSubtract(FMesh[MeshIndex + 2][j].Position,
- FMesh[MeshIndex + 1][j].Position));
- FMesh[MeshIndex + 2][j].Tangent := FMesh[MeshIndex + 1][j].Tangent;
- FMesh[MeshIndex + 1][j].Binormal := Vector3fMake(cosTheta1 * -sinPhi,
- sinTheta1 * sinPhi, cosPhi);
- FMesh[MeshIndex + 2][j].Binormal := FMesh[MeshIndex + 1][j].Binormal;
- FMesh[MeshIndex + 1][j].Normal :=
- VectorCrossProduct(FMesh[MeshIndex + 1][j].Binormal,
- FMesh[MeshIndex + 1][j].Tangent);
- FMesh[MeshIndex + 2][j].Normal := FMesh[MeshIndex + 1][j].Normal;
- FMesh[MeshIndex + 1][j].TexCoord := Vector2fMake(1, j * jFact);
- FMesh[MeshIndex + 2][j].TexCoord := Vector2fMake(0, j * jFact);
- end;
- ConeCenter.Normal := FMesh[MeshIndex][0].Normal;
- ConeCenter.Tangent := FMesh[MeshIndex][0].Tangent;
- ConeCenter.Binormal := FMesh[MeshIndex][0].Binormal;
- ConeCenter.TexCoord := Vector2fMake(1, 1);
- begin
- if GL.ARB_shader_objects and (rci.GLStates.CurrentProgram > 0) then
- begin
- TanLoc := gl.GetAttribLocation(rci.GLStates.CurrentProgram,
- PAnsiChar(TangentAttributeName));
- BinLoc := gl.GetAttribLocation(rci.GLStates.CurrentProgram,
- PAnsiChar(BinormalAttributeName));
- end
- else
- begin
- TanLoc := -1;
- BinLoc := TanLoc;
- end;
- gl.Begin_(GL_TRIANGLE_FAN);
- pVertex := @ConeCenter;
- EmitVertex(pVertex, TanLoc, BinLoc);
- for j := 0 to FSlices do
- begin
- pVertex := @FMesh[MeshIndex][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- end;
- gl.End_;
- gl.Begin_(GL_TRIANGLES);
- for j := FSlices - 1 downto 0 do
- begin
- pVertex := @FMesh[MeshIndex + 2][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[MeshIndex + 2][j + 1];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[MeshIndex + 1][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[MeshIndex + 1][j + 1];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[MeshIndex + 1][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- pVertex := @FMesh[MeshIndex + 2][j + 1];
- EmitVertex(pVertex, TanLoc, BinLoc);
- end;
- gl.End_;
- end;
- end
- else
- begin
- SetLength(FMesh[MeshIndex], FSlices + 1);
- Theta1 := DegToRadian(FStopAngle);
- SinCosine(Theta1, sinTheta1, cosTheta1);
- Phi := 0;
- for j := FSlices downto 0 do
- begin
- Phi := Phi + sideDelta;
- SinCosine(Phi, sinPhi, cosPhi);
- dist := fArcRadius + fTopRadius * cosPhi;
- FMesh[MeshIndex][j].Position := Vector3fMake(cosTheta1 * dist,
- -sinTheta1 * dist, fTopRadius * sinPhi);
- ringDir := FMesh[MeshIndex][j].Position;
- ringDir.Z := 0.0;
- NormalizeVector(ringDir);
- FMesh[MeshIndex][j].Normal := VectorCrossProduct(ringDir, ZVector);
- FMesh[MeshIndex][j].Tangent := ringDir;
- FMesh[MeshIndex][j].Binormal := VectorNegate(ZVector);
- FMesh[MeshIndex][j].TexCoord := Vector2fMake(1, j * jFact);
- end;
- ConeCenter.Position := Vector3fMake(cosTheta1 * fArcRadius,
- -sinTheta1 * fArcRadius, 0);
- ConeCenter.Normal := FMesh[MeshIndex][0].Normal;
- ConeCenter.Tangent := FMesh[MeshIndex][0].Tangent;
- ConeCenter.Binormal := FMesh[MeshIndex][0].Binormal;
- ConeCenter.TexCoord := Vector2fMake(0, 0);
- begin
- if GL.ARB_shader_objects and (rci.GLStates.CurrentProgram > 0) then
- begin
- TanLoc := gl.GetAttribLocation(rci.GLStates.CurrentProgram,
- PAnsiChar(TangentAttributeName));
- BinLoc := gl.GetAttribLocation(rci.GLStates.CurrentProgram,
- PAnsiChar(BinormalAttributeName));
- end
- else
- begin
- TanLoc := -1;
- BinLoc := TanLoc;
- end;
- gl.Begin_(GL_TRIANGLE_FAN);
- pVertex := @ConeCenter;
- EmitVertex(pVertex, TanLoc, BinLoc);
- for j := FSlices downto 0 do
- begin
- pVertex := @FMesh[MeshIndex][j];
- EmitVertex(pVertex, TanLoc, BinLoc);
- end;
- gl.End_;
- end;
- end;
- end;
- end;
- procedure TGLArrowArc.Assign(Source: TPersistent);
- begin
- if Assigned(Source) and (Source is TGLArrowLine) then
- begin
- FStartAngle := TGLArrowArc(Source).FStartAngle;
- FStopAngle := TGLArrowArc(Source).FStopAngle;
- fArcRadius := TGLArrowArc(Source).fArcRadius;
- FParts := TGLArrowArc(Source).FParts;
- FTopRadius := TGLArrowArc(Source).FTopRadius;
- fTopArrowHeadHeight := TGLArrowArc(Source).fTopArrowHeadHeight;
- fTopArrowHeadRadius := TGLArrowArc(Source).fTopArrowHeadRadius;
- fBottomArrowHeadHeight := TGLArrowArc(Source).fBottomArrowHeadHeight;
- fBottomArrowHeadRadius := TGLArrowArc(Source).fBottomArrowHeadRadius;
- FHeadStackingStyle := TGLArrowArc(Source).FHeadStackingStyle;
- end;
- inherited Assign(Source);
- end;
- // ------------------
- // ------------------ TGLFrustrum ------------------
- // ------------------
- constructor TGLFrustrum.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FApexHeight := 1;
- FBaseWidth := 1;
- FBaseDepth := 1;
- FHeight := 0.5;
- FParts := cAllFrustrumParts;
- FNormalDirection := ndOutside;
- end;
- procedure TGLFrustrum.BuildList(var rci: TGLRenderContextInfo);
- var
- HBW, HBD: Single; // half of width, half of depth at base
- HTW, HTD: Single; // half of width, half of depth at top of frustrum
- HFH: Single; // half of height, for align to center
- Sign: Single; // +1 or -1
- angle: Single; // in radians
- ASin, ACos: Single;
- begin
- if FNormalDirection = ndInside then
- Sign := -1
- else
- Sign := 1;
- HBW := FBaseWidth * 0.5;
- HBD := FBaseDepth * 0.5;
- HTW := HBW * (FApexHeight - FHeight) / FApexHeight;
- HTD := HBD * (FApexHeight - FHeight) / FApexHeight;
- HFH := FHeight * 0.5;
- gl.Begin_(GL_QUADS);
- if [fpFront, fpBack] * FParts <> [] then
- begin
- angle := ArcTan(FApexHeight / HBD);
- // angle of front plane with bottom plane
- SinCosine(angle, ASin, ACos);
- if fpFront in FParts then
- begin
- gl.Normal3f(0, Sign * ACos, Sign * ASin);
- XGL.TexCoord2fv(@XYTexPoint);
- gl.Vertex3f(HTW, HFH, HTD);
- XGL.TexCoord2fv(@YTexPoint);
- gl.Vertex3f(-HTW, HFH, HTD);
- XGL.TexCoord2fv(@NullTexPoint);
- gl.Vertex3f(-HBW, -HFH, HBD);
- XGL.TexCoord2fv(@XTexPoint);
- gl.Vertex3f(HBW, -HFH, HBD);
- end;
- if fpBack in FParts then
- begin
- gl.Normal3f(0, Sign * ACos, -Sign * ASin);
- XGL.TexCoord2fv(@YTexPoint);
- gl.Vertex3f(HTW, HFH, -HTD);
- XGL.TexCoord2fv(@NullTexPoint);
- gl.Vertex3f(HBW, -HFH, -HBD);
- XGL.TexCoord2fv(@XTexPoint);
- gl.Vertex3f(-HBW, -HFH, -HBD);
- XGL.TexCoord2fv(@XYTexPoint);
- gl.Vertex3f(-HTW, HFH, -HTD);
- end;
- end;
- if [fpLeft, fpRight] * FParts <> [] then
- begin
- angle := ArcTan(FApexHeight / HBW); // angle of side plane with bottom plane
- SinCosine(angle, ASin, ACos);
- if fpLeft in FParts then
- begin
- gl.Normal3f(-Sign * ASin, Sign * ACos, 0);
- XGL.TexCoord2fv(@XYTexPoint);
- gl.Vertex3f(-HTW, HFH, HTD);
- XGL.TexCoord2fv(@YTexPoint);
- gl.Vertex3f(-HTW, HFH, -HTD);
- XGL.TexCoord2fv(@NullTexPoint);
- gl.Vertex3f(-HBW, -HFH, -HBD);
- XGL.TexCoord2fv(@XTexPoint);
- gl.Vertex3f(-HBW, -HFH, HBD);
- end;
- if fpRight in FParts then
- begin
- gl.Normal3f(Sign * ASin, Sign * ACos, 0);
- XGL.TexCoord2fv(@YTexPoint);
- gl.Vertex3f(HTW, HFH, HTD);
- XGL.TexCoord2fv(@NullTexPoint);
- gl.Vertex3f(HBW, -HFH, HBD);
- XGL.TexCoord2fv(@XTexPoint);
- gl.Vertex3f(HBW, -HFH, -HBD);
- XGL.TexCoord2fv(@XYTexPoint);
- gl.Vertex3f(HTW, HFH, -HTD);
- end;
- end;
- if (fpTop in FParts) and (FHeight < FApexHeight) then
- begin
- gl.Normal3f(0, Sign, 0);
- XGL.TexCoord2fv(@YTexPoint);
- gl.Vertex3f(-HTW, HFH, -HTD);
- XGL.TexCoord2fv(@NullTexPoint);
- gl.Vertex3f(-HTW, HFH, HTD);
- XGL.TexCoord2fv(@XTexPoint);
- gl.Vertex3f(HTW, HFH, HTD);
- XGL.TexCoord2fv(@XYTexPoint);
- gl.Vertex3f(HTW, HFH, -HTD);
- end;
- if fpBottom in FParts then
- begin
- gl.Normal3f(0, -Sign, 0);
- XGL.TexCoord2fv(@NullTexPoint);
- gl.Vertex3f(-HBW, -HFH, -HBD);
- XGL.TexCoord2fv(@XTexPoint);
- gl.Vertex3f(HBW, -HFH, -HBD);
- XGL.TexCoord2fv(@XYTexPoint);
- gl.Vertex3f(HBW, -HFH, HBD);
- XGL.TexCoord2fv(@YTexPoint);
- gl.Vertex3f(-HBW, -HFH, HBD);
- end;
- gl.End_;
- end;
- procedure TGLFrustrum.SetApexHeight(const aValue: Single);
- begin
- if (aValue <> FApexHeight) and (aValue >= 0) then
- begin
- FApexHeight := aValue;
- if FHeight > aValue then
- FHeight := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLFrustrum.SetBaseDepth(const aValue: Single);
- begin
- if (aValue <> FBaseDepth) and (aValue >= 0) then
- begin
- FBaseDepth := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLFrustrum.SetBaseWidth(const aValue: Single);
- begin
- if (aValue <> FBaseWidth) and (aValue >= 0) then
- begin
- FBaseWidth := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLFrustrum.SetHeight(const aValue: Single);
- begin
- if (aValue <> FHeight) and (aValue >= 0) then
- begin
- FHeight := aValue;
- if FApexHeight < aValue then
- FApexHeight := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLFrustrum.SetParts(aValue: TFrustrumParts);
- begin
- if aValue <> FParts then
- begin
- FParts := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLFrustrum.SetNormalDirection(aValue: TGLNormalDirection);
- begin
- if aValue <> FNormalDirection then
- begin
- FNormalDirection := aValue;
- StructureChanged;
- end;
- end;
- procedure TGLFrustrum.Assign(Source: TPersistent);
- begin
- if Assigned(Source) and (Source is TGLFrustrum) then
- begin
- FApexHeight := TGLFrustrum(Source).FApexHeight;
- FBaseDepth := TGLFrustrum(Source).FBaseDepth;
- FBaseWidth := TGLFrustrum(Source).FBaseWidth;
- FHeight := TGLFrustrum(Source).FHeight;
- FParts := TGLFrustrum(Source).FParts;
- FNormalDirection := TGLFrustrum(Source).FNormalDirection;
- end;
- inherited Assign(Source);
- end;
- function TGLFrustrum.TopDepth: Single;
- begin
- Result := FBaseDepth * (FApexHeight - FHeight) / FApexHeight;
- end;
- function TGLFrustrum.TopWidth: Single;
- begin
- Result := FBaseWidth * (FApexHeight - FHeight) / FApexHeight;
- end;
- procedure TGLFrustrum.DefineProperties(Filer: TFiler);
- begin
- inherited;
- Filer.DefineBinaryProperty('FrustrumSize', ReadData, WriteData,
- (FApexHeight <> 1) or (FBaseDepth <> 1) or (FBaseWidth <> 1) or
- (FHeight <> 0.5));
- end;
- procedure TGLFrustrum.ReadData(Stream: TStream);
- begin
- with Stream do
- begin
- Read(FApexHeight, SizeOf(FApexHeight));
- Read(FBaseDepth, SizeOf(FBaseDepth));
- Read(FBaseWidth, SizeOf(FBaseWidth));
- Read(FHeight, SizeOf(FHeight));
- end;
- end;
- procedure TGLFrustrum.WriteData(Stream: TStream);
- begin
- with Stream do
- begin
- Write(FApexHeight, SizeOf(FApexHeight));
- Write(FBaseDepth, SizeOf(FBaseDepth));
- Write(FBaseWidth, SizeOf(FBaseWidth));
- Write(FHeight, SizeOf(FHeight));
- end;
- end;
- function TGLFrustrum.AxisAlignedBoundingBoxUnscaled: TAABB;
- var
- aabb: TAABB;
- child: TGLBaseSceneObject;
- i: integer;
- begin
- SetAABB(Result, AxisAlignedDimensionsUnscaled);
- OffsetAABB(Result, VectorMake(0, FHeight * 0.5, 0));
- // not tested for child objects
- for i := 0 to Count - 1 do
- begin
- child := TGLBaseSceneObject(Children[i]);
- aabb := child.AxisAlignedBoundingBoxUnscaled;
- AABBTransform(aabb, child.Matrix^);
- AddAABB(Result, aabb);
- end;
- end;
- function TGLFrustrum.AxisAlignedDimensionsUnscaled: TVector;
- begin
- Result.X := FBaseWidth * 0.5;
- Result.Y := FHeight * 0.5;
- Result.Z := FBaseDepth * 0.5;
- Result.W := 0;
- end;
- // ------------------
- // ------------------ TGLPolygon ------------------
- // ------------------
- constructor TGLPolygon.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FParts := [ppTop, ppBottom];
- end;
- destructor TGLPolygon.Destroy;
- begin
- inherited Destroy;
- end;
- procedure TGLPolygon.SetParts(const val: TGLPolygonParts);
- begin
- if FParts <> val then
- begin
- FParts := val;
- StructureChanged;
- end;
- end;
- procedure TGLPolygon.Assign(Source: TPersistent);
- begin
- if Source is TGLPolygon then
- begin
- FParts := TGLPolygon(Source).FParts;
- end;
- inherited Assign(Source);
- end;
- procedure TGLPolygon.BuildList(var rci: TGLRenderContextInfo);
- var
- Normal: TAffineVector;
- pNorm: PAffineVector;
- begin
- if (Nodes.Count > 1) then
- begin
- Normal := Nodes.Normal;
- if VectorIsNull(Normal) then
- pNorm := nil
- else
- pNorm := @Normal;
- if ppTop in FParts then
- begin
- if SplineMode = lsmLines then
- Nodes.RenderTesselatedPolygon(true, pNorm, 1)
- else
- Nodes.RenderTesselatedPolygon(true, pNorm, Division);
- end;
- // tessellate bottom polygon
- if ppBottom in FParts then
- begin
- if Assigned(pNorm) then
- NegateVector(Normal);
- if SplineMode = lsmLines then
- Nodes.RenderTesselatedPolygon(true, pNorm, 1, true)
- else
- Nodes.RenderTesselatedPolygon(true, pNorm, Division, true);
- end;
- end;
- end;
- // -------------------------------------------------------------
- initialization
- // -------------------------------------------------------------
- RegisterClasses([TGLCylinder, TGLCone, TGLTorus, TGLDisk, TGLArrowLine,
- TGLAnnulus, TGLFrustrum, TGLPolygon, TGLCapsule, TGLArrowArc]);
- end.
|