├── .gitignore ├── bundle.wasm ├── go.mod ├── go.sum ├── makefile ├── README.md ├── index.html ├── main.go ├── gltypes ├── gltypes.go └── arrays.go ├── static └── index.html ├── bundle.go └── wasm_exec.js /.gitignore: -------------------------------------------------------------------------------- 1 | wasm-rotating-cube 2 | .idea 3 | -------------------------------------------------------------------------------- /bundle.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bobcob7/wasm-rotating-cube/HEAD/bundle.wasm -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/bobcob7/wasm-rotating-cube 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/go-gl/mathgl v0.0.0-20180804195959-cdf14b6b8f8a 7 | golang.org/x/image v0.0.0-20180926015637-991ec62608f3 // indirect 8 | ) 9 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/go-gl/mathgl v0.0.0-20180804195959-cdf14b6b8f8a h1:2n5w2v3knlspzjJWyQPC0j88Mwvq0SZV0Jdws34GJwc= 2 | github.com/go-gl/mathgl v0.0.0-20180804195959-cdf14b6b8f8a/go.mod h1:dvrdneKbyWbK2skTda0nM4B9zSlS2GZSnjX7itr/skQ= 3 | golang.org/x/image v0.0.0-20180926015637-991ec62608f3 h1:5IfA9fqItkh2alJW94tvQk+6+RF9MW2q9DzwE8DBddQ= 4 | golang.org/x/image v0.0.0-20180926015637-991ec62608f3/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= 5 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | GOROOT := $(shell go env GOROOT) 2 | 3 | all: wasm-rotating-cube 4 | 5 | wasm-rotating-cube: output.wasm main.go 6 | go build -o wasm-rotating-cube main.go 7 | 8 | output.wasm: bundle.go wasm_exec.js 9 | GOOS=js GOARCH=wasm go build -o bundle.wasm bundle.go 10 | 11 | run: output.wasm wasm-rotating-cube 12 | ./wasm-rotating-cube 13 | 14 | wasm_exec.js: 15 | cp $(GOROOT)/misc/wasm/wasm_exec.js . 16 | 17 | clean: 18 | rm -f wasm-rotating-cube wasm_exec.js *.wasm 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WASM Rotating Cube 2 | 3 | This project is built on my wasm-basic-triangle project, Check it out for a more basic example. Instead of drawing a 2D triangle, this project draws a 4 | 3D cube and rotates it. 5 | 6 | # Demo 7 | To see this project running check out this link: [https://bobcob7.github.io/wasm-rotating-cube/](https://bobcob7.github.io/wasm-rotating-cube/) 8 | 9 | # Running 10 | To run you can use the makefile with `make run` 11 | 12 | # References: 13 | - https://www.tutorialspoint.com/webgl/webgl_cube_rotation.htm 14 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 11 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | "log" 6 | "io/ioutil" 7 | "runtime" 8 | ) 9 | 10 | func main() { 11 | indexData, err := ioutil.ReadFile("static/index.html") 12 | if err != nil { 13 | log.Fatalf("Could not read index file: %s\n", err) 14 | } 15 | 16 | wasmExecLocation := runtime.GOROOT() + "/misc/wasm/wasm_exec.js" 17 | wasmExecData, err := ioutil.ReadFile(wasmExecLocation) 18 | if err != nil { 19 | log.Fatalf("Could not read wasm_exec file: %s\n", err) 20 | } 21 | 22 | wasmData, err := ioutil.ReadFile("bundle.wasm") 23 | if err != nil { 24 | log.Fatalf("Could not read wasm file: %s\n", err) 25 | } 26 | 27 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 28 | w.Write(indexData) 29 | }) 30 | 31 | http.HandleFunc("/wasm_exec.js", func(w http.ResponseWriter, r *http.Request) { 32 | w.Write(wasmExecData) 33 | }) 34 | 35 | http.HandleFunc("/bundle.wasm", func(w http.ResponseWriter, r *http.Request) { 36 | w.Header().Set("Content-Type", "application/wasm") 37 | w.WriteHeader(http.StatusOK) 38 | w.Write(wasmData) 39 | }) 40 | 41 | log.Fatal(http.ListenAndServe(":8080", nil)) 42 | } -------------------------------------------------------------------------------- /gltypes/gltypes.go: -------------------------------------------------------------------------------- 1 | package gltypes 2 | 3 | import "syscall/js" 4 | 5 | // GLTypes provides WebGL bindings. 6 | type GLTypes struct { 7 | StaticDraw js.Value 8 | ArrayBuffer js.Value 9 | ElementArrayBuffer js.Value 10 | VertexShader js.Value 11 | FragmentShader js.Value 12 | Float js.Value 13 | DepthTest js.Value 14 | ColorBufferBit js.Value 15 | DepthBufferBit js.Value 16 | Triangles js.Value 17 | UnsignedShort js.Value 18 | LEqual js.Value 19 | LineLoop js.Value 20 | } 21 | 22 | // New grabs the WebGL bindings from a GL context. 23 | func (types *GLTypes) New(gl js.Value) { 24 | types.StaticDraw = gl.Get("STATIC_DRAW") 25 | types.ArrayBuffer = gl.Get("ARRAY_BUFFER") 26 | types.ElementArrayBuffer = gl.Get("ELEMENT_ARRAY_BUFFER") 27 | types.VertexShader = gl.Get("VERTEX_SHADER") 28 | types.FragmentShader = gl.Get("FRAGMENT_SHADER") 29 | types.Float = gl.Get("FLOAT") 30 | types.DepthTest = gl.Get("DEPTH_TEST") 31 | types.ColorBufferBit = gl.Get("COLOR_BUFFER_BIT") 32 | types.Triangles = gl.Get("TRIANGLES") 33 | types.UnsignedShort = gl.Get("UNSIGNED_SHORT") 34 | types.LEqual = gl.Get("LEQUAL") 35 | types.DepthBufferBit = gl.Get("DEPTH_BUFFER_BIT") 36 | types.LineLoop = gl.Get("LINE_LOOP") 37 | } 38 | -------------------------------------------------------------------------------- /static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |