Sitelet https://pkg.go.dev/github.com/donomii/sceneCamera

sceneCamera

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: AGPL-3.0 Imports: 3 Imported by: 13

README

SceneCamera

CI Codecov Go Reference Go Report Card

SceneCamera provides camera movement and view/projection matrices for Go 3D applications. It supports museum, first-person, and real-time strategy movement, plus side-by-side stereo rendering.

Demos

FPS mode

FPS camera moving over a flat plane with trees

RTS mode

RTS camera circling over the map and trees

Flight mode

Flight camera diving toward the trees, leveling out, banking, and turning

It is designed for OpenGL but only depends on mathgl. The returned matrices can be copied into another graphics library's matrix format.

Install

go get github.com/donomii/sceneCamera@latest

Quick start

package main

import (
	"fmt"

	sceneCamera "github.com/donomii/sceneCamera"
)

func main() {
	camera := sceneCamera.New(2)
	camera.Move(0, 0.5)

	viewMatrix := camera.ViewMatrix()
	fmt.Println(viewMatrix)
}

Modes are selected when creating a camera:

  • sceneCamera.New(1) — museum mode, which orbits a target and zooms in or out.
  • sceneCamera.New(2) — FPS/flight mode, with translation, pitch, and yaw. Roll inputs are ignored.
  • sceneCamera.New(3) — RTS mode, which moves over a ground plane and orbits a point on that plane.

Move takes a direction and an amount. Translation amounts use world units; rotation amounts use radians.

Direction Operation
0 Forward
1 Backward
2 Left
3 Right
4 Up
5 Down
6 Pitch up
7 Pitch down
8 Yaw left
9 Yaw right
10 Roll left
11 Roll right

Each mode applies only the operations that make sense for that camera style.

Side-by-side stereo rendering

SceneCamera returns separate view and projection matrices for each eye without taking control of rendering:

func RenderStereoFrame(state *State) {
	// Set the inter-pupillary distance in world units.
	camera.SetIPD(2.0)

	width, height := MainWin.GetSize()
	camera.Screenwidth = float32(width) / 2
	camera.Screenheight = float32(height)

	leftViewMatrix := camera.LeftEyeViewMatrix()
	leftProjectionMatrix := camera.LeftEyeFrustum()
	gl.Viewport(0, 0, int32(width/2), int32(height))
	RenderFrame(state, leftViewMatrix, leftProjectionMatrix)

	rightViewMatrix := camera.RightEyeViewMatrix()
	rightProjectionMatrix := camera.RightEyeFrustum()
	gl.Viewport(int32(width/2), 0, int32(width/2), int32(height))
	RenderFrame(state, rightViewMatrix, rightProjectionMatrix)

	gl.Viewport(0, 0, int32(width), int32(height))
}

Default position

Museum and FPS cameras start at (0, 0, 5), looking at the origin, with positive Y as up.

RTS cameras start at (5, 5, 5), looking at the origin, with positive Z as up. The default ground plane is z=0, with the normal (0, 0, 1).

These settings can be changed through the camera fields and setter methods.

Example application

The example directory contains an OpenGL application:

cd example
go run .
Regenerating the demo GIFs

Run either no-argument launcher from the example directory:

./record-rts-demo.sh
./record-flight-demo.sh

Each launcher renders a deterministic 480×270 animation with 72 frames over 5.04 seconds and replaces its corresponding GIF in the repository root.

Documentation

Overview

Package sceneCamera provides camera movement and view/projection matrices for 3D applications.

Index

Examples

Constants

This section is empty.

Variables

View Source
var PI = float32(3.1415927)

PI is a single-precision approximation of pi retained for compatibility.

Functions

func PlaneIntercept

func PlaneIntercept(groundNormal, rayOrigin, rayDirection mgl32.Vec3) mgl32.Vec3

Find the point on the plane that the ray intercepts groundNormal is the normal of the plane rayOrigin is the origin of the ray rayDirection is the direction of the ray Returns the point on the plane that the ray intercepts

The plane is assumed to pass through the origin

func PlaneIntercept2

func PlaneIntercept2(groundOrigin, groundNormal, rayOrigin, rayDirection mgl32.Vec3) mgl32.Vec3

Find the point on the plane that the ray intercepts as for PlaneIntercept, but the plane is not assumed to pass through the origin

func ProjectPlane

func ProjectPlane(v1, v2 mgl32.Vec3) mgl32.Vec3

Project a vector onto a plane, given the normal of the plane, where v1 is the normal of the plane and v2 is the vector to be projected

Types

type Camera

type Camera struct {
	Position          mgl32.Vec3 //The position of the camera in world space
	Target            mgl32.Vec3 //The target of the camera in world space.  Note: not the focal point
	Up                mgl32.Vec3 //The up vector of the camera
	Orientation       mgl32.Quat //The orientation of the camera, quaternion
	Mode              int        //The mode of the camera.  1 - Museum mode, 2 - FPS mode, 3 - RTS mode
	GroundPlaneNormal mgl32.Vec3 //The normal of the ground plane
	IPD               float32    //The inter-pupillary distance, in world space
	FocalLength       float32    //The focal length of the camera, in world space
	Near              float32    //The near clipping plane
	Far               float32    //The far clipping plane
	Screenheight      float32    //The height of the screen, in pixels
	Screenwidth       float32    //The width of the screen, in pixels
	Aperture          float32    //The aperture of the camera, in world space
	FOV               float32    //The field of view of the camera, in radians

}

