Skip to content

Instantly share code, notes, and snippets.

@kiryl
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiryl/9331436 to your computer and use it in GitHub Desktop.
Save kiryl/9331436 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/go-gl/gl"
glfw "github.com/go-gl/glfw3"
"log"
"runtime"
)
var (
program gl.Program
)
func initShaders() {
shaders := []struct {
stype gl.GLenum
source string
}{
{gl.VERTEX_SHADER, vertex_shader},
{gl.FRAGMENT_SHADER, fragment_shader},
}
program = gl.CreateProgram()
for i, shaderDescriptor := range shaders {
shader := gl.CreateShader(shaderDescriptor.stype)
defer shader.Delete()
shader.Source(shaderDescriptor.source)
shader.Compile()
if shader.Get(gl.COMPILE_STATUS) != gl.TRUE {
log.Panicf("shader[%d] error: %v", i, shader.GetInfoLog())
}
program.AttachShader(shader)
}
program.Link()
if program.Get(gl.LINK_STATUS) != gl.TRUE {
log.Panic("linker error: " + program.GetInfoLog())
}
program.Use()
}
func initGL() {
gl.Init()
initShaders()
gl.ClearColor(0, 0, 0.4, 1)
vertices := []float32{
-1.0, -1.0,
1.0, -1.0,
0.0, 1.0,
}
vertexArray := gl.GenVertexArray()
vertexArray.Bind()
buf := gl.GenBuffer()
buf.Bind(gl.ARRAY_BUFFER)
gl.BufferData(gl.ARRAY_BUFFER, 4 * len(vertices), vertices, gl.STATIC_DRAW)
attr := program.GetAttribLocation("position")
attr.EnableArray()
buf.Bind(gl.ARRAY_BUFFER)
attr.AttribPointer(2, gl.FLOAT, false, 0, nil)
}
func draw() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.DrawArrays(gl.TRIANGLES, 0, 3)
}
func keys(window *glfw.Window, k glfw.Key, s int, action glfw.Action, mods glfw.ModifierKey) {
if action == glfw.Press && glfw.Key(k) == glfw.KeyEscape {
window.SetShouldClose(true)
}
}
func errorCallback(err glfw.ErrorCode, desc string) {
log.Printf("%v: %v\n", err, desc)
}
func main() {
runtime.LockOSThread()
glfw.SetErrorCallback(errorCallback)
if !glfw.Init() {
panic("Failed to initialize GLFW")
}
defer glfw.Terminate()
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile)
glfw.WindowHint(glfw.Resizable, 0)
window, err := glfw.CreateWindow(1024, 768, "test", nil, nil)
if err != nil {
log.Panic(err)
}
defer window.Destroy()
window.SetKeyCallback(keys)
window.MakeContextCurrent()
initGL()
for !window.ShouldClose() {
draw()
window.SwapBuffers()
glfw.PollEvents()
}
}
const vertex_shader = `
#version 330 core
in vec4 position;
void main()
{
gl_Position = position;
}
`
const fragment_shader = `
#version 330 core
out vec3 color;
void main()
{
color = vec3(1, 0, 0);
}
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment