mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d4fe6d4071 | |||
| 51057b8a0d | |||
| e1bf0697fc | |||
| 901d8e2b5e | |||
| 89d04c9d24 |
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -3,6 +3,7 @@ package engine
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"github.com/bloeys/nmage/asserts"
|
||||
"github.com/bloeys/nmage/input"
|
||||
"github.com/bloeys/nmage/renderer"
|
||||
"github.com/bloeys/nmage/timing"
|
||||
@ -11,6 +12,10 @@ import (
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
)
|
||||
|
||||
var (
|
||||
isInited = false
|
||||
)
|
||||
|
||||
type Window struct {
|
||||
SDLWin *sdl.Window
|
||||
GlCtx sdl.GLContext
|
||||
@ -96,6 +101,8 @@ func (w *Window) Destroy() error {
|
||||
|
||||
func Init() error {
|
||||
|
||||
isInited = true
|
||||
|
||||
runtime.LockOSThread()
|
||||
timing.Init()
|
||||
err := initSDL()
|
||||
@ -139,6 +146,7 @@ func CreateOpenGLWindowCentered(title string, width, height int32, flags WindowF
|
||||
|
||||
func createWindow(title string, x, y, width, height int32, flags WindowFlags, rend renderer.Render) (*Window, error) {
|
||||
|
||||
asserts.T(isInited, "engine.Init was not called!")
|
||||
if x == -1 && y == -1 {
|
||||
x = sdl.WINDOWPOS_CENTERED
|
||||
y = sdl.WINDOWPOS_CENTERED
|
||||
@ -178,11 +186,16 @@ func initOpenGL() error {
|
||||
gl.CullFace(gl.BACK)
|
||||
gl.FrontFace(gl.CCW)
|
||||
|
||||
gl.Enable(gl.BLEND)
|
||||
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
|
||||
|
||||
gl.ClearColor(0, 0, 0, 1)
|
||||
return nil
|
||||
}
|
||||
|
||||
func SetVSync(enabled bool) {
|
||||
asserts.T(isInited, "engine.Init was not called!")
|
||||
|
||||
if enabled {
|
||||
sdl.GLSetSwapInterval(1)
|
||||
} else {
|
||||
|
||||
@ -6,37 +6,32 @@ import (
|
||||
"github.com/go-gl/gl/v4.1-core/gl"
|
||||
)
|
||||
|
||||
var (
|
||||
isRunning = false
|
||||
)
|
||||
|
||||
type Game interface {
|
||||
Init()
|
||||
Start()
|
||||
|
||||
FrameStart()
|
||||
Update()
|
||||
Render()
|
||||
FrameEnd()
|
||||
ShouldRun() bool
|
||||
|
||||
GetWindow() *Window
|
||||
GetImGUI() nmageimgui.ImguiInfo
|
||||
|
||||
Deinit()
|
||||
DeInit()
|
||||
}
|
||||
|
||||
func Run(g Game) {
|
||||
|
||||
w := g.GetWindow()
|
||||
ui := g.GetImGUI()
|
||||
func Run(g Game, w *Window, ui nmageimgui.ImguiInfo) {
|
||||
|
||||
isRunning = true
|
||||
g.Init()
|
||||
|
||||
//Simulate an imgui frame during init so any imgui calls are allowed within init
|
||||
tempWidth, tempHeight := w.SDLWin.GetSize()
|
||||
tempFBWidth, tempFBHeight := w.SDLWin.GLGetDrawableSize()
|
||||
ui.FrameStart(float32(tempWidth), float32(tempHeight))
|
||||
g.Start()
|
||||
ui.Render(float32(tempWidth), float32(tempHeight), tempFBWidth, tempFBHeight)
|
||||
|
||||
for g.ShouldRun() {
|
||||
for isRunning {
|
||||
|
||||
//PERF: Cache these
|
||||
width, height := w.SDLWin.GetSize()
|
||||
@ -46,8 +41,6 @@ func Run(g Game) {
|
||||
w.handleInputs()
|
||||
ui.FrameStart(float32(width), float32(height))
|
||||
|
||||
g.FrameStart()
|
||||
|
||||
g.Update()
|
||||
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
|
||||
@ -60,5 +53,9 @@ func Run(g Game) {
|
||||
timing.FrameEnded()
|
||||
}
|
||||
|
||||
g.Deinit()
|
||||
g.DeInit()
|
||||
}
|
||||
|
||||
func Quit() {
|
||||
isRunning = false
|
||||
}
|
||||
|
||||
2
go.mod
2
go.mod
@ -8,6 +8,6 @@ require github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784
|
||||
|
||||
require (
|
||||
github.com/bloeys/assimp-go v0.4.2
|
||||
github.com/bloeys/gglm v0.3.1
|
||||
github.com/bloeys/gglm v0.41.10
|
||||
github.com/inkyblackness/imgui-go/v4 v4.3.0
|
||||
)
|
||||
|
||||
2
go.sum
2
go.sum
@ -2,6 +2,8 @@ github.com/bloeys/assimp-go v0.4.2 h1:ArVK74BCFcTO/rCGj2NgZG9xtbjnJdEn5npIeJx1Z0
|
||||
github.com/bloeys/assimp-go v0.4.2/go.mod h1:my3yRxT7CfOztmvi+0svmwbaqw0KFrxaHxncoyaEIP0=
|
||||
github.com/bloeys/gglm v0.3.1 h1:Sy9upW7SBsBfDXrSmEhid3aQ+7J7itej+upwcxOnPMQ=
|
||||
github.com/bloeys/gglm v0.3.1/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk=
|
||||
github.com/bloeys/gglm v0.41.10 h1:R9FMiI+VQVXAI+vDwCB7z9xqzy5VAR1657u8TQTDNKA=
|
||||
github.com/bloeys/gglm v0.41.10/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784 h1:1Zi56D0LNfvkzM+BdoxKryvUEdyWO7LP8oRT+oSYJW0=
|
||||
|
||||
35
main.go
35
main.go
@ -25,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?
|
||||
@ -35,8 +36,7 @@ import (
|
||||
// Material system editor with fields automatically extracted from the shader
|
||||
|
||||
var (
|
||||
isRunning bool = true
|
||||
window *engine.Window
|
||||
window *engine.Window
|
||||
|
||||
simpleMat *materials.Material
|
||||
cubeMesh *meshes.Mesh
|
||||
@ -53,7 +53,6 @@ var (
|
||||
type OurGame struct {
|
||||
Win *engine.Window
|
||||
ImGUIInfo nmageimgui.ImguiInfo
|
||||
Quitting bool
|
||||
}
|
||||
|
||||
func (g *OurGame) Init() {
|
||||
@ -69,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))
|
||||
@ -98,16 +96,10 @@ func (g *OurGame) Init() {
|
||||
simpleMat.SetUnifVec3("lightColor1", lightColor1)
|
||||
}
|
||||
|
||||
func (g *OurGame) Start() {
|
||||
}
|
||||
|
||||
func (g *OurGame) FrameStart() {
|
||||
}
|
||||
|
||||
func (g *OurGame) Update() {
|
||||
|
||||
if input.IsQuitClicked() {
|
||||
g.Quitting = true
|
||||
engine.Quit()
|
||||
}
|
||||
|
||||
winWidth, winHeight := g.Win.SDLWin.GetSize()
|
||||
@ -163,25 +155,13 @@ func (g *OurGame) Render() {
|
||||
tempModelMat.Translate(gglm.NewVec3(float32(rowSize), -1, 0))
|
||||
}
|
||||
|
||||
g.GetWindow().SDLWin.SetTitle(fmt.Sprint("nMage (", timing.GetAvgFPS(), " fps)"))
|
||||
g.Win.SDLWin.SetTitle(fmt.Sprint("nMage (", timing.GetAvgFPS(), " fps)"))
|
||||
}
|
||||
|
||||
func (g *OurGame) FrameEnd() {
|
||||
}
|
||||
|
||||
func (g *OurGame) ShouldRun() bool {
|
||||
return !g.Quitting
|
||||
}
|
||||
|
||||
func (g *OurGame) GetWindow() *engine.Window {
|
||||
return g.Win
|
||||
}
|
||||
|
||||
func (g *OurGame) GetImGUI() nmageimgui.ImguiInfo {
|
||||
return g.ImGUIInfo
|
||||
}
|
||||
|
||||
func (g *OurGame) Deinit() {
|
||||
func (g *OurGame) DeInit() {
|
||||
g.Win.Destroy()
|
||||
}
|
||||
|
||||
@ -207,8 +187,7 @@ func main() {
|
||||
ImGUIInfo: nmageimgui.NewImGUI(),
|
||||
}
|
||||
|
||||
engine.Run(game)
|
||||
return
|
||||
engine.Run(game, window, game.ImGUIInfo)
|
||||
}
|
||||
|
||||
func updateViewMat() {
|
||||
|
||||
@ -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)))
|
||||
}
|
||||
|
||||
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -120,7 +120,6 @@ func (i *ImguiInfo) Render(winWidth, winHeight float32, fbWidth, fbHeight int32)
|
||||
}
|
||||
|
||||
//Reset gl state
|
||||
gl.Disable(gl.BLEND)
|
||||
gl.Disable(gl.SCISSOR_TEST)
|
||||
gl.Enable(gl.CULL_FACE)
|
||||
gl.Enable(gl.DEPTH_TEST)
|
||||
|
||||
Reference in New Issue
Block a user