forked from go-gl/example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (92 loc) · 2.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// 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.
// Originally put together by github.com/segfault88, but
// I thought it might be useful to somebody else too.
// It took me quite a lot of frustration and messing around
// to get a basic example of glfw3 with modern OpenGL (3.3)
// with shaders etc. working. Hopefully this will save you
// some trouble. Enjoy!
package main
import (
"fmt"
"github.com/go-gl/gl"
glfw "github.com/go-gl/glfw3"
"github.com/go-gl/glu"
"runtime"
)
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() {
// lock glfw/gl calls to a single thread
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()
vao := gl.GenVertexArray()
vao.Bind()
vbo := gl.GenBuffer()
vbo.Bind(gl.ARRAY_BUFFER)
verticies := []float32{0, 1, 0, -1, -1, 0, 1, -1, 0}
gl.BufferData(gl.ARRAY_BUFFER, len(verticies)*4, verticies, gl.STATIC_DRAW)
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()
positionAttrib := program.GetAttribLocation("position")
positionAttrib.AttribPointer(3, gl.FLOAT, false, 0, nil)
positionAttrib.EnableArray()
defer positionAttrib.DisableArray()
gl.ClearColor(0.3, 0.3, 0.3, 1.0)
for !window.ShouldClose() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.DrawArrays(gl.TRIANGLES, 0, 3)
window.SwapBuffers()
glfw.PollEvents()
if window.GetKey(glfw.KeyEscape) == glfw.Press {
window.SetShouldClose(true)
}
}
}
func checkGLerror() {
if glerr := gl.GetError(); glerr != gl.NO_ERROR {
string, _ := glu.ErrorString(glerr)
panic(string)
}
}