-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_rotatingcube.go
176 lines (147 loc) · 5.94 KB
/
app_rotatingcube.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
package main
import (
"image"
"image/color"
"log"
n "github.com/lachee/noodle"
)
//RotatingCubeApp shows off the 3D capabilities
type RotatingCubeApp struct {
vertexBuffer n.WebGLBuffer
colorBuffer n.WebGLBuffer
indexBuffer n.WebGLBuffer
uvBuffer n.WebGLBuffer
shader *n.Shader
uProjMatrixLoc n.WebGLUniformLocation
uViewMatrixLoc n.WebGLUniformLocation
uModelMatrixLoc n.WebGLUniformLocation
uSamplerLoc n.WebGLUniformLocation
projMatrix Matrix
viewMatrix Matrix
modelMatrix Matrix
moveMatrix Matrix
rotation float32
texture *n.Texture
}
func (app *RotatingCubeApp) prepareImage() (*n.Image, error) {
//Size of the image
const width = 255
const height = 255
//Create the image
upLeft := image.Point{0, 0}
lowRight := image.Point{width, height}
img := image.NewRGBA(image.Rectangle{upLeft, lowRight})
// Set color for each pixel.
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
color := color.RGBA{uint8(x), uint8(y), 128, 0xff}
img.Set(x, y, color)
}
}
return n.LoadImageRGBA(img)
//return n.LoadImage("resources/moomin.png") // The image URL
}
//Start is called by the noodle engine when ready
func (app *RotatingCubeApp) Start() bool {
// Create vertex buffer
app.vertexBuffer = n.GL.NewBuffer(n.GlArrayBuffer, rotCubeVerts, n.GlStaticDraw)
app.colorBuffer = n.GL.NewBuffer(n.GlArrayBuffer, rotCubeColours, n.GlStaticDraw)
app.indexBuffer = n.GL.NewBuffer(n.GlElementArrayBuffer, rotCubeTris, n.GlStaticDraw)
app.uvBuffer = n.GL.NewBuffer(n.GlArrayBuffer, rotCubeUV, n.GlStaticDraw)
// == Load the cube image and the shaders
image, err := app.prepareImage()
if err != nil {
log.Fatalln("Failed to load image", err)
return false
}
app.texture = n.NewTexture(image)
//Load the cube shader
shader, err := n.LoadShaderFromURL("resources/shader/rotatingCube.vert", "resources/shader/rotatingCube.frag")
if err != nil {
log.Fatalln("Failed to load the shaders! ", err)
return false
}
app.shader = shader
// == Link the shaders up
// Associate attributes to vertex shader
app.uProjMatrixLoc = app.shader.GetUniformLocation("Pmatrix")
app.uViewMatrixLoc = app.shader.GetUniformLocation("Vmatrix")
app.uModelMatrixLoc = app.shader.GetUniformLocation("Mmatrix")
app.uSamplerLoc = app.shader.GetUniformLocation("uSampler")
app.shader.BindVertexData("position", n.GlArrayBuffer, app.vertexBuffer, 3, n.GlFloat, false, 0, 0)
app.shader.BindVertexData("color", n.GlArrayBuffer, app.colorBuffer, 3, n.GlFloat, false, 0, 0)
app.shader.BindVertexData("textureCoord", n.GlArrayBuffer, app.uvBuffer, 2, n.GlFloat, false, 0, 0)
app.shader.Use()
// == Set WeebGL properties
n.GL.ClearDepth(1)
n.GL.DepthFunc(n.GlLEqual)
// == Create Matrixes
// Generate and apply projection matrix
app.projMatrix = n.NewMatrixPerspective(45.0, n.GL.AspectRatio(), 1, 100.0)
n.GL.UniformMatrix4fv(app.uProjMatrixLoc, app.projMatrix)
// Generate and apply view matrix
app.viewMatrix = n.NewMatrixLookAt(Vector3{3.0, 3.0, 3.0}, Vector3{0, 0, 0}, Vector3{0, 1, 0})
n.GL.UniformMatrix4fv(app.uViewMatrixLoc, app.viewMatrix)
//Update the texture shit
// Activate the text0, tell the texture to bind, then tell the
// sampler that it should be on 0
app.texture.SetSampler(app.uSamplerLoc, 0)
return true
}
//Update occurs once a frame
func (app *RotatingCubeApp) Update(dt float32) {
app.rotation = app.rotation + dt/500*5
//Update the move matrix
//movMatrix := n.NewMatrixRotate(n.NewVector3Up(), 0.5)
//movMatrix = movMatrix.Multiply(n.NewMatrixRotate(n.NewVector3Forward(), 0.3*app.rotation))
//movMatrix = movMatrix.Multiply(n.NewMatrixRotate(n.NewVector3Right(), 0.2*app.rotation))
//app.moveMatrix = movMatrix
}
//Render occurs when the screen needs updating
func (app *RotatingCubeApp) Render() {
//Update matrix
n.GL.UniformMatrix4fv(app.uModelMatrixLoc, app.moveMatrix)
//Clear
n.GL.BindBuffer(n.GlElementArrayBuffer, app.indexBuffer)
n.GL.Enable(n.GlDepthTest)
n.GL.Clear(n.GlColorBufferBit | n.GlDepthBufferBit)
// Draw the cube
n.GL.DrawElements(n.GlTriangles, len(rotCubeTris), n.GlUnsignedShort, 0)
}
var rotCubeVerts = []Vector3{
Vector3{-1, -1, -1}, Vector3{1, -1, -1}, Vector3{1, 1, -1}, Vector3{-1, 1, -1},
Vector3{-1, -1, 1}, Vector3{1, -1, 1}, Vector3{1, 1, 1}, Vector3{-1, 1, 1},
Vector3{-1, -1, -1}, Vector3{-1, 1, -1}, Vector3{-1, 1, 1}, Vector3{-1, -1, 1},
Vector3{1, -1, -1}, Vector3{1, 1, -1}, Vector3{1, 1, 1}, Vector3{1, -1, 1},
Vector3{-1, -1, -1}, Vector3{-1, -1, 1}, Vector3{1, -1, 1}, Vector3{1, -1, -1},
Vector3{-1, 1, -1}, Vector3{-1, 1, 1}, Vector3{1, 1, 1}, Vector3{1, 1, -1},
}
var rotCubeVertsSquish = []Vector3{
Vector3{-0.5, -1, -1}, Vector3{0.5, -1, -1}, Vector3{0.5, 1, -1}, Vector3{-0.5, 1, -1},
Vector3{-1, -1, 1}, Vector3{1, -1, 1}, Vector3{1, 1, 1}, Vector3{-1, 1, 1},
Vector3{-0.5, -1, -1}, Vector3{-0.5, 1, -1}, Vector3{-1, 1, 1}, Vector3{-1, -1, 1},
Vector3{0.5, -1, -1}, Vector3{0.5, 1, -1}, Vector3{1, 1, 1}, Vector3{1, -1, 1},
Vector3{-0.5, -1, -1}, Vector3{-1, -1, 1}, Vector3{1, -1, 1}, Vector3{0.5, -1, -1},
Vector3{-0.5, 1, -1}, Vector3{-1, 1, 1}, Vector3{1, 1, 1}, Vector3{0.5, 1, -1},
}
var rotCubeUV = []Vector2{
Vector2{0.0, 0.0}, Vector2{1.0, 0.0}, Vector2{1.0, 1.0}, Vector2{0.0, 1.0},
Vector2{0.0, 0.0}, Vector2{1.0, 0.0}, Vector2{1.0, 1.0}, Vector2{0.0, 1.0},
Vector2{0.0, 0.0}, Vector2{1.0, 0.0}, Vector2{1.0, 1.0}, Vector2{0.0, 1.0},
Vector2{0.0, 0.0}, Vector2{1.0, 0.0}, Vector2{1.0, 1.0}, Vector2{0.0, 1.0},
Vector2{0.0, 0.0}, Vector2{1.0, 0.0}, Vector2{1.0, 1.0}, Vector2{0.0, 1.0},
Vector2{0.0, 0.0}, Vector2{1.0, 0.0}, Vector2{1.0, 1.0}, Vector2{0.0, 1.0},
}
var rotCubeColours = []float32{
5, 3, 7, 5, 3, 7, 5, 3, 7, 5, 3, 7,
1, 1, 3, 1, 1, 3, 1, 1, 3, 1, 1, 3,
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
}
var rotCubeTris = []uint16{
0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7,
8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15,
16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23,
}