@@ -7,6 +7,7 @@ package main
77import (
88 "fmt"
99 "github.com/go-gl/gl/v3.3-core/gl"
10+ "github.com/go-gl/mathgl/mgl32"
1011 "github.com/jackmott/gogl"
1112 "github.com/veandco/go-sdl2/sdl"
1213)
@@ -45,28 +46,68 @@ func main() {
4546 texture := gogl .LoadTextureAlpha ("assets/tex.png" )
4647
4748 vertices := []float32 {
48- 0.5 , 0.5 , 0.0 , 1.0 , 1.0 ,
49- 0.5 , - 0.5 , 0.0 , 1.0 , 0.0 ,
50- - 0.5 , - 0.5 , 0.0 , 0.0 , 0.0 ,
51- - 0.5 , 0.5 , 0.0 , 0.0 , 1.0 }
52-
53- indices := []uint32 {
54- 0 , 1 , 3 , // triangle 1
55- 1 , 2 , 3 , // triangle 2
56- }
49+ - 0.5 , - 0.5 , - 0.5 , 0.0 , 0.0 ,
50+ 0.5 , - 0.5 , - 0.5 , 1.0 , 0.0 ,
51+ 0.5 , 0.5 , - 0.5 , 1.0 , 1.0 ,
52+ 0.5 , 0.5 , - 0.5 , 1.0 , 1.0 ,
53+ - 0.5 , 0.5 , - 0.5 , 0.0 , 1.0 ,
54+ - 0.5 , - 0.5 , - 0.5 , 0.0 , 0.0 ,
55+
56+ - 0.5 , - 0.5 , 0.5 , 0.0 , 0.0 ,
57+ 0.5 , - 0.5 , 0.5 , 1.0 , 0.0 ,
58+ 0.5 , 0.5 , 0.5 , 1.0 , 1.0 ,
59+ 0.5 , 0.5 , 0.5 , 1.0 , 1.0 ,
60+ - 0.5 , 0.5 , 0.5 , 0.0 , 1.0 ,
61+ - 0.5 , - 0.5 , 0.5 , 0.0 , 0.0 ,
62+
63+ - 0.5 , 0.5 , 0.5 , 1.0 , 0.0 ,
64+ - 0.5 , 0.5 , - 0.5 , 1.0 , 1.0 ,
65+ - 0.5 , - 0.5 , - 0.5 , 0.0 , 1.0 ,
66+ - 0.5 , - 0.5 , - 0.5 , 0.0 , 1.0 ,
67+ - 0.5 , - 0.5 , 0.5 , 0.0 , 0.0 ,
68+ - 0.5 , 0.5 , 0.5 , 1.0 , 0.0 ,
69+
70+ 0.5 , 0.5 , 0.5 , 1.0 , 0.0 ,
71+ 0.5 , 0.5 , - 0.5 , 1.0 , 1.0 ,
72+ 0.5 , - 0.5 , - 0.5 , 0.0 , 1.0 ,
73+ 0.5 , - 0.5 , - 0.5 , 0.0 , 1.0 ,
74+ 0.5 , - 0.5 , 0.5 , 0.0 , 0.0 ,
75+ 0.5 , 0.5 , 0.5 , 1.0 , 0.0 ,
76+
77+ - 0.5 , - 0.5 , - 0.5 , 0.0 , 1.0 ,
78+ 0.5 , - 0.5 , - 0.5 , 1.0 , 1.0 ,
79+ 0.5 , - 0.5 , 0.5 , 1.0 , 0.0 ,
80+ 0.5 , - 0.5 , 0.5 , 1.0 , 0.0 ,
81+ - 0.5 , - 0.5 , 0.5 , 0.0 , 0.0 ,
82+ - 0.5 , - 0.5 , - 0.5 , 0.0 , 1.0 ,
83+
84+ - 0.5 , 0.5 , - 0.5 , 0.0 , 1.0 ,
85+ 0.5 , 0.5 , - 0.5 , 1.0 , 1.0 ,
86+ 0.5 , 0.5 , 0.5 , 1.0 , 0.0 ,
87+ 0.5 , 0.5 , 0.5 , 1.0 , 0.0 ,
88+ - 0.5 , 0.5 , 0.5 , 0.0 , 0.0 ,
89+ - 0.5 , 0.5 , - 0.5 , 0.0 , 1.0 }
90+
91+ cubePositions := []mgl32.Vec3 {
92+ mgl32.Vec3 {0.0 , 0.0 , 0.0 },
93+ mgl32.Vec3 {2.0 , 5.0 , - 10.0 },
94+ mgl32.Vec3 {1.0 , - 5.0 , 1.0 }}
5795
5896 gogl .GenBindBuffer (gl .ARRAY_BUFFER )
5997 VAO := gogl .GenBindVertexArray ()
6098 gogl .BufferDataFloat (gl .ARRAY_BUFFER , vertices , gl .STATIC_DRAW )
61- gogl .GenBindBuffer (gl .ELEMENT_ARRAY_BUFFER )
62- gogl .BufferDataInt (gl .ELEMENT_ARRAY_BUFFER , indices , gl .STATIC_DRAW )
6399
64100 gl .VertexAttribPointer (0 , 3 , gl .FLOAT , false , 5 * 4 , nil )
65101 gl .EnableVertexAttribArray (0 )
66102 gl .VertexAttribPointer (1 , 2 , gl .FLOAT , false , 5 * 4 , gl .PtrOffset (3 * 4 ))
67103 gl .EnableVertexAttribArray (1 )
68104 gogl .UnbindVertexArray ()
69- var x float32 = 0.0
105+
106+ keyboardState := sdl .GetKeyboardState ()
107+
108+ var x float32
109+ var z float32
110+
70111 for {
71112 for event := sdl .PollEvent (); event != nil ; event = sdl .PollEvent () {
72113 switch event .(type ) {
@@ -78,14 +119,38 @@ func main() {
78119 gl .ClearColor (0.0 , 0.0 , 0.0 , 0.0 )
79120 gl .Clear (gl .COLOR_BUFFER_BIT )
80121
122+ if keyboardState [sdl .SCANCODE_LEFT ] != 0 {
123+ x = x - .1
124+ }
125+ if keyboardState [sdl .SCANCODE_RIGHT ] != 0 {
126+ x = x + .1
127+ }
128+ if keyboardState [sdl .SCANCODE_UP ] != 0 {
129+ z = z + .1
130+ }
131+ if keyboardState [sdl .SCANCODE_DOWN ] != 0 {
132+ z = z - .1
133+ }
134+
81135 shaderProgram .Use ()
82- shaderProgram .SetFloat ("x" , x )
83- shaderProgram .SetFloat ("y" , 0.0 )
136+ projectionMatrix := mgl32 .Perspective (mgl32 .DegToRad (45.0 ), float32 (winWidth )/ float32 (winHeight ), 0.1 , 100.0 )
137+ viewMatrix := mgl32 .Ident4 ()
138+ viewMatrix = mgl32 .Translate3D (x , 0.0 , z )
139+ shaderProgram .SetMat4 ("projection" , projectionMatrix )
140+ shaderProgram .SetMat4 ("view" , viewMatrix )
84141 gogl .BindTexture (texture )
85142 gogl .BindVertexArray (VAO )
86- gl .DrawElements (gl .TRIANGLES , 6 , gl .UNSIGNED_INT , gl .PtrOffset (0 ))
143+ for i , pos := range cubePositions {
144+ modelMatrix := mgl32 .Ident4 ()
145+ angle := 20.0 * float32 (i )
146+ //todo normalize vec3?
147+ modelMatrix = mgl32 .HomogRotate3D (mgl32 .DegToRad (angle ), mgl32.Vec3 {1.0 , 0.3 , 0.5 }).Mul4 (modelMatrix )
148+ modelMatrix = mgl32 .Translate3D (pos .X (), pos .Y (), pos .Z ()).Mul4 (modelMatrix )
149+ shaderProgram .SetMat4 ("model" , modelMatrix )
150+ gl .DrawArrays (gl .TRIANGLES , 0 , 36 )
151+ }
152+
87153 window .GLSwap ()
88154 shaderProgram .CheckShaderForChanges ()
89- x = x + .01
90155 }
91156}
0 commit comments