Skip to content

Commit 51f1136

Browse files
committed
coordinate systems
1 parent 44eede6 commit 51f1136

File tree

3 files changed

+95
-18
lines changed

3 files changed

+95
-18
lines changed

gogl/shader.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gogl
33
import (
44
"fmt"
55
"github.com/go-gl/gl/v3.3-core/gl"
6+
"github.com/go-gl/mathgl/mgl32"
67
"os"
78
"time"
89
)
@@ -42,6 +43,13 @@ func (shader *Shader) SetFloat(name string, f float32) {
4243
gl.Uniform1f(location, f)
4344
}
4445

46+
func (shader *Shader) SetMat4(name string, mat mgl32.Mat4) {
47+
name_cstr := gl.Str(name + "\x00")
48+
location := gl.GetUniformLocation(uint32(shader.id), name_cstr)
49+
m4 := [16]float32(mat)
50+
gl.UniformMatrix4fv(location, 1, false, &m4[0])
51+
}
52+
4553
func getModifiedTime(filePath string) (time.Time, error) {
4654

4755
file, err := os.Stat(filePath)

hellotriangle/hellotriangle.go

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package main
77
import (
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,
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, -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
}

hellotriangle/shaders/hello.vert

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ layout (location = 1) in vec2 aTexCoord;
44

55
out vec2 TexCoord;
66

7+
uniform mat4 model;
8+
uniform mat4 view;
9+
uniform mat4 projection;
10+
711
void main()
812
{
9-
gl_Position = vec4(aPos.x,aPos.y,aPos.z,1.0);
10-
TexCoord = aTexCoord;
13+
gl_Position = projection * view * model * vec4(aPos,1.0f);
14+
TexCoord = vec2(aTexCoord.x,1.0 - aTexCoord.y);
1115
}

0 commit comments

Comments
 (0)