Camera holds the position, orientation, projection settings, and movement mode of a 3D camera.

Example
camera := New(2)
camera.Move(0, 1)
x, y, z := camera.WorldPosition()
fmt.Printf("%.0f %.0f %.0f\n", x, y, z)
Output:
0 0 4

func New

func New(mode int) *Camera

New creates a camera in the selected movement mode. 1 - Museum mode 2 - FPS mode 3 - RTS mode

func (*Camera) Dump

func (c *Camera) Dump()

Print some information about the camera to stdout

func (*Camera) ForwardsVector

func (c *Camera) ForwardsVector() mgl32.Vec3

The forward unit vector of the camera, in world space

func (*Camera) LeftEyeFrustrum

func (c *Camera) LeftEyeFrustrum() mgl32.Mat4

LeftEyeFrustrum returns the frustum matrix for the left eye. Deprecated: use LeftEyeFrustum.

func (*Camera) LeftEyeFrustum

func (c *Camera) LeftEyeFrustum() mgl32.Mat4

LeftEyeFrustum returns the frustum matrix for the left eye.

func (*Camera) LeftEyeViewMatrix

func (c *Camera) LeftEyeViewMatrix() mgl32.Mat4

LeftEyeViewMatrix returns the view matrix for the left eye.

func (*Camera) LookAt

func (c *Camera) LookAt(x, y, z float32)

One of the more important functions, LookAt sets the target of the camera.

func (*Camera) Move

func (c *Camera) Move(direction int, amount float32)

Move the camera, according to the parameter 0 - forward 1 - backward 2 - left 3 - right 4 - up 5 - down 6 - pitch up 7 - pitch down 8 - yaw left 9 - yaw right 10 - roll left 11 - roll right

func (*Camera) Reset

func (c *Camera) Reset()

Reset the camera to its initial position

func (*Camera) RightEyeFrustrum

func (c *Camera) RightEyeFrustrum() mgl32.Mat4

RightEyeFrustrum returns the frustum matrix for the right eye. Deprecated: use RightEyeFrustum.

func (*Camera) RightEyeFrustum

func (c *Camera) RightEyeFrustum() mgl32.Mat4

RightEyeFrustum returns the frustum matrix for the right eye.

func (*Camera) RightEyeViewMatrix

func (c *Camera) RightEyeViewMatrix() mgl32.Mat4

RightEyeViewMatrix returns the view matrix for the right eye.

func (*Camera) RightWardsVector

func (c *Camera) RightWardsVector() mgl32.Vec3

The right unit vector of the camera, in world space

func (*Camera) Rotate

func (c *Camera) Rotate(x, y, z float32)

Rotate the camera, probably not around the axes that you want

func (*Camera) RotationMatrix

func (c *Camera) RotationMatrix() mgl32.Mat4

Returns the rotation matrix of the camera. (the rotation part of the view matrix)

func (*Camera) SetFocalLength

func (c *Camera) SetFocalLength(focalLength float32)

SetFocalLength sets the camera's focal length in world-space units.

func (*Camera) SetGroundPlaneNormal

func (c *Camera) SetGroundPlaneNormal(x, y, z float32)

Set the normal of the ground plane. This is used in RTS mode, and ignored in other modes.

func (*Camera) SetIPD

func (c *Camera) SetIPD(ipd float32)

Set the inter-pupillary distance for 3D displays (in world coordinates)

func (*Camera) SetMode

func (c *Camera) SetMode(mode int)

Choose the mode of the camera. 1 - Museum mode 2 - FPS mode 3 - RTS mode

func (*Camera) SetPosition

func (c *Camera) SetPosition(x, y, z float32)

Teleport to a position in world space

func (*Camera) SetUp

func (c *Camera) SetUp(x, y, z float32)

SetUp sets the camera's up vector.

func (*Camera) TargetPosition

func (c *Camera) TargetPosition() mgl32.Vec3

The position of the target, in world space. This is not the object that the camera is following

func (*Camera) TargetVector

func (c *Camera) TargetVector() mgl32.Vec3

Scenecam keeps an invisible target point to which the camera is always looking. Not normalised. This is the vector from the camera to the target. This is not the object that the camera is following

func (*Camera) Translate

func (c *Camera) Translate(x, y, z float32)

Move the camera through world space

func (*Camera) UpwardsVector

func (c *Camera) UpwardsVector() mgl32.Vec3

The up unit vector of the camera, in world space

func (*Camera) ViewMatrix

func (c *Camera) ViewMatrix() mgl32.Mat4

Return the ViewMatrix for the camera. This is the matrix that transforms world space to camera space. It contains both the rotation and translation of the camera. It can be passed directly to OpenGL as the ViewMatrix, and used in GLSL shaders as the ViewMatrix.

func (*Camera) WorldPosition

func (c *Camera) WorldPosition() (float32, float32, float32)

Returns the position of the camera in world space

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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