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

Aktuelle Zeit: Mi Jul 16, 2025 16:43

Foren-Übersicht » Programmierung » Allgemein
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 5 Beiträge ] 
Autor Nachricht
BeitragVerfasst: Do Mär 23, 2006 19:25 
Offline
DGL Member

Registriert: Mi Mär 22, 2006 20:16
Beiträge: 22
Wohnort: Wuppertal
Wie kann ich im 3D-Raum Punkte drehen und zwar um ein Normalvektor?

Wenn ich sage Turn(..., [0,1,0]) drehen sich die Punkte um die Y-Ache, sage ich Turn(..., [1,0,0]) dreht sich das ganze um die X-Achse, natürlich sollte auch Turn(..., [0.707 {=1/Sqrt/(2)}, 0.707, 0]) gehen.

Ich habe folgende Formel zusammengebaut
Code:
  1. function _Turn(p, n: TPoint3DF; d: float): TPoint3DF;
  2. begin
  3.   result.x := Cos(d)*p.x*n.z - Sin(d)*p.y*n.z
  4.             + Cos(d)*p.x*n.y - Sin(d)*p.z*n.y;
  5.   result.y := Cos(d)*p.y*n.z + Sin(d)*p.x*n.z
  6.             + Cos(d)*p.y*n.x - Sin(d)*p.z*n.x;
  7.   result.z := Cos(d)*p.z*n.y + Sin(d)*p.x*n.y
  8.             + Cos(d)*p.z*n.x + Sin(d)*p.y*n.x;
  9. end;
  10.  

Aber die funktioniert eher schlecht als recht.

Es muss doch möglich sein, ohne weitere Vektoren/Matrizien ein Punkt 2D-Mäßig zu drehen nur das halt eine Normale angegeben wird statt direkt X,Y oder Z.

Wie denn?[/pascal]


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Do Mär 23, 2006 22:12 
Offline
DGL Member

Registriert: Sa Jan 22, 2005 21:10
Beiträge: 225
Hab mir deinen Code jetzt nicht genauer angeguckt, aber mit Quaternionen kann man um beliebige Achsen drehen. Googel einfach mal danach, da gibts unmengen von Informationen zu.

_________________
[18:30] tomok: so wie ich das sehe : alles. was nich was anderes ist als nen Essay ist nen Essay

hi, i'm a signature viruz, plz set me as your signature and help me spread :)


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Do Mär 23, 2006 23:30 
Offline
DGL Member

Registriert: Mi Mär 22, 2006 20:16
Beiträge: 22
Wohnort: Wuppertal
Quaternionen benutzen, nur um einen 3D-Vektor (x,y,z) Flächenmäßig zu drehen, wobei der Normalvektor angibt, wo oben ist? :?
Hab mir das vor einiger Zeit schon mal angeguckt, aber nicht so wirkich verstanden.

Mir wär es lieber, wenn ich eine kompakte Formel hätte, anstatt jetzt großartig was aufzuziehen.
Wenn nicht anders gehen sollte, Ok ...

Eine verständliche Einführung in die Welt der Quaternionen wäre auch nicht schlecht


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Fr Mär 24, 2006 00:03 
Offline
DGL Member

Registriert: So Sep 26, 2004 05:57
Beiträge: 190
Wohnort: Linz
Wenn dir ein bisschen C nichts ausmacht, dann käme der MESA-Code für glRotatef in Frage. Obwohl dieser eine Matrix berechnet, lässt er sich auch leicht auf nur einen Punkt umwandeln.

