diff --git a/main.go b/main.go index 1dcd07d..b733023 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "os" "runtime" "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/shader" "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" "github.com/go-gl/gl/v4.6-core/gl" "github.com/go-gl/glfw/v3.3/glfw" @@ -24,7 +24,6 @@ const ( ) func main() { - // init glfw runtime.LockOSThread() if err := glfw.Init(); err != nil { @@ -49,65 +48,50 @@ func main() { panic(err) } - gl.Enable(gl.DEPTH_TEST) gl.Enable(gl.CULL_FACE) 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))) // load assets + assetManager := assetmanager.Get() + defer assetManager.Delete() + // shader - vertexSource, err := os.ReadFile("assets/shader/deferred/deferred.vs") - if err != nil { - panic(err) - } - fragmentSource, err := os.ReadFile("assets/shader/deferred/deferred.fs") + tmpShader, err := shader.FromFiles("assets/shader/deferred/deferred.vs", "assets/shader/deferred/deferred.fs") if err != nil { 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 { panic(err) } - defer deferredShader.Delete() + assetManager.Add("pbr", tmpShader) - vertexSource, err = os.ReadFile("assets/shader/pbr/pbr.vs") - 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") + tmpShader, err = shader.FromFiles("assets/shader/screen/screen.vs", "assets/shader/screen/screen.fs") if err != nil { panic(err) } + assetManager.Add("screen", tmpShader) - screenShader, err := shader.New(string(vertexSource), string(fragmentSource)) - if err != nil { - panic(err) - } - defer screenShader.Delete() + deferredShader := assetManager.Shadres["deferred"] + screenShader := assetManager.Shadres["pbr"] // geometry - var geometries []geometry.Geometry - - cube, err := geometry.LoadOBJ("assets/geometries/cube.obj") + tmpGeometry, err := geometry.LoadOBJ("assets/geometries/20.obj") if err != nil { panic(err) } - defer cube.Delete() + assetManager.Add("teapot", tmpGeometry) - geometries = append(geometries, cube) - - plane, err := geometry.LoadOBJ("assets/geometries/plane.obj") - if err != nil { - panic(err) - } - defer plane.Delete() - - geometries = append(geometries, plane) + // tmpGeometry, err = geometry.LoadOBJ("assets/geometries/plane.obj") + // if err != nil { + // panic(err) + // } + // assetManager.Add("plane", tmpGeometry) screen, err := geometry.Screen() if err != nil { @@ -116,47 +100,35 @@ func main() { defer screen.Delete() // 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 { 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 { 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 { 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 { 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 { panic(err) } - defer metalic.Delete() - - 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() + assetManager.Add("black", tmpTexture) // lights lights := []light.Light{ @@ -176,7 +148,7 @@ func main() { // transformations projection := mgl32.Perspective(mgl32.DegToRad(FOV), float32(WIDTH)/HEIGHT, 0.1, 1000) 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() { glfw.PollEvents() @@ -189,11 +161,11 @@ func main() { 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) + assetManager.Textures["albedo"].Bind(gl.TEXTURE0) + assetManager.Textures["normal"].Bind(gl.TEXTURE1) + assetManager.Textures["roughness"].Bind(gl.TEXTURE2) + assetManager.Textures["roughness"].Bind(gl.TEXTURE3) + assetManager.Textures["roughness"].Bind(gl.TEXTURE4) // transfer uniforms deferredShader.Use() @@ -206,7 +178,7 @@ func main() { deferredShader.Int("s_specular", 2) deferredShader.Int("s_roughness", 3) deferredShader.Int("s_metalic", 4) - for _, g := range geometries { + for _, g := range assetManager.Geometries { g.Draw() } diff --git a/types/shader/shader.go b/types/shader/shader.go index 21a120a..f9752c1 100644 --- a/types/shader/shader.go +++ b/types/shader/shader.go @@ -3,6 +3,7 @@ package shader import ( "errors" "fmt" + "os" "strings" "git.tek.govt.hu/dowerx/opengl-deferred/types/light" @@ -91,7 +92,7 @@ func compileShader(source string, shaderType uint32) (uint32, error) { 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) vertexShader, err := compileShader(vertexSource, gl.VERTEX_SHADER) @@ -99,17 +100,17 @@ func New(vertexSource string, fragmantSource string) (shader Shader, _ error) { return shader, err } - fragmantShader, err := compileShader(fragmantSource, gl.FRAGMENT_SHADER) + fragmentShader, err := compileShader(fragmentSource, gl.FRAGMENT_SHADER) if err != nil { return shader, err } shader.program = gl.CreateProgram() gl.AttachShader(shader.program, vertexShader) - gl.AttachShader(shader.program, fragmantShader) + gl.AttachShader(shader.program, fragmentShader) gl.LinkProgram(shader.program) defer gl.DeleteShader(vertexShader) - defer gl.DeleteShader(fragmantShader) + defer gl.DeleteShader(fragmentShader) var status int32 gl.GetProgramiv(shader.program, gl.LINK_STATUS, &status) @@ -125,3 +126,21 @@ func New(vertexSource string, fragmantSource string) (shader Shader, _ error) { 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 +} diff --git a/utils/assetmanager/assetmanager.go b/utils/assetmanager/assetmanager.go new file mode 100644 index 0000000..8e9e3b8 --- /dev/null +++ b/utils/assetmanager/assetmanager.go @@ -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() + } +}