modeling

package
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

Index

Constants

View Source
const (
	// CAMERA_INFINITY is the distance from the camera to infinity.
	//
	// This is used when projecting a 3D point onto a 2D plane would
	// result in a division by zero.
	CAMERA_INFINITY float32 = 1e-13

	// DEFAULT_FOCAL_LENGTH is the default focal length of the camera.
	DEFAULT_FOCAL_LENGTH float32 = 1500.0
)
View Source
const (
	// MAX_TRIGS is the maximum number of triangles that can be stored in
	// a polygon mesh.
	MAX_TRIGS uint32 = (slices.MAX_SLICE_SIZE / 3) * 3

	// MAX_VERTS is the maximum number of vertices that can be stored in
	// a polygon mesh.
	MAX_VERTS uint32 = 0x7FFFFFFF

	// MAX_FACES is the maximum number of faces that can be stored in a
	// polygon mesh.
	MAX_FACES uint32 = slices.MAX_SLICE_SIZE

	// MAX_VARIANTS is the maximum number of variants that can be stored
	// in a model.
	MAX_VARIANTS uint32 = slices.MAX_SLICE_SIZE
)

Variables

View Source
var (
	// MESH is the namespace for accessing mesh builder functions.
	MESH meshT = meshT{}
)

Functions

func Clamp

func Clamp[E types.Ordered](min, value, max E) E

Clamp clampes the value between the given min and max values.

If the min value is greater than the max value, the values are swapped.

Parameters:

  • min: The minimum value.
  • value: The value to clamp.
  • max: The maximum value.

Returns:

  • E: The clamped value.

func GetEdges

func GetEdges(p *Polygon) [][2]math32.Vec3

func PointInTriangle

func PointInTriangle(point *sdl.FPoint, v1, v2, v3 *sdl.FPoint) bool

PointInTriangle reports whether the given point is inside the given triangle. If any of the points is `nil`, this call returns `false`.

Parameters:

  • point: The point to check.
  • v1: The first vertex of the triangle.
  • v2: The second vertex of the triangle.
  • v3: The third vertex of the triangle.

Returns:

  • bool: `true` if the point is inside the triangle, `false` otherwise.

Types

type Camera

type Camera interface {
	// ProjectPoint projects a 3D point to a screen point. If the
	// receiver is `nil`, the zero point is returned.
	//
	// If no point is provided, it is treated as the zero point.
	//
	// Parameters:
	//   - point: The point to project.
	//
	// Returns:
	//   - sdl.FPoint: The projected screen point.
	//   - float32: The depth of the projected point.
	ProjectPoint(point *math32.Vec3) (sdl.FPoint, float32)
}

Camera is the interface that defines the basic behaviors of a camera.

type Color

type Color struct {
	// Red is the red component of the color.
	Red uint8

	// Green is the green component of the color.
	Green uint8

	// Blue is the blue component of the color.
	Blue uint8

	// Alpha is the alpha component of the color.
	Alpha uint8
}

Color is a color in RGBA color space.

var (
	// NullColor is the black color ( #000000 ) with the alpha
	// component set to transparent.
	NullColor Color = NewColor(0x00, 0x00, 0x00, 0x00)

	// DefaultColorMod is the default color modulation of a texture.
	DefaultColorMod Color = NewColor(0xFF, 0xFF, 0xFF, 0xFF)
)

func NewColor

func NewColor(red, green, blue uint8, alpha uint8) Color

NewColor creates a new color with the given components.

Parameters:

  • red: The red component of the color.
  • green: The green component of the color.
  • blue: The blue component of the color.
  • alpha: The alpha component of the color.

Returns:

  • Color: The newly created color.

func (*Color) SDL

func (c *Color) SDL() sdl.Color

SDL returns the SDL color representation of the color. If the receiver is `nil`, NullColor is returned.

Returns:

  • sdl.Color: The SDL color representation of the color.

type ColorType added in v0.1.8

type ColorType uint8

ColorType is the type for common colors.

const (
	// BLACK is the black color with RGB value of #000000.
	BLACK ColorType = iota

	// GRAY is the gray color with RGB value of #808080.
	GRAY

	// WHITE is the white color with RGB value of #FFFFFF.
	WHITE

	// RED is the red color with RGB value of #FF0000.
	RED

	// YELLOW is the yellow color with RGB value of #FFFF00.
	YELLOW

	// GREEN is the green color with RGB value of #00FF00.
	GREEN

	// CYAN is the cyan color with RGB value of #00FFFF.
	CYAN

	// BLUE is the blue color with RGB value of #0000FF.
	BLUE

	// MAGENTA is the magenta color with RGB value of #FF00FF.
	MAGENTA

	// MAX_COLOR_TYPE is the number of color types. It is not a real
	// color type. Instead, it is used to check the validity of return
	// values and parameters.
	MAX_COLOR_TYPE
)

