diff --git a/hlgl.go b/hlgl.go index 0bff41e..45960cf 100644 --- a/hlgl.go +++ b/hlgl.go @@ -1,7 +1,11 @@ -// HLGL High Level GL Abstraction Layer package main -import "github.com/go-gl/gl/v4.1-core/gl" +import ( + "math" + + "github.com/go-gl/gl/v4.1-core/gl" + "github.com/go-gl/mathgl/mgl32" +) type HLRectangle struct { X int @@ -17,30 +21,96 @@ type HLColorRGBA struct { A int } +var defaultShader *Shader +var projectionMatrix mgl32.Mat4 + +func InitHLGL(projection mgl32.Mat4) { + projectionMatrix = projection + + vertexShaderSource := ` + #version 410 core + layout(location = 0) in vec3 position; + uniform mat4 u_Projection; + void main() { + gl_Position = u_Projection * vec4(position, 1.0); + } + ` + fragmentShaderSource := ` + #version 410 core + out vec4 color; + uniform vec4 u_Color; + void main() { + color = u_Color; + } + ` + var err error + defaultShader, err = NewShader(vertexShaderSource, fragmentShaderSource) + if err != nil { + panic("Failed to initialize default shader: " + err.Error()) + } +} + func DrawRectangleWH(x, y, width, height int, color HLColorRGBA) { - // Triangle ONE - var triangleOneVerticies = []float32{ + defaultShader.Use() + defaultShader.SetUniform4f("u_Color", float32(color.R)/255.0, float32(color.G)/255.0, float32(color.B)/255.0, float32(color.A)/255.0) + defaultShader.SetUniformM("u_Projection", projectionMatrix) + + var vertices = []float32{ float32(x), float32(y), 0.0, float32(x), float32(y + height), 0.0, float32(x + width), float32(y), 0.0, - } - var triangleOneVAO = NewVertexArray() - var trianglOneVBO = NewVertexBuffer(triangleOneVerticies) - triangleOneVAO.AddBuffer(trianglOneVBO, []int32{3}) - triangleOneVAO.Bind() - gl.DrawArrays(gl.TRIANGLES, 0, 3) - // Triangle TWO - var triangleTwoVerticies = []float32{ - float32(x + width), float32(y + height), 0.0, - float32(x), float32(y + height), 0.0, float32(x + width), float32(y), 0.0, + float32(x), float32(y + height), 0.0, + float32(x + width), float32(y + height), 0.0, } - var triangleTwoVAO = NewVertexArray() - var trianglTwoVBO = NewVertexBuffer(triangleTwoVerticies) - triangleTwoVAO.AddBuffer(trianglTwoVBO, []int32{3}) - triangleTwoVAO.Bind() - gl.DrawArrays(gl.TRIANGLES, 0, 3) + + vao := NewVertexArray() + vbo := NewVertexBuffer(vertices) + vao.AddBuffer(vbo, []int32{3}) + vao.Bind() + + gl.DrawArrays(gl.TRIANGLES, 0, 6) + + vao.Unbind() + vbo.Unbind() } -func DrawRectangleRec(rectangle HLRectangle, color HLColorRGBA) {} +func DrawRoundedRectangle(x, y, width, height, radius float32, color HLColorRGBA) { + var vertices []float32 + segments := 12 // maybe change it + + defaultShader.Use() + defaultShader.SetUniform4f("u_Color", float32(color.R)/255.0, float32(color.G)/255.0, float32(color.B)/255.0, float32(color.A)/255.0) + defaultShader.SetUniformM("u_Projection", projectionMatrix) + + vertices = append(vertices, x+radius, y, 0.0, x+width-radius, y, 0.0, x+width-radius, y+height, 0.0) + vertices = append(vertices, x+radius, y, 0.0, x+width-radius, y+height, 0.0, x+radius, y+height, 0.0) + + corners := []struct { + cx, cy, startAngle float32 + }{ + {x + radius, y + radius, 0}, // borrom left + {x + width - radius, y + radius, -math.Pi / 2}, // bottom right + {x + width - radius, y + height - radius, math.Pi}, // top right + {x + radius, y + height - radius, math.Pi / 2}, // top left + } + for _, corner := range corners { + for i := 0; i <= segments; i++ { + angle := corner.startAngle + float32(i)/float32(segments)*(math.Pi/2) + vertices = append(vertices, corner.cx+float32(math.Cos(float64(angle)))*radius) + vertices = append(vertices, corner.cy+float32(math.Sin(float64(angle)))*radius) + vertices = append(vertices, 0.0) + } + } + + vao := NewVertexArray() + vbo := NewVertexBuffer(vertices) + vao.AddBuffer(vbo, []int32{3}) + vao.Bind() + + gl.DrawArrays(gl.TRIANGLE_FAN, 0, int32(len(vertices)/3)) + + vao.Unbind() + vbo.Unbind() +} diff --git a/main.go b/main.go index 04f8465..477e89a 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "runtime" "github.com/go-gl/gl/v4.1-core/gl" @@ -14,94 +13,43 @@ const ( start_height = 1080 ) -var vertices = []float32{ - 960, 540, 0.0, - 1460, 540, 0.0, - 960, 1040, 0.0, -} - -const vertexShaderSource = ` -#version 410 core -layout(location = 0) in vec3 position; -uniform mat4 u_Projection; -void main() { - gl_Position = u_Projection * vec4(position, 1.0); -} -` - -const fragmentShaderSource = ` -#version 410 core -out vec4 color; -uniform vec4 u_Color; -void main() { - color = u_Color; -} -` - func main() { runtime.LockOSThread() if err := glfw.Init(); err != nil { } defer glfw.Terminate() - glfw.WindowHint(glfw.Decorated, glfw.True) - glfw.WindowHint(glfw.TransparentFramebuffer, glfw.False) - glfw.WindowHint(glfw.Resizable, glfw.False) - glfw.WindowHint(glfw.ContextVersionMajor, 4) glfw.WindowHint(glfw.ContextVersionMinor, 1) glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) - // glfw.WindowHint(glfw.TransparentFramebuffer, glfw.True) window, err := glfw.CreateWindow(start_width, start_height, "Learning OpenGL", nil, nil) if err != nil { + panic(err) } - defer window.Destroy() window.MakeContextCurrent() - if err := gl.Init(); err != nil { - glfw.Terminate() + panic(err) } - gl.Viewport(0, 0, int32(start_width), int32(start_height)) - - window.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) { - println("Mouse Button Event!") - }) - - window.SetSizeCallback(func(w *glfw.Window, width int, height int) { - fmt.Printf("Window resize event - Width: %d, Height: %d\n", width, height) - }) - - shader, _ := NewShader(vertexShaderSource, fragmentShaderSource) - projection := CreateOrthographicProjection(start_width, start_height) - - vao := NewVertexArray() - vbo := NewVertexBuffer(vertices) - vao.AddBuffer(vbo, []int32{3}) + InitHLGL(projection) gl.ClearColor(0.2, 0.3, 0.3, 1.0) for !window.ShouldClose() { - shader.Use() - - shader.SetUniformM("u_Projection", projection) - color := HLColorRGBA{255, 0, 0, 255} - shader.SetUniform4f("u_Color", float32(color.R)/255.0, float32(color.G)/255.0, float32(color.B)/255.0, float32(color.A)/255.0) - gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) - // Handled by the Draw Rectangle function - // vao.Bind() - // gl.DrawArrays(gl.TRIANGLES, 0, 3) - DrawRectangleWH(0, 0, 100, 200, HLColorRGBA{255, 0, 0, 255}) + // DrawRectangleWH(100, 100, 700, 150, HLColorRGBA{255, 0, 0, 255}) + // DrawRectangleWH(400, 200, 300, 300, HLColorRGBA{0, 255, 0, 255}) + DrawRoundedRectangle(200, 500, 200, 200, 10, HLColorRGBA{0, 0, 255, 255}) window.SwapBuffers() - glfw.PollEvents() + glfw.WaitEvents() } - + window.Destroy() + return } func CreateOrthographicProjection(width, height int) mgl32.Mat4 {