Merge pull request #9 from basilarchia/master

set filename,width,height and drift from the command line
This commit is contained in:
Michal Štrba 2019-01-08 23:16:46 +01:00 committed by GitHub
commit ba4c6901ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 5 deletions

View File

@ -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

View File

@ -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?

View File

@ -2,6 +2,7 @@ package main
import (
"time"
"log"
"github.com/go-gl/mathgl/mgl32"
@ -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,
}
@ -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)
}
@ -46,6 +62,7 @@ func run() {
"uResolution", &uResolution,
"uTime", &uTime,
"uMouse", &uMouse,
"uDrift", &uDrift,
)
canvas.SetFragmentShader(fragSource)
@ -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)
}

View File

@ -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;
@ -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);