func (ColorType) IsValid added in v0.1.8

func (c ColorType) IsValid() bool

IsValid implements types.Enum.

func (ColorType) NewColor added in v0.1.8

func (c ColorType) NewColor(alpha uint8) sdl.Color

NewColor creates a new color with the given alpha value.

Parameters:

  • alpha: The alpha value of the color.

Returns:

  • sdl.Color: The new color.

Panics:

  • types.ErrBadSymbol: If the color type is invalid.

func (ColorType) String added in v0.1.8

func (i ColorType) String() string

type FrontCamera added in v0.1.9

type FrontCamera struct {
	// contains filtered or unexported fields
}

FrontCamera is the implementation of Camera for when the camera is aligned with the positive Z axis.

func NewFrontCamera added in v0.1.9

func NewFrontCamera(x, y, z float32, is_2d bool, width, height uint16) *FrontCamera

NewFrontCamera creates a new camera with the given width and height and position.

Parameters:

  • x: The x position of the camera.
  • y: The y position of the camera.
  • z: The z position of the camera.
  • is_2d: Whether the camera is in 2D mode.
  • width: The width of the screen.
  • height: The height of the screen.

Returns:

  • *FrontCamera: The newly created camera. It is never `nil`.

func (*FrontCamera) ProjectPoint added in v0.1.9

func (c *FrontCamera) ProjectPoint(point *math32.Vec3) (sdl.FPoint, float32)

ProjectPoint implements Camera.

type Hit added in v0.1.8

type Hit struct {
	// T is the distance along the ray at which the hit occurred.
	T float32

	// Point is the point of intersection.
	Point math32.Vec3

	// Normal is the normal at the intersection.
	Normal math32.Vec3

	// U is the barycentric coordinates of the intersection.
	U float32

	// V is the barycentric coordinates of the intersection.
	V float32

	// W is the barycentric coordinates of the intersection.
	W float32
}

Hit is the result of an intersection between a ray and an object.

func IntersectTriangles added in v0.1.8

func IntersectTriangles(lr *LightRay, tris []*math32.Triangle) (*Hit, bool)

IntersectTriangles returns the intersection between the ray and the given triangles. If the ray is `nil`, no intersection is returned.

If any of the triangles is `nil`, no intersection is returned.

Parameters:

  • lr: The ray to intersect with.
  • tris: The triangles to intersect with.

Returns:

  • *Hit: The intersection between the ray and the triangles.
  • bool: `true` if an intersection occurred, `false` otherwise.

type LightRay added in v0.1.8

type LightRay struct {
	// contains filtered or unexported fields
}

LightRay represents a ray that is emitted from a light source.

func NewLightRay added in v0.1.8

func NewLightRay(origin *math32.Vec3, direction *math32.Vec3) *LightRay

NewLightRay creates a new light ray with the given origin and direction. No light ray is created if the origin or direction are `nil`.

Parameters:

  • origin: The origin of the ray.
  • direction: The direction of the ray.

Returns:

  • *LightRay: The newly created light ray.

func (*LightRay) IntersectTriangle added in v0.1.8

func (lr *LightRay) IntersectTriangle(tri *math32.Triangle) (*Hit, bool)

IntersectTriangle returns the intersection between the ray and the given triangle. If the receiver is `nil`, no intersection is returned.

If the triangle is `nil`, no intersection is returned.

Parameters:

  • tri: The triangle to intersect with.

Returns:

  • *Hit: The intersection between the ray and the triangle.
  • bool: `true` if an intersection occurred, `false` otherwise.

type Mesh

