Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
fogleman committed May 29, 2017
1 parent dc20d00 commit 56a4777
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 27 deletions.
76 changes: 49 additions & 27 deletions examples/pipes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import (

const (
scale = 4
width = 1024
height = 1024
width = 1920
height = 1080
fovy = 40
near = 1
far = 100
)

var (
eye = V(4, 0, 0)
eye = V(0, 0, 4)
center = V(0, 0, 0)
up = V(0, 0, 1)
light = V(2, 1, 1).Normalize()
up = V(0, 1, 0)
light = V(0.5, 0.5, 2).Normalize()
)

func RandomColor() Color {
Expand Down Expand Up @@ -64,19 +64,24 @@ func (c Cell) Mesh() *Mesh {
}

type Grid struct {
Size int
Cells map[Cell]bool
W, H, D int
Cells map[Cell]bool
}

func NewGrid(size int) *Grid {
func NewGrid(w, h, d int) *Grid {
cells := make(map[Cell]bool)
return &Grid{size, cells}
return &Grid{w, h, d, cells}
}

func (g *Grid) Load() float64 {
capacity := g.W * g.H * g.D
size := len(g.Cells)
return float64(size) / float64(capacity)
}

func (g *Grid) RandomEmptyCell() Cell {
s := g.Size
for {
c := Cell{rand.Intn(s), rand.Intn(s), rand.Intn(s)}
c := Cell{rand.Intn(g.W), rand.Intn(g.H), rand.Intn(g.D)}
if !g.Get(c) {
g.Set(c)
return c
Expand All @@ -85,11 +90,10 @@ func (g *Grid) RandomEmptyCell() Cell {
}

func (g *Grid) Get(c Cell) bool {
s := g.Size
if c.X < 0 || c.Y < 0 || c.Z < 0 {
return true
}
if c.X >= s || c.Y >= s || c.Z >= s {
if c.X >= g.W || c.Y >= g.H || c.Z >= g.D {
return true
}
return g.Cells[c]
Expand Down Expand Up @@ -169,41 +173,59 @@ func main() {
context.ClearColor = Black
context.Shader = NewPhongShader(matrix, light, eye)

grid := NewGrid(11)
grid := NewGrid(19, 11, 11)
pipes := make([]*Pipe, 8)
for i := range pipes {
pipes[i] = NewPipe(grid.RandomEmptyCell())
}

for i := 0; i < 100; i++ {
mesh := NewEmptyMesh()
for grid.Load() < 0.9 {
// mesh := NewEmptyMesh()
dead := 0
for _, pipe := range pipes {
if pipe.Done {
mesh.Add(pipe.GetMesh())
// mesh.Add(pipe.GetMesh())
continue
}
pipe.Update(grid)
mesh.Add(pipe.GetMesh())
// mesh.Add(pipe.GetMesh())
if pipe.Done {
dead++
}
}
for j := 0; j < dead; j++ {
pipes = append(pipes, NewPipe(grid.RandomEmptyCell()))
}
mesh.Transform(Translate(V(-5, -5, -5)).Scale(V(0.2, 0.2, 0.2)))
mesh.SmoothNormals()
// mesh.Transform(Translate(V(-5, -5, -5)).Scale(V(0.2, 0.2, 0.2)))
// mesh.SmoothNormals()

fmt.Println(i, len(pipes), len(mesh.Triangles))
// fmt.Println(i, len(pipes), len(mesh.Triangles))

context.ClearColorBuffer()
context.ClearDepthBuffer()
context.DrawMesh(mesh)
// context.ClearColorBuffer()
// context.ClearDepthBuffer()
// context.DrawMesh(mesh)

image := context.Image()
image = resize.Resize(width, height, image, resize.Bilinear)
// image := context.Image()
// image = resize.Resize(width, height, image, resize.Bilinear)

// SavePNG(fmt.Sprintf("frame%06d.png", i), image)
}

SavePNG(fmt.Sprintf("frame%06d.png", i), image)
mesh := NewEmptyMesh()
for _, pipe := range pipes {
mesh.Add(pipe.GetMesh())
}
mesh.Transform(Translate(V(-9, -5, -5)).Scale(V(0.2, 0.2, 0.2)))
mesh.SmoothNormals()

fmt.Println(len(pipes), len(mesh.Triangles))

context.ClearColorBuffer()
context.ClearDepthBuffer()
context.DrawMesh(mesh)

image := context.Image()
image = resize.Resize(width, height, image, resize.Bilinear)

SavePNG("out.png", image)
}
69 changes: 69 additions & 0 deletions examples/simplify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"fmt"
"image"

. "github.com/fogleman/fauxgl"
"github.com/nfnt/resize"
)

const (
scale = 4
width = 2048
height = 2048
fovy = 30
near = 1
far = 10
)

var (
eye = V(-2, -4, 2)
center = V(-0.1, 0, -0.1)
up = V(0, 0, 1)
)

func render(mesh *Mesh) image.Image {
context := NewContext(width*scale, height*scale)
context.ClearColorBufferWith(White)

aspect := float64(width) / float64(height)
matrix := LookAt(eye, center, up).Perspective(fovy, aspect, near, far)
light := V(-0.75, -0.25, 1).Normalize()

shader := NewPhongShader(matrix, light, eye)
shader.ObjectColor = HexColor("FFD34E")
shader.DiffuseColor = Gray(0.9)
shader.SpecularColor = Gray(0.25)
shader.SpecularPower = 100
context.Shader = shader
context.DrawMesh(mesh)

context.Shader = NewSolidColorShader(matrix, Black)
context.DepthBias = -1e-4
context.Wireframe = true
context.LineWidth = 4
context.DrawMesh(mesh)

image := context.Image()
image = resize.Resize(width, height, image, resize.Bilinear)
return image
}

func main() {
mesh, err := LoadSTL("examples/bunny.stl")
if err != nil {
panic(err)
}
mesh.Transform(Matrix{0.023175793856519147, 0, 0, 0, 0, 0.023175793856519147, 0, 0, 0, 0, 0.023175793856519147, -0.9704647076255632, 0, 0, 0, 1})
fmt.Println(len(mesh.Triangles))
f := 1.0
for i := 1; ; i++ {
m := mesh.Copy()
m.Simplify(f)
fmt.Println(i, f, len(m.Triangles))
image := render(m)
SavePNG(fmt.Sprintf("bunny/out%02d.png", i), image)
f *= 0.75
}
}
22 changes: 22 additions & 0 deletions mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package fauxgl

import (
"math"

"github.com/fogleman/simplify"
)

type Mesh struct {
Expand Down Expand Up @@ -155,6 +157,26 @@ func (m *Mesh) ReverseWinding() {
}
}

func (m *Mesh) Simplify(factor float64) {
st := make([]*simplify.Triangle, len(m.Triangles))
for i, t := range m.Triangles {
v1 := simplify.Vector(t.V1.Position)
v2 := simplify.Vector(t.V2.Position)
v3 := simplify.Vector(t.V3.Position)
st[i] = simplify.NewTriangle(v1, v2, v3)
}
sm := simplify.NewMesh(st)
sm = sm.Simplify(factor)
m.Triangles = make([]*Triangle, len(sm.Triangles))
for i, t := range sm.Triangles {
v1 := Vector(t.V1)
v2 := Vector(t.V2)
v3 := Vector(t.V3)
m.Triangles[i] = NewTriangleForPoints(v1, v2, v3)
}
m.dirty()
}

func (m *Mesh) SaveSTL(path string) error {
return SaveSTL(path, m)
}
10 changes: 10 additions & 0 deletions vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ func (a Vector) Length() float64 {
return math.Sqrt(a.X*a.X + a.Y*a.Y + a.Z*a.Z)
}

func (a Vector) Less(b Vector) bool {
if a.X != b.X {
return a.X < b.X
}
if a.Y != b.Y {
return a.Y < b.Y
}
return a.Z < b.Z
}

func (a Vector) Distance(b Vector) float64 {
return a.Sub(b).Length()
}
Expand Down

0 comments on commit 56a4777

Please sign in to comment.