///
/// This file is part of ILNumerics Community Edition.
///
/// ILNumerics Community Edition - high performance computing for applications.
/// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
///
/// ILNumerics Community Edition is free software: you can redistribute it and/or modify
/// it under the terms of the GNU General Public License version 3 as published by
/// the Free Software Foundation.
///
/// ILNumerics Community Edition is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with ILNumerics Community Edition. See the file License.txt in the root
/// of your distribution package. If not, see .
///
/// In addition this software uses the following components and/or licenses:
///
/// =================================================================================
/// The Open Toolkit Library License
///
/// Copyright (c) 2006 - 2009 the Open Toolkit library.
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights to
/// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
/// the Software, and to permit persons to whom the Software is furnished to do
/// so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all
/// copies or substantial portions of the Software.
///
/// =================================================================================
///
using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Math;
using ILNumerics;
using ILNumerics.Drawing;
///
/// This is a managed replacement for the OpenGL utilities library
///
public partial class ILGLU {
///
/// Looks at eye, target, top and moveOffset.
///
///
/// Position of camera
///
///
/// Point the camera aims at
///
///
/// 'up' vector of the camera
///
///
/// offset for moving the whole scene slightly out of center
///
/// Reference: http://www.opengl.org/sdk/docs/man/xhtml/gluLookAt.xml
public static void LookAt (ILPoint3Df eye, ILPoint3Df target, ILPoint3Df top, ILPoint3Df moveOffset)
{
ILPoint3Df f = ILPoint3Df.normalize(target - eye);
ILPoint3Df s = ILPoint3Df.cross(f, top);
ILPoint3Df u = ILPoint3Df.cross(s, f);
// todo: cache this temp matrix!
double[] tmpMat = new double[16];
tmpMat[0] = s.X; tmpMat[4] = s.Y; tmpMat[8] = s.Z; tmpMat[12] = 0;
tmpMat[1] = u.X; tmpMat[5] = u.Y; tmpMat[9] = u.Z; tmpMat[13] = 0;
tmpMat[2] = -f.X; tmpMat[6] = -f.Y; tmpMat[10] = -f.Z; tmpMat[14] = 0;
tmpMat[3] = 0; tmpMat[7] = 0; tmpMat[11] = 0; tmpMat[15] = 1;
GL.MultMatrix(tmpMat);
s = moveOffset - eye;
GL.Translate( s.X,s.Y,s.Z);
}
///
/// Get the screen coords of a point from world space coords
///
///
/// World coords.
///
///
/// Modelview matrix (as 1d double vector)
///
///
/// Projection matrix (as 1d double vector)
///
///
/// View matrix (as 1d double vector)
///
///
/// [Output] point with screen coords (and a z value, see OpenGL man pages)
///
/// Reference: http://www.opengl.org/sdk/docs/man/xhtml/gluProject.xml
public static void Project(ILPoint3Df worldCoords, double[] mm, double[] pm, int[] vm, out ILPoint3Df screen) {
screen = new ILPoint3Df();
screen.X = (float)((pm[0] * mm[00] + pm[4] * mm[01] + pm[8] * mm[02] + pm[12] * mm[03]) * worldCoords.X
+(pm[0] * mm[04] + pm[4] * mm[05] + pm[8] * mm[06] + pm[12] * mm[07]) * worldCoords.Y
+(pm[0] * mm[08] + pm[4] * mm[09] + pm[8] * mm[10] + pm[12] * mm[11]) * worldCoords.Z
+(pm[0] * mm[12] + pm[4] * mm[13] + pm[8] * mm[14] + pm[12] * mm[15]));
screen.Y = (float)((pm[1] * mm[00] + pm[5] * mm[01] + pm[9] * mm[02] + pm[13] * mm[03]) * worldCoords.X
+(pm[1] * mm[04] + pm[5] * mm[05] + pm[9] * mm[06] + pm[13] * mm[07]) * worldCoords.Y
+(pm[1] * mm[08] + pm[5] * mm[09] + pm[9] * mm[10] + pm[13] * mm[11]) * worldCoords.Z
+(pm[1] * mm[12] + pm[5] * mm[13] + pm[9] * mm[14] + pm[13] * mm[15]));
screen.Z = (float)((pm[2] * mm[00] + pm[6] * mm[01] + pm[10] * mm[02] + pm[14] * mm[03]) * worldCoords.X
+(pm[2] * mm[04] + pm[6] * mm[05] + pm[10] * mm[06] + pm[14] * mm[07]) * worldCoords.Y
+(pm[2] * mm[08] + pm[6] * mm[09] + pm[10] * mm[10] + pm[14] * mm[11]) * worldCoords.Z
+(pm[2] * mm[12] + pm[6] * mm[13] + pm[10] * mm[14] + pm[14] * mm[15]));
screen.X = vm[0] + (vm[2] * (screen.X + 1f)) / 2f;
screen.Y = vm[1] + (vm[3] * (screen.Y + 1f)) / 2f;
screen.Z = (screen.Z + 1) / 2;
}
///
/// Convert screen coordinates into world coordinates
///
///
/// Screen coordinates X,Y (and Z: see the OpenGL manual)
///
///
/// Modelview matrix
///
///
/// Projection matrix
///
///
/// View matrix
///
///
/// [Output] World coordinates
///
/// Reference: http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml
public static void UnProject(ILPoint3Df screen, double[] mm, double[] pm, int[] vm, out ILPoint3Df world) {
Matrix4 p = new Matrix4 (new Vector4((float)pm[0],(float)pm[4],(float)pm[8],(float)pm[12]),
new Vector4((float)pm[1],(float)pm[5],(float)pm[9],(float)pm[13]),
new Vector4((float)pm[2],(float)pm[6],(float)pm[10],(float)pm[14]),
new Vector4((float)pm[3],(float)pm[7],(float)pm[11],(float)pm[15]));
Matrix4 m = new Matrix4 (new Vector4((float)mm[0],(float)mm[4],(float)mm[8],(float)mm[12]),
new Vector4((float)mm[1],(float)mm[5],(float)mm[9],(float)mm[13]),
new Vector4((float)mm[2],(float)mm[6],(float)mm[10],(float)mm[14]),
new Vector4((float)mm[3],(float)mm[7],(float)mm[11],(float)mm[15]));
Vector4 normScreen = new Vector4(
2f * ( screen.X - vm[0]) / vm[2] - 1f,
2f * ( screen.Y - vm[1]) / vm[3] - 1f,
2f * screen.Z - 1f,
1);
Matrix4 p_m = Matrix4.Mult(p, m);
Matrix4 inv_mp = Matrix4.Invert( p_m );
world.X = inv_mp.Row0.X*normScreen.X+inv_mp.Row0.Y*normScreen.Y+inv_mp.Row0.Z*normScreen.Z+inv_mp.Row0.W;
world.Y = inv_mp.Row1.X*normScreen.X+inv_mp.Row1.Y*normScreen.Y+inv_mp.Row1.Z*normScreen.Z+inv_mp.Row1.W;
world.Z = inv_mp.Row2.X*normScreen.X+inv_mp.Row2.Y*normScreen.Y+inv_mp.Row2.Z*normScreen.Z+inv_mp.Row2.W;
}
}