changeset 574:911297a277c4

AffineTransform2D::ConvertToOpenGLMatrix()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 18 Apr 2019 18:06:29 +0200
parents adc1be326b62
children 919226caca82
files Framework/Toolbox/AffineTransform2D.cpp Framework/Toolbox/AffineTransform2D.h
diffstat 2 files changed, 60 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Toolbox/AffineTransform2D.cpp	Thu Apr 18 17:29:44 2019 +0200
+++ b/Framework/Toolbox/AffineTransform2D.cpp	Thu Apr 18 18:06:29 2019 +0200
@@ -90,6 +90,46 @@
   }
 
 
+  void AffineTransform2D::ConvertToOpenGLMatrix(float target[16],
+                                                unsigned int canvasWidth,
+                                                unsigned int canvasHeight) const
+  {
+    const AffineTransform2D t = AffineTransform2D::Combine(
+      CreateOpenGLClipspace(canvasWidth, canvasHeight), *this);
+    
+    const Matrix source = t.GetHomogeneousMatrix();
+  
+    if (source.size1() != 3 ||
+        source.size2() != 3)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+    }
+
+    // "z" must be in the [-1,1] range, otherwise the texture does not show up
+    float z = 0;
+
+    // Embed the 3x3 affine transform of the 2D plane into a 4x4
+    // matrix (3D) for OpenGL. The matrix must be transposed.
+
+    target[0] = static_cast<float>(source(0, 0)); 
+    target[1] = static_cast<float>(source(1, 0)); 
+    target[2] = 0; 
+    target[3] = static_cast<float>(source(2, 0));
+    target[4] = static_cast<float>(source(0, 1)); 
+    target[5] = static_cast<float>(source(1, 1));
+    target[6] = 0;
+    target[7] = static_cast<float>(source(2, 1));
+    target[8] = 0; 
+    target[9] = 0; 
+    target[10] = -1; 
+    target[11] = 0;
+    target[12] = static_cast<float>(source(0, 2)); 
+    target[13] = static_cast<float>(source(1, 2));
+    target[14] = -z;
+    target[15] = static_cast<float>(source(2, 2));
+  }
+
+
   AffineTransform2D AffineTransform2D::Invert(const AffineTransform2D& a)
   {
     AffineTransform2D t;
@@ -163,4 +203,17 @@
 
     return t;
   }
+
+
+  AffineTransform2D AffineTransform2D::CreateOpenGLClipspace(unsigned int canvasWidth,
+                                                             unsigned int canvasHeight)
+  {
+    AffineTransform2D t;
+    t.matrix_(0, 0) = 2.0 / static_cast<double>(canvasWidth);
+    t.matrix_(0, 2) = -1.0;
+    t.matrix_(1, 1) = -2.0 / static_cast<double>(canvasHeight);
+    t.matrix_(1, 2) = 1.0;
+    
+    return t;
+  }
 }
--- a/Framework/Toolbox/AffineTransform2D.h	Thu Apr 18 17:29:44 2019 +0200
+++ b/Framework/Toolbox/AffineTransform2D.h	Thu Apr 18 18:06:29 2019 +0200
@@ -56,6 +56,10 @@
                const Orthanc::ImageAccessor& source,
                ImageInterpolation interpolation,
                bool clear) const;
+
+    void ConvertToOpenGLMatrix(float target[16],
+                               unsigned int canvasWidth,
+                               unsigned int canvasHeight) const;
     
     static AffineTransform2D Invert(const AffineTransform2D& a);
 
@@ -78,5 +82,8 @@
                                            double sy);
     
     static AffineTransform2D CreateRotation(double angle);
+
+    static AffineTransform2D CreateOpenGLClipspace(unsigned int canvasWidth,
+                                                   unsigned int canvasHeight);
   };
 }