// RotateTo adjusts the yaw and pitch to face a point. func (c *QuatCamera) RotateTo(center mgl.Vec3) { direction := center.Sub(c.position).Normalize() right := direction.Cross(AxisUp) up := right.Cross(direction) c.rotation = mgl.QuatLookAtV(c.position, center, up) }
// Lerp will interpolate between the desired position/center by amount. func (c *QuatCamera) Lerp(position mgl.Vec3, center mgl.Vec3, amount float32) { direction := center.Sub(position).Normalize() right := direction.Cross(AxisUp) up := right.Cross(direction) targetRot := mgl.QuatLookAtV(position, center, up) c.rotation = mgl.QuatNlerp(c.rotation, targetRot, amount) c.position = c.position.Add(position.Sub(c.position).Mul(amount)) }
func main() { app = NewApp() app.InitGraphics("Cube Drop", 800, 600) app.SetKeyCallback(keyCallback) app.OnRender = renderCallback app.OnUpdate = updateCallback defer app.Terminate() // compile the shaders var err error diffuseShader, err = LoadShaderProgram(DiffuseTextureVertShader, DiffuseTextureFragShader) if err != nil { panic("Failed to compile the diffuse shader! " + err.Error()) } // setup the slice of cubes to render cubes = make([]*Entity, 0, 128) // load the grass texture for the ground grassTex, err := LoadImageToTexture(grassTexturePath) if err != nil { panic("Failed to load the grass texture! " + err.Error()) } // load the crate texture for the cubes crateTexture, err = LoadImageToTexture(crateTexturePath) if err != nil { panic("Failed to load the crate texture! " + err.Error()) } // create the ground plane groundPlane = cubez.NewCollisionPlane(m.Vector3{0.0, 1.0, 0.0}, 0.0) // make a ground plane to draw ground = CreatePlaneXZ(-500.0, 500.0, 500.0, -500.0, 64.0) ground.Shader = diffuseShader ground.Color = mgl.Vec4{1.0, 1.0, 1.0, 1.0} ground.Tex0 = grassTex // setup the camera app.CameraPos = mgl.Vec3{0.0, 3.0, 10.0} app.CameraRotation = mgl.QuatLookAtV( mgl.Vec3{0.0, 5.0, 10.0}, mgl.Vec3{0.0, 0.0, 0.0}, mgl.Vec3{0.0, 1.0, 0.0}) gl.Enable(gl.DEPTH_TEST) app.RenderLoop() }
func main() { app = NewApp() app.InitGraphics("Ballistic", 800, 600) app.SetKeyCallback(keyCallback) app.OnRender = renderCallback app.OnUpdate = updateCallback defer app.Terminate() // compile the shaders var err error colorShader, err = LoadShaderProgram(DiffuseColorVertShader, DiffuseColorFragShader) if err != nil { panic("Failed to compile the shader! " + err.Error()) } // create the ground plane groundPlane = cubez.NewCollisionPlane(m.Vector3{0.0, 1.0, 0.0}, 0.0) // make a ground plane to draw ground = CreatePlaneXZ(-500.0, 500.0, 500.0, -500.0, 1.0) ground.Shader = colorShader ground.Color = mgl.Vec4{0.6, 0.6, 0.6, 1.0} // create a test cube to render cubeNode := CreateCube(-1.0, -1.0, -1.0, 1.0, 1.0, 1.0) cubeNode.Shader = colorShader cubeNode.Color = mgl.Vec4{1.0, 0.0, 0.0, 1.0} // create the collision box for the the cube var cubeMass m.Real = 8.0 var cubeInertia m.Matrix3 cubeCollider := cubez.NewCollisionCube(nil, m.Vector3{1.0, 1.0, 1.0}) cubeCollider.Body.Position = m.Vector3{0.0, 5.0, 0.0} cubeCollider.Body.SetMass(cubeMass) cubeInertia.SetBlockInertiaTensor(&cubeCollider.HalfSize, cubeMass) cubeCollider.Body.SetInertiaTensor(&cubeInertia) cubeCollider.Body.CalculateDerivedData() cubeCollider.CalculateDerivedData() // make the entity out of the renerable and collider cube = NewEntity(cubeNode, cubeCollider) // make a slice of entities for bullets bullets = make([]*Entity, 0, 16) // make the backboard to bound the bullets off of backboardNode := CreateCube(-0.5, -2.0, -0.25, 0.5, 2.0, 0.25) backboardNode.Shader = colorShader backboardNode.Color = mgl.Vec4{0.25, 0.2, 0.2, 1.0} backboardCollider := cubez.NewCollisionCube(nil, m.Vector3{0.5, 2.0, 0.25}) backboardCollider.Body.Position = m.Vector3{0.0, 2.0, -10.0} backboardCollider.Body.SetInfiniteMass() backboardCollider.Body.CalculateDerivedData() backboardCollider.CalculateDerivedData() SetGlVector3(&backboardNode.Location, &backboardCollider.Body.Position) // make the backboard entity backboard = NewEntity(backboardNode, backboardCollider) // setup the camera app.CameraPos = mgl.Vec3{-3.0, 3.0, 15.0} app.CameraRotation = mgl.QuatLookAtV( mgl.Vec3{-3.0, 3.0, 15.0}, mgl.Vec3{0.0, 1.0, 0.0}, mgl.Vec3{0.0, 1.0, 0.0}) gl.Enable(gl.DEPTH_TEST) app.RenderLoop() }