progress using my own graphics interface

This commit is contained in:
2026-01-06 00:35:24 -06:00
parent 59a2f08349
commit bbad63c60a
7 changed files with 230 additions and 195 deletions

View File

@@ -1,9 +1,9 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
"math"
"math/rand"
"github.com/gen2brain/raylib-go/raylib"
)
type ContourLayer struct {
@@ -133,4 +133,3 @@ func (w *Worm) Draw(ctx *RenderCtx) {
rl.PopMatrix()
w.angleIndex = (w.angleIndex + 1) % len(w.angles)
}

View File

@@ -2,9 +2,9 @@ package main
import (
"fmt"
"math"
rl "github.com/gen2brain/raylib-go/raylib"
"github.com/ojrac/opensimplex-go"
"math"
)
type Field interface {
@@ -92,7 +92,6 @@ func (f *ImageField) Get(x, y float32) float32 {
return Brightness(c)
}
/** LAYER HELPERS **/
type FieldLayer struct {
@@ -103,7 +102,7 @@ type FieldLayer struct {
}
func (fl *FieldLayer) Update(ctx *RenderCtx) {
;
}
func (fl *FieldLayer) Draw(ctx *RenderCtx) {

View File

@@ -3,9 +3,83 @@ package graphics
import (
"fmt"
"math/rand"
"image/color"
rl "github.com/gen2brain/raylib-go/raylib"
)
type Graphics struct {
layout Layout
style Style
}
type Layout struct {
// total monitor bounds
Monitor Rect
// bounds of the application window
Window Rect
// bounds of the ui controls area
Controls Rect
// bounds of the region of the app window reserved for rendering the graphics buffer
Viewport Rect
// bounds of the off screen graphics buffer where rendering happens
Graphics Rect
}
type Color color.RGBA
type HSBA struct {
H uint
S, B float32
A uint8
}
type Style struct {
StrokeColor Color
StrokeWeight float32
FillColor Color
}
type Rect rl.Rectangle
/**
* scale the given rect down to the target rect
* maintaining the aspect ratio of the original rect
*/
func (r Rect) ToRL() *rl.Rectangle {
return &rl.Rectangle { X: r.X, Y: r.Y, Width: r.Width, Height: r.Height }
}
func (r Rect) ScaleTo(tgt Rect) Rect {
outputWidth := tgt.Width
outputHeight := tgt.Height
aspect := r.Width / r.Height
tgtAspect := outputWidth / outputHeight
if aspect < tgtAspect {
// source is relatively taller than the target
// so we set the output height to the target height
// and calculate the width based on source aspect and center
outputWidth = float32(outputHeight) * aspect
} else {
// source is relatively wider than the target
// so we set the output width to the target width
// and calculate the height based on source aspect and center
outputHeight = float32(outputWidth) / aspect
}
// output width and height are correct -- center within TargetBounds
x := tgt.X + tgt.Width / 2.0 - outputWidth / 2.0
y := tgt.Y + tgt.Height / 2.0 - outputHeight / 2.0
return Rect {
X: x, Y: y,
Width: outputWidth,
Height: outputHeight,
}
}
var (
FlourescentHues = []float32{
0, // hot magenta-red

48
main.go
View File

@@ -22,15 +22,7 @@ var (
storage *Storage
)
type Layout struct {
monitor rl.RectangleInt32
window rl.RectangleInt32
controls rl.RectangleInt32
viewport rl.RectangleInt32
graphics rl.RectangleInt32
}
func bootstrap() Layout {
func Bootstrap() g.Layout {
rl.InitWindow(800, 600, "bootstrap")
@@ -82,12 +74,12 @@ func bootstrap() Layout {
os.Exit(1)
}
return Layout {
monitor: rl.RectangleInt32{ X: 0, Y: 0, Width: int32(monitorWidth), Height: int32(monitorHeight) },
window: rl.RectangleInt32{ X: 0, Y: 0, Width: int32(windowWidth), Height: int32(windowHeight) },
controls: rl.RectangleInt32{ X: 0, Y: 0, Width: int32(controlsWidth), Height: int32(windowHeight) },
viewport: rl.RectangleInt32{ X: int32(controlsWidth), Y: 0, Width: int32(viewportWidth), Height: int32(windowHeight) },
graphics: rl.RectangleInt32{ X: 0, Y: 0, Width: int32(graphicsWidth), Height: int32(graphicsHeight) },
return g.Layout {
Monitor: g.Rect{X: 0, Y: 0, Width: float32(monitorWidth), Height: float32(monitorHeight)},
Window: g.Rect{X: 0, Y: 0, Width: float32(windowWidth), Height: float32(windowHeight)},
Controls: g.Rect{X: 0, Y: 0, Width: float32(controlsWidth), Height: float32(windowHeight)},
Viewport: g.Rect{X: float32(controlsWidth), Y: 0, Width: float32(viewportWidth), Height: float32(windowHeight)},
Graphics: g.Rect{X: 0, Y: 0, Width: float32(graphicsWidth), Height: float32(graphicsHeight)},
}
}
@@ -95,10 +87,10 @@ func main() {
log := log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
layout := bootstrap()
layout := Bootstrap()
//rl.SetConfigFlags(rl.FlagMsaa4xHint)
rl.InitWindow(layout.window.Width, layout.window.Height, "sumi sierpinski arrow")
rl.InitWindow(int32(layout.Window.Width), int32(layout.Window.Height), "sumi sierpinski arrow")
// reproducable flourescent color cycle
colorCycle := g.NewFixedColorCycle(g.FlourescentColors).Shuffle(0)
@@ -116,8 +108,8 @@ func main() {
//imageField := NewImageField("/home/d/Dropbox/art/data/moses_statue.jpg")
field :=
&TranslateField{
x: -float32(layout.graphics.Width / 2.0),
y: -float32(layout.graphics.Height / 2.0),
x: -float32(layout.Graphics.Width / 2.0),
y: -float32(layout.Graphics.Height / 2.0),
field: &ScaleField{
scale: 100.0,
field: &AdderField{
@@ -131,12 +123,11 @@ func main() {
//sierpinskiLayer := &SierpinskiArrow { dirty: true }
sketch := NewSketch(layout.graphics.Width, layout.graphics.Height)
sketch := NewSketch(int32(layout.Graphics.Width), int32(layout.Graphics.Height))
fieldColor := colorCycle.Next()
fmt.Printf("field color = %v\n", fieldColor)
sketch.AddColorLayer("background-magenta", rl.Magenta)
sketch.AddColorLayer("background-black", rl.Black)
//sketch.AddLayer("field", &FieldLayer{field: field, loColor: rl.NewColor(0, 0, 0, 0), hiColor: fieldColor, dirty: true})
@@ -144,7 +135,7 @@ func main() {
fmt.Printf("actor color = %v\n", actorColor)
hsv := rl.ColorToHSV(actorColor);
hsv := rl.ColorToHSV(actorColor)
hsv.Z *= 0.7
actorColor = rl.ColorFromHSV(hsv.X, hsv.Y, hsv.Z)
actorColor = g.Clamp(actorColor, 10, 255)
@@ -178,7 +169,6 @@ func main() {
V: 8000,
}
for !rl.WindowShouldClose() {
// begin drawing
@@ -186,9 +176,9 @@ func main() {
// set up RenderCtx
renderCtx := &RenderCtx {
TargetBounds: layout.viewport,
SourceWidth: layout.graphics.Width,
SourceHeight: layout.graphics.Height,
TargetBounds: layout.Viewport,
SourceWidth: int32(layout.Graphics.Width),
SourceHeight: int32(layout.Graphics.Height),
Time: t,
Ports: ports.Eval(t),
}
@@ -214,13 +204,15 @@ func main() {
y := float32(10)
minX := float32(60)
maxX := float32(layout.controls.X + layout.controls.Width - 20)
maxX := float32(layout.Controls.X + layout.Controls.Width - 20)
sliderWidth := maxX - minX - 20
controlRowHeight := 20
for _, layerTools := range sketch.layerToolsOrdered {
config := layerTools.config
//layerTools.texture.Texture
gui.Label(rl.Rectangle{X: minX, Y: y, Width: 120, Height: 24}, layerTools.name)
y += float32(controlRowHeight + 10)
@@ -243,7 +235,6 @@ func main() {
config.bVisible = gui.Toggle(rl.Rectangle{X: minX, Y: y, Width: 16, Height: 16}, "B", config.bVisible)
config.b = uint8(gui.Slider(rl.Rectangle{X: minX + 20, Y: y, Width: sliderWidth, Height: 16}, "", "", float32(config.b), 0, 255))
/*
// don't do anything with saturation / k values yet
y += float32(controlRowHeight)
@@ -284,4 +275,3 @@ func main() {
rl.CloseWindow()
}

View File

@@ -45,5 +45,3 @@ func curve(ctx *RenderCtx, order int, length float64, angle float64) {
curve(ctx, order-1, length/2, -angle)
}
}

View File

@@ -1,8 +1,10 @@
package main
import (
"math"
"fmt"
g "github.com/d2fn/sumi/internal/graphics"
"github.com/gen2brain/raylib-go/raylib"
"math"
)
type Sketch struct {
@@ -21,7 +23,7 @@ type TextureCam struct {
/** RenderCtx **/
type RenderCtx struct {
TargetBounds rl.RectangleInt32
TargetBounds g.Rect
SourceWidth int32
SourceHeight int32
Time float64
@@ -115,19 +117,23 @@ func (s *Sketch) Draw(ctx *RenderCtx) {
s.Redraw(ctx)
// copy from full texture for compositing, with vertical flipping
src := rl.Rectangle {
src := g.Rect {
X: 0, Y: 0,
Width: float32(ctx.SourceWidth),
Height: -float32(ctx.SourceHeight),
}
dst := rl.Rectangle {
dst := g.Rect {
X: 0, Y: 0,
Width: float32(ctx.SourceWidth),
Height: float32(ctx.SourceHeight),
}
viewport := s.CalcViewport(ctx)
outputRect := s.calcOutputRectKeepingAspectRatio(ctx)
sourceRect := g.Rect{X: 0, Y: 0, Width: float32(ctx.SourceWidth), Height: float32(ctx.SourceHeight)}
targetRect := g.Rect{X: float32(ctx.TargetBounds.X), Y: float32(ctx.TargetBounds.Y), Width: float32(ctx.TargetBounds.Width), Height: float32(ctx.TargetBounds.Height)}
outputRect := sourceRect.ScaleTo(targetRect)
fmt.Printf("outputRect = %v\n", outputRect)
x := float32(0)
y := float32(0)
@@ -158,7 +164,6 @@ func (s *Sketch) Draw(ctx *RenderCtx) {
rl.EndScissorMode()
rl.PopMatrix()
rl.BeginBlendMode(rl.BlendAlphaPremultiply)
//rl.BeginBlendMode(rl.BlendAlpha)
rl.BeginTextureMode(s.composite)
@@ -192,46 +197,16 @@ func (s *Sketch) Draw(ctx *RenderCtx) {
rl.GenTextureMipmaps(&s.composite.Texture)
rl.SetTextureFilter(s.composite.Texture, rl.FilterTrilinear)
rl.DrawTexturePro(s.composite.Texture, viewport, outputRect, rl.Vector2{}, 0, rl.White)
rl.DrawTexturePro(s.composite.Texture, viewport, *outputRect.ToRL(), rl.Vector2{}, 0, rl.White)
outlineRect := outputRect.ToInt32()
outlineRect := outputRect.ToRL().ToInt32()
rl.DrawRectangleLines(outlineRect.X, outlineRect.Y, outlineRect.Width, outlineRect.Height, rl.Gray)
}
func (s *Sketch) calcOutputRectKeepingAspectRatio(ctx *RenderCtx) rl.Rectangle {
sourceAspect := float32(ctx.SourceWidth) / float32(ctx.SourceHeight)
targetAspect := float32(ctx.TargetBounds.Width) / float32(ctx.TargetBounds.Height)
outputWidth := ctx.TargetBounds.Width
outputHeight := ctx.TargetBounds.Height
if sourceAspect < targetAspect {
// source is relatively taller than the target
// so we set the output height to the target height
// and calculate the width based on source aspect and center
outputWidth = int32(float32(outputHeight) * sourceAspect)
} else {
// source is relatively wider than the target
// so we set the output width to the target width
// and calculate the height based on source aspect and center
outputHeight = int32(float32(outputWidth) / sourceAspect)
}
// output width and height are correct -- center within TargetBounds
x := ctx.TargetBounds.X + ctx.TargetBounds.Width / 2.0 - outputWidth / 2.0
y := ctx.TargetBounds.Y + ctx.TargetBounds.Height / 2.0 - outputHeight / 2.0
return rl.Rectangle{
X: float32(x), Y: float32(y),
Width: float32(outputWidth),
Height: float32(outputHeight),
}
}
func (s *Sketch) CalcViewport(ctx *RenderCtx) rl.Rectangle {
func (s *Sketch) CalcViewport(ctx *RenderCtx) g.Rect {
viewportWidth := rl.Clamp(float32(ctx.SourceWidth)/s.cam.Zoom, 0, float32(ctx.SourceWidth))
viewportHeight := rl.Clamp(float32(ctx.SourceHeight)/s.cam.Zoom, 0, float32(ctx.SourceHeight))
return rl.Rectangle{
return g.Rect {
X: rl.Clamp(s.cam.LookAt.X-viewportWidth/2.0, 0, float32(ctx.SourceWidth)-viewportWidth),
Y: rl.Clamp(s.cam.LookAt.Y-viewportHeight/2.0, 0, float32(ctx.SourceHeight)-viewportHeight),
Width: float32(viewportWidth),
@@ -326,7 +301,7 @@ type ColorLayer struct {
}
func (cl *ColorLayer) Update(ctx *RenderCtx) {
;
}
func (cl *ColorLayer) Draw(ctx *RenderCtx) {
@@ -353,7 +328,7 @@ func NewImageLayer(path string) *ImageLayer {
}
func (il *ImageLayer) Update(ctx *RenderCtx) {
;
}
func (il *ImageLayer) Draw(ctx *RenderCtx) {
@@ -370,7 +345,7 @@ type TestPattern struct{
}
func (tp *TestPattern) Update(ctx *RenderCtx) {
;
}
func (tp *TestPattern) Draw(ctx *RenderCtx) {
@@ -379,7 +354,7 @@ func (tp *TestPattern) Draw(ctx *RenderCtx) {
centerX := float32(ctx.SourceWidth) / 2
centerY := float32(ctx.SourceHeight) / 2
rl.DrawRectangleRec(rl.Rectangle{X: 0, Y: 0, Width: centerX, Height: centerY}, rl.Red)
rl.DrawRectangleRec(*g.Rect{X: 0, Y: 0, Width: centerX, Height: centerY}.ToRL(), rl.Red)
rl.DrawRectangleRec(rl.Rectangle{X: centerX, Y: 0, Width: centerX, Height: centerY}, rl.Green)
rl.DrawRectangleRec(rl.Rectangle{X: 0, Y: centerY, Width: centerX, Height: centerY}, rl.Blue)
rl.DrawRectangleRec(rl.Rectangle{X: centerX, Y: centerY, Width: centerX, Height: centerY}, rl.White)