Migration from libeye to Gutamatics.

Updated credits.
This commit is contained in:
Mari the Deer 2021-04-02 19:52:13 +02:00
commit 3b3918de8a
22 changed files with 1022 additions and 967 deletions

View file

@ -1,115 +0,0 @@
/*
Quaternion math helper class.
Provides only the bare minimum needed to implement 6DOF functionality. For a
more extensive implementation, please refer to Gutamatics.
Copyright (c)2018-2021 Marisa Kirisame, UnSX Team
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.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
Class swwm_Quat
{
protected double W, X, Y, Z;
swwm_Quat init( double w, double x, double y, double z )
{
self.W = w;
self.X = x;
self.Y = y;
self.Z = z;
return self;
}
void copy( swwm_Quat q )
{
W = q.W;
X = q.X;
Y = q.Y;
Z = q.Z;
}
static swwm_Quat create( double w, double x, double y, double z )
{
return new("swwm_Quat").init(w,x,y,z);
}
static swwm_Quat create_axis( Vector3 axis, double theta )
{
double scale = axis dot axis;
if ( scale < double.epsilon ) return swwm_Quat.create(1,0,0,0);
theta *= 0.5;
double f = sin(theta)/sqrt(scale);
return swwm_Quat.create(cos(theta),axis.x*f,axis.y*f,axis.z*f);
}
static swwm_Quat create_euler( double pitch, double yaw, double roll )
{
swwm_Quat zrot = swwm_Quat.create_axis((0,0,1),yaw);
swwm_Quat yrot = swwm_Quat.create_axis((0,1,0),pitch);
swwm_Quat xrot = swwm_Quat.create_axis((1,0,0),roll);
swwm_Quat sum = zrot.qmul(yrot);
sum = sum.qmul(xrot);
return sum;
}
// copied here since Actor.Normalize180 is not (yet) clearscope
static double Normalize180( double ang )
{
ang = ang%360;
ang = (ang+360)%360;
if ( ang > 180 ) ang -= 360;
return ang;
}
double, double, double to_euler()
{
double stest = z*x-w*y;
double yawY = 2*(w*z+x*y);
double yawX = 1-2*(y*y+z*z);
double st = 0.4999995;
double pitch = 0;
double yaw = 0;
double roll = 0;
if ( stest <= -st )
{
pitch = 90;
yaw = atan2(yawY,yawX);
roll = Normalize180(yaw+(2*atan2(x,w)));
}
else if ( stest > st )
{
pitch = -90;
yaw = atan2(yawY,yawX);
roll = Normalize180(yaw+(2*atan2(x,w)));
}
else
{
pitch = -asin(2*stest);
yaw = atan2(yawY,yawX);
roll = atan2(2*(w*x+y*z),(1-2*(x*x+y*y)));
}
return pitch, yaw, roll;
}
swwm_Quat qmul( swwm_Quat q ) const
{
return swwm_Quat.create(w*q.w-x*q.x-y*q.y-z*q.z,w*q.x+x*q.w+y*q.z-z*q.y,w*q.y+y*q.w+z*q.x-x*q.z,w*q.z+z*q.w+x*q.y-y*q.x);
}
}

View file

@ -22,6 +22,12 @@ Class SWWMAchievement
bool hasformat;
}
Struct SWWMProjectionData
{
swwm_Matrix wtc;
int viewx, viewy, vieww, viewh;
}
Class SWWMUtility
{
// loaded from a file, neater than having a bunch of static arrays
@ -137,6 +143,48 @@ Class SWWMUtility
cv.SetFloat(cval+inc);
}
// gutamatics caching
static void PrepareProjData( out SWWMProjectionData d, Vector3 viewpos, double angle, double pitch, double roll, double fov )
{
double aspect = Screen.GetAspectRatio();
// vertical fov
double fovratio = (aspect>=1.3)?1.333333:aspect;
double fovy = 2.*atan(tan(clamp(fov,5,170)/2.)/fovratio);
// world→clip matrix
swwm_Matrix view = swwm_Matrix.view(viewpos,angle,pitch,roll);
swwm_Matrix perp = swwm_Matrix.perspective(fovy,aspect,5,65535);
d.wtc = perp.multiplyMatrix(view);
// screen coord data
int sblocks = CVar.FindCVar('screenblocks').GetInt();
int viewx, viewy, vieww, viewh;
[viewx, viewy, vieww, viewh] = Screen.GetViewWindow();
int sh = Screen.GetHeight();
int h = sh;
if ( sblocks < 10 ) h = (sblocks*sh/10)&~7;
int bottom = sh-(h+viewy-((h-viewh)/2));
d.viewx = viewx;
d.viewy = sh-bottom-h;
d.vieww = vieww;
d.viewh = h;
}
static Vector3 ProjectPoint( SWWMProjectionData d, Vector3 worldpos )
{
return d.wtc.multiplyVector3(worldpos).asVector3();
}
static Vector2 NDCToViewport( SWWMProjectionData d, Vector3 ndc )
{
return (d.viewx,d.viewy)+(((ndc.x+1)*d.vieww)/2,((-ndc.y+1)*d.viewh)/2);
}
// checks if a point is inside the viewport
static bool TestScreenBounds( SWWMProjectionData d, Vector2 vpos )
{
return ((vpos.x == clamp(vpos.x,d.viewx,d.viewx+d.vieww))
&& (vpos.y == clamp(vpos.y,d.viewy,d.viewy+d.viewh)));
}
// thanks zscript
static clearscope double fract( double a )
{