opengl-deferred/main.go

137 lines
3.2 KiB
Go
Raw Permalink Normal View History

2024-10-11 16:38:21 +00:00
package main
import (
"fmt"
2024-10-11 23:02:12 +00:00
"runtime"
2024-10-11 16:38:21 +00:00
2024-10-12 02:03:59 +00:00
"git.tek.govt.hu/dowerx/opengl-deferred/types/gbuffer"
2024-10-11 23:02:12 +00:00
"git.tek.govt.hu/dowerx/opengl-deferred/types/geometry"
2024-10-12 12:31:15 +00:00
"git.tek.govt.hu/dowerx/opengl-deferred/types/light"
2024-12-17 01:59:59 +00:00
"git.tek.govt.hu/dowerx/opengl-deferred/types/material"
am "git.tek.govt.hu/dowerx/opengl-deferred/utils/assetmanager"
2024-10-12 16:25:54 +00:00
"git.tek.govt.hu/dowerx/opengl-deferred/utils/fpscontrols"
2024-10-11 16:38:21 +00:00
"github.com/go-gl/gl/v4.6-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
2024-10-11 23:02:12 +00:00
"github.com/go-gl/mathgl/mgl32"
2024-10-11 16:38:21 +00:00
)
const (
2024-12-17 01:59:59 +00:00
WIDTH = 1280
HEIGHT = 720
2024-10-11 16:38:21 +00:00
TITLE = "opengl-deferred"
2024-12-17 01:59:59 +00:00
FOV = 75.0
2024-10-11 16:38:21 +00:00
)
func main() {
// init glfw
2024-10-11 23:02:12 +00:00
runtime.LockOSThread()
2024-10-11 16:38:21 +00:00
if err := glfw.Init(); err != nil {
panic(err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.Resizable, glfw.False)
glfw.WindowHint(glfw.ContextVersionMajor, 4)
glfw.WindowHint(glfw.ContextVersionMinor, 6)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
window, err := glfw.CreateWindow(WIDTH, HEIGHT, TITLE, nil, nil)
if err != nil {
panic(err)
}
window.MakeContextCurrent()
2024-10-11 23:02:12 +00:00
// init gl
2024-10-11 16:38:21 +00:00
if err := gl.Init(); err != nil {
panic(err)
}
2024-10-12 12:31:15 +00:00
gl.Enable(gl.CULL_FACE)
2024-10-11 23:02:12 +00:00
gl.DepthFunc(gl.LESS)
2024-12-16 12:24:09 +00:00
gl.ClearColor(0, 0, 0, 1)
2024-10-11 23:02:12 +00:00
2024-10-11 16:38:21 +00:00
fmt.Println("OpenGL version:", gl.GoStr(gl.GetString(gl.VERSION)))
// load assets
2024-12-17 01:59:59 +00:00
assetManager, err := am.Init("assets")
2024-10-11 16:38:21 +00:00
if err != nil {
panic(err)
}
2024-12-17 00:16:17 +00:00
defer assetManager.Destroy()
2024-10-15 14:52:21 +00:00
2024-10-12 02:03:59 +00:00
screen, err := geometry.Screen()
if err != nil {
panic(err)
}
defer screen.Delete()
2024-10-12 12:31:15 +00:00
// lights
lights := []light.Light{
2024-10-18 14:12:01 +00:00
{Color: [3]float32{1, 1, 1}, Position: mgl32.Vec3{0, 10, 0}, Intensity: 40, Type: light.POINT},
{Color: [3]float32{1, 0, 0}, Position: mgl32.Vec3{5, 5, 5}, Intensity: 30, Type: light.POINT},
{Color: [3]float32{0, 0, 1}, Position: mgl32.Vec3{-5, 5, -5}, Intensity: 30, Type: light.POINT},
2024-10-12 12:31:15 +00:00
}
2024-10-12 02:03:59 +00:00
// gbuffer
gbuff, err := gbuffer.New(WIDTH, HEIGHT)
if err != nil {
panic(err)
}
defer gbuff.Delete()
gbuff.Bind()
2024-10-11 23:02:12 +00:00
// transformations
2024-10-12 16:25:54 +00:00
projection := mgl32.Perspective(mgl32.DegToRad(FOV), float32(WIDTH)/HEIGHT, 0.1, 1000)
model := mgl32.Ident4()
2024-12-16 12:24:09 +00:00
controls := fpscontrols.Get(10, 10, mgl32.Vec3{0, 0, -10}, mgl32.Vec2{0, 0}, window)
2024-10-11 23:02:12 +00:00
2024-12-23 14:49:51 +00:00
demoMaterial := am.GetAsset[material.Material]("assets/material/demo")
screenMaterial := am.GetAsset[material.Material]("assets/material/screen-debug")
2024-12-17 01:59:59 +00:00
2024-12-23 14:49:51 +00:00
teapot := am.GetAsset[geometry.Geometry]("assets/geometry/teapot")
2024-12-17 00:16:17 +00:00
2024-10-11 23:02:12 +00:00
for !window.ShouldClose() {
2024-10-12 16:25:54 +00:00
glfw.PollEvents()
controls.Update()
2024-10-12 02:03:59 +00:00
// first pass
2024-10-12 12:31:15 +00:00
gl.Enable(gl.DEPTH_TEST)
2024-10-12 02:03:59 +00:00
gbuff.Bind()
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
2024-12-17 01:59:59 +00:00
// apply the material
demoMaterial.Apply()
2024-10-12 02:03:59 +00:00
// transfer uniforms
2024-12-17 01:59:59 +00:00
demoMaterial.Shader.Mat4("projection", projection)
demoMaterial.Shader.Mat4("view", controls.View())
demoMaterial.Shader.Mat4("model", model)
2024-12-17 00:16:17 +00:00
teapot.Draw()
2024-10-11 23:02:12 +00:00
2024-10-12 02:03:59 +00:00
// second pass
2024-10-12 12:31:15 +00:00
gl.Disable(gl.DEPTH_TEST)
2024-10-12 02:03:59 +00:00
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
2024-10-12 12:31:15 +00:00
gl.Clear(gl.COLOR_BUFFER_BIT)
2024-10-12 02:03:59 +00:00
gbuff.BindTextures()
2024-12-17 01:59:59 +00:00
screenMaterial.Apply()
2024-10-15 14:52:21 +00:00
2024-10-12 16:25:54 +00:00
pos := controls.Position()
2024-12-17 01:59:59 +00:00
screenMaterial.Shader.Vec3("view_pos", &pos[0])
2024-10-12 12:31:15 +00:00
for _, light := range lights {
2024-12-17 01:59:59 +00:00
screenMaterial.Shader.Light(&light)
2024-10-12 12:31:15 +00:00
}
2024-10-12 02:03:59 +00:00
screen.Draw()
2024-10-11 23:02:12 +00:00
window.SwapBuffers()
}
defer fmt.Println("exiting...")
2024-10-11 16:38:21 +00:00
}