Last active
August 29, 2015 14:08
-
-
Save mdmarek/9fa25ce05adad0511c35 to your computer and use it in GitHub Desktop.
Vertex shader that passes color information to fragment shader.
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 ( | |
"fmt" | |
"runtime" | |
"github.com/go-gl/gl" | |
glfw "github.com/go-gl/glfw3" | |
) | |
const ( | |
vertex = `#version 330 | |
in vec4 position; | |
in vec4 color; | |
smooth out vec4 base_color; | |
void main() | |
{ | |
gl_Position = position; | |
base_color = color; | |
}` | |
fragment = `#version 330 | |
smooth in vec4 base_color; | |
out vec4 frag_color; | |
void main() | |
{ | |
frag_color = base_color; | |
}` | |
) | |
type Object struct { | |
vao gl.VertexArray | |
vbo gl.Buffer | |
pos gl.AttribLocation | |
col gl.AttribLocation | |
} | |
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(program.GetFragDataLocation("frag_color"), "frag_color") | |
program.Link() | |
program.Use() | |
defer program.Delete() | |
gl.ClearColor(0.3, 0.3, 0.3, 1.0) | |
// Objects | |
verticies1 := []float32{ | |
0, 0, 0, 1, | |
0, 1, 0, 1, | |
1, 1, 0, 1, | |
1, 0, 0, 1, | |
0, 1, 0, 1, | |
0, 0, 1, 1} | |
verticies2 := []float32{ | |
-1, -1, 0, 1, | |
-1, 0, 0, 1, | |
0, 0, 0, 1, | |
1, 0, 0, 1, | |
1, 0, 0, 1, | |
1, 0, 0, 1} | |
o1, _ := NewObject(program, verticies1) | |
o2, _ := NewObject(program, verticies2) | |
// Main loop. | |
for !window.ShouldClose() { | |
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) | |
o1.Draw() | |
o2.Draw() | |
window.SwapBuffers() | |
glfw.PollEvents() | |
if window.GetKey(glfw.KeyEscape) == glfw.Press { | |
window.SetShouldClose(true) | |
} | |
} | |
} | |
func NewObject(program gl.Program, verticies []float32) (*Object, error) { | |
vao := gl.GenVertexArray() | |
vao.Bind() | |
vbo := gl.GenBuffer() | |
vbo.Bind(gl.ARRAY_BUFFER) | |
gl.BufferData(gl.ARRAY_BUFFER, len(verticies)*4, verticies, gl.STATIC_DRAW) | |
pos := program.GetAttribLocation("position") | |
pos.AttribPointer(4, gl.FLOAT, false, 0, nil) | |
col := program.GetAttribLocation("color") | |
col.AttribPointer(4, gl.FLOAT, false, 0, uintptr(48)) | |
vbo.Unbind(gl.ARRAY_BUFFER) | |
vao.Unbind() | |
return &Object{ | |
vao: vao, | |
vbo: vbo, | |
pos: pos, | |
col: col, | |
}, nil | |
} | |
func (o *Object) Draw() { | |
o.vao.Bind() | |
o.pos.EnableArray() | |
o.col.EnableArray() | |
gl.DrawArrays(gl.TRIANGLES, 0, 3) | |
o.col.DisableArray() | |
o.pos.DisableArray() | |
o.vao.Unbind() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment