Implemented drawing triangles.

This commit is contained in:
Andrey Parhomenko 2023-06-15 20:18:58 +03:00
parent 6fdc733f5c
commit fea745e6e1
5 changed files with 107 additions and 111 deletions

View file

@ -23,6 +23,27 @@ type Rect struct {
*gx.DrawableRectangle *gx.DrawableRectangle
} }
type Tri struct {
*gx.DrawableTriangles
}
func NewTri() *Tri {
ret := &Tri{}
ret.DrawableTriangles = &gx.DrawableTriangles{}
ret.Triangles = gx.Triangles{
gx.Triangle{
gx.V(0, 0),
gx.V(100, 100),
gx.V(0, -50),
},
}
ret.Color = gx.Color{gx.MaxColorV, gx.MaxColorV, 0, gx.MaxColorV}
ret.Visible = true
return ret
}
func NewRect() *Rect { func NewRect() *Rect {
return &Rect{&gx.DrawableRectangle{ return &Rect{&gx.DrawableRectangle{
Rectangle: gx.Rectangle{ Rectangle: gx.Rectangle{
@ -215,10 +236,12 @@ func main() {
player = NewPlayer() player = NewPlayer()
rect = NewRect() rect = NewRect()
tri := NewTri()
e.Add(1, &Debug{}) e.Add(1, &Debug{})
e.Add(0, player) e.Add(0, player)
e.Add(-1, rect) e.Add(-1, rect)
e.Add(100, tri)
e.Run() e.Run()
} }

View file

@ -8,13 +8,22 @@ import (
"math" "math"
) )
type ColorV uint32
type Image = ebiten.Image type Image = ebiten.Image
type ColorV uint32
type ColorM = ebiten.ColorM type ColorM = ebiten.ColorM
type Color struct { type Color struct {
R, G, B, A ColorV R, G, B, A ColorV
} }
type Colority struct {
Color Color
}
type Visibility struct {
Visible bool
}
// The interface describes anything that can be // The interface describes anything that can be
// drawn. It will be drew corresponding to // drawn. It will be drew corresponding to
// the layers order. // the layers order.
@ -30,6 +39,10 @@ const (
MaxColorV = math.MaxUint32 MaxColorV = math.MaxUint32
) )
func (v Visibility) IsVisible() bool {
return v.Visible
}
func LoadImage(input io.Reader) (*Image, error) { func LoadImage(input io.Reader) (*Image, error) {
img, _, err := image.Decode(input) img, _, err := image.Decode(input)
if err != nil { if err != nil {

View file

@ -1,83 +1,29 @@
package gx package gx
// The type represents polygons. type PolygonTriangle struct {
// Fuck. The package gets too big. P, S int
// Should split it somehow. }
type Polygon []Point
// Returns slice of edges. type Polygon struct {
func (p Polygon) Edges() Edges { Transform
ret := Edges{} base Triangle
triangles []PolygonTriangle
l := p.Len() }
for i := range p {
ret = append(ret, Edge{p[i], p[(i+1)%l]}) type DrawablePolygon struct {
Polygon
ShaderOptions
}
func NewPolygon(base Triangle) *Polygon {
ret := &Polygon{
Transform: T(),
base: base,
} }
return ret return ret
} }
func (p Polygon) Len() int { func (p *Polygon) Triangles() Triangles {
return len(p)
}
func (p Polygon) Vertices() []Vertex {
return p
}
func (p Polygon) Barycenter() Point {
ret := Point{}
for _, v := range p {
ret.X += v.X
ret.Y += v.Y
}
l := Float(len(p))
ret.X /= l
ret.Y /= l
return ret
}
/*
func (p Polygon) anyPointInside() Point {
edges := p.Edges()
for _, e := range edges {
if
}
}
*/
func (p Polygon) Triangles() Triangles {
/*
if len(p) < 3 {
return Triangles{}
} else
vertices = p.Vertices()
ret := Triangles{}
i := 0
for len(vertices) != 3{
i1 = i % len(vertices)
i2 = (i+1) % len(vertices)
i3 = (i+2) % len(vertices)
l1 = LineSegment{vertices[i1], vertices[i2]}.Line()
l2 = LineSegment{vertices[i1], vertices[i3]}.Line()
if l1.K > 0 {
if l1.K
} else {
}
}
return append(
ret,
Triangle{p[0], p[1], p[2]},
)
*/
return Triangles{} return Triangles{}
} }

View file

@ -2,14 +2,37 @@ package gx
import ( import (
"math" "math"
"github.com/hajimehoshi/ebiten/v2"
) )
// The structure of a triangle. What more you want to hear? // Ebitens vector in better abstractions like Vectors.
type Triangle [3]Point type Vertex struct {
Dst Vector
Src Vector
Colority
}
type Triangle [3]Vector
type Triangles []Triangle type Triangles []Triangle
type DrawableTriangle struct { type DrawableTriangles struct {
Triangle Triangles
Visibility
Colority
ShaderOptions ShaderOptions
ebiten.DrawTrianglesOptions
}
func (v Vertex) Ebiten() ebiten.Vertex {
return ebiten.Vertex {
DstX: float32(v.Dst.X),
DstY: float32(v.Dst.Y),
SrcX: float32(v.Src.X),
SrcY: float32(v.Src.Y),
ColorR: float32(v.Color.R)/(float32(MaxColorV)),
ColorG: float32(v.Color.G)/(float32(MaxColorV)),
ColorB: float32(v.Color.B)/(float32(MaxColorV)),
ColorA: float32(v.Color.A)/(float32(MaxColorV)),
}
} }
// Returns the area of the triangle. // Returns the area of the triangle.
@ -65,52 +88,44 @@ func (ts Triangles) ContainsPoint(p Point) bool {
return false return false
} }
//func (t Triangle) func (r *DrawableTriangles) Draw(
/*
func (r *DrawableRectangle) Draw(
e *Engine, e *Engine,
i *Image, i *Image,
) { ) {
t := r.T m := e.Camera().RealMatrix(e)
cm := &m
// Draw solid color if no shader. // Draw solid color if no shader.
if r.Shader == nil { if r.Shader == nil {
t.S.X *= r.W vs := make([]ebiten.Vertex, len(r.Triangles) * 3)
t.S.Y *= r.H var buf Vertex
buf.Color = r.Color
m := t.Matrix(e) for i := range r.Triangles {
rm := e.Camera().RealMatrix(e, true) for j := range r.Triangles[i] {
buf.Dst = r.Triangles[i][j].Apply(cm)
m.Concat(rm) vs[i*3 + j] = buf.Ebiten()
}
opts := &ebiten.DrawImageOptions{
GeoM: m,
} }
i.DrawTriangles(img, opts)
is := make([]uint16, len(r.Triangles) * 3)
for i := 0 ; i < len(is) ; i++ {
is[i] = uint16(i)
}
img := NewImage(1, 1)
img.Set(0, 0, r.Color)
i.DrawTriangles(vs, is, img, &r.DrawTrianglesOptions)
return return
} }
// Use the Color as base image if no is provided. // Use the Color as base image if no is provided.
var did bool /*if r.Images[0] == nil {
if r.Images[0] == nil {
r.Images[0] = NewImage(1, 1) r.Images[0] = NewImage(1, 1)
r.Images[0].Set(0, 0, r.Color) r.Images[0].Set(0, 0, r.Color)
did = true
} }
w, h := r.Images[0].Size() 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. // Drawing with shader.
opts := &ebiten.DrawRectShaderOptions{ opts := &ebiten.DrawRectShaderOptions{
@ -118,7 +133,6 @@ func (r *DrawableRectangle) Draw(
Images: r.Images, Images: r.Images,
Uniforms: r.Uniforms, Uniforms: r.Uniforms,
} }
i.DrawRectShader(w, h, r.Shader, opts) i.DrawRectShader(w, h, r.Shader, opts)*/
} }
*/

View file

@ -9,7 +9,7 @@ type Vector struct {
X, Y Float X, Y Float
} }
type Point = Vector type Point = Vector
type Vertex = Vector //type Vertex = Vector
type Vectors []Vector type Vectors []Vector
type Points []Point type Points []Point