GLPipelineTransformation.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. unit GLPipelineTransformation;
  5. (* Pipeline transformations *)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. Winapi.OpenGL,
  10. OpenGLTokens,
  11. GLVectorGeometry,
  12. GLVectorTypes,
  13. GLS.Logger;
  14. const
  15. MAX_MATRIX_STACK_DEPTH = 128;
  16. type
  17. TGLPipelineTransformationState =
  18. (
  19. trsModelViewChanged,
  20. trsInvModelViewChanged,
  21. trsInvModelChanged,
  22. trsNormalModelChanged,
  23. trsViewProjChanged,
  24. trsFrustum
  25. );
  26. TGLPipelineTransformationStates = set of TGLPipelineTransformationState;
  27. const
  28. cAllStatesChanged = [trsModelViewChanged, trsInvModelViewChanged, trsInvModelChanged, trsViewProjChanged, trsNormalModelChanged, trsFrustum];
  29. type
  30. PTransformationRec = ^TTransformationRec;
  31. TTransformationRec = record
  32. FStates: TGLPipelineTransformationStates;
  33. FModelMatrix: TMatrix;
  34. FViewMatrix: TMatrix;
  35. FProjectionMatrix: TMatrix;
  36. FInvModelMatrix: TMatrix;
  37. FNormalModelMatrix: TAffineMatrix;
  38. FModelViewMatrix: TMatrix;
  39. FInvModelViewMatrix: TMatrix;
  40. FViewProjectionMatrix: TMatrix;
  41. FFrustum: TFrustum;
  42. end;
  43. type
  44. TOnMatricesPush = procedure() of object;
  45. TGLTransformation = class(TObject)
  46. private
  47. FStackPos: Integer;
  48. FStack: array of TTransformationRec;
  49. FLoadMatricesEnabled: Boolean;
  50. FOnPush: TOnMatricesPush;
  51. function GetModelMatrix: PMatrix; inline;
  52. function GetViewMatrix: PMatrix; inline;
  53. function GetProjectionMatrix: PMatrix; inline;
  54. function GetModelViewMatrix: PMatrix; inline;
  55. function GetInvModelViewMatrix: PMatrix; inline;
  56. function GetInvModelMatrix: PMatrix; inline;
  57. function GetNormalModelMatrix: PAffineMatrix; inline;
  58. function GetViewProjectionMatrix: PMatrix; inline;
  59. function GetFrustum: TFrustum; inline;
  60. protected
  61. procedure LoadModelViewMatrix; inline;
  62. procedure LoadProjectionMatrix; inline;
  63. procedure DoMatricesLoaded; inline;
  64. property OnPush: TOnMatricesPush read FOnPush write FOnPush;
  65. public
  66. constructor Create;
  67. procedure SetModelMatrix(const AMatrix: TMatrix); inline;
  68. procedure SetViewMatrix(const AMatrix: TMatrix); inline;
  69. procedure SetProjectionMatrix(const AMatrix: TMatrix); inline;
  70. procedure IdentityAll; inline;
  71. procedure Push(AValue: PTransformationRec); overload;
  72. procedure Push(); overload; inline;
  73. procedure Pop;
  74. procedure ReplaceFromStack;
  75. function StackTop: TTransformationRec; inline;
  76. property ModelMatrix: PMatrix read GetModelMatrix;
  77. property ViewMatrix: PMatrix read GetViewMatrix;
  78. property ProjectionMatrix: PMatrix read GetProjectionMatrix;
  79. property InvModelMatrix: PMatrix read GetInvModelMatrix;
  80. property ModelViewMatrix: PMatrix read GetModelViewMatrix;
  81. property NormalModelMatrix: PAffineMatrix read GetNormalModelMatrix;
  82. property InvModelViewMatrix: PMatrix read GetInvModelViewMatrix;
  83. property ViewProjectionMatrix: PMatrix read GetViewProjectionMatrix;
  84. property Frustum: TFrustum read GetFrustum;
  85. property LoadMatricesEnabled: Boolean read FLoadMatricesEnabled write FLoadMatricesEnabled;
  86. end;
  87. //=====================================================================
  88. implementation
  89. //=====================================================================
  90. uses
  91. GLContext;
  92. constructor TGLTransformation.Create;
  93. begin
  94. FStackPos := 0;
  95. SetLength(FStack, MAX_MATRIX_STACK_DEPTH);
  96. IdentityAll;
  97. end;
  98. procedure TGLTransformation.LoadProjectionMatrix;
  99. begin
  100. gl.MatrixMode(GL_PROJECTION);
  101. gl.LoadMatrixf(PGLFloat(@FStack[FStackPos].FProjectionMatrix));
  102. gl.MatrixMode(GL_MODELVIEW);
  103. end;
  104. function TGLTransformation.GetModelViewMatrix: PMatrix;
  105. begin
  106. if trsModelViewChanged in FStack[FStackPos].FStates then
  107. begin
  108. FStack[FStackPos].FModelViewMatrix :=
  109. MatrixMultiply(FStack[FStackPos].FModelMatrix, FStack[FStackPos].FViewMatrix);
  110. Exclude(FStack[FStackPos].FStates, trsModelViewChanged);
  111. end;
  112. Result := @FStack[FStackPos].FModelViewMatrix;
  113. end;
  114. procedure TGLTransformation.LoadModelViewMatrix;
  115. begin
  116. gl.LoadMatrixf(PGLFloat(GetModelViewMatrix));
  117. end;
  118. procedure TGLTransformation.IdentityAll;
  119. begin
  120. with FStack[FStackPos] do
  121. begin
  122. FModelMatrix := IdentityHmgMatrix;
  123. FViewMatrix := IdentityHmgMatrix;
  124. FProjectionMatrix := IdentityHmgMatrix;
  125. FStates := cAllStatesChanged;
  126. end;
  127. if LoadMatricesEnabled then
  128. begin
  129. LoadModelViewMatrix;
  130. LoadProjectionMatrix;
  131. end;
  132. end;
  133. procedure TGLTransformation.DoMatricesLoaded;
  134. begin
  135. if Assigned(FOnPush) then
  136. FOnPush();
  137. end;
  138. procedure TGLTransformation.Push;
  139. var
  140. prevPos: Integer;
  141. begin
  142. prevPos := FStackPos;
  143. Inc(FStackPos);
  144. FStack[FStackPos] := FStack[prevPos];
  145. end;
  146. procedure TGLTransformation.Push(AValue: PTransformationRec);
  147. var
  148. prevPos: Integer;
  149. begin
  150. {$IFDEF USE_LOGGING}
  151. if FStackPos > MAX_MATRIX_STACK_DEPTH then
  152. begin
  153. GLSLogger.LogWarningFmt('Transformation stack overflow, more then %d values',
  154. [MAX_MATRIX_STACK_DEPTH]);
  155. end;
  156. {$ENDIF}
  157. prevPos := FStackPos;
  158. Inc(FStackPos);
  159. if Assigned(AValue) then
  160. begin
  161. FStack[FStackPos] := AValue^;
  162. if LoadMatricesEnabled then
  163. begin
  164. LoadModelViewMatrix;
  165. LoadProjectionMatrix;
  166. end;
  167. DoMatricesLoaded;
  168. end
  169. else
  170. FStack[FStackPos] := FStack[prevPos];
  171. end;
  172. procedure TGLTransformation.Pop;
  173. begin
  174. {$IFDEF USE_LOGGING}
  175. if FStackPos = 0 then
  176. begin
  177. GLSLogger.LogError('Transformation stack underflow');
  178. exit;
  179. end;
  180. {$ENDIF}
  181. Dec(FStackPos);
  182. if LoadMatricesEnabled then
  183. begin
  184. LoadModelViewMatrix;
  185. LoadProjectionMatrix;
  186. end;
  187. end;
  188. procedure TGLTransformation.ReplaceFromStack;
  189. var
  190. prevPos: Integer;
  191. begin
  192. {$IFDEF USE_LOGGING}
  193. if FStackPos = 0 then
  194. begin
  195. GLSLogger.LogError('Transformation stack underflow');
  196. exit;
  197. end;
  198. {$ENDIF}
  199. prevPos := FStackPos - 1;
  200. FStack[FStackPos].FModelMatrix := FStack[prevPos].FModelMatrix;
  201. FStack[FStackPos].FViewMatrix:= FStack[prevPos].FViewMatrix;
  202. FStack[FStackPos].FProjectionMatrix:= FStack[prevPos].FProjectionMatrix;
  203. FStack[FStackPos].FStates := FStack[prevPos].FStates;
  204. if LoadMatricesEnabled then
  205. begin
  206. LoadModelViewMatrix;
  207. LoadProjectionMatrix;
  208. end;
  209. end;
  210. function TGLTransformation.GetModelMatrix: PMatrix;
  211. begin
  212. Result := @FStack[FStackPos].FModelMatrix;
  213. end;
  214. function TGLTransformation.GetViewMatrix: PMatrix;
  215. begin
  216. Result := @FStack[FStackPos].FViewMatrix;
  217. end;
  218. function TGLTransformation.GetProjectionMatrix: PMatrix;
  219. begin
  220. Result := @FStack[FStackPos].FProjectionMatrix;
  221. end;
  222. procedure TGLTransformation.SetModelMatrix(const AMatrix: TMatrix);
  223. begin
  224. FStack[FStackPos].FModelMatrix := AMatrix;
  225. FStack[FStackPos].FStates := FStack[FStackPos].FStates +
  226. [trsModelViewChanged, trsInvModelViewChanged, trsInvModelChanged, trsNormalModelChanged];
  227. if LoadMatricesEnabled then
  228. LoadModelViewMatrix;
  229. end;
  230. procedure TGLTransformation.SetViewMatrix(const AMatrix: TMatrix);
  231. begin
  232. FStack[FStackPos].FViewMatrix:= AMatrix;
  233. FStack[FStackPos].FStates := FStack[FStackPos].FStates +
  234. [trsModelViewChanged, trsInvModelViewChanged, trsViewProjChanged, trsFrustum];
  235. if LoadMatricesEnabled then
  236. LoadModelViewMatrix;
  237. end;
  238. function TGLTransformation.StackTop: TTransformationRec;
  239. begin
  240. Result := FStack[FStackPos];
  241. end;
  242. procedure TGLTransformation.SetProjectionMatrix(const AMatrix: TMatrix);
  243. begin
  244. FStack[FStackPos].FProjectionMatrix := AMatrix;
  245. FStack[FStackPos].FStates := FStack[FStackPos].FStates +
  246. [trsViewProjChanged, trsFrustum];
  247. if LoadMatricesEnabled then
  248. LoadProjectionMatrix;
  249. end;
  250. function TGLTransformation.GetInvModelViewMatrix: PMatrix;
  251. begin
  252. if trsInvModelViewChanged in FStack[FStackPos].FStates then
  253. begin
  254. FStack[FStackPos].FInvModelViewMatrix := GetModelViewMatrix^;
  255. InvertMatrix(FStack[FStackPos].FInvModelViewMatrix);
  256. Exclude(FStack[FStackPos].FStates, trsInvModelViewChanged);
  257. end;
  258. Result := @FStack[FStackPos].FInvModelViewMatrix;
  259. end;
  260. function TGLTransformation.GetInvModelMatrix: PMatrix;
  261. begin
  262. if trsInvModelChanged in FStack[FStackPos].FStates then
  263. begin
  264. FStack[FStackPos].FInvModelMatrix := MatrixInvert(FStack[FStackPos].FModelMatrix);
  265. Exclude(FStack[FStackPos].FStates, trsInvModelChanged);
  266. end;
  267. Result := @FStack[FStackPos].FInvModelMatrix;
  268. end;
  269. function TGLTransformation.GetNormalModelMatrix: PAffineMatrix;
  270. var
  271. M: TMatrix;
  272. begin
  273. if trsNormalModelChanged in FStack[FStackPos].FStates then
  274. begin
  275. M := FStack[FStackPos].FModelMatrix;
  276. NormalizeMatrix(M);
  277. SetMatrix(FStack[FStackPos].FNormalModelMatrix, M);
  278. Exclude(FStack[FStackPos].FStates, trsNormalModelChanged);
  279. end;
  280. Result := @FStack[FStackPos].FNormalModelMatrix;
  281. end;
  282. function TGLTransformation.GetViewProjectionMatrix: PMatrix;
  283. begin
  284. if trsViewProjChanged in FStack[FStackPos].FStates then
  285. begin
  286. FStack[FStackPos].FViewProjectionMatrix :=
  287. MatrixMultiply(FStack[FStackPos].FViewMatrix, FStack[FStackPos].FProjectionMatrix);
  288. Exclude(FStack[FStackPos].FStates, trsViewProjChanged);
  289. end;
  290. Result := @FStack[FStackPos].FViewProjectionMatrix;
  291. end;
  292. function TGLTransformation.GetFrustum: TFrustum;
  293. begin
  294. if trsFrustum in FStack[FStackPos].FStates then
  295. begin
  296. FStack[FStackPos].FFrustum := ExtractFrustumFromModelViewProjection(GetViewProjectionMatrix^);
  297. Exclude(FStack[FStackPos].FStates, trsFrustum);
  298. end;
  299. Result := FStack[FStackPos].FFrustum;
  300. end;
  301. end.