Skip to content

Commit

Permalink
set filename,width,height and drift from the command line
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Carr <[email protected]>
  • Loading branch information
Jeff Carr committed Jan 2, 2019
1 parent c732821 commit 8098225
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
18 changes: 18 additions & 0 deletions community/seascape-shader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# go get -u github.com/faiface/pixel-examples
# cd ~/go/src/github.com/faiface/pixel-examples/community/seascape-shader

all:
go build
./seascape-shader

push:
git pull
git add --all
-git commit -a -s
git push

update:
git pull

diff:
git diff
9 changes: 9 additions & 0 deletions community/seascape-shader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ Looking at the seascape.glsl example you can see **iResolution**, **iTime** and

These are commonly needed to be exposed because these things are coming from outside and needs to be updated. Any other variable you need to have changed/updated from code can be exposed like those.

## Command line arguments

```
./seascape-shader -h # will show you the command line options
./seascape-shader -filename ./shaders/seascape.glsl # Seascape
./seascape-shader -filename ./shaders/planetfall.glsl # Planet Fall demo
```

## Exposing variables

How to expose variables like this?
Expand Down
46 changes: 42 additions & 4 deletions community/seascape-shader/seascape.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"time"
"log"

"github.com/go-gl/mathgl/mgl32"

Expand All @@ -10,11 +11,25 @@ import (
"golang.org/x/image/colornames"
)

import "flag"
import "os"

var (
version string
race bool
debug = os.Getenv("BUILDDEBUG") != ""
filename string
width int
height int
timeout = "120s"
uDrift float32
)

func run() {
// Set up window configs
cfg := pixelgl.WindowConfig{ // Default: 1024 x 768
Title: "Golang Seascape from Shadertoy",
Bounds: pixel.R(0, 0, 1024, 768),
Title: "Golang GLSL",
Bounds: pixel.R(0, 0, float64(width), float64(height)),
VSync: true,
}

Expand All @@ -30,8 +45,9 @@ func run() {

// I am putting all shader example initializing stuff here for
// easier reference to those learning to use this functionality
fragSource, err := LoadFileToString("shaders/seascape.glsl")
// fragSource, err := LoadFileToString("shaders/planetfall.glsl")

fragSource, err := LoadFileToString(filename)

if err != nil {
panic(err)
}
Expand All @@ -46,6 +62,7 @@ func run() {
"uResolution", &uResolution,
"uTime", &uTime,
"uMouse", &uMouse,
"uDrift", &uDrift,
)

canvas.SetFragmentShader(fragSource)
Expand All @@ -69,6 +86,27 @@ func run() {

}

func parseFlags() {
flag.StringVar (&version, "version", "v0.1", "Set compiled in version string")
flag.StringVar (&filename, "filename", "shaders/seascape.glsl", "path to GLSL file")
flag.IntVar (&width, "width", 1024, "Width of the OpenGL Window")
flag.IntVar (&height, "height", 768, "Height of the OpenGL Window")
var tmp float64
flag.Float64Var (&tmp, "drift", 0.01, "Speed of the gradual camera drift")
flag.BoolVar (&race, "race", race, "Use race detector")

// this parses the arguements
flag.Parse()

uDrift = float32(tmp)
log.Println("width=",width)
log.Println("height=",height)
log.Println("uDrift=",uDrift)

}

func main() {
parseFlags()

pixelgl.Run(run)
}
5 changes: 4 additions & 1 deletion community/seascape-shader/shaders/seascape.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ uniform vec2 uResolution;
uniform float uTime;
uniform vec4 uMouse;

// This is how much you are drifting around. Zero means it only moves when the mouse moves
uniform float uDrift;

out vec4 fragColor;

const int NUM_STEPS = 8;
Expand Down Expand Up @@ -167,7 +170,7 @@ void main()
vec2 uv = gl_FragCoord.xy / uResolution.xy;
uv = uv * 2.0 - 1.0;
uv.x *= uResolution.x / uResolution.y;
float time = uTime * 0.3 + uMouse.x*0.01;
float time = uTime * uDrift * 5 + uMouse.x*0.01;

// ray
vec3 ang = vec3(sin(time*3.0)*0.1,sin(time)*0.2+0.3,time);
Expand Down

0 comments on commit 8098225

Please sign in to comment.