运行效果:
shader.vert:
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
out vec3 ourColor;
void main()
{
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
}
shader.frag:
#version 330 core
in vec3 ourColor;
out vec4 FragColor;
void main()
{
FragColor = vec4(ourColor, 1.0);
}
game.go:
package engine
import (
_ "embed"
"fmt"
"unsafe"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
var mIsRunning = false
var mWidth = 1024
var mHeight = 768
var VAO uint32
var VBO uint32
var shaderProgram uint32
var vertices = [...]float32{
// 位置 // 颜色
0.5, -0.5, 0.0, 1.0, 0.0, 0.0, // 右下
-0.5, -0.5, 0.0, 0.0, 1.0, 0.0, // 左下
0.0, 0.5, 0.0, 0.0, 0.0, 1.0, // 顶部
}
//go:embed shader.vert
var vertexShaderSource string
//go:embed shader.frag
var fragmentShaderSource string
type Game struct {
MWindow *glfw.Window
}
//初始化引擎
func (g *Game) Initialize() bool {
//获取glfw版本
fmt.Println(glfw.GetVersionString())
//初始化glfw
var err error
if err = glfw.Init(); err != nil {
fmt.Printf("Unable to initialize glfw: %v", err)
return false
}
glfw.WindowHint(glfw.RedBits, 8)
glfw.WindowHint(glfw.GreenBits, 8)
glfw.WindowHint(glfw.BlueBits, 8)
glfw.WindowHint(glfw.AlphaBits, 8)
glfw.WindowHint(glfw.DepthBits, 24)
glfw.WindowHint(glfw.DoubleBuffer, 1)
//glfw.WindowHint(glfw.Resizable, 0) //禁止调整窗口大小
//设置版本
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
//窗口模式
if g.MWindow, err = glfw.CreateWindow(mWidth, mHeight, "着色器", nil, nil); err != nil {
fmt.Printf("Failed to create window: %v", err)
return false
}
//回调函数
g.MWindow.SetCloseCallback(CloseCallback) //关闭回调
g.MWindow.SetSizeCallback(SizeCallback) //调整回调
g.MWindow.SetKeyCallback(KeyCallback) //按键回调
//上下文
g.MWindow.MakeContextCurrent()
//初始化opengl
if err = gl.Init(); err != nil {
fmt.Printf("Unable to initialize gl: %v", err)
return false
}
gl.Viewport(0, 0, int32(mWidth), int32(mHeight))
//gl.Enable(gl.CULL_FACE) //开启剔除
//gl.CullFace(gl.FRONT) //剔除背面
version := gl.GoStr(gl.GetString(gl.VERSION))
fmt.Println(version)
//顶点着色器
vertexShader := gl.CreateShader(gl.VERTEX_SHADER)
vertexString, _ := gl.Strs(vertexShaderSource)
gl.ShaderSource(vertexShader, 1, vertexString, nil)
gl.CompileShader(vertexShader)
var success int32
var infoLog [512]uint8
gl.GetShaderiv(vertexShader, gl.COMPILE_STATUS, &success)
if success != 1 {
gl.GetShaderInfoLog(vertexShader, 512, nil, &infoLog[0])
fmt.Printf("顶点着色器错误:%s\n", infoLog)
}
//片段着色器
fragmentShader := gl.CreateShader(gl.FRAGMENT_SHADER)
fragmentString, _ := gl.Strs(fragmentShaderSource)
gl.ShaderSource(fragmentShader, 1, fragmentString, nil)
gl.CompileShader(fragmentShader)
gl.GetShaderiv(fragmentShader, gl.COMPILE_STATUS, &success)
if success != 1 {
gl.GetShaderInfoLog(fragmentShader, 512, nil, &infoLog[0])
fmt.Printf("片段着色器错误:%s\n", infoLog)
}
//着色器程序
shaderProgram = gl.CreateProgram()
gl.AttachShader(shaderProgram, vertexShader)
gl.AttachShader(shaderProgram, fragmentShader)
gl.LinkProgram(shaderProgram)
gl.GetProgramiv(shaderProgram, gl.LINK_STATUS, &success)
if success != 1 {
gl.GetProgramInfoLog(shaderProgram, 512, nil, &infoLog[0])
fmt.Printf("着色器程序错误:%s\n", infoLog)
}
//删除着色器
gl.DeleteShader(vertexShader)
gl.DeleteShader(fragmentShader)
gl.GenVertexArrays(1, &VAO)
gl.GenBuffers(1, &VBO)
gl.BindVertexArray(VAO)
gl.BindBuffer(gl.ARRAY_BUFFER, VBO)
gl.BufferData(gl.ARRAY_BUFFER, int(unsafe.Sizeof(vertices)), unsafe.Pointer(&vertices), gl.STATIC_DRAW)
// 位置属性
gl.VertexAttribPointerWithOffset(0, 3, gl.FLOAT, false, 6*int32(unsafe.Sizeof(float32(0))), 0)
gl.EnableVertexAttribArray(0)
// 颜色属性
gl.VertexAttribPointerWithOffset(1, 3, gl.FLOAT, false, 6*int32(unsafe.Sizeof(float32(0))), uintptr(3*int32(unsafe.Sizeof(float32(0)))))
gl.EnableVertexAttribArray(1)
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
gl.BindVertexArray(0)
//gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE)//线框模式
//保持运行
mIsRunning = true
return true
}
//引擎循环
func (g *Game) RunLoop() {
for mIsRunning {
//清屏
gl.ClearColor(0.2, 0.3, 0.3, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT)
//....
g.ProcessInput()
g.UpdateGame()
g.GenerateOutput()
//交换缓存
g.MWindow.SwapBuffers()
glfw.PollEvents()
}
}
//引擎销毁
func (g *Game) Shutdown() {
gl.DeleteVertexArrays(1, &VAO)
gl.DeleteBuffers(1, &VBO)
gl.DeleteProgram(shaderProgram)
g.MWindow.Destroy()
glfw.Terminate()
}
func (g *Game) ProcessInput() {
}
func (g *Game) UpdateGame() {
}
func (g *Game) GenerateOutput() {
gl.UseProgram(shaderProgram)
gl.BindVertexArray(VAO)
gl.DrawArrays(gl.TRIANGLES, 0, 3)
}
func CloseCallback(w *glfw.Window) {
mIsRunning = false
}
func SizeCallback(w *glfw.Window, width int, height int) {
gl.Viewport(0, 0, int32(width), int32(height))
}
func KeyCallback(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
switch key {
case glfw.KeyEscape:
if action == glfw.Press {
CloseCallback(w)
}
}
}