New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Matrix4x4.CreateViewport #79961
Comments
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsBackground and motivationThe I propose to add a method for creating a viewport matrix, which is shown in the figure below. API Proposalnamespace System.Numerics;
public struct Matrix4x4
{
public static Matrix4x4 CreateViewport(float x, float y, float width, float height)
{
Matrix4x4 result = Identity;
result.M11 = width * 0.5f;
result.M21 = -height * 0.5f;
result.M41 = x + result.M11;
result.M42 = y - result.M21;
return result;
}
} API UsageMatrix4x4 scaleMatrix = Matrix4x4.CreateScale(model.Scale);
Matrix4x4 rotationMatrix = Matrix4x4.CreateFromYawPitchRoll(model.Yaw, model.Pitch, model.Roll);
Matrix4x4 translationMatrix = Matrix4x4.CreateTranslation(model.Position);
Matrix4x4 modelMatrix = scaleMatrix * rotationMatrix * translationMatrix;
Matrix4x4 viewMatrix = Matrix4x4.CreateLookAt(camera.Position, camera.Target, camera.Up);
Matrix4x4 projectionMatrix = Matrix4x4.CreatePerspectiveFieldOfView(camera.Fov, camera.AspectRatio, camera.Near, camera.Far);
Matrix4x4 modelViewProjectionMatrix = modelMatrix * viewMatrix * projectionMatrix;
Matrix4x4 viewportMatrix = Matrix4x4.CreateViewport(0, 0, bitmap.PixelWidth, bitmap.PixelHeight);
Vector4[] windowVertices = new Vector4[model.Vertices.Length];
for (int i = 0; i < windowVertices.Length; i++)
{
windowVertices[i] = Vector4.Transform(model.Vertices[i], modelViewProjectionMatrix);
windowVertices[i] /= windowVertices[i].W;
windowVertices[i] = Vector4.Transform(windowVertices[i], viewportMatrix);
}
DrawPoints(windowVertices); Alternative DesignsNo response RisksNo response
|
Viewports are typically represented as distinct types and not directly as matrices. They likewise typically include a min/max depth in their representation and are not exclusively location + size. I'd expect a more typical matrix created from a viewport would be (noting this matches yours for the "typical"
That being said, exposing a "proper" viewport type is probably more than desirable and exposing some API that creates the corresponding matrix from the relevant 6 parameters is probably fine. There are potentially some open issues/questions here, however. In particular, this is just one example of a valid "viewport" matrix and is one that (iirc) places Much of that can be answered in the docs, but we may want to consider whether just "CreateViewport" is the correct name and what parameter validation, if any, should be done; etc. |
This issue has been marked |
If you could update the API proposal to include the |
@tannergooding Updated. |
Looks good as proposed namespace System.Numerics;
public partial struct Matrix4x4
{
public static Matrix4x4 CreateViewport(float x, float y, float width, float height, float minDepth, float maxDepth);
} |
Background and motivation
Matrix4x4
currently provides a large number of methods for creating various transformation matrices. The figure below shows a geometric pipeline for transforming vertices from object coordinates to window coordinates. Unfortunately,Matrix4x4
does not provide a method for creating a matrix that transforms vertices from normalized device coordinates to window coordinates.I propose to add a method for creating a viewport matrix, which is shown in the figure below.
width
andheight
specify viewport dimensions,x
andy
stand for coordinates of the upper left corner of the viewport,zmin
andzmax
define the minimum and maximum viewport depth.API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: