Last active
August 29, 2015 14:08
-
-
Save mdmarek/0f73890ae2547cdba3a7 to your computer and use it in GitHub Desktop.
Modern OpenGL to draw using multiple Vertex Attribute Objects.
This file contains 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
// Copyright 2014 The go-gl Authors. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
package main | |
import ( | |
"fmt" | |
"runtime" | |
"github.com/go-gl/gl" | |
glfw "github.com/go-gl/glfw3" | |
) | |
const ( | |
vertex = `#version 330 | |
in vec2 position; | |
void main() | |
{ | |
gl_Position = vec4(position, 0.0, 1.0); | |
}` | |
fragment = `#version 330 | |
out vec4 outColor; | |
void main() | |
{ | |
outColor = vec4(1.0, 1.0, 1.0, 1.0); | |
}` | |
) | |
func main() { | |
runtime.LockOSThread() | |
glfw.Init() | |
defer glfw.Terminate() | |
glfw.WindowHint(glfw.ContextVersionMajor, 3) | |
glfw.WindowHint(glfw.ContextVersionMinor, 3) | |
glfw.WindowHint(glfw.OpenglForwardCompatible, glfw.True) | |
glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile) | |
window, err := glfw.CreateWindow(800, 600, "Example", nil, nil) | |
if err != nil { | |
panic(err) | |
} | |
defer window.Destroy() | |
window.MakeContextCurrent() | |
glfw.SwapInterval(1) | |
gl.Init() | |
vertex_shader := gl.CreateShader(gl.VERTEX_SHADER) | |
vertex_shader.Source(vertex) | |
vertex_shader.Compile() | |
fmt.Println(vertex_shader.GetInfoLog()) | |
defer vertex_shader.Delete() | |
fragment_shader := gl.CreateShader(gl.FRAGMENT_SHADER) | |
fragment_shader.Source(fragment) | |
fragment_shader.Compile() | |
fmt.Println(fragment_shader.GetInfoLog()) | |
defer fragment_shader.Delete() | |
program := gl.CreateProgram() | |
program.AttachShader(vertex_shader) | |
program.AttachShader(fragment_shader) | |
program.BindFragDataLocation(0, "outColor") | |
program.Link() | |
program.Use() | |
defer program.Delete() | |
gl.ClearColor(0.3, 0.3, 0.3, 1.0) | |
// VAO 1 | |
vao1 := gl.GenVertexArray() | |
vao1.Bind() | |
vbo1 := gl.GenBuffer() | |
vbo1.Bind(gl.ARRAY_BUFFER) | |
verticies1 := []float32{0, 0, 0, 0, 1, 0, 1, 1, 0} | |
gl.BufferData(gl.ARRAY_BUFFER, len(verticies1)*4, verticies1, gl.STATIC_DRAW) | |
// Implicitly affects the previously bound object, | |
// in this case vbo1. | |
pa1 := program.GetAttribLocation("position") | |
pa1.AttribPointer(3, gl.FLOAT, false, 0, nil) | |
pa1.EnableArray() | |
defer pa1.DisableArray() | |
vao1.Unbind() | |
// VAO 2 | |
vao2 := gl.GenVertexArray() | |
vao2.Bind() | |
vbo2 := gl.GenBuffer() | |
vbo2.Bind(gl.ARRAY_BUFFER) | |
verticies2 := []float32{-1, -1, 0, -1, 0, 0, 0, 0, 0} | |
gl.BufferData(gl.ARRAY_BUFFER, len(verticies2)*4, verticies2, gl.STATIC_DRAW) | |
// Implicitly affects the previously bound object, | |
// in this case vbo2. | |
pa2 := program.GetAttribLocation("position") | |
pa2.AttribPointer(3, gl.FLOAT, false, 0, nil) | |
pa2.EnableArray() | |
defer pa2.DisableArray() | |
vao2.Unbind() | |
// Main loop. | |
for !window.ShouldClose() { | |
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) | |
vao1.Bind() | |
gl.DrawArrays(gl.TRIANGLES, 0, 3) | |
vao1.Unbind() | |
vao2.Bind() | |
gl.DrawArrays(gl.TRIANGLES, 0, 3) | |
vao2.Unbind() | |
window.SwapBuffers() | |
glfw.PollEvents() | |
if window.GetKey(glfw.KeyEscape) == glfw.Press { | |
window.SetShouldClose(true) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment