// vim: shiftwidth=4 tabstop=4 noexpandtab package main import ( "fmt" "log" "strings" "github.com/go-gl/gl/v4.6-core/gl" "github.com/go-gl/mathgl/mgl32" ) var ( vertices []float32 ) const ( // vertex shader to draw points VERTEX_SHADER = ` #version 410 uniform mat4 trafo; in vec3 vp; void main() { gl_Position = trafo * vec4(vp, 1.0); } ` + "\x00" // fragment shader to draw surfaces FRAGMENT_SHADER = ` #version 410 out vec4 frag_colour; void main() { frag_colour = vec4(0, 0, 1, 1); // RGBA color format } ` + "\x00" ) type Graphics struct { version string vao uint32 vertexShader uint32 fragmentShader uint32 program uint32 trafo mgl32.Mat4 trafoUniform int32 } func newGraphics() Graphics { var graphics Graphics = Graphics{} graphics.trafo = mgl32.Ident4() // init OpenGL and save/log version log.Println("OpenGL init") err := gl.Init() if err != nil { log.Fatal(err) } graphics.version = gl.GoStr(gl.GetString(gl.VERSION)) log.Println("OpenGL version", graphics.version) // compile shaders graphics.vertexShader, err = compileShader(VERTEX_SHADER, gl.VERTEX_SHADER) if err != nil { log.Fatal(err) } graphics.fragmentShader, err = compileShader(FRAGMENT_SHADER, gl.FRAGMENT_SHADER) if err != nil { log.Fatal(err) } // create GL program graphics.program = gl.CreateProgram() gl.AttachShader(graphics.program, graphics.vertexShader) gl.AttachShader(graphics.program, graphics.fragmentShader) gl.LinkProgram(graphics.program) // create VAO graphics.vao = makeVao(vertices) // create transformation matrix //graphics.trafo = mgl32.HomogRotate3D(float32(glfw.GetTime()) * OMEGA, mgl32.Vec3{ROT_X, ROT_Y, ROT_Z}) //graphics.trafoUniform = gl.GetUniformLocation(graphics.program, gl.Str("trafo\x00")) gl.UniformMatrix4fv(graphics.trafoUniform, 1, false, &graphics.trafo[0]) return graphics } func (graphics Graphics) draw() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(graphics.program) //graphics.trafo = mgl32.HomogRotate3D(float32(glfw.GetTime()) * OMEGA, mgl32.Vec3{ROT_X, ROT_Y, ROT_Z}) gl.UniformMatrix4fv(graphics.trafoUniform, 1, false, &graphics.trafo[0]) gl.BindVertexArray(graphics.vao) gl.DrawArrays(gl.LINE_STRIP, 0, int32(len(vertices)/3)) // POINTS, LINES, LINE_STRIP, LINE_LOOP, TRIANGLES, TRIANGLE_STRIP, TRIANGLE_FAN } // makeVao initializes and returns a vertex array from the points provided. func makeVao(points []float32) uint32 { log.Println("Creating VAO") // start with vertex buffer object (VBO): var vbo uint32 // VBO ID gl.GenBuffers(1, &vbo) gl.BindBuffer(gl.ARRAY_BUFFER, vbo) gl.BufferData(gl.ARRAY_BUFFER, 4*len(points), gl.Ptr(points), gl.STATIC_DRAW) // vertex array objects (VAO) are a feature of newer GL implementations: var vao uint32 // VAO ID gl.GenVertexArrays(1, &vao) gl.BindVertexArray(vao) gl.EnableVertexAttribArray(0) gl.BindBuffer(gl.ARRAY_BUFFER, vbo) gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 0, nil) // tell GL to use 3D float vectors return vao } func compileShader(source string, shaderType uint32) (uint32, error) { log.Println("Compiling shader") shader := gl.CreateShader(shaderType) csources, free := gl.Strs(source) gl.ShaderSource(shader, 1, csources, nil) free() gl.CompileShader(shader) var status int32 gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status) if status == gl.FALSE { var logLength int32 gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log)) return 0, fmt.Errorf("Failed to compile %v: %v", source, log) } return shader, nil } func (graphics *Graphics) setTrafo(trafo mgl32.Mat4) { graphics.trafo = trafo }