daily uodate on 20240411 by nitload youmu, camera complete
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user