1- // Make shader hotloading fail gracefully and retry
2- // Make an attribpointer helper function
3- // Make a texture
4-
51package main
62
73import (
@@ -13,7 +9,7 @@ import (
139 "time"
1410)
1511
16- const winWidth = 1280
12+ const winWidth = 720
1713const winHeight = 720
1814
1915func main () {
@@ -91,19 +87,45 @@ func main() {
9187 - 0.5 , 0.5 , 0.5 , 0.0 , 0.0 ,
9288 - 0.5 , 0.5 , - 0.5 , 0.0 , 1.0 }
9389
90+ normals := make ([]float32 , 36 * 3 )
91+ for tri := 0 ; tri < 12 ; tri ++ {
92+ index := tri * 15
93+ p1 := mgl32.Vec3 {vertices [index ], vertices [index + 1 ], vertices [index + 2 ]}
94+ index += 5
95+ p2 := mgl32.Vec3 {vertices [index ], vertices [index + 1 ], vertices [index + 2 ]}
96+ index += 5
97+ p3 := mgl32.Vec3 {vertices [index ], vertices [index + 1 ], vertices [index + 2 ]}
98+ normal := gogl .TriangleNormal (p1 , p2 , p3 )
99+ normals [tri * 9 ] = normal .X ()
100+ normals [tri * 9 + 1 ] = normal .Y ()
101+ normals [tri * 9 + 2 ] = normal .Z ()
102+
103+ normals [tri * 9 + 3 ] = normal .X ()
104+ normals [tri * 9 + 4 ] = normal .Y ()
105+ normals [tri * 9 + 5 ] = normal .Z ()
106+
107+ normals [tri * 9 + 6 ] = normal .X ()
108+ normals [tri * 9 + 7 ] = normal .Y ()
109+ normals [tri * 9 + 8 ] = normal .Z ()
110+
111+ }
112+
94113 cubePositions := []mgl32.Vec3 {
95114 mgl32.Vec3 {0.0 , 0.0 , 0.0 },
96115 mgl32.Vec3 {2.0 , 5.0 , - 10.0 },
97116 mgl32.Vec3 {1.0 , - 5.0 , 1.0 }}
98117
99- gogl .GenBindBuffer (gl .ARRAY_BUFFER )
100118 VAO := gogl .GenBindVertexArray ()
119+ gogl .GenBindBuffer (gl .ARRAY_BUFFER )
101120 gogl .BufferDataFloat (gl .ARRAY_BUFFER , vertices , gl .STATIC_DRAW )
102-
103121 gl .VertexAttribPointer (0 , 3 , gl .FLOAT , false , 5 * 4 , nil )
104122 gl .EnableVertexAttribArray (0 )
105123 gl .VertexAttribPointer (1 , 2 , gl .FLOAT , false , 5 * 4 , gl .PtrOffset (3 * 4 ))
106124 gl .EnableVertexAttribArray (1 )
125+ gogl .GenBindBuffer (gl .ARRAY_BUFFER )
126+ gogl .BufferDataFloat (gl .ARRAY_BUFFER , normals , gl .STATIC_DRAW )
127+ gl .VertexAttribPointer (2 , 3 , gl .FLOAT , false , 3 * 4 , nil )
128+ gl .EnableVertexAttribArray (2 )
107129 gogl .UnbindVertexArray ()
108130
109131 keyboardState := sdl .GetKeyboardState ()
@@ -149,6 +171,8 @@ func main() {
149171 viewMatrix := camera .GetViewMatrix ()
150172 shaderProgram .SetMat4 ("projection" , projectionMatrix )
151173 shaderProgram .SetMat4 ("view" , viewMatrix )
174+ shaderProgram .SetVec3 ("lightPos" , mgl32.Vec3 {2.0 , 5.0 , 5.0 })
175+ shaderProgram .SetVec3 ("lightColor" , mgl32.Vec3 {1.0 , 0.0 , 0.0 })
152176 gogl .BindTexture (texture )
153177 gogl .BindVertexArray (VAO )
154178 for i , pos := range cubePositions {
0 commit comments