type Mesh interface {
	memory.Allocater

	// Triangles returns the list of triangles of the mesh. If the
	// receiver is `nil`, no triangles are returned.
	//
	// Returns:
	//   - []Triangle: The list of triangles of the mesh.
	Triangles() []Triangle

	// ApplyProjection projects the points of the mesh onto the screen
	// using a perspective projection. If the receiver is `nil`, this
	// call is a no-op.
	//
	// Since this call can be expensive, it is recommended to use this
	// only when changes have been made that affect visuals. Better yet,
	// if it is possible to avoid this call like with culling, it is
	// always a good idea to do so.
	//
	// If no camera is provided, this call is a no-op.
	//
	// Parameters:
	//   - camera: The camera to use for the projection.
	ApplyProjection(camera Camera)

	// Translate moves the mesh in 3D space using the specified
	// translation vector. If the receiver is `nil`, this call is a
	// no-op.
	//
	// If no vector is provided, this call is a no-op.
	//
	// Parameters:
	//   - vec: The amount to translation vector.
	Translate(vec *math32.Vec3)

	// Scale scales the mesh in 3D space using the specified scaling
	// vector. If the receiver is `nil`, this call is a no-op.
	//
	// If no vector is provided, this call is a no-op.
	//
	// Scaling is applied relative to the specified pivot. If no pivot
	// is provided, the scaling is applied to the center of the mesh.
	//
	// Parameters:
	//   - pivot: The point to scale around.
	//   - vec: The amount to scale on each axis.
	Scale(pivot, vec *math32.Vec3)

	// Rotate rotates the mesh in 3D space using the specified rotation
	// quaternion. If the receiver is `nil`, this call is a no-op.
	//
	// If no quaternion is provided, this call is a no-op.
	//
	// Rotation is applied in clockwise direction around the provided
	// pivot. If not provided, the rotation is applied relative to
	// the center of the mesh.
	//
	// Parameters:
	//   - pivot: The point of the rotation.
	//   - q: The quaternion to rotate by.
	Rotate(pivot *math32.Vec3, q *math32.Quaternion)
}

Mesh is the interface that defines the basic behaviors of meshes.

A "mesh" is defined to be a collection of vertices, edges, and faces that defines the shape of a polyhedral surface.

type PlaneType

type PlaneType uint8

PlaneType is the type for axis-aligned planes.

const (
	// XY_PLANE represents the X-Y plane.
	XY_PLANE PlaneType = iota

	// XZ_PLANE represents the X-Z plane.
	XZ_PLANE

	// YZ_PLANE represents the Y-Z plane.
	YZ_PLANE

	// MAX_PLANE_TYPE is the number of plane types. This is not a real
	// plane type. Instead, it is used for checking the validity of
	// parameters and return values.
	MAX_PLANE_TYPE
)

func (PlaneType) IsValid

func (pt PlaneType) IsValid() bool

IsValid implements types.Enum.

func (PlaneType) String

func (i PlaneType) String() string

type PolyMesh

type PolyMesh struct {
	// contains filtered or unexported fields
}

PolyMesh is the implementation of Mesh for generic polygonal meshes.

func NewPolyMesh

func NewPolyMesh(npoints, ntrigs uint32) *PolyMesh

NewPolyMesh creates a new polygonal mesh with the given number of points and triangles.

The value of `npoints` is clamped to the range `[3, MAX_VERTS]`, inclusive. Similarly, the value of `ntrigs` is clamped to the range `[1, MAX_TRIGS]`, inclusive.

Parameters:

  • npoints: The number of points in the mesh.
  • ntrigs: The number of triangles in the mesh.

Returns:

  • *PolyMesh: The newly created mesh. It is never `nil`.

func (*PolyMesh) ApplyProjection

func (pm *PolyMesh) ApplyProjection(camera Camera)

ApplyProjection implements Mesh.

func (*PolyMesh) Destroy

func (pm *PolyMesh) Destroy()

Destroy implements Mesh.

func (*PolyMesh) GetID

func (pm *PolyMesh) GetID() uint32

GetID implements Mesh.

func (*PolyMesh) Points

func (pm *PolyMesh) Points() ([]math32.Vec3, uint32)

Points returns a slice of the points in the mesh. If the receiver is `nil`, no points are returned.

Returns:

  • []math32.Vec3: The points in the mesh.
  • uint32: The number of points returned.

func (*PolyMesh) Rotate

func (pm *PolyMesh) Rotate(pivot *math32.Vec3, q *math32.Quaternion)

Rotate implements Mesh.

func (*PolyMesh) Scale

func (pm *PolyMesh) Scale(pivot, vec *math32.Vec3)

Scale implements Mesh.

func (*PolyMesh) SetColors

func (pm *PolyMesh) SetColors(start, amount uint32, color Color)

SetColors sets the color of all vertices in the mesh within the given range. If the receiver is `nil`, this call is a no-op.

If the start index is out of bounds or the amount is zero, this call is a no-op.

Lastly, if the end index would exceed the number of vertices, all the vertices starting from the start index are set.

Parameters:

  • start: The index of the first vertex to set.
  • amount: The number of vertices to set.
  • color: The color to set.

func (*PolyMesh) SetID

func (pm *PolyMesh) SetID(id uint32)

SetID implements Mesh.

func (*PolyMesh) SetPoints

func (pm *PolyMesh) SetPoints(start uint32, ps ...math32.Vec3)

SetPoints sets the positions of points in the mesh starting at the given index. If the receiver is `nil`, this call is a no-op.

