Compare commits

...

10 Commits

Author SHA1 Message Date
f1b6f3a7c0 Start function in Game interface 2022-02-27 11:21:15 +04:00
d1f47316ae Allow imgui within init 2022-02-27 11:11:20 +04:00
709dc062cc Day 15: Basic renderer+improve material system+lockosthread on init 2022-02-26 22:07:59 +04:00
660c41bc06 ensure dt is never zero 2022-02-24 14:34:21 +04:00
99f5548ce2 Fix imgui dt 2022-02-24 14:22:23 +04:00
5a54b1b465 Make all ImguiInfo public 2022-02-24 08:58:25 +04:00
36ac96d641 Allow configuration when loading fonts 2022-02-24 07:50:35 +04:00
577e6250a8 Return imgui font after set 2022-02-24 06:59:19 +04:00
c311a0981c Imgui setfont func 2022-02-24 06:51:24 +04:00
064a932037 Allow app to receive window event callbacks 2022-02-24 05:48:35 +04:00
9 changed files with 195 additions and 98 deletions

View File

@ -1,7 +1,10 @@
package engine
import (
"runtime"
"github.com/bloeys/nmage/input"
"github.com/bloeys/nmage/renderer"
"github.com/bloeys/nmage/timing"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/inkyblackness/imgui-go/v4"
@ -9,8 +12,10 @@ import (
)
type Window struct {
SDLWin *sdl.Window
GlCtx sdl.GLContext
SDLWin *sdl.Window
GlCtx sdl.GLContext
EventCallbacks []func(sdl.Event)
Rend renderer.Render
}
func (w *Window) handleInputs() {
@ -20,6 +25,12 @@ func (w *Window) handleInputs() {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
//Fire callbacks
for i := 0; i < len(w.EventCallbacks); i++ {
w.EventCallbacks[i](event)
}
//Internal processing
switch e := event.(type) {
case *sdl.MouseWheelEvent:
@ -85,6 +96,7 @@ func (w *Window) Destroy() error {
func Init() error {
runtime.LockOSThread()
timing.Init()
err := initSDL()
@ -117,15 +129,15 @@ func initSDL() error {
return nil
}
func CreateOpenGLWindow(title string, x, y, width, height int32, flags WindowFlags) (*Window, error) {
return createWindow(title, x, y, width, height, WindowFlags_OPENGL|flags)
func CreateOpenGLWindow(title string, x, y, width, height int32, flags WindowFlags, rend renderer.Render) (*Window, error) {
return createWindow(title, x, y, width, height, WindowFlags_OPENGL|flags, rend)
}
func CreateOpenGLWindowCentered(title string, width, height int32, flags WindowFlags) (*Window, error) {
return createWindow(title, -1, -1, width, height, WindowFlags_OPENGL|flags)
func CreateOpenGLWindowCentered(title string, width, height int32, flags WindowFlags, rend renderer.Render) (*Window, error) {
return createWindow(title, -1, -1, width, height, WindowFlags_OPENGL|flags, rend)
}
func createWindow(title string, x, y, width, height int32, flags WindowFlags) (*Window, error) {
func createWindow(title string, x, y, width, height int32, flags WindowFlags, rend renderer.Render) (*Window, error) {
if x == -1 && y == -1 {
x = sdl.WINDOWPOS_CENTERED
@ -136,7 +148,11 @@ func createWindow(title string, x, y, width, height int32, flags WindowFlags) (*
if err != nil {
return nil, err
}
win := &Window{SDLWin: sdlWin}
win := &Window{
SDLWin: sdlWin,
EventCallbacks: make([]func(sdl.Event), 0),
Rend: rend,
}
win.GlCtx, err = sdlWin.GLCreateContext()
if err != nil {

View File

@ -8,6 +8,7 @@ import (
type Game interface {
Init()
Start()
FrameStart()
Update()
@ -23,12 +24,21 @@ type Game interface {
func Run(g Game) {
g.Init()
w := g.GetWindow()
ui := g.GetImGUI()
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() {
//PERF: Cache these
width, height := w.SDLWin.GetSize()
fbWidth, fbHeight := w.SDLWin.GLGetDrawableSize()
@ -46,6 +56,7 @@ func Run(g Game) {
w.SDLWin.GLSwap()
g.FrameEnd()
w.Rend.FrameEnd()
timing.FrameEnded()
}

62
main.go
View File

@ -2,7 +2,6 @@ package main
import (
"fmt"
"runtime"
"github.com/bloeys/assimp-go/asig"
"github.com/bloeys/gglm/gglm"
@ -12,24 +11,28 @@ import (
"github.com/bloeys/nmage/logging"
"github.com/bloeys/nmage/materials"
"github.com/bloeys/nmage/meshes"
"github.com/bloeys/nmage/renderer/rend3dgl"
"github.com/bloeys/nmage/timing"
nmageimgui "github.com/bloeys/nmage/ui/imgui"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/inkyblackness/imgui-go/v4"
"github.com/veandco/go-sdl2/sdl"
)
//TODO: Tasks:
//Proper rendering setup
//Entities and components
//Camera class
//Audio
//Flesh out the material system
// Build simple game
// Integrate physx
// Entities and components
// Camera class
//Low Priority:
// Renderer batching
// Scene graph
// Separate engine loop from rendering loop? or leave it to the user?
// Abstract keys enum away from sdl
// Abstract UI
// Proper Asset loading
// Audio
// Frustum culling
// Material system editor with fields automatically extracted from the shader
var (
isRunning bool = true
@ -70,14 +73,9 @@ func (g *OurGame) Init() {
if err != nil {
logging.ErrLog.Fatalln("Failed to load texture. Err: ", err)
}
cubeMesh.AddTexture(tex)
//Set mesh textures on material
for _, v := range cubeMesh.TextureIDs {
simpleMat.AddTextureID(v)
}
//Enable vertex attributes
//Configure material
simpleMat.DiffuseTex = tex.TexID
simpleMat.SetAttribute(cubeMesh.Buf)
//Movement, scale and rotation
@ -100,6 +98,9 @@ func (g *OurGame) Init() {
simpleMat.SetUnifVec3("lightColor1", lightColor1)
}
func (g *OurGame) Start() {
}
func (g *OurGame) FrameStart() {
}
@ -149,42 +150,23 @@ func (g *OurGame) Update() {
imgui.DragFloat3("Cam Pos", &camPos.Data)
}
var dtAccum float32 = 0
var lastElapsedTime uint64 = 0
var framesSinceLastFPSUpdate uint = 0
func (g *OurGame) Render() {
simpleMat.Bind()
cubeMesh.Buf.Bind()
tempModelMat := modelMat.Clone()
rowSize := 100
for y := 0; y < rowSize; y++ {
for x := 0; x < rowSize; x++ {
simpleMat.SetUnifMat4("modelMat", &tempModelMat.Translate(gglm.NewVec3(-1, 0, 0)).Mat4)
gl.DrawElements(gl.TRIANGLES, cubeMesh.Buf.IndexBufCount, gl.UNSIGNED_INT, gl.PtrOffset(0))
tempModelMat.Translate(gglm.NewVec3(-1, 0, 0))
window.Rend.Draw(cubeMesh, tempModelMat, simpleMat)
}
simpleMat.SetUnifMat4("modelMat", &tempModelMat.Translate(gglm.NewVec3(float32(rowSize), -1, 0)).Mat4)
}
simpleMat.SetUnifMat4("modelMat", &modelMat.Mat4)
dtAccum += timing.DT()
framesSinceLastFPSUpdate++
if timing.ElapsedTime() > lastElapsedTime {
avgDT := dtAccum / float32(framesSinceLastFPSUpdate)
g.GetWindow().SDLWin.SetTitle(fmt.Sprint("nMage (", 1/avgDT, " fps)"))
dtAccum = 0
framesSinceLastFPSUpdate = 0
tempModelMat.Translate(gglm.NewVec3(float32(rowSize), -1, 0))
}
lastElapsedTime = timing.ElapsedTime()
g.GetWindow().SDLWin.SetTitle(fmt.Sprint("nMage (", timing.GetAvgFPS(), " fps)"))
}
func (g *OurGame) FrameEnd() {
}
func (g *OurGame) ShouldRun() bool {
@ -205,8 +187,6 @@ func (g *OurGame) Deinit() {
func main() {
runtime.LockOSThread()
//Init engine
err := engine.Init()
if err != nil {
@ -214,7 +194,7 @@ func main() {
}
//Create window
window, err = engine.CreateOpenGLWindowCentered("nMage", 1280, 720, engine.WindowFlags_RESIZABLE)
window, err = engine.CreateOpenGLWindowCentered("nMage", 1280, 720, engine.WindowFlags_RESIZABLE, rend3dgl.NewRend3DGL())
if err != nil {
logging.ErrLog.Fatalln("Failed to create window. Err: ", err)
}

View File

@ -12,7 +12,8 @@ import (
type Material struct {
Name string
ShaderProg shaders.ShaderProgram
TexIDs []uint32
DiffuseTex uint32
UnifLocs map[string]int32
AttribLocs map[string]int32
@ -21,19 +22,17 @@ type Material struct {
func (m *Material) Bind() {
gl.UseProgram(m.ShaderProg.ID)
for i, v := range m.TexIDs {
gl.ActiveTexture(gl.TEXTURE0 + uint32(i))
gl.BindTexture(gl.TEXTURE_2D, v)
}
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, m.DiffuseTex)
}
func (m *Material) UnBind() {
gl.UseProgram(0)
for i := range m.TexIDs {
gl.ActiveTexture(gl.TEXTURE0 + uint32(i))
gl.BindTexture(gl.TEXTURE_2D, 0)
}
//TODO: Should we unbind textures here? Are these two lines needed?
// gl.ActiveTexture(gl.TEXTURE0)
// gl.BindTexture(gl.TEXTURE_2D, 0)
}
func (m *Material) GetAttribLoc(attribName string) int32 {
@ -79,10 +78,6 @@ func (m *Material) SetAttribute(bufObj buffers.Buffer) {
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
func (m *Material) AddTextureID(texID uint32) {
m.TexIDs = append(m.TexIDs, texID)
}
func (m *Material) EnableAttribute(attribName string) {
gl.EnableVertexAttribArray(uint32(m.GetAttribLoc(attribName)))
}

View File

@ -7,18 +7,12 @@ import (
"github.com/bloeys/assimp-go/asig"
"github.com/bloeys/gglm/gglm"
"github.com/bloeys/nmage/asserts"
"github.com/bloeys/nmage/assets"
"github.com/bloeys/nmage/buffers"
)
type Mesh struct {
Name string
TextureIDs []uint32
Buf buffers.Buffer
}
func (m *Mesh) AddTexture(tex assets.Texture) {
m.TextureIDs = append(m.TextureIDs, tex.TexID)
Name string
Buf buffers.Buffer
}
func NewMesh(name, modelPath string, postProcessFlags asig.PostProcess) (*Mesh, error) {

41
renderer/rend3dgl/rend3dgl.go Executable file
View File

@ -0,0 +1,41 @@
package rend3dgl
import (
"github.com/bloeys/gglm/gglm"
"github.com/bloeys/nmage/materials"
"github.com/bloeys/nmage/meshes"
"github.com/bloeys/nmage/renderer"
"github.com/go-gl/gl/v4.1-core/gl"
)
var _ renderer.Render = &Rend3DGL{}
type Rend3DGL struct {
BoundMesh *meshes.Mesh
BoundMat *materials.Material
}
func (r3d *Rend3DGL) Draw(mesh *meshes.Mesh, trMat *gglm.TrMat, mat *materials.Material) {
if mesh != r3d.BoundMesh {
mesh.Buf.Bind()
r3d.BoundMesh = mesh
}
if mat != r3d.BoundMat {
mat.Bind()
r3d.BoundMat = mat
}
mat.SetUnifMat4("modelMat", &trMat.Mat4)
gl.DrawElements(gl.TRIANGLES, mesh.Buf.IndexBufCount, gl.UNSIGNED_INT, gl.PtrOffset(0))
}
func (r3d *Rend3DGL) FrameEnd() {
r3d.BoundMesh = nil
r3d.BoundMat = nil
}
func NewRend3DGL() *Rend3DGL {
return &Rend3DGL{}
}

12
renderer/renderer.go Executable file
View File

@ -0,0 +1,12 @@
package renderer
import (
"github.com/bloeys/gglm/gglm"
"github.com/bloeys/nmage/materials"
"github.com/bloeys/nmage/meshes"
)
type Render interface {
Draw(mesh *meshes.Mesh, trMat *gglm.TrMat, mat *materials.Material)
FrameEnd()
}

View File

@ -1,11 +1,19 @@
package timing
import "time"
import (
"time"
)
var (
dt float32 = 0.01
frameStart time.Time
startTime time.Time
//fps calculator vars
dtAccum float32 = 1
lastElapsedTime uint64 = 0
framesSinceLastFPSUpdate uint = 0
avgFps float32 = 1
)
func Init() {
@ -13,11 +21,30 @@ func Init() {
}
func FrameStarted() {
frameStart = time.Now()
//fps stuff
dtAccum += dt
framesSinceLastFPSUpdate++
et := ElapsedTime()
if et > lastElapsedTime {
avgDT := dtAccum / float32(framesSinceLastFPSUpdate)
avgFps = 1 / avgDT
dtAccum = 0
framesSinceLastFPSUpdate = 0
}
lastElapsedTime = et
}
func FrameEnded() {
//Calculate new dt
dt = float32(time.Since(frameStart).Seconds())
if dt == 0 {
dt = float32(time.Microsecond)
}
}
//DT is frame deltatime in seconds
@ -25,6 +52,11 @@ func DT() float32 {
return dt
}
//GetAvgFPS returns the fps averaged over 1 second
func GetAvgFPS() float32 {
return avgFps
}
//ElapsedTime is time since game start
func ElapsedTime() uint64 {
return uint64(time.Since(startTime).Seconds())

View File

@ -14,10 +14,10 @@ type ImguiInfo struct {
ImCtx *imgui.Context
Mat *materials.Material
vaoID uint32
vboID uint32
indexBufID uint32
texID uint32
VaoID uint32
VboID uint32
IndexBufID uint32
TexID uint32
}
func (i *ImguiInfo) FrameStart(winWidth, winHeight float32) {
@ -28,15 +28,7 @@ func (i *ImguiInfo) FrameStart(winWidth, winHeight float32) {
imIO := imgui.CurrentIO()
imIO.SetDisplaySize(imgui.Vec2{X: float32(winWidth), Y: float32(winHeight)})
// Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
frequency := sdl.GetPerformanceFrequency()
currentTime := sdl.GetPerformanceCounter()
if timing.ElapsedTime() > 0 {
imIO.SetDeltaTime(float32(currentTime-timing.ElapsedTime()) / float32(frequency))
} else {
imIO.SetDeltaTime(1.0 / 60.0)
}
imIO.SetDeltaTime(timing.DT())
imgui.NewFrame()
}
@ -85,8 +77,8 @@ func (i *ImguiInfo) Render(winWidth, winHeight float32, fbWidth, fbHeight int32)
// Recreate the VAO every time
// (This is to easily allow multiple GL contexts. VAO are not shared among GL contexts, and
// we don't track creation/deletion of windows so we don't have an obvious key to use to cache them.)
gl.BindVertexArray(i.vaoID)
gl.BindBuffer(gl.ARRAY_BUFFER, i.vboID)
gl.BindVertexArray(i.VaoID)
gl.BindBuffer(gl.ARRAY_BUFFER, i.VboID)
vertexSize, vertexOffsetPos, vertexOffsetUv, vertexOffsetCol := imgui.VertexBufferLayout()
i.Mat.EnableAttribute("Position")
@ -106,11 +98,11 @@ func (i *ImguiInfo) Render(winWidth, winHeight float32, fbWidth, fbHeight int32)
for _, list := range drawData.CommandLists() {
vertexBuffer, vertexBufferSize := list.VertexBuffer()
gl.BindBuffer(gl.ARRAY_BUFFER, i.vboID)
gl.BindBuffer(gl.ARRAY_BUFFER, i.VboID)
gl.BufferData(gl.ARRAY_BUFFER, vertexBufferSize, vertexBuffer, gl.STREAM_DRAW)
indexBuffer, indexBufferSize := list.IndexBuffer()
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, i.indexBufID)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, i.IndexBufID)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, indexBufferSize, indexBuffer, gl.STREAM_DRAW)
for _, cmd := range list.Commands() {
@ -118,7 +110,7 @@ func (i *ImguiInfo) Render(winWidth, winHeight float32, fbWidth, fbHeight int32)
cmd.CallUserCallback(list)
} else {
gl.BindTexture(gl.TEXTURE_2D, i.texID)
gl.BindTexture(gl.TEXTURE_2D, i.TexID)
clipRect := cmd.ClipRect()
gl.Scissor(int32(clipRect.X), int32(fbHeight)-int32(clipRect.W), int32(clipRect.Z-clipRect.X), int32(clipRect.W-clipRect.Y))
@ -134,6 +126,30 @@ func (i *ImguiInfo) Render(winWidth, winHeight float32, fbWidth, fbHeight int32)
gl.Enable(gl.DEPTH_TEST)
}
func (i *ImguiInfo) AddFontTTF(fontPath string, fontSize float32, fontConfig *imgui.FontConfig, glyphRanges *imgui.GlyphRanges) imgui.Font {
fontConfigToUse := imgui.DefaultFontConfig
if fontConfig != nil {
fontConfigToUse = *fontConfig
}
glyphRangesToUse := imgui.EmptyGlyphRanges
if glyphRanges != nil {
glyphRangesToUse = *glyphRanges
}
imIO := imgui.CurrentIO()
a := imIO.Fonts()
f := a.AddFontFromFileTTFV(fontPath, fontSize, fontConfigToUse, glyphRangesToUse)
image := a.TextureDataAlpha8()
gl.BindTexture(gl.TEXTURE_2D, i.TexID)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, int32(image.Width), int32(image.Height), 0, gl.RED, gl.UNSIGNED_BYTE, image.Pixels)
return f
}
func NewImGUI() ImguiInfo {
imguiInfo := ImguiInfo{
@ -144,13 +160,13 @@ func NewImGUI() ImguiInfo {
imIO := imgui.CurrentIO()
imIO.SetBackendFlags(imIO.GetBackendFlags() | imgui.BackendFlagsRendererHasVtxOffset)
gl.GenVertexArrays(1, &imguiInfo.vaoID)
gl.GenBuffers(1, &imguiInfo.vboID)
gl.GenBuffers(1, &imguiInfo.indexBufID)
gl.GenTextures(1, &imguiInfo.texID)
gl.GenVertexArrays(1, &imguiInfo.VaoID)
gl.GenBuffers(1, &imguiInfo.VboID)
gl.GenBuffers(1, &imguiInfo.IndexBufID)
gl.GenTextures(1, &imguiInfo.TexID)
// Upload font to gpu
gl.BindTexture(gl.TEXTURE_2D, imguiInfo.texID)
gl.BindTexture(gl.TEXTURE_2D, imguiInfo.TexID)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.PixelStorei(gl.UNPACK_ROW_LENGTH, 0)
@ -159,7 +175,7 @@ func NewImGUI() ImguiInfo {
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, int32(image.Width), int32(image.Height), 0, gl.RED, gl.UNSIGNED_BYTE, image.Pixels)
// Store our identifier
imIO.Fonts().SetTextureID(imgui.TextureID(imguiInfo.texID))
imIO.Fonts().SetTextureID(imgui.TextureID(imguiInfo.TexID))
//Shader attributes
imguiInfo.Mat.Bind()