hlgl/hlgl.go

117 lines
3.0 KiB
Go
Raw Permalink Normal View History

2024-11-23 21:54:20 -07:00
package main
import (
"math"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/mathgl/mgl32"
)
2024-11-23 21:54:20 -07:00
type HLRectangle struct {
X int
Y int
Width int
Height int
}
type HLColorRGBA struct {
R int
G int
B int
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())
}
}
2024-11-23 21:54:20 -07:00
func DrawRectangleWH(x, y, width, height int, color HLColorRGBA) {
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{
2024-11-23 21:54:20 -07:00
float32(x), float32(y), 0.0,
float32(x), float32(y + height), 0.0,
float32(x + width), float32(y), 0.0,
2024-11-23 21:54:20 -07:00
float32(x + width), float32(y), 0.0,
float32(x), float32(y + height), 0.0,
float32(x + width), float32(y + height), 0.0,
2024-11-23 21:54:20 -07:00
}
vao := NewVertexArray()
vbo := NewVertexBuffer(vertices)
vao.AddBuffer(vbo, []int32{3})
vao.Bind()
gl.DrawArrays(gl.TRIANGLES, 0, 6)
vao.Unbind()
vbo.Unbind()
2024-11-23 21:54:20 -07:00
}
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()
}