From 51057b8a0daa15c6eda433b328f5b4653d59a744 Mon Sep 17 00:00:00 2001 From: bloeys Date: Sat, 2 Jul 2022 21:38:48 +0400 Subject: [PATCH] TextureLoadOptions+fix DT bug+remove SetAttribute --- assets/assets.go | 6 +++--- assets/textures.go | 29 +++++++++++++++++++++++------ buffers/buffers.go | 16 ++++++++++++++++ main.go | 13 ++----------- materials/material.go | 18 ------------------ timing/timing.go | 2 +- 6 files changed, 45 insertions(+), 39 deletions(-) diff --git a/assets/assets.go b/assets/assets.go index c67013b..1dbecf8 100755 --- a/assets/assets.go +++ b/assets/assets.go @@ -5,7 +5,7 @@ var ( TexturePaths map[string]uint32 = make(map[string]uint32) ) -func SetTexture(t Texture) { +func AddTextureToCache(t Texture) { if _, ok := TexturePaths[t.Path]; ok { return @@ -16,12 +16,12 @@ func SetTexture(t Texture) { TexturePaths[t.Path] = t.TexID } -func GetTexture(texID uint32) (Texture, bool) { +func GetTextureFromCacheID(texID uint32) (Texture, bool) { tex, ok := Textures[texID] return tex, ok } -func GetTexturePath(path string) (Texture, bool) { +func GetTextureFromCachePath(path string) (Texture, bool) { tex, ok := Textures[TexturePaths[path]] return tex, ok } diff --git a/assets/textures.go b/assets/textures.go index fed036d..066d8b2 100755 --- a/assets/textures.go +++ b/assets/textures.go @@ -24,10 +24,22 @@ type Texture struct { Pixels []byte } -func LoadPNGTexture(file string) (Texture, error) { +type TextureLoadOptions struct { + TryLoadFromCache bool + WriteToCache bool + GenMipMaps bool +} - if tex, ok := GetTexturePath(file); ok { - return tex, nil +func LoadPNGTexture(file string, loadOptions *TextureLoadOptions) (Texture, error) { + + if loadOptions == nil { + loadOptions = &TextureLoadOptions{} + } + + if loadOptions.TryLoadFromCache { + if tex, ok := GetTextureFromCachePath(file); ok { + return tex, nil + } } //Load from disk @@ -72,14 +84,19 @@ func LoadPNGTexture(file string) (Texture, error) { // set the texture wrapping/filtering options (on the currently bound texture object) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) - gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR) + gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) // load and generate the texture gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, tex.Width, tex.Height, 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tex.Pixels[0])) - gl.GenerateMipmap(gl.TEXTURE_2D) - SetTexture(tex) + if loadOptions.GenMipMaps { + gl.GenerateMipmap(tex.TexID) + } + + if loadOptions.WriteToCache { + AddTextureToCache(tex) + } return tex, nil } diff --git a/buffers/buffers.go b/buffers/buffers.go index bd0a8b2..882b07a 100755 --- a/buffers/buffers.go +++ b/buffers/buffers.go @@ -54,6 +54,8 @@ func (b *Buffer) GetLayout() []Element { return e } +//SetLayout updates the layout object and the corresponding vertex attributes. +//Vertex attributes are also enabled. func (b *Buffer) SetLayout(layout ...Element) { b.layout = layout @@ -64,6 +66,20 @@ func (b *Buffer) SetLayout(layout ...Element) { b.layout[i].Offset = int(b.Stride) b.Stride += b.layout[i].Size() } + + //Set opengl stuff + b.Bind() + + //NOTE: VBOs are only bound at 'VertexAttribPointer', not BindBUffer, so we need to bind the buffer and vao here + gl.BindBuffer(gl.ARRAY_BUFFER, b.BufID) + + for i := 0; i < len(layout); i++ { + gl.EnableVertexAttribArray(uint32(i)) + gl.VertexAttribPointer(uint32(i), layout[i].ElementType.CompCount(), layout[i].ElementType.GLType(), false, b.Stride, gl.PtrOffset(layout[i].Offset)) + } + + b.UnBind() + gl.BindBuffer(gl.ARRAY_BUFFER, 0) } func NewBuffer(layout ...Element) Buffer { diff --git a/main.go b/main.go index bb51e85..837adf2 100755 --- a/main.go +++ b/main.go @@ -18,15 +18,6 @@ import ( "github.com/veandco/go-sdl2/sdl" ) -//BUGS: -// need to rebind the texutre if the texture (or any material values) change between draw calls -// DT handling for when it is zero is wrong! (Gives 1000 DT) - -//nMage TODO: -// * Allow texture loading without cache -// * Move SetAttribute away from material struct -// * Create VAO struct independent from VBO to support multi-VBO use cases (e.g. instancing) - //TODO: Tasks: // Build simple game // Integrate physx @@ -34,6 +25,7 @@ import ( // Camera class //Low Priority: +// Create VAO struct independent from VBO to support multi-VBO use cases (e.g. instancing) // Renderer batching // Scene graph // Separate engine loop from rendering loop? or leave it to the user? @@ -76,14 +68,13 @@ func (g *OurGame) Init() { } //Load textures - tex, err := assets.LoadPNGTexture("./res/textures/Low poly planet.png") + tex, err := assets.LoadPNGTexture("./res/textures/Low poly planet.png", nil) if err != nil { logging.ErrLog.Fatalln("Failed to load texture. Err: ", err) } //Configure material simpleMat.DiffuseTex = tex.TexID - simpleMat.SetAttribute(cubeMesh.Buf) //Movement, scale and rotation translationMat := gglm.NewTranslationMat(gglm.NewVec3(0, 0, 0)) diff --git a/materials/material.go b/materials/material.go index ae3cac9..55f8cee 100755 --- a/materials/material.go +++ b/materials/material.go @@ -3,7 +3,6 @@ package materials import ( "github.com/bloeys/gglm/gglm" "github.com/bloeys/nmage/asserts" - "github.com/bloeys/nmage/buffers" "github.com/bloeys/nmage/logging" "github.com/bloeys/nmage/shaders" "github.com/go-gl/gl/v4.1-core/gl" @@ -61,23 +60,6 @@ func (m *Material) GetUnifLoc(uniformName string) int32 { return loc } -func (m *Material) SetAttribute(bufObj buffers.Buffer) { - - bufObj.Bind() - - //NOTE: VBOs are only bound at 'VertexAttribPointer', not BindBUffer, so we need to bind the buffer and vao here - gl.BindBuffer(gl.ARRAY_BUFFER, bufObj.BufID) - - layout := bufObj.GetLayout() - for i := 0; i < len(layout); i++ { - gl.EnableVertexAttribArray(uint32(i)) - gl.VertexAttribPointer(uint32(i), layout[i].ElementType.CompCount(), layout[i].ElementType.GLType(), false, bufObj.Stride, gl.PtrOffset(layout[i].Offset)) - } - - bufObj.UnBind() - gl.BindBuffer(gl.ARRAY_BUFFER, 0) -} - func (m *Material) EnableAttribute(attribName string) { gl.EnableVertexAttribArray(uint32(m.GetAttribLoc(attribName))) } diff --git a/timing/timing.go b/timing/timing.go index e19d5ed..6fa5430 100755 --- a/timing/timing.go +++ b/timing/timing.go @@ -43,7 +43,7 @@ func FrameEnded() { //Calculate new dt dt = float32(time.Since(frameStart).Seconds()) if dt == 0 { - dt = float32(time.Microsecond) + dt = float32(time.Microsecond.Seconds()) } }