From bc91c2210b70d172beea3a61a627034fba22b007 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 20 Jun 2013 00:49:44 +0900 Subject: [PATCH] Add RotateMatrix and TranslateMatrix --- examples/glut/main.go | 10 +++++++--- graphics/color_matrix.go | 4 ++++ graphics/geometry_matrix.go | 25 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/examples/glut/main.go b/examples/glut/main.go index 3c91f8981..3dac9f988 100644 --- a/examples/glut/main.go +++ b/examples/glut/main.go @@ -117,10 +117,13 @@ func (game *DemoGame) Update() { func (game *DemoGame) Draw(g graphics.GraphicsContext, offscreen graphics.TextureID) { g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255}) geometryMatrix := graphics.IdentityGeometryMatrix() - geometryMatrix.SetTx(float64(game.x)) - geometryMatrix.SetTy(float64(game.x)) + tx, ty := float64(game.ebitenTexture.Width), float64(game.ebitenTexture.Height) + geometryMatrix = geometryMatrix.Concat(graphics.TranslateMatrix(-tx/2, -ty/2)) + geometryMatrix = geometryMatrix.Concat(graphics.RotateMatrix(float64(game.x) / 60)) + geometryMatrix = geometryMatrix.Concat(graphics.TranslateMatrix(tx/2, ty/2)) + geometryMatrix = geometryMatrix.Concat(graphics.TranslateMatrix(100, 100)) g.DrawTexture(game.ebitenTexture.ID, - 0, 0, game.ebitenTexture.Width, game.ebitenTexture.Height, + 0, 0, int(tx), int(ty), geometryMatrix, graphics.IdentityColorMatrix()) } @@ -133,4 +136,5 @@ func main() { currentUI.Init() ebiten.OpenGLRun(game, currentUI) + } diff --git a/graphics/color_matrix.go b/graphics/color_matrix.go index 6a31977a6..7ddd1513f 100644 --- a/graphics/color_matrix.go +++ b/graphics/color_matrix.go @@ -14,6 +14,10 @@ func IdentityColorMatrix() *ColorMatrix { return &ColorMatrix{*IdentityAffineMatrix(colorMatrixDimension)} } +func (matrix *ColorMatrix) Concat(other *ColorMatrix) *ColorMatrix { + return &ColorMatrix{*matrix.AffineMatrix.Concat(&other.AffineMatrix)} +} + func (matrix *ColorMatrix) Clone() *ColorMatrix { return &ColorMatrix{*(matrix.AffineMatrix.Clone())} } diff --git a/graphics/geometry_matrix.go b/graphics/geometry_matrix.go index fbe0381a2..60f61ed22 100644 --- a/graphics/geometry_matrix.go +++ b/graphics/geometry_matrix.go @@ -1,5 +1,9 @@ package graphics +import ( + "math" +) + type GeometryMatrix struct { AffineMatrix } @@ -14,6 +18,27 @@ func IdentityGeometryMatrix() *GeometryMatrix { return &GeometryMatrix{*IdentityAffineMatrix(geometryMatrixDimension)} } +func TranslateMatrix(tx, ty float64) *GeometryMatrix { + matrix := IdentityGeometryMatrix() + matrix.SetTx(tx) + matrix.SetTy(ty) + return matrix +} + +func RotateMatrix(theta float64) *GeometryMatrix { + matrix := NewGeometryMatrix() + cos, sin := math.Cos(theta), math.Sin(theta) + matrix.SetA(cos) + matrix.SetB(-sin) + matrix.SetC(sin) + matrix.SetD(cos) + return matrix +} + +func (matrix *GeometryMatrix) Concat(other *GeometryMatrix) *GeometryMatrix { + return &GeometryMatrix{*matrix.AffineMatrix.Concat(&other.AffineMatrix)} +} + func (matrix *GeometryMatrix) Clone() *GeometryMatrix { return &GeometryMatrix{*(matrix.AffineMatrix.Clone())} }