If the index is out of bounds, this call is a no-op.

NOTE: Remember to call [WriteBack] to commit the changes.

Parameters:

  • start: The index of the first point to set.
  • ps: The positions to set.

func (*PolyMesh) SetTexCoords

func (pm *PolyMesh) SetTexCoords(start uint32, tcs ...sdl.FPoint)

SetTexCoords sets the texture coordinates of vertices in the mesh starting at the given index. If the receiver is `nil`, this call is a no-op.

If the index is out of bounds, this call is a no-op.

Parameters:

  • start: The index of the first vertex to set.
  • tcs: The texture coordinates to set.

func (*PolyMesh) SetTexture

func (pm *PolyMesh) SetTexture(tex_id uint32)

SetTexture sets the texture ID for the mesh. If the receiver is `nil`, this call is a no-op.

Parameters:

  • tex_id: The texture ID to set.

func (*PolyMesh) SetTriangles

func (pm *PolyMesh) SetTriangles(start uint32, new_trigs ...[3]int32)

SetTriangles sets the triangles in the mesh starting at the given index. If the receiver is `nil`, this call is a no-op.

If the index is out of bounds, this call is a no-op.

Parameters:

  • start: The index of the first triangle to set.
  • new_trigs: The triangles to set.

func (*PolyMesh) Translate

func (pm *PolyMesh) Translate(vec *math32.Vec3)

Translate implements Mesh.

func (*PolyMesh) TriangleList

func (pm *PolyMesh) TriangleList() ([]int32, uint32)

TriangleList returns the underlying slice of triangle indices. If the receiver is `nil`, no triangles are returned.

Returns:

  • []int32: The underlying slice of triangle indices.
  • uint32: The number of triangles.

func (*PolyMesh) Triangles

func (pm *PolyMesh) Triangles() []Triangle

Triangles implements Mesh.

func (*PolyMesh) VertexList

func (pm *PolyMesh) VertexList() ([]sdl.Vertex, uint32)

VertexList returns the underlying slice of SDL vertices. If the receiver is `nil`, no vertices are returned.

Returns:

  • []sdl.Vertex: The underlying slice of SDL vertices.
  • uint32: The number of vertices.

type Polygon

type Polygon struct {
	// contains filtered or unexported fields
}

func (*Polygon) At

func (p *Polygon) At(idx uint32) (math32.Vec3, bool)

func (*Polygon) N

func (p *Polygon) N() uint32

type TexCoord added in v0.1.8

type TexCoord struct {
	// U is the U coordinate of the texture.
	U float32

	// V is the V coordinate of the texture.
	V float32
}

TexCoord holds the UV coordinates of a texture.

func NewTexCoord added in v0.1.8

func NewTexCoord(u, v float32) TexCoord

NewTexCoord creates a new UV texture coordinate.

Parameters:

  • u: The U coordinate of the texture.
  • v: The V coordinate of the texture.

Returns:

  • TexCoord: The newly created texture coordinate.

func (*TexCoord) Equals added in v0.1.8

func (tc *TexCoord) Equals(other *TexCoord) bool

Equals reports whether the given texture coordinate is equal to the receiver. If any of them is `nil` they are defined to be not equal.

Parameters:

  • other: The other texture coordinate.

Returns:

  • bool: `true` if the texture coordinates are equal, `false` otherwise.

func (*TexCoord) SDL added in v0.1.8

func (tc *TexCoord) SDL() sdl.FPoint

SDL returns the SDL representation of the texture coordinate. If the receiver is `nil` the zero texture coordinate is returned.

Returns:

  • sdl.FPoint: The SDL representation of the texture coordinate.

type Triangle

type Triangle struct {
	// TexID is the texture ID to use for the triangle.
	TexID uint32

	// Verts is the list of vertices of the triangle.
	Verts [3]sdl.Vertex

	// Trigs is the list of indices of the triangle.
	Trigs [3]int32

	// Points is the list of points of the triangle.
	Points [3]math32.Vec3

	// MinZ is the minimum Z value of the triangle.
	MinZ float32

	// MaxZ is the maximum Z value of the triangle.
	MaxZ float32
}

Triangle represents a triangle in the rendering pipeline.

func (*Triangle) Occludes

func (t *Triangle) Occludes(other *Triangle) bool

Occludes is a helper function for culling that reports whether the triangle occludes the other one. If the receiver is `nil`, this call returns `false`

If the other triangle is `nil`, this call returns `false`.

Parameters:

  • other: The other triangle to check.

Returns:

  • bool: `true` if the triangle occludes the other one, `false` otherwise.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL