From d6891a58f907d075399cd6a41edbfa3bfc41e0a0 Mon Sep 17 00:00:00 2001 From: Ifenna Awagu Date: Sat, 13 Apr 2019 22:19:09 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + lines/bresenham.go | 11 +++++++++++ main.go | 37 +++++++++++++++++++++++++++++++++++++ window.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 lines/bresenham.go create mode 100644 main.go create mode 100644 window.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/lines/bresenham.go b/lines/bresenham.go new file mode 100644 index 0000000..c1effa9 --- /dev/null +++ b/lines/bresenham.go @@ -0,0 +1,11 @@ +package lines + +import "github.com/go-gl/gl/all-core/gl" + +func DrawPixel(x float32, y float32) { + gl.PointSize(10) + gl.Begin(gl.POINTS) + gl.Color3f(1.0, 1.0, 1.0) + gl.Vertex2f(x, y) + gl.End() +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..d038218 --- /dev/null +++ b/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "github.com/go-gl/gl/all-core/gl" + "github.com/go-gl/glfw/v3.2/glfw" + "go-graphics/lines" + "runtime" +) + +func init() { + runtime.LockOSThread() +} + +func main() { + window := buildWindow() + defer glfw.Terminate() + + program := initOpenGL() + + for !window.ShouldClose() { + draw(window, program) + } + +} + +func draw(window *glfw.Window, program uint32) { + gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) + gl.UseProgram(program) + + //gl.LoadIdentity() + //gl.Ortho(0, 0, 0, 0, 0, 0) + + lines.DrawPixel(0, 0) + + window.SwapBuffers() // swap rendering and drawing buffers + glfw.PollEvents() // poll input events +} diff --git a/window.go b/window.go new file mode 100644 index 0000000..3481dae --- /dev/null +++ b/window.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "github.com/go-gl/gl/all-core/gl" + "github.com/go-gl/glfw/v3.2/glfw" + "log" +) + +func buildWindow() *glfw.Window { + if err := glfw.Init(); err != nil { + panic(fmt.Errorf("couldn't initialize glfw: %v", err)) + } + + setWindowHints() + + window, err := glfw.CreateWindow(800, 600, "Graphics", nil, nil) + if err != nil { + panic(fmt.Errorf("could not create opengl renderer: %v", err)) + } + window.MakeContextCurrent() + + return window +} + +func initOpenGL() uint32 { + if err := gl.Init(); err != nil { + panic(err) + } + version := gl.GoStr(gl.GetString(gl.VERSION)) + log.Println("OpenGL version", version) + + program := gl.CreateProgram() + gl.LinkProgram(program) + return program +} + +func setWindowHints() { + glfw.WindowHint(glfw.ContextVersionMajor, 2) + glfw.WindowHint(glfw.ContextVersionMinor, 1) + glfw.WindowHint(glfw.Resizable, glfw.True) + //glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) + //glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) +}