Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Platform/OpenGL/ILGLU.cs @ 10102

Last change on this file since 10102 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 7.2 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using OpenTK;
42using OpenTK.Graphics;
43using OpenTK.Math;
44using ILNumerics;
45using ILNumerics.Drawing;
46
47/// <summary>
48/// This is a managed replacement for the OpenGL utilities library
49/// </summary>
50public partial class ILGLU {
51 
52  /// <summary>
53  /// Looks at eye, target, top and moveOffset.
54  /// </summary>
55  /// <param name='eye'>
56  /// Position of camera
57  /// </param>
58  /// <param name='target'>
59  /// Point the camera aims at
60  /// </param>
61  /// <param name='top'>
62  /// 'up' vector of the camera
63  /// </param>
64  /// <param name='moveOffset'>
65  /// offset for moving the whole scene slightly out of center
66  /// </param>
67  /// <remarks>Reference: http://www.opengl.org/sdk/docs/man/xhtml/gluLookAt.xml </remarks>
68  public static void LookAt (ILPoint3Df eye, ILPoint3Df target, ILPoint3Df top, ILPoint3Df moveOffset)
69  {
70    ILPoint3Df f = ILPoint3Df.normalize(target - eye);
71      ILPoint3Df s = ILPoint3Df.cross(f, top);
72      ILPoint3Df u = ILPoint3Df.cross(s, f);
73    // todo: cache this temp matrix!
74      double[] tmpMat = new double[16];
75      tmpMat[0] = s.X;  tmpMat[4] = s.Y;  tmpMat[8] = s.Z;  tmpMat[12] = 0;
76      tmpMat[1] = u.X;  tmpMat[5] = u.Y;  tmpMat[9] = u.Z;  tmpMat[13] = 0;
77      tmpMat[2] = -f.X;   tmpMat[6] = -f.Y;   tmpMat[10] = -f.Z;  tmpMat[14] = 0;
78      tmpMat[3] = 0;    tmpMat[7] = 0;    tmpMat[11] = 0;     tmpMat[15] = 1;
79      GL.MultMatrix(tmpMat);
80      s = moveOffset - eye;
81      GL.Translate( s.X,s.Y,s.Z);
82  }
83 
84  /// <summary>
85  /// Get the screen coords of a point from world space coords
86  /// </summary>
87  /// <param name='worldCoords'>
88  /// World coords.
89  /// </param>
90  /// <param name='mm'>
91  /// Modelview matrix (as 1d double vector)
92  /// </param>
93  /// <param name='pm'>
94  /// Projection matrix (as 1d double vector)
95  /// </param>
96  /// <param name='vm'>
97  /// View matrix (as 1d double vector)
98  /// </param>
99  /// <param name='screen'>
100  /// [Output] point with screen coords (and a z value, see OpenGL man pages)
101  /// </param>
102  /// <remarks>Reference: http://www.opengl.org/sdk/docs/man/xhtml/gluProject.xml </remarks>
103  public static void Project(ILPoint3Df worldCoords, double[] mm, double[] pm, int[] vm, out ILPoint3Df screen) {
104    screen = new ILPoint3Df();
105    screen.X = (float)((pm[0] * mm[00] + pm[4] * mm[01] + pm[8] * mm[02] + pm[12] * mm[03]) * worldCoords.X
106          +(pm[0] * mm[04] + pm[4] * mm[05] + pm[8] * mm[06] + pm[12] * mm[07]) * worldCoords.Y
107          +(pm[0] * mm[08] + pm[4] * mm[09] + pm[8] * mm[10] + pm[12] * mm[11]) * worldCoords.Z
108          +(pm[0] * mm[12] + pm[4] * mm[13] + pm[8] * mm[14] + pm[12] * mm[15])); 
109   
110    screen.Y = (float)((pm[1] * mm[00] + pm[5] * mm[01] + pm[9] * mm[02] + pm[13] * mm[03]) * worldCoords.X
111          +(pm[1] * mm[04] + pm[5] * mm[05] + pm[9] * mm[06] + pm[13] * mm[07]) * worldCoords.Y
112          +(pm[1] * mm[08] + pm[5] * mm[09] + pm[9] * mm[10] + pm[13] * mm[11]) * worldCoords.Z
113          +(pm[1] * mm[12] + pm[5] * mm[13] + pm[9] * mm[14] + pm[13] * mm[15])); 
114
115    screen.Z = (float)((pm[2] * mm[00] + pm[6] * mm[01] + pm[10] * mm[02] + pm[14] * mm[03]) * worldCoords.X
116          +(pm[2] * mm[04] + pm[6] * mm[05] + pm[10] * mm[06] + pm[14] * mm[07]) * worldCoords.Y
117          +(pm[2] * mm[08] + pm[6] * mm[09] + pm[10] * mm[10] + pm[14] * mm[11]) * worldCoords.Z
118          +(pm[2] * mm[12] + pm[6] * mm[13] + pm[10] * mm[14] + pm[14] * mm[15])); 
119   
120    screen.X = vm[0] + (vm[2] * (screen.X + 1f)) / 2f;
121    screen.Y = vm[1] + (vm[3] * (screen.Y + 1f)) / 2f;
122    screen.Z = (screen.Z + 1) / 2;
123  }
124 
125  /// <summary>
126  /// Convert screen coordinates into world coordinates
127  /// </summary>
128  /// <param name='screen'>
129  /// Screen coordinates X,Y (and Z: see the OpenGL manual)
130  /// </param>
131  /// <param name='mm'>
132  /// Modelview matrix
133  /// </param>
134  /// <param name='pm'>
135  /// Projection matrix
136  /// </param>
137  /// <param name='vm'>
138  /// View matrix
139  /// </param>
140  /// <param name='world'>
141  /// [Output] World coordinates
142  /// </param>
143  /// <remarks>Reference: http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml </remarks>
144  public static void UnProject(ILPoint3Df screen, double[] mm, double[] pm, int[] vm, out ILPoint3Df world) {
145    Matrix4 p = new Matrix4 (new Vector4((float)pm[0],(float)pm[4],(float)pm[8],(float)pm[12]),
146                 new Vector4((float)pm[1],(float)pm[5],(float)pm[9],(float)pm[13]),
147                 new Vector4((float)pm[2],(float)pm[6],(float)pm[10],(float)pm[14]),
148                 new Vector4((float)pm[3],(float)pm[7],(float)pm[11],(float)pm[15]));
149   
150    Matrix4 m = new Matrix4 (new Vector4((float)mm[0],(float)mm[4],(float)mm[8],(float)mm[12]),
151                 new Vector4((float)mm[1],(float)mm[5],(float)mm[9],(float)mm[13]),
152                 new Vector4((float)mm[2],(float)mm[6],(float)mm[10],(float)mm[14]),
153                 new Vector4((float)mm[3],(float)mm[7],(float)mm[11],(float)mm[15]));
154   
155    Vector4 normScreen = new Vector4(
156        2f * ( screen.X - vm[0]) / vm[2] - 1f,
157        2f * ( screen.Y - vm[1]) / vm[3] - 1f,
158        2f * screen.Z - 1f,
159        1);
160    Matrix4 p_m = Matrix4.Mult(p, m);
161    Matrix4 inv_mp = Matrix4.Invert( p_m );
162   
163    world.X = inv_mp.Row0.X*normScreen.X+inv_mp.Row0.Y*normScreen.Y+inv_mp.Row0.Z*normScreen.Z+inv_mp.Row0.W;
164    world.Y = inv_mp.Row1.X*normScreen.X+inv_mp.Row1.Y*normScreen.Y+inv_mp.Row1.Z*normScreen.Z+inv_mp.Row1.W;
165    world.Z = inv_mp.Row2.X*normScreen.X+inv_mp.Row2.Y*normScreen.Y+inv_mp.Row2.Z*normScreen.Z+inv_mp.Row2.W;
166   
167  }
168}
Note: See TracBrowser for help on using the repository browser.