TextureLoadOptions+fix DT bug+remove SetAttribute

This commit is contained in:
bloeys
2022-07-02 21:38:48 +04:00
parent e1bf0697fc
commit 51057b8a0d
6 changed files with 45 additions and 39 deletions

View File

@ -5,7 +5,7 @@ var (
TexturePaths map[string]uint32 = make(map[string]uint32) TexturePaths map[string]uint32 = make(map[string]uint32)
) )
func SetTexture(t Texture) { func AddTextureToCache(t Texture) {
if _, ok := TexturePaths[t.Path]; ok { if _, ok := TexturePaths[t.Path]; ok {
return return
@ -16,12 +16,12 @@ func SetTexture(t Texture) {
TexturePaths[t.Path] = t.TexID TexturePaths[t.Path] = t.TexID
} }
func GetTexture(texID uint32) (Texture, bool) { func GetTextureFromCacheID(texID uint32) (Texture, bool) {
tex, ok := Textures[texID] tex, ok := Textures[texID]
return tex, ok return tex, ok
} }
func GetTexturePath(path string) (Texture, bool) { func GetTextureFromCachePath(path string) (Texture, bool) {
tex, ok := Textures[TexturePaths[path]] tex, ok := Textures[TexturePaths[path]]
return tex, ok return tex, ok
} }

View File

@ -24,11 +24,23 @@ type Texture struct {
Pixels []byte Pixels []byte
} }
func LoadPNGTexture(file string) (Texture, error) { type TextureLoadOptions struct {
TryLoadFromCache bool
WriteToCache bool
GenMipMaps bool
}
if tex, ok := GetTexturePath(file); ok { 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 return tex, nil
} }
}
//Load from disk //Load from disk
fileBytes, err := os.ReadFile(file) fileBytes, err := os.ReadFile(file)
@ -72,14 +84,19 @@ func LoadPNGTexture(file string) (Texture, error) {
// set the texture wrapping/filtering options (on the currently bound texture object) // 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_S, gl.REPEAT)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, 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) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
// load and generate the texture // 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.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 return tex, nil
} }

View File

@ -54,6 +54,8 @@ func (b *Buffer) GetLayout() []Element {
return e return e
} }
//SetLayout updates the layout object and the corresponding vertex attributes.
//Vertex attributes are also enabled.
func (b *Buffer) SetLayout(layout ...Element) { func (b *Buffer) SetLayout(layout ...Element) {
b.layout = layout b.layout = layout
@ -64,6 +66,20 @@ func (b *Buffer) SetLayout(layout ...Element) {
b.layout[i].Offset = int(b.Stride) b.layout[i].Offset = int(b.Stride)
b.Stride += b.layout[i].Size() 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 { func NewBuffer(layout ...Element) Buffer {

13
main.go
View File

@ -18,15 +18,6 @@ import (
"github.com/veandco/go-sdl2/sdl" "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: //TODO: Tasks:
// Build simple game // Build simple game
// Integrate physx // Integrate physx
@ -34,6 +25,7 @@ import (
// Camera class // Camera class
//Low Priority: //Low Priority:
// Create VAO struct independent from VBO to support multi-VBO use cases (e.g. instancing)
// Renderer batching // Renderer batching
// Scene graph // Scene graph
// Separate engine loop from rendering loop? or leave it to the user? // Separate engine loop from rendering loop? or leave it to the user?
@ -76,14 +68,13 @@ func (g *OurGame) Init() {
} }
//Load textures //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 { if err != nil {
logging.ErrLog.Fatalln("Failed to load texture. Err: ", err) logging.ErrLog.Fatalln("Failed to load texture. Err: ", err)
} }
//Configure material //Configure material
simpleMat.DiffuseTex = tex.TexID simpleMat.DiffuseTex = tex.TexID
simpleMat.SetAttribute(cubeMesh.Buf)
//Movement, scale and rotation //Movement, scale and rotation
translationMat := gglm.NewTranslationMat(gglm.NewVec3(0, 0, 0)) translationMat := gglm.NewTranslationMat(gglm.NewVec3(0, 0, 0))

View File

@ -3,7 +3,6 @@ package materials
import ( import (
"github.com/bloeys/gglm/gglm" "github.com/bloeys/gglm/gglm"
"github.com/bloeys/nmage/asserts" "github.com/bloeys/nmage/asserts"
"github.com/bloeys/nmage/buffers"
"github.com/bloeys/nmage/logging" "github.com/bloeys/nmage/logging"
"github.com/bloeys/nmage/shaders" "github.com/bloeys/nmage/shaders"
"github.com/go-gl/gl/v4.1-core/gl" "github.com/go-gl/gl/v4.1-core/gl"
@ -61,23 +60,6 @@ func (m *Material) GetUnifLoc(uniformName string) int32 {
return loc 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) { func (m *Material) EnableAttribute(attribName string) {
gl.EnableVertexAttribArray(uint32(m.GetAttribLoc(attribName))) gl.EnableVertexAttribArray(uint32(m.GetAttribLoc(attribName)))
} }

View File

@ -43,7 +43,7 @@ func FrameEnded() {
//Calculate new dt //Calculate new dt
dt = float32(time.Since(frameStart).Seconds()) dt = float32(time.Since(frameStart).Seconds())
if dt == 0 { if dt == 0 {
dt = float32(time.Microsecond) dt = float32(time.Microsecond.Seconds())
} }
} }