2024-10-11 16:38:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
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-10-11 16:38:21 +00:00
|
|
|
"git.tek.govt.hu/dowerx/opengl-deferred/types/shader"
|
2024-10-11 23:02:12 +00:00
|
|
|
"git.tek.govt.hu/dowerx/opengl-deferred/types/texture"
|
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-10-12 12:31:15 +00:00
|
|
|
WIDTH = 800
|
|
|
|
HEIGHT = 600
|
2024-10-11 16:38:21 +00:00
|
|
|
TITLE = "opengl-deferred"
|
2024-10-11 23:02:12 +00:00
|
|
|
FOV = 45.0
|
2024-10-11 16:38:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-10-11 23:02:12 +00:00
|
|
|
|
2024-10-11 16:38:21 +00:00
|
|
|
// 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-11 23:02:12 +00:00
|
|
|
gl.Enable(gl.DEPTH_TEST)
|
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)
|
|
|
|
gl.ClearColor(0, 0, 0, 0)
|
|
|
|
|
2024-10-11 16:38:21 +00:00
|
|
|
fmt.Println("OpenGL version:", gl.GoStr(gl.GetString(gl.VERSION)))
|
|
|
|
|
|
|
|
// load assets
|
|
|
|
// shader
|
|
|
|
vertexSource, err := os.ReadFile("assets/shader/deferred/deferred.vs")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fragmentSource, err := os.ReadFile("assets/shader/deferred/deferred.fs")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
deferredShader, err := shader.New(string(vertexSource), string(fragmentSource))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer deferredShader.Delete()
|
|
|
|
|
2024-10-12 02:03:59 +00:00
|
|
|
vertexSource, err = os.ReadFile("assets/shader/screen/screen.vs")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fragmentSource, err = os.ReadFile("assets/shader/screen/screen.fs")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
screenShader, err := shader.New(string(vertexSource), string(fragmentSource))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer screenShader.Delete()
|
|
|
|
|
2024-10-11 23:02:12 +00:00
|
|
|
// geometry
|
2024-10-12 12:31:15 +00:00
|
|
|
cube, err := geometry.LoadOBJ("assets/geometries/cube.obj")
|
2024-10-11 23:02:12 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-10-12 00:22:54 +00:00
|
|
|
defer cube.Delete()
|
2024-10-11 23:02:12 +00:00
|
|
|
|
2024-10-12 02:03:59 +00:00
|
|
|
screen, err := geometry.Screen()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer screen.Delete()
|
|
|
|
|
2024-10-11 23:02:12 +00:00
|
|
|
// textures
|
2024-10-12 00:22:54 +00:00
|
|
|
albedo, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_albedo.tif")
|
2024-10-11 23:02:12 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer albedo.Delete()
|
|
|
|
|
2024-10-12 00:22:54 +00:00
|
|
|
normal, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_normal.tif")
|
2024-10-11 23:02:12 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer normal.Delete()
|
|
|
|
|
2024-10-12 00:22:54 +00:00
|
|
|
specular, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_roughness.tif")
|
2024-10-11 23:02:12 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer specular.Delete()
|
|
|
|
|
2024-10-12 00:22:54 +00:00
|
|
|
roughness, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_roughness.tif")
|
2024-10-11 23:02:12 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer roughness.Delete()
|
|
|
|
|
2024-10-12 00:22:54 +00:00
|
|
|
metalic, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_roughness.tif")
|
2024-10-11 23:02:12 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer metalic.Delete()
|
|
|
|
|
2024-10-12 12:31:15 +00:00
|
|
|
// lights
|
|
|
|
lights := []light.Light{
|
|
|
|
{Color: [3]float32{1, 1, 1}, Position: mgl32.Vec3{3, 3, 3}, Intensity: 10, Type: light.POINT},
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
projection := mgl32.Perspective(mgl32.DegToRad(FOV), float32(WIDTH)/HEIGHT, 0.1, 10)
|
2024-10-12 12:31:15 +00:00
|
|
|
viewPositon := mgl32.Vec3{3, 3, 3}
|
|
|
|
view := mgl32.LookAtV(viewPositon, mgl32.Vec3{0, 0, 0}, mgl32.Vec3{0, 1, 0})
|
2024-10-11 23:02:12 +00:00
|
|
|
|
2024-10-12 00:22:54 +00:00
|
|
|
angle := 0.0
|
|
|
|
previousTime := glfw.GetTime()
|
2024-10-11 23:02:12 +00:00
|
|
|
for !window.ShouldClose() {
|
2024-10-12 00:22:54 +00:00
|
|
|
// rotate
|
|
|
|
time := glfw.GetTime()
|
|
|
|
elapsed := time - previousTime
|
|
|
|
previousTime = time
|
|
|
|
|
|
|
|
angle += elapsed
|
2024-10-12 12:31:15 +00:00
|
|
|
model := mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0})
|
2024-10-12 02:03:59 +00:00
|
|
|
|
|
|
|
// first pass
|
|
|
|
// bind textures
|
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)
|
|
|
|
|
|
|
|
albedo.Bind(gl.TEXTURE0)
|
|
|
|
normal.Bind(gl.TEXTURE1)
|
|
|
|
specular.Bind(gl.TEXTURE2)
|
|
|
|
roughness.Bind(gl.TEXTURE3)
|
|
|
|
metalic.Bind(gl.TEXTURE4)
|
|
|
|
|
|
|
|
// transfer uniforms
|
|
|
|
deferredShader.Use()
|
|
|
|
deferredShader.Mat4("projection", projection)
|
2024-10-12 12:31:15 +00:00
|
|
|
deferredShader.Mat4("view", view)
|
2024-10-12 00:22:54 +00:00
|
|
|
deferredShader.Mat4("model", model)
|
|
|
|
|
2024-10-12 02:03:59 +00:00
|
|
|
deferredShader.Int("s_albedo", 0)
|
|
|
|
deferredShader.Int("s_normal", 1)
|
|
|
|
deferredShader.Int("s_specular", 2)
|
|
|
|
deferredShader.Int("s_roughness", 3)
|
|
|
|
deferredShader.Int("s_metalic", 4)
|
2024-10-12 00:22:54 +00:00
|
|
|
cube.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()
|
|
|
|
|
|
|
|
screenShader.Use()
|
2024-10-12 12:31:15 +00:00
|
|
|
screenShader.Int("s_albedo", 0)
|
|
|
|
screenShader.Int("s_depth", 1)
|
|
|
|
screenShader.Int("s_normal", 2)
|
|
|
|
screenShader.Int("s_material", 3)
|
|
|
|
screenShader.Int("s_position", 4)
|
|
|
|
screenShader.Vec3("view_pos", &viewPositon[0])
|
|
|
|
|
|
|
|
for _, light := range lights {
|
|
|
|
screenShader.Light(&light)
|
|
|
|
}
|
2024-10-12 02:03:59 +00:00
|
|
|
|
|
|
|
screen.Draw()
|
|
|
|
|
2024-10-11 23:02:12 +00:00
|
|
|
window.SwapBuffers()
|
|
|
|
glfw.PollEvents()
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fmt.Println("exiting...")
|
2024-10-11 16:38:21 +00:00
|
|
|
}
|