hlgl/main.go

110 lines
2.5 KiB
Go
Raw Normal View History

2024-11-23 21:54:20 -07:00
package main
import (
"fmt"
"runtime"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/go-gl/mathgl/mgl32"
)
const (
start_width = 1920
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 {
}
defer window.Destroy()
window.MakeContextCurrent()
if err := gl.Init(); err != nil {
glfw.Terminate()
}
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})
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})
window.SwapBuffers()
glfw.PollEvents()
}
}
func CreateOrthographicProjection(width, height int) mgl32.Mat4 {
return mgl32.Ortho(0, float32(width), 0, float32(height), -1, 1)
}