set filename,width,height and drift from the command line

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-01-02 08:10:40 -08:00
parent c732821123
commit 80982259a0
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. 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 ## Exposing variables
How to expose variables like this? How to expose variables like this?

View File

@ -2,6 +2,7 @@ package main
import ( import (
"time" "time"
"log"
"github.com/go-gl/mathgl/mgl32" "github.com/go-gl/mathgl/mgl32"
@ -10,11 +11,25 @@ import (
"golang.org/x/image/colornames" "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() { func run() {
// Set up window configs // Set up window configs
cfg := pixelgl.WindowConfig{ // Default: 1024 x 768 cfg := pixelgl.WindowConfig{ // Default: 1024 x 768
Title: "Golang Seascape from Shadertoy", Title: "Golang GLSL",
Bounds: pixel.R(0, 0, 1024, 768), Bounds: pixel.R(0, 0, float64(width), float64(height)),
VSync: true, VSync: true,
} }
@ -30,8 +45,9 @@ func run() {
// I am putting all shader example initializing stuff here for // I am putting all shader example initializing stuff here for
// easier reference to those learning to use this functionality // 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 { if err != nil {
panic(err) panic(err)
} }
@ -46,6 +62,7 @@ func run() {
"uResolution", &uResolution, "uResolution", &uResolution,
"uTime", &uTime, "uTime", &uTime,
"uMouse", &uMouse, "uMouse", &uMouse,
"uDrift", &uDrift,
) )
canvas.SetFragmentShader(fragSource) 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() { func main() {
parseFlags()
pixelgl.Run(run) pixelgl.Run(run)
} }

View File

@ -10,6 +10,9 @@ uniform vec2 uResolution;
uniform float uTime; uniform float uTime;
uniform vec4 uMouse; 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; out vec4 fragColor;
const int NUM_STEPS = 8; const int NUM_STEPS = 8;
@ -167,7 +170,7 @@ void main()
vec2 uv = gl_FragCoord.xy / uResolution.xy; vec2 uv = gl_FragCoord.xy / uResolution.xy;
uv = uv * 2.0 - 1.0; uv = uv * 2.0 - 1.0;
uv.x *= uResolution.x / uResolution.y; 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 // ray
vec3 ang = vec3(sin(time*3.0)*0.1,sin(time)*0.2+0.3,time); vec3 ang = vec3(sin(time*3.0)*0.1,sin(time)*0.2+0.3,time);