Skip to content

Commit

Permalink
Added TorusWires
Browse files Browse the repository at this point in the history
  • Loading branch information
MrScautHD committed Feb 22, 2025
1 parent d16ecda commit c5240b1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Bliss.Test/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ protected virtual void Draw(GraphicsDevice graphicsDevice, CommandList commandLi
this._immediateRenderer.SetTexture(this._customMeshTexture);
this._immediateRenderer.DrawTorus(new Transform() { Translation = new Vector3(42, 0, 6) }, 2, 1, 40, 40);

this._immediateRenderer.SetTexture(null);
this._immediateRenderer.DrawTorusWires(new Transform() { Translation = new Vector3(44, 0, 6) }, 2, 1, 40, 40, Color.Green);

this._immediateRenderer.End();

this._customPoly.Draw(commandList, new Transform() { Translation = new Vector3(9, 0, 0)}, this.FullScreenTexture.Framebuffer.OutputDescription);
Expand Down
59 changes: 57 additions & 2 deletions src/Bliss/CSharp/Graphics/Rendering/Renderers/ImmediateRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,8 +1351,63 @@ public void DrawTorus(Transform transform, float radius, float size, int radSeg,
this.DrawVertices(transform, this._tempVertices, this._tempIndices, PrimitiveTopology.TriangleList);
}

public void DrawTorusWires() {

/// <summary>
/// Renders a wireframe torus using the specified transformation, dimensions, and color.
/// </summary>
/// <param name="transform">The transformation applied to the torus, including position, rotation, and scale.</param>
/// <param name="radius">The radius of the inner circle of the torus.</param>
/// <param name="size">The thickness of the torus.</param>
/// <param name="radSeg">The number of segments in the radial direction. Minimum value is 3.</param>
/// <param name="sides">The number of subdivisions of the circular cross-section. Minimum value is 3.</param>
/// <param name="color">The optional color for rendering the torus wireframe.</param>
public void DrawTorusWires(Transform transform, float radius, float size, int radSeg, int sides, Color? color = null) {
Color finalColor = color ?? Color.White;

if (radSeg < 3) {
radSeg = 3;
}

if (sides < 3) {
sides = 3;
}

float circusStep = MathF.Tau / radSeg;
float sideStep = MathF.Tau / sides;

// Calculate the vertices for wireframe.
for (int rad = 0; rad <= radSeg; rad++) {
float radAngle = rad * circusStep;
float cosRad = MathF.Cos(radAngle);
float sinRad = MathF.Sin(radAngle);

for (int side = 0; side <= sides; side++) {
float sideAngle = side * sideStep;
float cosSide = MathF.Cos(sideAngle);
float sinSide = MathF.Sin(sideAngle);

Vector3 position = new Vector3(cosSide * cosRad, sinSide, cosSide * sinRad) * (size / 4.0F) + new Vector3(cosRad * (radius / 4.0F), 0.0F, sinRad * (radius / 4.0F));

this._tempVertices.Add(new ImmediateVertex3D() {
Position = position,
Color = finalColor.ToRgbaFloatVec4()
});

// Add indices for connecting radial and side segments.
if (rad < radSeg && side < sides) {
int current = rad * (sides + 1) + side;
int nextSide = current + 1;
int nextRad = current + (sides + 1);

this._tempIndices.Add((uint) current);
this._tempIndices.Add((uint) nextSide);

this._tempIndices.Add((uint) current);
this._tempIndices.Add((uint) nextRad);
}
}
}

this.DrawVertices(transform, this._tempVertices, this._tempIndices, PrimitiveTopology.LineList);
}

public void DrawKnot() {
Expand Down

0 comments on commit c5240b1

Please sign in to comment.