-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
222 lines (169 loc) · 5.5 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"fmt"
"math"
"time"
"github.com/gowebapi/webapi"
"github.com/gowebapi/webapi/core/js"
"github.com/gowebapi/webapi/core/jsconv"
"github.com/gowebapi/webapi/graphics/webgl"
"github.com/gowebapi/webapi/html/canvas"
)
//see https://github.com/golang/go/wiki/WebAssembly
//see https://github.com/bobcob7/wasm-basic-triangle
var gl *webgl.RenderingContext
var vBuffer, iBuffer *webgl.Buffer
var icount int
var prog *webgl.Program
var angle float32
var width, height int
func main() {
c := make(chan struct{}, 0)
fmt.Println("Go/WASM loaded")
addCanvas()
<-c
}
func addCanvas() {
doc := webapi.GetWindow().Document()
app := doc.GetElementById("app")
body := doc.GetElementById("body")
width := body.ClientWidth()
height := body.ClientHeight()
canvasE := webapi.GetWindow().Document().CreateElement("canvas", &webapi.Union{js.ValueOf("dom.Node")})
canvasE.SetId("canvas")
app.AppendChild(&canvasE.Node)
canvasHTML := canvas.HTMLCanvasElementFromWrapper(canvasE)
canvasHTML.SetWidth(uint(width))
canvasHTML.SetHeight(uint(height))
contextU := canvasHTML.GetContext("webgl", nil)
gl = webgl.RenderingContextFromWrapper(contextU)
vBuffer, iBuffer, icount = createBuffers(gl)
prog = setupShaders(gl)
// Start the animation loop
js.Global().Call("requestAnimationFrame", js.FuncOf(drawScene))
}
func drawScene(this js.Value, p []js.Value) interface{} {
// Start a timer
startTime := time.Now()
angle += 0.01 // Update the angle for rotation
gl.ClearColor(0.5, 0.5, 0.5, 0.9)
gl.Clear(webgl.COLOR_BUFFER_BIT)
// Enable the depth test
gl.Enable(webgl.DEPTH_TEST)
// Set the view port
gl.Viewport(0, 0, 800, 800)
// Update the model-view matrix for rotation
rotationMatrix := getRotationMatrix(angle)
coord := gl.GetAttribLocation(prog, "coordinates")
// Bind vertex buffer object
gl.BindBuffer(webgl.ARRAY_BUFFER, vBuffer)
// Bind index buffer object
gl.BindBuffer(webgl.ELEMENT_ARRAY_BUFFER, iBuffer)
// Point an attribute to the currently bound VBO
gl.VertexAttribPointer(uint(coord), 3, webgl.FLOAT, false, 0, 0)
// Enable the attribute
gl.EnableVertexAttribArray(uint(coord))
// Set the model-view matrix in the vertex shader
modelviewLoc := gl.GetUniformLocation(prog, "modelview")
gl.UniformMatrix4fv(modelviewLoc, false, rotationMatrix)
// Draw the triangle
gl.DrawElements(webgl.TRIANGLES, icount, webgl.UNSIGNED_SHORT, 0)
// Stop the timer and calculate the FPS
endTime := time.Now()
elapsedTime := endTime.Sub(startTime)
fps := float64(1.0 / elapsedTime.Seconds())
// Display the FPS
fpsDisplay := fmt.Sprintf("FPS: %.2f", fps)
doc := webapi.GetWindow().Document()
fpsElem := doc.GetElementById("fps")
fpsElem.SetTextContent(&fpsDisplay)
// Request the next animation frame
js.Global().Call("requestAnimationFrame", js.FuncOf(drawScene))
return nil
}
func createBuffers(gl *webgl.RenderingContext) (*webgl.Buffer, *webgl.Buffer, int) {
//// VERTEX BUFFER ////
var verticesNative = []float32{
-0.5, 0.5, 0,
-0.5, -0.5, 0,
0.5, -0.5, 0,
}
var vertices = jsconv.Float32ToJs(verticesNative)
// Create buffer
vBuffer := gl.CreateBuffer()
// Bind to buffer
gl.BindBuffer(webgl.ARRAY_BUFFER, vBuffer)
// Pass data to buffer
gl.BufferData2(webgl.ARRAY_BUFFER, webgl.UnionFromJS(vertices), webgl.STATIC_DRAW)
// Unbind buffer
gl.BindBuffer(webgl.ARRAY_BUFFER, &webgl.Buffer{})
// INDEX BUFFER ////
var indicesNative = []uint32{
2, 1, 0,
}
var indices = jsconv.UInt32ToJs(indicesNative)
// Create buffer
iBuffer := gl.CreateBuffer()
// Bind to buffer
gl.BindBuffer(webgl.ELEMENT_ARRAY_BUFFER, iBuffer)
// Pass data to buffer
gl.BufferData2(webgl.ELEMENT_ARRAY_BUFFER, webgl.UnionFromJS(indices), webgl.STATIC_DRAW)
// Unbind buffer
gl.BindBuffer(webgl.ELEMENT_ARRAY_BUFFER, &webgl.Buffer{})
return vBuffer, iBuffer, len(indicesNative)
}
func setupShaders(gl *webgl.RenderingContext) *webgl.Program {
// Vertex shader source code
vertCode := `
attribute vec3 coordinates;
uniform mat4 modelview;
void main(void) {
gl_Position = modelview * vec4(coordinates, 1.0);
}
`
// Create a vertex shader object
vShader := gl.CreateShader(webgl.VERTEX_SHADER)
// Attach vertex shader source code
gl.ShaderSource(vShader, vertCode)
// Compile the vertex shader
gl.CompileShader(vShader)
//fragment shader source code
fragCode := `
void main(void) {
gl_FragColor = vec4(0.0, 1.0, 0.0, 0.7);
}`
// Create fragment shader object
fShader := gl.CreateShader(webgl.FRAGMENT_SHADER)
// Attach fragment shader source code
gl.ShaderSource(fShader, fragCode)
// Compile the fragmentt shader
gl.CompileShader(fShader)
// Create a shader program object to store
// the combined shader program
prog := gl.CreateProgram()
// Attach a vertex shader
gl.AttachShader(prog, vShader)
// Attach a fragment shader
gl.AttachShader(prog, fShader)
// Link both the programs
gl.LinkProgram(prog)
// Use the combined shader program object
gl.UseProgram(prog)
// Get the location of the model-view matrix in the vertex shader
modelviewLoc := gl.GetUniformLocation(prog, "modelview")
// Set the initial model-view matrix
rotationMatrix := getRotationMatrix(angle)
gl.UniformMatrix4fv(modelviewLoc, false, rotationMatrix)
return prog
}
func getRotationMatrix(angle float32) *webgl.Union {
// Create a 2D rotation matrix for the given angle
cosA := float32(math.Cos(float64(angle)))
sinA := float32(math.Sin(float64(angle)))
return webgl.UnionFromJS(jsconv.Float32ToJs([]float32{
cosA, -sinA, 0, 0,
sinA, cosA, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
}))
}