I've been learning how to do opengl/webgl graphics in Golang using 
https://github.com/goxjs/
It has conditional compilation so it can be compiled and run on either 
desktop as usual, or with gopherjs for web.

Most recently, I've made a spinning cube demo: 
https://omustardo.github.io/textured-cube/index.html
Code: https://github.com/Omustardo/demos/tree/master/texturedcube
Based off of an intro WebGL 
tutorial: 
https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Using_textures_in_WebGL

It worked out nicely, but gave me a bit of trouble when it came to loading 
the texture file. In order to maintain the desktop+web support, I had to 
add conditional compilation and deal with each case separately. The 
solution uses standard file reading for desktop and a http.Get request for 
web. I hard-coded my workspace base directory for desktop, and used the 
relative "assets/image.png" for web. Relevant code is in the link above in 
the assetloader folder. 

The issues that I've found with this approach are:
* I'm unable to run `gopherjs build` and load the demo locally because 
http.Get doesn't load local files. I can still use `gopherjs serve` so it 
isn't too bad.
* Hardcoding the base directory won't work well for collaboration / sharing.

I don't think there's a way to get around conditional compilation without 
embedding static asset files in the binary, which I'd rather avoid.
For the desktop version, I've considered using 
filepath.Join(os.Getenv("GOPATH"), "path/to/demo/basedir") rather than 
hardcoding the base directory, but then it depends on people having GOPATH 
set, and having only a single path (as opposed to multiple semicolon 
delimited ones). I also thought about somehow making symlinks to the assets 
directory, but that seems worse.
I don't have any better ideas for the web version, but I also haven't done 
much web development so perhaps someone knows a better way to get local 
assets in javascript than http.Get?

Thanks,
Omar

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to