diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index a6cab2b..2809c07 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -8,6 +8,7 @@ import ( "log" "strings" "fmt" + "math/rand" ) type Player struct { @@ -65,6 +66,7 @@ func NewPlayer() *Player { }, Visible: true, Shader: gx.SolidWhiteColorShader, + Uniforms: make(map[string] any), }, MoveSpeed: 90., ScaleSpeed: .2, @@ -85,7 +87,8 @@ func (p *Player) Update(e *gx.Engine) error { dt := e.DT() c := e.Camera() keys := e.Keys() - + + p.Uniforms["Random"] = any(rand.Float32()) for _, v := range keys {switch v { case ebiten.KeyArrowUp : c.T.P.Y += p.MoveSpeed * dt diff --git a/src/gx/line-segment.go b/src/gx/line-segment.go index 9a764c2..79c8e84 100644 --- a/src/gx/line-segment.go +++ b/src/gx/line-segment.go @@ -1,4 +1,17 @@ package gx +import ( + "math" +) + type LineSegment [2]Point +func (ls LineSegment) LenSqr() Float { + return Sqr(ls[0].X - ls[1].X) + + Sqr(ls[0].Y - ls[1].Y) +} + +func (ls LineSegment) Len() Float { + return Sqrt(ls.LenSqr()) +} + diff --git a/src/gx/main.go b/src/gx/main.go index e0e9a77..2ec580d 100644 --- a/src/gx/main.go +++ b/src/gx/main.go @@ -10,10 +10,6 @@ import ( "math" ) -// The type is used in all Engine interactions -// where you need floating values. -type Float = float64 - // The type represents order of drawing. type Layer int diff --git a/src/gx/math.go b/src/gx/math.go new file mode 100644 index 0000000..3578a0d --- /dev/null +++ b/src/gx/math.go @@ -0,0 +1,15 @@ +package gx + +// The type is used in all Engine interactions +// where you need floating values. +type Float = float64 + +const ( + MaxFloat = math.MaxFloat64 +) + +// Returns square of the value. +func Sqr(v Float) Float { + return v * v +} + diff --git a/src/gx/shader.go b/src/gx/shader.go index 8d10148..7e736fe 100644 --- a/src/gx/shader.go +++ b/src/gx/shader.go @@ -7,6 +7,7 @@ import ( type Shader = ebiten.Shader type ShaderOptions struct { + Shader *Shader Uniforms map[string] any Images [4]*Image } @@ -16,6 +17,8 @@ var ( SolidWhiteColorShader = MustNewShader([]byte(` package main + var Random float + func Fragment(position vec4, texCoord vec2, color vec4) vec4 { //ts := imageSrcTextureSize() @@ -37,14 +40,16 @@ var ( ) }*/ - ret := vec4( + /*ret := vec4( 0, sin(position.x), sin(position.y), 1, ) - return imageSrc0UnsafeAt(texCoord) * (ret) + return imageSrc0UnsafeAt(texCoord) * (ret)*/ + return imageSrc0UnsafeAt(texCoord) * + vec4(1, 1, Random, Random) } `)) ) diff --git a/src/gx/triangle.go b/src/gx/triangle.go index 84c77bb..6a36bae 100644 --- a/src/gx/triangle.go +++ b/src/gx/triangle.go @@ -7,6 +7,10 @@ import ( // The structure of a triangle. What more you want to hear? type Triangle [3]Point type Triangles []Triangle +type DrawableTriangle struct { + Triangle + ShaderOptions +} // Returns the area of the triangle. func (t Triangle) Area() Float { @@ -21,4 +25,76 @@ func (t Triangle) Area() Float { return math.Abs( (x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/2) } +func sqr(v Float) Float { + return v * v +} +// Return squares of lengths of sides of the triangle. +func (t Triangle) SideLengthSquares() ([3]Float) { + + l1 := LineSegment{t[0], t[1]}.LenSqr() + l2 := LineSegment{t[1], t[2]}.LenSqr() + l2 := LineSegment{t[2], t[0]}.LenSqr() + + return [3]Float{l1, l2, l3} +} + +// Check whether the point is in the triangle. +func (t Triangle) PointIsIn(p Point) bool { + return false +} +/* +func (r *DrawableRectangle) Draw( + e *Engine, + i *Image, +) { + t := r.T + + // Draw solid color if no shader. + if r.Shader == nil { + t.S.X *= r.W + t.S.Y *= r.H + + m := t.Matrix(e) + rm := e.Camera().RealMatrix(e, true) + + m.Concat(rm) + + opts := &ebiten.DrawImageOptions{ + GeoM: m, + } + i.DrawTriangles(img, opts) + return + } + + // Use the Color as base image if no is provided. + var did bool + if r.Images[0] == nil { + r.Images[0] = NewImage(1, 1) + r.Images[0].Set(0, 0, r.Color) + did = true + } + + w, h := r.Images[0].Size() + if !did { + t.S.X /= Float(w) + t.S.Y /= Float(h) + + t.S.X *= r.W + t.S.Y *= r.H + } + + + rm := e.Camera().RealMatrix(e, true) + m := t.Matrix(e) + m.Concat(rm) + + // Drawing with shader. + opts := &ebiten.DrawRectShaderOptions{ + GeoM: m, + Images: r.Images, + Uniforms: r.Uniforms, + } + i.DrawRectShader(w, h, r.Shader, opts) +} +*/