Code:
  1. /**********************************************************************/
  2. /** \name Matrix generation */
  3. /*@{*/
  4.  
  5. /**
  6.  * Generate a 4x4 transformation matrix from glRotate parameters, and
  7.  * post-multiply the input matrix by it.
  8.  *
  9.  * \author
  10.  * This function was contributed by Erich Boleyn (erich@uruk.org).
  11.  * Optimizations contributed by Rudolf Opalla (rudi@khm.de).
  12.  */
  13. void
  14. _math_matrix_rotate( GLmatrix *mat,
  15.              GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
  16. {
  17.    GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
  18.    GLfloat m[16];
  19.    GLboolean optimized;
  20.  
  21.    s = (GLfloat) sin( angle * DEG2RAD );
  22.    c = (GLfloat) cos( angle * DEG2RAD );
  23.  
  24.    MEMCPY(m, Identity, sizeof(GLfloat)*16);
  25.    optimized = GL_FALSE;
  26.  
  27. #define M(row,col)  m[col*4+row]
  28.  
  29.    if (x == 0.0F) {
  30.       if (y == 0.0F) {
  31.          if (z != 0.0F) {
  32.             optimized = GL_TRUE;
  33.             /* rotate only around z-axis */
  34.             M(0,0) = c;
  35.             M(1,1) = c;
  36.             if (z < 0.0F) {
  37.                M(0,1) = s;
  38.                M(1,0) = -s;
  39.             }
  40.             else {
  41.                M(0,1) = -s;
  42.                M(1,0) = s;
  43.             }
  44.          }
  45.       }
  46.       else if (z == 0.0F) {
  47.          optimized = GL_TRUE;
  48.          /* rotate only around y-axis */
  49.          M(0,0) = c;
  50.          M(2,2) = c;
  51.          if (y < 0.0F) {
  52.             M(0,2) = -s;
  53.             M(2,0) = s;
  54.          }
  55.          else {
  56.             M(0,2) = s;
  57.             M(2,0) = -s;
  58.          }
  59.       }
  60.    }
  61.    else if (y == 0.0F) {
  62.       if (z == 0.0F) {
  63.          optimized = GL_TRUE;
  64.          /* rotate only around x-axis */
  65.          M(1,1) = c;
  66.          M(2,2) = c;
  67.          if (x < 0.0F) {
  68.             M(1,2) = s;
  69.             M(2,1) = -s;
  70.          }
  71.          else {
  72.             M(1,2) = -s;
  73.             M(2,1) = s;
  74.          }
  75.       }
  76.    }
  77.  
  78.    if (!optimized) {
  79.       const GLfloat mag = SQRTF(x * x + y * y + z * z);
  80.  
  81.       if (mag <= 1.0e-4) {
  82.          /* no rotation, leave mat as-is */
  83.          return;
  84.       }
  85.  
  86.       x /= mag;
  87.       y /= mag;
  88.       z /= mag;
  89.  
  90.  
  91.       /*
  92.        *     Arbitrary axis rotation matrix.
  93.        *
  94.        *  This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
  95.        *  like so:  Rz * Ry * T * Ry' * Rz'.  T is the final rotation
  96.        *  (which is about the X-axis), and the two composite transforms
  97.        *  Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
  98.        *  from the arbitrary axis to the X-axis then back.  They are
  99.        *  all elementary rotations.
  100.        *
  101.        *  Rz' is a rotation about the Z-axis, to bring the axis vector
  102.        *  into the x-z plane.  Then Ry' is applied, rotating about the
  103.        *  Y-axis to bring the axis vector parallel with the X-axis.  The
  104.        *  rotation about the X-axis is then performed.  Ry and Rz are
  105.        *  simply the respective inverse transforms to bring the arbitrary
  106.        *  axis back to it's original orientation.  The first transforms
  107.        *  Rz' and Ry' are considered inverses, since the data from the
  108.        *  arbitrary axis gives you info on how to get to it, not how
  109.        *  to get away from it, and an inverse must be applied.
  110.        *
  111.        *  The basic calculation used is to recognize that the arbitrary
  112.        *  axis vector (x, y, z), since it is of unit length, actually
  113.        *  represents the sines and cosines of the angles to rotate the
  114.        *  X-axis to the same orientation, with theta being the angle about
  115.        *  Z and phi the angle about Y (in the order described above)
  116.        *  as follows:
  117.        *
  118.        *  cos ( theta ) = x / sqrt ( 1 - z^2 )
  119.        *  sin ( theta ) = y / sqrt ( 1 - z^2 )
  120.        *
  121.        *  cos ( phi ) = sqrt ( 1 - z^2 )
  122.        *  sin ( phi ) = z
  123.        *
  124.        *  Note that cos ( phi ) can further be inserted to the above
  125.        *  formulas:
  126.        *
  127.        *  cos ( theta ) = x / cos ( phi )
  128.        *  sin ( theta ) = y / sin ( phi )
  129.        *
  130.        *  ...etc.  Because of those relations and the standard trigonometric
  131.        *  relations, it is pssible to reduce the transforms down to what
  132.        *  is used below.  It may be that any primary axis chosen will give the
  133.        *  same results (modulo a sign convention) using thie method.
  134.        *
  135.        *  Particularly nice is to notice that all divisions that might
  136.        *  have caused trouble when parallel to certain planes or
  137.        *  axis go away with care paid to reducing the expressions.
  138.        *  After checking, it does perform correctly under all cases, since
  139.        *  in all the cases of division where the denominator would have
  140.        *  been zero, the numerator would have been zero as well, giving
  141.        *  the expected result.
  142.        */
  143.  
  144.       xx = x * x;
  145.       yy = y * y;
  146.       zz = z * z;
  147.       xy = x * y;
  148.       yz = y * z;
  149.       zx = z * x;
  150.       xs = x * s;
  151.       ys = y * s;
  152.       zs = z * s;
  153.       one_c = 1.0F - c;
  154.  
  155.       /* We already hold the identity-matrix so we can skip some statements */
  156.       M(0,0) = (one_c * xx) + c;
  157.       M(0,1) = (one_c * xy) - zs;
  158.       M(0,2) = (one_c * zx) + ys;
  159. /*    M(0,3) = 0.0F; */
  160.  
  161.       M(1,0) = (one_c * xy) + zs;
  162.       M(1,1) = (one_c * yy) + c;
  163.       M(1,2) = (one_c * yz) - xs;
  164. /*    M(1,3) = 0.0F; */
  165.  
  166.       M(2,0) = (one_c * zx) - ys;
  167.       M(2,1) = (one_c * yz) + xs;
  168.       M(2,2) = (one_c * zz) + c;
  169. /*    M(2,3) = 0.0F; */
  170.  
  171. /*
  172.       M(3,0) = 0.0F;
  173.       M(3,1) = 0.0F;
  174.       M(3,2) = 0.0F;
  175.       M(3,3) = 1.0F;
  176. */
  177.    }
  178. #undef M
  179.  
  180.    matrix_multf( mat, m, MAT_FLAG_ROTATION );
  181. }


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Fr Mär 24, 2006 01:17 
Offline
DGL Member

Registriert: Mi Mär 22, 2006 20:16
Beiträge: 22
Wohnort: Wuppertal
Danke, das war, was ich gesucht habe!
Eine Funktion, wo ich nur Punkt und eine Normale brauche. :)

