Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki

Aktuelle Zeit: Sa Okt 25, 2014 08:00

Foren-Übersicht » English » English Programming Forum
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 20 Beiträge ]  Gehe zu Seite 1, 2  Nächste
Autor Nachricht
 Betreff des Beitrags: OpenGL 3 Matrix's
BeitragVerfasst: Fr Feb 08, 2013 19:07 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
Are there any units/libraries for Delphi to replace the Matrix functions OpenGL 3/4 lacks?

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: Sa Feb 09, 2013 12:09 
Offline
DGL Member
Benutzeravatar

Registriert: Di Sep 06, 2005 18:34
Beiträge: 352
Wohnort: Hamburg
Hi …

I don't know anything about what's out there in the delphi world, but i would just use some more advanced math library that knows about vectors, matrices and/or quaternions.
If you are looking for something like the matrix stack I don't know of any library, at least for C/C++. But it's very simple to implement such a stack yourself.

There is one math lib I know about, that's specifically for OpenGL: http://glm.g-truc.net/
It's C++, but maybe you are lucky and there exist Delphi bindings.

_________________
Der Mensch hat neben dem Trieb der Fortpflanzung und dem zu essen und zu trinken zwei Leidenschaften: Krach zu machen und nicht zuzuhören. (Kurt Tucholsky)
Schwabbeldiwapp, hier kommt die Grütze. (Der Quästor)


Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: Sa Feb 09, 2013 12:22 
Offline
DGL Member

Registriert: Mo Nov 09, 2009 12:01
Beiträge: 141
https://glscene.svn.sourceforge.net/svnroot/glscene/trunk/Source/VectorGeometry.pas


Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: Sa Feb 09, 2013 20:33 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
Thanks :) i now have a rotating Coloured Triangle.

Anyone have a better way of handling Push/Pull's than what i came up with?
Code:
  1. procedure TOGL.MMPush;
  2. begin
  3.  if FMatrixStackID = 49 then Exit; //Error: Out of Space!
  4.  Inc(FMatrixStackID);
  5.  FMatrixStack[FMatrixStackID] := FModelMatrix;
  6. end;
  7.  
  8. procedure TOGL.MMPull;
  9. begin
  10.  if FMatrixStackID = -1 then Exit;
  11.  FModelMatrix := FMatrixStack[FMatrixStackID];
  12.  Dec(FMatrixStackID);
  13. end;

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: So Feb 10, 2013 13:30 
Offline
DGL Member
Benutzeravatar

Registriert: So Sep 26, 2010 12:54
Beiträge: 238
Wohnort: wieder in Berlin
Programmiersprache: Englisch
make use of TStack (or TStack<T> when using >=D12)


Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: So Feb 10, 2013 13:50 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
Wouldn't it be slower to be constantly Allocating/Freeing memory each frame?

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: Di Feb 12, 2013 10:48 
Offline
Forenkatze
Benutzeravatar

Registriert: Mi Okt 22, 2003 18:30
Beiträge: 1850
Wohnort: Närnberch
Programmiersprache: Scala, Java, C*
Did you take a look at TStack's source code? I don't have any Delphi around, so I can not. I assume that they also use an array internally similar to your approach. So you don't have any memory allocation per frame, as the the array gets preallocated.

On a sidenote: If Delphi had a good memory manager with good pooling allocations of small objects would not hurt at all. I recall that there is a recommended replacement memory manager, although I forgot it's name. And then you might try that GC memory manager which can be used in C if allocations are a major concern to you.

