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
}
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 {
return &Rect{&gx.DrawableRectangle{
Rectangle: gx.Rectangle{
@ -215,10 +236,12 @@ func main() {
player = NewPlayer()
rect = NewRect()
tri := NewTri()
e.Add(1, &Debug{})
e.Add(0, player)
e.Add(-1, rect)
e.Add(100, tri)
e.Run()
}

View file

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

View file

@ -1,83 +1,29 @@
package gx
// The type represents polygons.
// Fuck. The package gets too big.
// Should split it somehow.
type Polygon []Point
type PolygonTriangle struct {
P, S int
}
// Returns slice of edges.
func (p Polygon) Edges() Edges {
ret := Edges{}
l := p.Len()
for i := range p {
ret = append(ret, Edge{p[i], p[(i+1)%l]})
type Polygon struct {
Transform
base Triangle
triangles []PolygonTriangle
}
type DrawablePolygon struct {
Polygon
ShaderOptions
}
func NewPolygon(base Triangle) *Polygon {
ret := &Polygon{
Transform: T(),
base: base,
}
return ret
}
func (p Polygon) Len() int {
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]},
)
*/
func (p *Polygon) Triangles() Triangles {
return Triangles{}
}

View file

@ -2,14 +2,37 @@ package gx
import (
"math"
"github.com/hajimehoshi/ebiten/v2"
)
// The structure of a triangle. What more you want to hear?
type Triangle [3]Point
// Ebitens vector in better abstractions like Vectors.
type Vertex struct {
Dst Vector
Src Vector
Colority
}
type Triangle [3]Vector
type Triangles []Triangle
type DrawableTriangle struct {
Triangle
type DrawableTriangles struct {
Triangles
Visibility
Colority
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.
@ -65,52 +88,44 @@ func (ts Triangles) ContainsPoint(p Point) bool {
return false
}
//func (t Triangle)
/*
func (r *DrawableRectangle) Draw(
func (r *DrawableTriangles) Draw(
e *Engine,
i *Image,
) {
t := r.T
m := e.Camera().RealMatrix(e)
cm := &m
// 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,
vs := make([]ebiten.Vertex, len(r.Triangles) * 3)
var buf Vertex
buf.Color = r.Color
for i := range r.Triangles {
for j := range r.Triangles[i] {
buf.Dst = r.Triangles[i][j].Apply(cm)
vs[i*3 + j] = buf.Ebiten()
}
}
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
}
// 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].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{
@ -118,7 +133,6 @@ func (r *DrawableRectangle) Draw(
Images: r.Images,
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
}
type Point = Vector
type Vertex = Vector
//type Vertex = Vector
type Vectors []Vector
type Points []Point