basic assetmaanager

This commit is contained in:
BENEDEK László 2024-12-16 13:24:09 +01:00
parent e43a73d316
commit 5da859f0e7
3 changed files with 127 additions and 69 deletions

102
main.go
View File

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"os"
"runtime" "runtime"
"git.tek.govt.hu/dowerx/opengl-deferred/types/gbuffer" "git.tek.govt.hu/dowerx/opengl-deferred/types/gbuffer"
@ -10,6 +9,7 @@ import (
"git.tek.govt.hu/dowerx/opengl-deferred/types/light" "git.tek.govt.hu/dowerx/opengl-deferred/types/light"
"git.tek.govt.hu/dowerx/opengl-deferred/types/shader" "git.tek.govt.hu/dowerx/opengl-deferred/types/shader"
"git.tek.govt.hu/dowerx/opengl-deferred/types/texture" "git.tek.govt.hu/dowerx/opengl-deferred/types/texture"
"git.tek.govt.hu/dowerx/opengl-deferred/utils/assetmanager"
"git.tek.govt.hu/dowerx/opengl-deferred/utils/fpscontrols" "git.tek.govt.hu/dowerx/opengl-deferred/utils/fpscontrols"
"github.com/go-gl/gl/v4.6-core/gl" "github.com/go-gl/gl/v4.6-core/gl"
"github.com/go-gl/glfw/v3.3/glfw" "github.com/go-gl/glfw/v3.3/glfw"
@ -24,7 +24,6 @@ const (
) )
func main() { func main() {
// init glfw // init glfw
runtime.LockOSThread() runtime.LockOSThread()
if err := glfw.Init(); err != nil { if err := glfw.Init(); err != nil {
@ -49,65 +48,50 @@ func main() {
panic(err) panic(err)
} }
gl.Enable(gl.DEPTH_TEST)
gl.Enable(gl.CULL_FACE) gl.Enable(gl.CULL_FACE)
gl.DepthFunc(gl.LESS) gl.DepthFunc(gl.LESS)
gl.ClearColor(0, 0, 0, 0) gl.ClearColor(0, 0, 0, 1)
fmt.Println("OpenGL version:", gl.GoStr(gl.GetString(gl.VERSION))) fmt.Println("OpenGL version:", gl.GoStr(gl.GetString(gl.VERSION)))
// load assets // load assets
assetManager := assetmanager.Get()
defer assetManager.Delete()
// shader // shader
vertexSource, err := os.ReadFile("assets/shader/deferred/deferred.vs") tmpShader, err := shader.FromFiles("assets/shader/deferred/deferred.vs", "assets/shader/deferred/deferred.fs")
if err != nil {
panic(err)
}
fragmentSource, err := os.ReadFile("assets/shader/deferred/deferred.fs")
if err != nil { if err != nil {
panic(err) panic(err)
} }
assetManager.Add("deferred", tmpShader)
deferredShader, err := shader.New(string(vertexSource), string(fragmentSource)) tmpShader, err = shader.FromFiles("assets/shader/pbr/pbr.vs", "assets/shader/pbr/pbr.fs")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer deferredShader.Delete() assetManager.Add("pbr", tmpShader)
vertexSource, err = os.ReadFile("assets/shader/pbr/pbr.vs") tmpShader, err = shader.FromFiles("assets/shader/screen/screen.vs", "assets/shader/screen/screen.fs")
if err != nil {
panic(err)
}
// fragmentSource, err = os.ReadFile("assets/shader/screen/screen-debug.fs")
// fragmentSource, err = os.ReadFile("assets/shader/screen/screen.fs")
fragmentSource, err = os.ReadFile("assets/shader/pbr/pbr.fs")
if err != nil { if err != nil {
panic(err) panic(err)
} }
assetManager.Add("screen", tmpShader)
screenShader, err := shader.New(string(vertexSource), string(fragmentSource)) deferredShader := assetManager.Shadres["deferred"]
if err != nil { screenShader := assetManager.Shadres["pbr"]
panic(err)
}
defer screenShader.Delete()
// geometry // geometry
var geometries []geometry.Geometry tmpGeometry, err := geometry.LoadOBJ("assets/geometries/20.obj")
cube, err := geometry.LoadOBJ("assets/geometries/cube.obj")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer cube.Delete() assetManager.Add("teapot", tmpGeometry)
geometries = append(geometries, cube) // tmpGeometry, err = geometry.LoadOBJ("assets/geometries/plane.obj")
// if err != nil {
plane, err := geometry.LoadOBJ("assets/geometries/plane.obj") // panic(err)
if err != nil { // }
panic(err) // assetManager.Add("plane", tmpGeometry)
}
defer plane.Delete()
geometries = append(geometries, plane)
screen, err := geometry.Screen() screen, err := geometry.Screen()
if err != nil { if err != nil {
@ -116,47 +100,35 @@ func main() {
defer screen.Delete() defer screen.Delete()
// textures // textures
albedo, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_albedo.tif") tmpTexture, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_albedo.tif")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer albedo.Delete() assetManager.Add("albedo", tmpTexture)
normal, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_normal.tif") tmpTexture, err = texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_normal.tif")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer normal.Delete() assetManager.Add("normal", tmpTexture)
specular, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_roughness.tif") tmpTexture, err = texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_roughness.tif")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer specular.Delete() assetManager.Add("roughness", tmpTexture)
roughness, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_roughness.tif") tmpTexture, err = texture.Load("assets/textures/white.png")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer roughness.Delete() assetManager.Add("white", tmpTexture)
metalic, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_roughness.tif") tmpTexture, err = texture.Load("assets/textures/black.png")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer metalic.Delete() assetManager.Add("black", tmpTexture)
white, err := texture.Load("assets/textures/white.png")
if err != nil {
panic(err)
}
defer white.Delete()
black, err := texture.Load("assets/textures/black.png")
if err != nil {
panic(err)
}
defer black.Delete()
// lights // lights
lights := []light.Light{ lights := []light.Light{
@ -176,7 +148,7 @@ func main() {
// transformations // transformations
projection := mgl32.Perspective(mgl32.DegToRad(FOV), float32(WIDTH)/HEIGHT, 0.1, 1000) projection := mgl32.Perspective(mgl32.DegToRad(FOV), float32(WIDTH)/HEIGHT, 0.1, 1000)
model := mgl32.Ident4() model := mgl32.Ident4()
controls := fpscontrols.Get(15, 10, mgl32.Vec3{0, 0, -10}, mgl32.Vec2{0, 0}, window) controls := fpscontrols.Get(10, 10, mgl32.Vec3{0, 0, -10}, mgl32.Vec2{0, 0}, window)
for !window.ShouldClose() { for !window.ShouldClose() {
glfw.PollEvents() glfw.PollEvents()
@ -189,11 +161,11 @@ func main() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
albedo.Bind(gl.TEXTURE0) assetManager.Textures["albedo"].Bind(gl.TEXTURE0)
normal.Bind(gl.TEXTURE1) assetManager.Textures["normal"].Bind(gl.TEXTURE1)
specular.Bind(gl.TEXTURE2) assetManager.Textures["roughness"].Bind(gl.TEXTURE2)
roughness.Bind(gl.TEXTURE3) assetManager.Textures["roughness"].Bind(gl.TEXTURE3)
metalic.Bind(gl.TEXTURE4) assetManager.Textures["roughness"].Bind(gl.TEXTURE4)
// transfer uniforms // transfer uniforms
deferredShader.Use() deferredShader.Use()
@ -206,7 +178,7 @@ func main() {
deferredShader.Int("s_specular", 2) deferredShader.Int("s_specular", 2)
deferredShader.Int("s_roughness", 3) deferredShader.Int("s_roughness", 3)
deferredShader.Int("s_metalic", 4) deferredShader.Int("s_metalic", 4)
for _, g := range geometries { for _, g := range assetManager.Geometries {
g.Draw() g.Draw()
} }

View File

@ -3,6 +3,7 @@ package shader
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"strings" "strings"
"git.tek.govt.hu/dowerx/opengl-deferred/types/light" "git.tek.govt.hu/dowerx/opengl-deferred/types/light"
@ -91,7 +92,7 @@ func compileShader(source string, shaderType uint32) (uint32, error) {
return shader, nil return shader, nil
} }
func New(vertexSource string, fragmantSource string) (shader Shader, _ error) { func New(vertexSource string, fragmentSource string) (shader Shader, _ error) {
shader.uniforms = make(map[string]int32) shader.uniforms = make(map[string]int32)
vertexShader, err := compileShader(vertexSource, gl.VERTEX_SHADER) vertexShader, err := compileShader(vertexSource, gl.VERTEX_SHADER)
@ -99,17 +100,17 @@ func New(vertexSource string, fragmantSource string) (shader Shader, _ error) {
return shader, err return shader, err
} }
fragmantShader, err := compileShader(fragmantSource, gl.FRAGMENT_SHADER) fragmentShader, err := compileShader(fragmentSource, gl.FRAGMENT_SHADER)
if err != nil { if err != nil {
return shader, err return shader, err
} }
shader.program = gl.CreateProgram() shader.program = gl.CreateProgram()
gl.AttachShader(shader.program, vertexShader) gl.AttachShader(shader.program, vertexShader)
gl.AttachShader(shader.program, fragmantShader) gl.AttachShader(shader.program, fragmentShader)
gl.LinkProgram(shader.program) gl.LinkProgram(shader.program)
defer gl.DeleteShader(vertexShader) defer gl.DeleteShader(vertexShader)
defer gl.DeleteShader(fragmantShader) defer gl.DeleteShader(fragmentShader)
var status int32 var status int32
gl.GetProgramiv(shader.program, gl.LINK_STATUS, &status) gl.GetProgramiv(shader.program, gl.LINK_STATUS, &status)
@ -125,3 +126,21 @@ func New(vertexSource string, fragmantSource string) (shader Shader, _ error) {
return shader, nil return shader, nil
} }
func FromFiles(vertexPath, fragmentPath string) (shader Shader, _ error) {
vertexSource, err := os.ReadFile(vertexPath)
if err != nil {
return shader, err
}
fragmentSource, err := os.ReadFile(fragmentPath)
if err != nil {
return shader, err
}
shader, err = New(string(vertexSource), string(fragmentSource))
if err != nil {
return shader, err
}
return shader, nil
}

View File

@ -0,0 +1,67 @@
package assetmanager
import (
"errors"
"reflect"
"git.tek.govt.hu/dowerx/opengl-deferred/types/geometry"
"git.tek.govt.hu/dowerx/opengl-deferred/types/shader"
"git.tek.govt.hu/dowerx/opengl-deferred/types/texture"
)
type AssetManager struct {
Geometries map[string]geometry.Geometry
Textures map[string]texture.Texture
Shadres map[string]shader.Shader
}
var instance *AssetManager
var (
geometryType = reflect.TypeOf(geometry.Geometry{})
textureType = reflect.TypeOf(texture.Texture{})
shaderType = reflect.TypeOf(shader.Shader{})
)
func Get() *AssetManager {
if instance != nil {
return &AssetManager{}
}
instance = &AssetManager{
Geometries: make(map[string]geometry.Geometry),
Textures: make(map[string]texture.Texture),
Shadres: make(map[string]shader.Shader),
}
return instance
}
func (m *AssetManager) Add(key string, item interface{}) error {
switch reflect.TypeOf(item) {
case geometryType:
m.Geometries[key] = item.(geometry.Geometry)
case textureType:
m.Textures[key] = item.(texture.Texture)
case shaderType:
m.Shadres[key] = item.(shader.Shader)
default:
return errors.New("unknown asset type")
}
return nil
}
func (m *AssetManager) Delete() {
for _, g := range m.Geometries {
g.Delete()
}
for _, t := range m.Textures {
t.Delete()
}
for _, s := range m.Shadres {
s.Delete()
}
}