_________________
"Für kein Tier wird so viel gearbeitet wie für die Katz'."
Aktuelle Projekte: www.swiftsteel.org + JSixAxis


Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: Di Feb 12, 2013 21:41 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
Its an extended TList basically (In Delphi 6 Anyway). So as it requires a pointer to be sent to it, memory would need to be allocated each time a matrix is added and freed when its removed(A TList doesn't allocate memory for each item you add to the list, just the core dynamic array for holding the pointers to each item).


FastMM was integrated into the latter versions of Delphi(>= Delphi 2006), replacing Borlands. Its meant to be faster in most situations. Its at least good for debugging memory leaks.

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: Di Feb 12, 2013 23:35 
Offline
Forenkatze
Benutzeravatar

Registriert: Mi Okt 22, 2003 18:30
Beiträge: 1850
Wohnort: Närnberch
Programmiersprache: Scala, Java, C*
Oh. I was under the impression that if the capacity was reached, TList would allocate more memory than needed for a single pointer so it would not have to do that again for the next few additions.

_________________
"Für kein Tier wird so viel gearbeitet wie für die Katz'."
Aktuelle Projekte: www.swiftsteel.org + JSixAxis


Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: Mi Feb 13, 2013 03:45 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
Well the array at the core of the TList does work like that. But its only allocating space for the actual pointer, not the data it points to. If you want to store Integer numbers it would be fine as you could just set the Pointers own data to your number.

Code:
  1. ReallocMem(FList, NewCapacity * SizeOf(Pointer));

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: Mi Feb 13, 2013 04:14 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
Wrote my own Stack based on how a TList works.

Code:
  1. unit ot_Stack;
  2.  
  3. {
  4.  Copyright (c) <2013> <Stuart Carey>
  5.  
  6.  This file is provided 'as-is', without any express or implied
  7.  warranty. In no event will the authors be held liable for any damages
  8.  arising from the use of this file.
  9.  
  10.  Permission is granted to anyone to use this file for any purpose,
  11.  including commercial applications, and to alter it and redistribute it
  12.  freely, subject to the following restrictions:
  13.  
  14.  1. The origin of this file must not be misrepresented; you must not
  15.  claim that you wrote the original file. If you use this file
  16.  in a product, an acknowledgment in the product documentation would be
  17.  appreciated but is not required.
  18.  
  19.  2. Altered versions must be plainly marked as such, and must not be
  20.  misrepresented as being the original file.
  21.  
  22.  3. This notice may not be removed or altered.
  23. }
  24.  
  25. interface
  26.  
  27. uses VectorGeometry, Classes;
  28.  
  29. const
  30.  MaxStackSize = MaxListSize div SizeOf(TMatrix);
  31.  
  32. type
  33.  PMatrixList = ^TMatrixList;
  34.  TMatrixList = Array[0..MaxStackSize-1] of TMatrix;
  35.  
  36. TOT_Stack = Class(TObject)
  37. private
  38.  FMatrixs  : PMatrixList;
  39.  FCount,
  40.  FSize     : Integer;
  41.  procedure IncSize;
  42. public
  43.  constructor Create;
  44.  destructor Destroy; override;
  45.  procedure Push(Matrix : TMatrix);
  46.  function Pull : TMatrix;
  47.  procedure Clear(All : Boolean = False);
  48. end;
  49.  
  50. implementation
  51.  
  52. {$IFDEF DEBUG}
  53. uses SysUtils;
  54. {$ENDIF}
  55.  
  56. constructor TOT_Stack.Create;
  57. begin
  58.  Clear(True);
  59. end;
  60.  
  61. destructor TOT_Stack.Destroy;
  62. begin
  63.  Clear(True);
  64. end;
  65.  
  66. procedure TOT_Stack.IncSize;
  67. begin
  68.  Inc(FCount);
  69.  if (FCount <= FSize) then Exit;
  70.  
  71.  Inc(FSize,10);
  72.  {$IFDEF DEBUG}
  73.   if FSize > MaxStackSize then
  74.   raise Exception.Create('TOT_Stack.SetSize: FSize > MaxStackSize');
  75.  {$ENDIF}
  76.  
  77.  ReallocMem(FMatrixs,FSize*SizeOf(TMatrix));
  78. end;
  79.  
  80. procedure TOT_Stack.Push(Matrix : TMatrix);
  81. begin
  82.  IncSize;
  83.  FMatrixs[FCount-1] := Matrix;
  84. end;
  85.  
  86. function TOT_Stack.Pull : TMatrix;
  87. begin
  88.  if FCount < 1 then
  89.  begin
  90.   {$IFDEF DEBUG}
  91.    raise Exception.Create('TOT_Stack.Pull: FCount < 1');
  92.   {$ELSE}
  93.    Result := IdentityHmgMatrix;
  94.    Exit; //Error!
  95.   {$ENDIF}
  96.  end;
  97.  Dec(FCount);
  98.  Result := FMatrixs[FCount];
  99. end;
  100.  
  101. procedure TOT_Stack.Clear(All : Boolean = False);
  102. begin
  103.  FCount := 0;
  104.  if All then
  105.  begin
  106.   FSize  := 0;
  107.   ReallocMem(FMatrixs,0);
  108.  end;
  109. end;
  110.  
  111. end.

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Zuletzt geändert von Stucuk am Mi Feb 13, 2013 21:25, insgesamt 6-mal geändert.

Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: Mi Feb 13, 2013 18:14 
Offline
DGL Member
Benutzeravatar

Registriert: So Sep 26, 2010 12:54
Beiträge: 238
Wohnort: wieder in Berlin
Programmiersprache: Englisch
- weird indentation
- add a constructor where you can pass a capacity already, just like with TList - in that case your Stack will never reallocmem and thus no "performance loss" when pushing/popping
(the default capacity could be 128*Sizeof(TMatrix) or something like that) you wont have this big stacks anyways, FFP was limited there too
- TMatrixList => TMatrixArray
- even tho TObject.Destroy is quite empty, inherited Destroy should be added to your TOTStack.Destroy dtor
- no need for the optional All parameter in Clear, just Clear everything, when you need a new stack, just instantiate a new one
- you could add a compiler switch for the behaviour when Pull is called too many times, rather throw an exception than just returning or like there Exit (but that could be a compiler switch)
- plural of matrix is matrices

just saying :)


Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: Mi Feb 13, 2013 20:57 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
phlegmatiker hat geschrieben:
- weird indentation

