sceneCamera

package module
v0.0.0-...-a33f2fb Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: AGPL-3.0 Imports: 4 Imported by: 0

README

Go Reference

SceneCamera

Easy 3D camera management

Description

SceneCamera manages the camera for 3D applications. It provides the V, in the GL trinity MVP. And if needed, the P as well.

It comes with 3 convenient modes, museum mode, first person mode, and RTS (Real Time Strategy) mode.

It is designed to work with OpenGL, but does not rely on any graphics libraries (just a matrix lib). It could be used with other graphics libraries, but you may have to copy the matrices into your graphics library's matrix format.

Museum mode

Museum mode is a simple camera that orbits around a point, and can zoom in and out. Draw your object at the origin, and the camera will orbit around it. It is useful for inspecting 3D models from all angles.

First person mode / flight mode

The classic game mode. Move forwards, backwards, strafe and turn. It can also pitch, roll, and yaw, making it useful for flight simulators.

RTS mode

The camera floats over a ground plane. Forwards, backwards and strafe slide the camera along the ground plane. It can also spin around a point on the the map, using the pitch and yaw.

SBS (Side by Side) rendering

SceneCamera provides the correct matrices for rendering in SBS (or twin display) mode. Rather than taking control of the render like many graphics libraries, sceneCamera just hands you the matrices so that you can use them in your own render. See the example directory for more details.

a quick summary:

func RenderStereoFrame(state *State) {
    // Set the inter-pupilary distance.  Not critical, but the wrong value will make you feel sick.
	camera.SetIPD(2.0)

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


    //Get the view matrix for the left eye
	LeftviewMatrix := camera.LeftEyeViewMatrix()

    //Get the projection matrix for the left eye
	LeftEyeFrustrum := camera.LeftEyeFrustrum()

	// Set the viewport to left half of the window
	gl.Viewport(0, 0, int32(width/2), int32(height))

    //Render the scene to the left eye
	RenderFrame(state, LeftviewMatrix, LeftEyeFrustrum)



    //Get the matrices for the right eye
	viewMatrix := camera.RightEyeViewMatrix()
	RightProjectionMatrix := camera.RightEyeFrustrum()

    //Set the viewport to right half of the window
	gl.Viewport(int32(width/2), 0, int32(width/2), int32(height))

    Draw the scene to the right eye display
	RenderFrame(state, viewMatrix, RightProjectionMatrix)


	//Set viewport to whole window
	gl.Viewport(0, 0, int32(width), int32(height))
}

Default position

The camera starts at z=5 (0,0,5), looking at the origin(0,0,0). Positive Y is up (0,1,0).

In RTS mode, the camera starts at (10,10,10), looking at the origin(0,0,0). Positive Z is up (0,0,1). The ground plane is at z=0, covering the x and y axes. i.e. the ground normal vector is (0,0,1).

All these settings can be changed by setting the appropriate field.

Typical use

"github.com/donomii/sceneCamera"

//Create a new camera
camera := Cameras.New(2)  //FPS mode

camera.Move(0,0.5)  //Move forwards 0.5 world units

viewMatrix := camera.ViewMatrix()  //Get the view matrix for the camera

cameraUniform := gl.GetUniformLocation(state.Program, gl.Str("camera\x00"))
gl.UniformMatrix4fv(cameraUniform, 1, false, &viewMatrix[0])

// Draw your scene

Examples

An example program is included in the examples directory. It can be run with

cd examples
go run .

Known issues

It's not fast, but it's fast enough to handle movement in a game.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var PI = float32(3.1415927)

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 degrees

}

func New

func New(mode int) *Camera

Choose the mode of the camera. 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

Calculate the frustrum matrix for the right eye

func (*Camera) LeftEyeViewMatrix

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

Support 3D displays, by returning 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

Calculate the frustrum matrix for the right eye

func (*Camera) RightEyeViewMatrix

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

Support 3D displays, by returning the view matrix for the left 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)

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)

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