Für denen, denen es interessiert, hier der von mir in Delphi übersetzte Code:


Code:
  1. type
  2.   float = double;
  3.   TPoint3DF = record x,y,z: float; end;
  4.   TMatrix4x4 = record
  5.     _11,_12,_13,_14: float;
  6.     _21,_22,_23,_24: float;
  7.     _31,_32,_33,_34: float;
  8.     _41,_42,_43,_44: float;
  9.   end;
  10.  

Code:
  1. function _RotN(n: TPoint3DF; d: float): TMatrix4x4;
  2. var
  3.   xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c, mag: float;
  4.   optimized: boolean;
  5.   m: TMatrix4x4;
  6. begin
  7. (*
  8.  * Generate a 4x4 transformation matrix from glRotate parameters, and
  9.  * post-multiply the input matrix by it.
  10.  *
  11.  * \author
  12.  * This function was contributed by Erich Boleyn (erich@uruk.org).
  13.  * Optimizations contributed by Rudolf Opalla (rudi@khm.de).
  14.  *
  15.  * Translated into Delphi by Stefan (slude007 [at] web [d0t] de)
  16. *)
  17.   s := Sin(d);
  18.   c := Cos(d);
  19.   optimized := False;
  20.   m._11 := 1.0; m._12 := 0.0; m._13 := 0.0; m._14 := 0.0;
  21.   m._21 := 0.0; m._22 := 1.0; m._23 := 0.0; m._24 := 0.0;
  22.   m._31 := 0.0; m._32 := 0.0; m._33 := 1.0; m._34 := 0.0;
  23.   m._41 := 0.0; m._42 := 0.0; m._43 := 0.0; m._44 := 1.0;
  24.   if n.x = 0 then
  25.   begin
  26.     if n.y = 0 then
  27.     begin
  28.       if n.z <> 0 then
  29.       begin
  30.         //Rotate only around Z axis
  31.         optimized := True;
  32.         m._11 := c;
  33.         m._22 := c;
  34.         if n.z < 0 then
  35.         begin
  36.           m._12 := s;
  37.           m._21 := -s;
  38.         end else
  39.         begin
  40.           m._12 := -s;
  41.           m._21 := s;
  42.         end;
  43.       end
  44.     end else
  45.     if n.z = 0 then
  46.     begin
  47.       //Rotate only around Y axis
  48.       optimized := True;
  49.       m._11 := c;
  50.       m._33 := c;
  51.       if n.y < 0 then
  52.       begin
  53.         m._13 := -s;
  54.         m._31 := s;
  55.       end else
  56.       begin
  57.         m._13 := s;
  58.         m._31 := -s;
  59.       end;
  60.     end;
  61.   end else
  62.   if n.y = 0 then
  63.   begin
  64.     if n.z = 0 then
  65.     begin
  66.       //Rotate only around X axis
  67.       optimized := True;
  68.       m._22 := c;
  69.       m._33 := c;
  70.       if (n.x < 0) then
  71.       begin
  72.         m._23 := s;
  73.         m._32 := -s;
  74.       end else
  75.       begin
  76.         m._23 := -s;
  77.         m._32 := s;
  78.       end;
  79.     end;
  80.   end;
  81.   if not optimized then
  82.   begin
  83.     mag := Sqrt(n.x*n.x + n.y*n.y + n.z*n.z);
  84.     if mag <= 0.0001 then exit;
  85.     n.x := n.x / mag;
  86.     n.y := n.y / mag;
  87.     n.z := n.z / mag;
  88. (*
  89.        *     Arbitrary axis rotation matrix.
  90.        *
  91.        *  This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
  92.        *  like so:  Rz * Ry * T * Ry' * Rz'.  T is the final rotation
  93.        *  (which is about the X-axis), and the two composite transforms
  94.        *  Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
  95.        *  from the arbitrary axis to the X-axis then back.  They are
  96.        *  all elementary rotations.
  97.        *
  98.        *  Rz' is a rotation about the Z-axis, to bring the axis vector
  99.        *  into the x-z plane.  Then Ry' is applied, rotating about the
  100.        *  Y-axis to bring the axis vector parallel with the X-axis.  The
  101.        *  rotation about the X-axis is then performed.  Ry and Rz are
  102.        *  simply the respective inverse transforms to bring the arbitrary
  103.        *  axis back to it's original orientation.  The first transforms
  104.        *  Rz' and Ry' are considered inverses, since the data from the
  105.        *  arbitrary axis gives you info on how to get to it, not how
  106.        *  to get away from it, and an inverse must be applied.
  107.        *
  108.        *  The basic calculation used is to recognize that the arbitrary
  109.        *  axis vector (x, y, z), since it is of unit length, actually
  110.        *  represents the sines and cosines of the angles to rotate the
  111.        *  X-axis to the same orientation, with theta being the angle about
  112.        *  Z and phi the angle about Y (in the order described above)
  113.        *  as follows:
  114.        *
  115.        *  cos ( theta ) = x / sqrt ( 1 - z^2 )
  116.        *  sin ( theta ) = y / sqrt ( 1 - z^2 )
  117.        *
  118.        *  cos ( phi ) = sqrt ( 1 - z^2 )
  119.        *  sin ( phi ) = z
  120.        *
  121.        *  Note that cos ( phi ) can further be inserted to the above
  122.        *  formulas:
  123.        *
  124.        *  cos ( theta ) = x / cos ( phi )
  125.        *  sin ( theta ) = y / sin ( phi )
  126.        *
  127.        *  ...etc.  Because of those relations and the standard trigonometric
  128.        *  relations, it is pssible to reduce the transforms down to what
  129.        *  is used below.  It may be that any primary axis chosen will give the
  130.        *  same results (modulo a sign convention) using thie method.
  131.        *
  132.        *  Particularly nice is to notice that all divisions that might
  133.        *  have caused trouble when parallel to certain planes or
  134.        *  axis go away with care paid to reducing the expressions.
  135.        *  After checking, it does perform correctly under all cases, since
  136.        *  in all the cases of division where the denominator would have
  137.        *  been zero, the numerator would have been zero as well, giving
  138.        *  the expected result.
  139. *)
  140.     xx := n.x * n.x;
  141.     yy := n.y * n.y;
  142.     zz := n.z * n.z;
  143.     xy := n.x * n.y;
  144.     yz := n.y * n.z;
  145.     zx := n.z * n.x;
  146.     xs := n.x * s;
  147.     ys := n.y * s;
  148.     zs := n.z * s;
  149.     one_c := 1.0 - c;
  150.     m._11 := (one_c * xx) + c;
  151.     m._12 := (one_c * xy) - zs;
  152.     m._13 := (one_c * zx) + ys;
  153.  
  154.     m._21 := (one_c * xy) + zs;
  155.     m._22 := (one_c * yy) + c;
  156.     m._23 := (one_c * yz) - xs;
  157.  
  158.     m._31 := (one_c * zx) - ys;
  159.     m._32 := (one_c * yz) + xs;
  160.     m._33 := (one_c * zz) + c;
  161.   end;
  162.   Result := m;
  163. end;
  164.  


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 5 Beiträge ] 
Foren-Übersicht » Programmierung » Allgemein


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 11 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:  
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.010s | 14 Queries | GZIP : On ]