I have never seen the point in having multiple space or tabbed indentation. Its easy to read code that has 1 space indentation.

phlegmatiker hat geschrieben:
- add a constructor where you can pass a capacity already, just like with TList - in that case your Stack will never reallocmem and thus no "performance loss" when pushing/popping

Delphi 6's TList doesn't even have a constructor. You would only have a performance loss if an insane amount of push's were done without any pulls, but you would only get that during a single frame and initialising it with a lowish value won't help.

For reference there is an additional once off ~0.0015307649 milliseconds for the 13 reallocmem's on my pc (For your suggested 128 initial size), so its not going to make any real difference on performance to allocate 128 from the beginning.

phlegmatiker hat geschrieben:
- even tho TObject.Destroy is quite empty, inherited Destroy should be added to your TOTStack.Destroy dtor

I don't see the point in that. You would only need to inherit if the base class changed from a TObject to something else. Its the same with create, its empty in a TObject as well.

Personally i am use to looking in the constructor and destructor to make sure it has inheritance if i change the base class of an object to one which has something in the constructor/destructor. Its something everyone should do, no one should rely on someone adding inheritance into the constructor/destructor when it wasn't needed at the time it was written.

phlegmatiker hat geschrieben:
- no need for the optional All parameter in Clear, just Clear everything, when you need a new stack, just instantiate a new one

I call clear each frame to make sure that the stack is "empty" each frame(Allows Pushing/Pulling errors in code to not cause the stack to constantly allocate more spaces). Freeing everything each frame would defeat the purpose of reallocating the memory only when it needs more capacity.

phlegmatiker hat geschrieben:
- plural of matrix is matrices

I'm not aiming for a spelling contest medal, so that can stay.


P.S I have updated the TOT_Stack code posted above.

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: Mi Feb 13, 2013 21:37 
Offline
DGL Member
Benutzeravatar

Registriert: Do Sep 02, 2004 19:42
Beiträge: 3964
Wohnort: 2a01:4f8:d16:1305::/64
Programmiersprache: FreePascal, C++
phlegmatiker has a valid point about indentation. If you want your code to be reused or even reviewed, it's not your opinion which is relevant, but the opinion and taste of the broader public. I for myself drop other peoples code if it looks not well maintained. Not having a nice (i.e. readable to most people) indentation scheme is one of the things which look like not well maintained. Same goes for calling the inherited constructor/destructor always. It's just a matter of good style to do so, because it avoids another source of mistakes. I found myself quite often hunting issues with uninitialized fields or memleaks which were related to that, although I usually take well care about that (sidenote: it's a bug that the FP compiler doesn't warn you on that one, but w/e).

regards,
Horazont

_________________
If you find any deadlinks, please send me a notification – Wenn du tote Links findest, sende mir eine Benachrichtigung.
current projects: ManiacLab; Pythonic Universe; eXtensible Web Framework in Python
zombofant networkmy photostream
„Writing code is like writing poetry“ - source unknown

OTR: zion=3BA7F5A6 868DC61E 6412ACD1 71157B5F 849D62B0 | voyager=4661E31D 3F9543A4 79C22BBC C5A7C016 B1C7DD49

„Give a man a fish, and you feed him for a day. Teach a man to fish and you feed him for a lifetime. “ ~ A Chinese Proverb


Nach oben
 Profil  
 
 Betreff des Beitrags: Re: OpenGL 3 Matrix's
BeitragVerfasst: Mi Feb 13, 2013 23:40 
Offline
Forenkatze
Benutzeravatar

Registriert: Mi Okt 22, 2003 18:30
Beiträge: 1850
Wohnort: Närnberch
Programmiersprache: Scala, Java, C*
Which version of Delphi do you use, Stucuk? Delphi6 or something not quite as ancient? More up-to-date versions of Delphi (and FPC) have generic lists and also stacks (probably) so you don't have to do to your compiler's job.

_________________
"Für kein Tier wird so viel gearbeitet wie für die Katz'."
Aktuelle Projekte: www.swiftsteel.org + JSixAxis


Nach oben
 Profil  
 
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 20 Beiträge ]  Gehe zu Seite 1, 2  Nächste
Foren-Übersicht » English » English Programming Forum


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 0 Gäste


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
cron
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de