Last active
August 29, 2015 14:02
-
-
Save kiljacken/89687636e0865bd053be to your computer and use it in GitHub Desktop.
Update to work with value matrices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/go-gl/gl" | |
glfw "github.com/go-gl/glfw3" | |
"github.com/kiljacken/gogame" | |
"github.com/kiljacken/gogame/math" | |
"log" | |
) | |
type Game struct { | |
shouldEnd bool | |
vertShader gl.Shader | |
fragShader gl.Shader | |
program gl.Program | |
triangleArray gl.VertexArray | |
triangleBuffer gl.Buffer | |
mvpLocation gl.UniformLocation | |
modelMatrix math.Matrix4f | |
viewMatrix math.Matrix4f | |
projectionMatrix math.Matrix4f | |
modelViewProjectionMatrix math.Matrix4f | |
} | |
func (g *Game) Init() { | |
log.Printf("g.Init()") | |
// Nothing to do here | |
} | |
func (g *Game) InitGL() { | |
log.Printf("g.InitGL()") | |
g.vertShader = gl.CreateShader(gl.VERTEX_SHADER) | |
g.vertShader.Source(`#version 330 core | |
layout(location = 0) in vec3 vertexPosition_modelspace; | |
uniform mat4 MVP; | |
void main() { | |
vec4 v = vec4(vertexPosition_modelspace, 1); | |
gl_Position = MVP * v; | |
}`) | |
g.vertShader.Compile() | |
if g.vertShader.Get(gl.COMPILE_STATUS) != gl.TRUE { | |
panic("vertex shader error: " + g.vertShader.GetInfoLog()) | |
} | |
g.fragShader = gl.CreateShader(gl.FRAGMENT_SHADER) | |
g.fragShader.Source(`#version 330 | |
out vec3 color; | |
void main() { | |
color = vec3(1, 0, 0); | |
}`) | |
g.fragShader.Compile() | |
if g.fragShader.Get(gl.COMPILE_STATUS) != gl.TRUE { | |
panic("fragment shader error: " + g.fragShader.GetInfoLog()) | |
} | |
g.program = gl.CreateProgram() | |
g.program.AttachShader(g.vertShader) | |
g.program.AttachShader(g.fragShader) | |
g.program.Link() | |
if g.program.Get(gl.LINK_STATUS) != gl.TRUE { | |
panic("program linker error: " + g.program.GetInfoLog()) | |
} | |
g.program.Use() | |
g.mvpLocation = g.program.GetUniformLocation("MVP") | |
g.program.Unuse() | |
{ | |
g.triangleArray = gl.GenVertexArray() | |
g.triangleArray.Bind() | |
g.triangleBuffer = gl.GenBuffer() | |
g.triangleBuffer.Bind(gl.ARRAY_BUFFER) | |
vertexData := [...]float32{ | |
-1.0, -1.0, 0.0, | |
1.0, -1.0, 0.0, | |
0.0, 1.0, 0.0, | |
} | |
gl.BufferData(gl.ARRAY_BUFFER, len(vertexData)*4, &vertexData, gl.STATIC_DRAW) | |
loc := g.program.GetAttribLocation("vertexPosition_modelspace") | |
loc.EnableArray() | |
loc.AttribPointer(3, gl.FLOAT, false, 0, nil) | |
g.triangleBuffer.Unbind(gl.ARRAY_BUFFER) | |
g.triangleArray.Unbind() | |
} | |
} | |
func (g *Game) Cleanup() { | |
log.Printf("g.Cleanup()") | |
// Nothing to do here | |
} | |
func (g *Game) CleanupGL() { | |
log.Printf("g.CleanupGL()") | |
g.program.Delete() | |
g.vertShader.Delete() | |
g.fragShader.Delete() | |
g.triangleArray.Delete() | |
g.triangleBuffer.Delete() | |
} | |
func (g *Game) HandleGlfwError(err glfw.ErrorCode, s string) { | |
log.Printf("g.HandleGlfwError()") | |
log.Printf("glfw error %d: %s", err, s) | |
} | |
func (g *Game) HandleReshape(window *glfw.Window, width, height int) { | |
log.Printf("g.HandleReshape()") | |
log.Printf("glfw: reshape %dx%d", width, height) | |
aspect := float32(width) / float32(height) | |
g.projectionMatrix = math.Perspective(45.0, aspect, 0.1, 100.0) | |
g.viewMatrix = math.LookAt(4.0, 3.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) | |
g.modelMatrix = math.Ident4f() | |
g.modelViewProjectionMatrix = g.projectionMatrix.Mul(g.viewMatrix).Mul(g.modelMatrix) | |
gl.Viewport(0, 0, width, height) | |
} | |
func (g *Game) HandleKey(window *glfw.Window, k glfw.Key, s int, action glfw.Action, mods glfw.ModifierKey) { | |
log.Printf("g.HandleKey()") | |
if action == glfw.Press && k == glfw.KeyEscape { | |
g.shouldEnd = true | |
} | |
} | |
func (g *Game) OnUpdate() bool { | |
return g.shouldEnd | |
} | |
func (g *Game) OnRender(substep float32) { | |
gl.ClearColor(0.0, 0.0, 0.0, 1.0) | |
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) | |
g.program.Use() | |
g.mvpLocation.UniformMatrix4fv(false, g.modelViewProjectionMatrix) | |
g.triangleArray.Bind() | |
gl.DrawArrays(gl.TRIANGLES, 0, 3) | |
g.triangleArray.Unbind() | |
g.program.Unuse() | |
} | |
func main() { | |
g := &Game{} | |
hints := map[glfw.Hint]int{ | |
glfw.OpenglProfile: glfw.OpenglCoreProfile, | |
glfw.ContextVersionMajor: 3, | |
glfw.ContextVersionMinor: 3, | |
glfw.DepthBits: 16, | |
} | |
if err := gogame.Gameloop(g, 848, 480, 20, hints); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment