Skip to content

Instantly share code, notes, and snippets.

@diamondburned
Created March 10, 2025 07:05
Show Gist options
  • Save diamondburned/6c0d9f39a3e673ea7403163da9da530e to your computer and use it in GitHub Desktop.
Save diamondburned/6c0d9f39a3e673ea7403163da9da530e to your computer and use it in GitHub Desktop.
Gio + OpenGL Core Reproducing Program
// SPDX-License-Identifier: Unlicense OR MIT
// GLFW doesn't build on OpenBSD and FreeBSD.
//go:build !openbsd && !freebsd && !android && !ios && !js
// +build !openbsd,!freebsd,!android,!ios,!js
// The glfw example demonstrates integration of Gio into a foreign
// windowing and rendering library, in this case GLFW
// (https://www.glfw.org).
//
// See the go-glfw package for installation of the native
// dependencies:
//
// https://github.com/go-gl/glfw
package main
import (
"image"
"log"
"runtime"
"time"
"gioui.org/font/gofont"
"gioui.org/gpu"
"gioui.org/io/input"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
func main() {
// Required by the OpenGL threading model.
runtime.LockOSThread()
err := glfw.Init()
if err != nil {
log.Fatal(err)
}
defer glfw.Terminate()
// Gio assumes a sRGB backbuffer.
glfw.WindowHint(glfw.SRGBCapable, glfw.True)
glfw.WindowHint(glfw.ScaleToMonitor, glfw.True)
glfw.WindowHint(glfw.CocoaRetinaFramebuffer, glfw.True)
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
window, err := glfw.CreateWindow(800, 600, "Gio + GLFW", nil, nil)
if err != nil {
log.Fatal(err)
}
window.MakeContextCurrent()
if err := gl.Init(); err != nil {
log.Fatalf("gl.Init failed: %v", err)
}
// Enable sRGB.
gl.Enable(gl.FRAMEBUFFER_SRGB)
// Set up default VBA, required for the forward-compatible core profile.
var defVBA uint32
gl.GenVertexArrays(1, &defVBA)
gl.BindVertexArray(defVBA)
var queue input.Router
var ops op.Ops
th := material.NewTheme()
th.Shaper = text.NewShaper(text.WithCollection(gofont.Collection()))
gpuCtx, err := gpu.New(gpu.OpenGL{ES: false, Shared: true})
if err != nil {
log.Fatal(err)
}
defer gpuCtx.Release()
for !window.ShouldClose() {
glfw.PollEvents()
scale, _ := window.GetContentScale()
width, height := window.GetFramebufferSize()
sz := image.Point{X: width, Y: height}
ops.Reset()
gtx := layout.Context{
Ops: &ops,
Now: time.Now(),
Source: queue.Source(),
Metric: unit.Metric{
PxPerDp: scale,
PxPerSp: scale,
},
Constraints: layout.Exact(sz),
}
drawOpenGL()
draw(gtx, th)
gpuCtx.Frame(gtx.Ops, gpu.OpenGLRenderTarget{}, sz)
queue.Frame(gtx.Ops)
window.SwapBuffers()
}
}
var (
button widget.Clickable
green float64 = 0.2
)
// drawOpenGL demonstrates the direct use of OpenGL commands
// to draw non-Gio content below the Gio UI.
func drawOpenGL() {
gl.ClearColor(0, float32(green), 0, 1)
gl.Clear(gl.COLOR_BUFFER_BIT)
}
func draw(gtx layout.Context, th *material.Theme) layout.Dimensions {
return layout.Center.Layout(gtx,
material.Button(th, &button, "Button").Layout,
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment