daily uodate on 20240411 by nitload youmu, camera complete

This commit is contained in:
YoumuLiu
2024-04-11 02:15:22 +08:00
parent e1e416bb41
commit f0f80f72f1
144 changed files with 111879 additions and 1249 deletions
+69 -89
View File
@@ -43,30 +43,31 @@ namespace StgSharp.Graphic
public sealed class Camera : IglConvertable
{
Uniform<Matrix44>[] convertedUniform ;
private Uniform<Matrix44> convertedUniform ;
glHandleSet uniformHandle;
internal bool _isLookAtAvailable;
internal Matrix44 cameraAtt;
internal vec3d cameraCoord;
internal vec3d _target, up, cameraCoord;
internal Matrix44 _lookAt;
internal Matrix44 _projection;
public Camera()
{
cameraAtt = Matrix44.Unit;
cameraCoord = Vec3d.Unit;
_target = Vec3d.Zero;
up = Vec3d.Zero;
cameraCoord = new vec3d(0,0,1);
_isLookAtAvailable = false;
_lookAt = Matrix44.Unit;
convertedUniform = new Uniform<Matrix44>[2];
_projection = new Matrix44();
}
public Camera(vec3d position, vec3d target, vec3d up) : this()
{
SetLookAt(position, target, up);
SetViewDirection(position, target, up);
}
public Matrix44 LookAt
public Matrix44 View
{
get
{
@@ -74,11 +75,9 @@ namespace StgSharp.Graphic
{
return _lookAt;
}
Matrix44 att = cameraAtt;
att.Colum1 = Linear.Normalize(att.Colum1);
Matrix44 coord = Matrix44.Unit;
coord.Colum3.vec -= cameraCoord.vec;
_lookAt = att.Transpose * coord;
Matrix44 move = Matrix44.Unit;
move.Colum3.vec -= cameraCoord.vec;
_lookAt = cameraAtt.Transpose * move;
_isLookAtAvailable = true;
return _lookAt;
}
@@ -102,28 +101,36 @@ namespace StgSharp.Graphic
cameraCoord.Y -= distance;
}
public void Perspective(Radius fovy, Vec2d size, float zNear, float zFar)
public void SetViewRange(Radius fovRadius, vec2d size, vec2d offset, (float front, float back) dephRange)
{
if (fovy == Radius.Zero)
{
fovy = new Radius(0.000001f);
}
if (zFar == zNear)
{
return;
}
float
distance = _target.GetLength(),
near = distance - dephRange.front,
far = distance + dephRange.back,
offsetX = offset.X,
offsetY = offset.Y,
width = Scaler.Abs(GeometryScaler.Tan(fovRadius / 2) * near *2),
height = width * size.Y / size.X;
float rad = fovy._radius;
float aspect = size.X / size.Y;
float tanHalfFovy = Scaler.Tan(rad / 2.0f);
tanHalfFovy = tanHalfFovy==float.PositiveInfinity? float.MaxValue : tanHalfFovy;
tanHalfFovy = tanHalfFovy == float.NegativeInfinity ? float.MinValue : tanHalfFovy;
_projection.Colum0.X = 2 * near / width;
_projection.Colum1.Y = 2 * near / height;
_projection.Colum2.X = 2 * offsetX / width;
_projection.Colum2.Y = 2 * offsetY / height;
_projection.Colum2.Z = (far + near) / (near - far);
_projection.Colum2.W = -1;
_projection.Colum3.Z = 2 * near * far / (near - far);
_projection.Colum0.X = 1.0f / (aspect * tanHalfFovy);
_projection.Colum1.Y = 1.0f / tanHalfFovy;
_projection.Colum2.Z = (-(zFar + zNear)) / (zFar - zNear);
_projection.Colum3.W = -1.0f;
_projection.Colum2.Y = (-(2.0f * zFar * zNear)) / (zFar - zNear);
}
public void Test(params vec4d[] vec)
{
Console.WriteLine(View);
Console.WriteLine(Projection);
Console.WriteLine(Projection * View);
foreach (var item in vec)
{
Console.Write($"{Projection * View * item};");
}
}
public void Pitch(Radius r)
@@ -142,27 +149,42 @@ namespace StgSharp.Graphic
cameraAtt.Colum2.vec = partialCoord.colum1.vec;
}
public void SetLookAt(vec3d position, vec3d target, vec3d up)
public void SetViewDirection(vec3d position, vec3d target, vec3d up)
{
vec3d direction = target - position;
vec3d direction = position - target;
if (position == cameraCoord && this.up == up && cameraAtt.Colum2.vec == direction.vec)
{
//no difference with precious value
return;
}
if (direction.GetLength() == 0)
{
//deadlock
throw new ArgumentException();
}
if (Vec3d.IsParallel(direction, up))
{
throw new ArgumentException();
}
_target = direction;
cameraCoord = position;
this.up = up;
_isLookAtAvailable = false;
Linear.Normalize(ref direction, ref up);
vec3d right = Linear.Cross(up, direction);
cameraAtt.Colum0.vec = direction.vec;
vec3d right = Linear.Normalize(Linear.Cross(up,direction));
cameraAtt.Colum0.vec = right.vec;
cameraAtt.Colum1.vec = up.vec;
cameraAtt.Colum2.vec = right.vec;
cameraAtt.Colum2.vec = direction.vec;
}
public void DisplayGLtypeDefinition()
{
Console.WriteLine(
$"struct camera\n" +
"\t{{\n" +
"\tmatrix4x4 Perspective;\n" +
"\tmatrix4x4 View;\n" +
"\t}}\n"
);
Console.WriteLine("uniform matrix4x4 cameraName;");
}
/// <summary>
@@ -176,48 +198,11 @@ namespace StgSharp.Graphic
/// <exception cref="ArgumentException"></exception>
public unsafe void GainAllUniforms(ShaderProgram source, params string[] uniformName)
{
GLcontext* gl = GL.api;
uniformHandle = new glHandleSet(2);
byte[] name1;
byte[] name2;
Console.WriteLine(uniformName.Length);
switch (uniformName.Length)
if (uniformName.Length!=1)
{
case 2:
name1 = Encoding.UTF8.GetBytes(uniformName[0]);
name2 = Encoding.UTF8.GetBytes(uniformName[1]);
fixed (byte* namePtr = name1)
{
uniformHandle[(uint)0].Value = gl->glGetUniformLocation(
source.handle.Value, namePtr);
}
fixed (byte* namePtr = name2)
{
uniformHandle[(uint)1].Value = gl->glGetUniformLocation(
source.handle.Value, namePtr);
}
convertedUniform[0] = Uniform<Matrix44>.FromHandle(uniformHandle[(uint)0]);
convertedUniform[1] = Uniform<Matrix44>.FromHandle(uniformHandle[(uint)1]);
break;
case 1:
name1 = Encoding.UTF8.GetBytes($"{uniformName[0]}.Perspective");
name2 = Encoding.UTF8.GetBytes($"{uniformName[0]}.View");
fixed (byte* namePtr = name1)
{
uniformHandle[(uint)0].Value = gl->glGetUniformLocation(
source.handle.Value, namePtr);
}
fixed (byte* namePtr = name2)
{
uniformHandle[(uint)1].Value = gl->glGetUniformLocation(
source.handle.Value, namePtr);
}
convertedUniform[0] = Uniform<Matrix44>.FromHandle(uniformHandle[(uint)0]);
convertedUniform[1] = Uniform<Matrix44>.FromHandle(uniformHandle[(uint)1]);
break;
default:
throw new ArgumentException();
throw new ArgumentException();
}
convertedUniform = source.GetUniform<Matrix44>(uniformName[0]);
}
ShaderStruct IglConvertable.GetConvertedGLtype()
@@ -227,16 +212,11 @@ namespace StgSharp.Graphic
public unsafe void SetAllUniforms()
{
GL.SetValue(convertedUniform[0], _projection);
GL.SetValue(convertedUniform[1], LookAt);
GL.SetValue(convertedUniform, Projection * View);
}
~Camera()
{
if (uniformHandle != null)
{
uniformHandle.Release();
}
}
}
+4 -4
View File
@@ -44,10 +44,10 @@ namespace StgSharp.Math
[FieldOffset(0)] internal Mat4 mat;
[FieldOffset(4 * 4 * sizeof(float))] internal Mat2 transpose;
[FieldOffset(0)] public Vec2d Colum0;
[FieldOffset(16)] public Vec2d Colum1;
[FieldOffset(32)] public Vec2d Colum2;
[FieldOffset(48)] public Vec2d Colum3;
[FieldOffset(0)] public vec2d Colum0;
[FieldOffset(16)] public vec2d Colum1;
[FieldOffset(32)] public vec2d Colum2;
[FieldOffset(48)] public vec2d Colum3;
internal Matrix24(
Vector4 c0,
+1 -1
View File
@@ -114,7 +114,7 @@ namespace StgSharp.Math
}
}
public vec3d VerticalVecMultiply(Vec2d vec)
public vec3d VerticalVecMultiply(vec2d vec)
{
return (colum0 * vec.X) + (colum1 * vec.Y);
}
+31 -2
View File
@@ -42,7 +42,7 @@ namespace StgSharp.Math
[StructLayout(LayoutKind.Explicit, Size = (2 * 16 * sizeof(float)) + sizeof(bool), Pack = 16)]
public unsafe struct Matrix44 : IEquatable<Matrix44>
public unsafe struct Matrix44 : IEquatable<Matrix44>, IMat
{
[FieldOffset(2 * 64 * sizeof(float))] internal bool isTransposed;
@@ -152,6 +152,36 @@ namespace StgSharp.Math
0, 0, 0, 1
);
public static Matrix44 XNegativeUnit
=> new Matrix44(
-1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
public static Matrix44 YNegativeUnit
=> new Matrix44(
1, 0, 0, 0,
0, -1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
public static Matrix44 ZNegativeUnit
=> new Matrix44(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, -1, 0,
0, 0, 0, 1
);
public static Matrix44 WNegativeUnit
=> new Matrix44(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, -1
);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe float Det()
{
@@ -295,7 +325,6 @@ namespace StgSharp.Math
Vector4.Dot(left.transpose.colum3, right.mat.colum1),
Vector4.Dot(left.transpose.colum3, right.mat.colum2),
Vector4.Dot(left.transpose.colum3, right.mat.colum3)
);
}
@@ -33,7 +33,7 @@ namespace StgSharp.Math
public static unsafe partial class Linear
{
public static float Cross(Vec2d vec1, Vec2d vec2)
public static float Cross(vec2d vec1, vec2d vec2)
{
return (vec1.X * vec2.Y) - (vec1.Y * vec2.X);
}
@@ -33,7 +33,7 @@ namespace StgSharp.Math
public static unsafe partial class Linear
{
public static float GetAngle(Vec2d vec1, Vec2d vec2)
public static float GetAngle(vec2d vec1, vec2d vec2)
{
return Scaler.ACos(1
//vec1.multiple(vec2)
@@ -35,19 +35,12 @@ namespace StgSharp.Math
public static float GetLength(this vec3d vec)
{
return Scaler.Sqrt(
(vec.X * vec.X) +
(vec.Y * vec.Y) +
(vec.Z * vec.Z)
);
return Scaler.Sqrt(vec.Dot(vec));
}
public static float GetLength(this Vec2d vec)
public static float GetLength(this vec2d vec)
{
return Scaler.Sqrt(
(vec.X * vec.X) +
(vec.Y * vec.Y)
);
return Scaler.Sqrt(vec.Dot(vec));
}
}
@@ -43,18 +43,16 @@ namespace StgSharp.Math
public static unsafe void Normalize(ref vec3d source, ref vec3d target)
{
Vector4 s = source.vec;
Vector4 t = target.vec;
s = Vector4.Normalize(s);
float dotProduct = Vector4.Dot(s, t);
Vector4 projection = Vector4.Multiply(s, t);
Vector3 s = source.v;
Vector3 t = target.v;
s = Vector3.Normalize(s);
Vector3 projection = Vector3.Dot(t, s) / Vector3.Dot(s, s) * s;
t -= projection;
t = Vector3.Normalize(t);
source.vec = s;
target.vec = t;
source.v = s;
target.v = t;
}
public static vec4d Normalize(vec4d vec)
@@ -36,25 +36,25 @@ namespace StgSharp.Math
public static unsafe partial class VectorCalc
{
public static Vec2d To2D(vec3d vec, CordinatePlain plain)
public static vec2d To2D(vec3d vec, CordinatePlain plain)
{
switch (plain)
{
case CordinatePlain.XY:
return new Vec2d(vec.X, vec.Y);
return new vec2d(vec.X, vec.Y);
break;
case CordinatePlain.YZ:
return new Vec2d(vec.Y, vec.Z);
return new vec2d(vec.Y, vec.Z);
break;
case CordinatePlain.XZ:
return new Vec2d(vec.X, vec.Z);
return new vec2d(vec.X, vec.Z);
break;
default:
return default(Vec2d);
return default(vec2d);
break;
}
}
public static vec3d To3D(Vec2d vec)
public static vec3d To3D(vec2d vec)
{
return new vec3d(vec.X, vec.Y, 0.0f);
}
+35 -17
View File
@@ -39,7 +39,7 @@ namespace StgSharp.Math
/// Vec2Ds in StgSharp are defaultly usaed as colum vector.
/// </summary>
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
public struct Vec2d
public struct vec2d
{
[FieldOffset(0)]
@@ -55,56 +55,74 @@ namespace StgSharp.Math
[FieldOffset(4)]
public float Y;
internal Vec2d(Vector2 vec)
internal vec2d(Vector2 vec)
{
v = vec;
}
public Vec2d(float x, float y)
public vec2d(float x, float y)
{
v = new Vector2(x, y);
}
public static Vec2d Unit => new Vec2d(1, 1);
public static Vec2d Zero => new Vec2d(0, 0);
public float Cross(Vec2d right)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Cross(vec2d right)
{
return (X * right.Y) - (Y * right.X);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec2d operator -(Vec2d left, Vec2d right)
public float Dot(vec2d vec)
{
return new Vec2d(left.v - right.v);
return Vector2.Dot(v, vec.v);
}
public static bool operator !=(Vec2d left, Vec2d right)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec2d operator -(vec2d left, vec2d right)
{
return new vec2d(left.v - right.v);
}
public static bool operator !=(vec2d left, vec2d right)
{
return !(left == right);
}
public static Vec2d operator *(Vec2d vec, float value)
public static vec2d operator *(vec2d vec, float value)
{
return new Vec2d(vec.v * value);
return new vec2d(vec.v * value);
}
public static Vec2d operator /(Vec2d vec, float value)
public static vec2d operator /(vec2d vec, float value)
{
return new Vec2d(vec.v / value);
return new vec2d(vec.v / value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec2d operator +(Vec2d left, Vec2d right)
public static vec2d operator +(vec2d left, vec2d right)
{
return new Vec2d(left.v + right.v);
return new vec2d(left.v + right.v);
}
public static bool operator ==(Vec2d left, Vec2d right)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(vec2d left, vec2d right)
{
return left.v == right.v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator vec2d((float, float) tuple)
{
return new vec2d(tuple.Item1, tuple.Item2);
}
}
public static class Vec2d
{
public static vec2d Unit => new vec2d(1, 1);
public static vec2d Zero => new vec2d(0, 0);
}
}
+17 -5
View File
@@ -88,10 +88,7 @@ namespace StgSharp.Math
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Dot(vec3d right)
{
Vector4 vector4 = this.vec * right.vec;
return vector4.X
+ vector4.Y
+ vector4.Z;
return Vector3.Dot(v,right.v);
}
public bool Equals(vec3d other)
@@ -200,8 +197,23 @@ namespace StgSharp.Math
public static class Vec3d
{
public static vec3d Unit => new vec3d(1, 1, 1);
public static bool IsParallel(vec3d left, vec3d right)
{
return Cross(left,right) == Zero;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec3d Cross(vec3d left, vec3d right)
{
return Linear.Cross(left, right);
}
public static vec3d One => new vec3d(1, 1, 1);
public static vec3d Zero => new vec3d(0, 0, 0);
public static vec3d UnitZ => new vec3d(0, 0, 1);
public static vec3d UnitX => new vec3d(1, 0, 0);
public static vec3d UnitY => new vec3d(0, 1, 0);
}
}
+18
View File
@@ -34,6 +34,7 @@ using StgSharp.Math;
using System;
using System.Data.Common;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Math
@@ -58,6 +59,11 @@ namespace StgSharp.Math
[FieldOffset(8)] public float Z;
[FieldOffset(12)] public float W;
public override string ToString()
{
return vec.ToString();
}
public vec4d(float x, float y, float z, float w)
{
vec = new Vector4(x, y, z, w);
@@ -68,5 +74,17 @@ namespace StgSharp.Math
return vec.reg;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec4d operator *(Matrix44 mat, vec4d vec)
{
mat.InternalTranspose();
return new vec4d(
Vector4.Dot( mat.transpose.colum0,vec.vec),
Vector4.Dot( mat.transpose.colum1, vec.vec),
Vector4.Dot( mat.transpose.colum2, vec.vec),
Vector4.Dot( mat.transpose.colum3, vec.vec)
);
}
}
}