mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 51057b8a0d | |||
| e1bf0697fc | |||
| 901d8e2b5e | |||
| 89d04c9d24 | |||
| f1b6f3a7c0 | |||
| d1f47316ae | |||
| 709dc062cc | |||
| 660c41bc06 |
@ -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 {
|
||||
|
||||
@ -1,17 +1,26 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"github.com/bloeys/nmage/asserts"
|
||||
"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"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
)
|
||||
|
||||
var (
|
||||
isInited = false
|
||||
)
|
||||
|
||||
type Window struct {
|
||||
SDLWin *sdl.Window
|
||||
GlCtx sdl.GLContext
|
||||
EventCallbacks []func(sdl.Event)
|
||||
Rend renderer.Render
|
||||
}
|
||||
|
||||
func (w *Window) handleInputs() {
|
||||
@ -92,6 +101,9 @@ func (w *Window) Destroy() error {
|
||||
|
||||
func Init() error {
|
||||
|
||||
isInited = true
|
||||
|
||||
runtime.LockOSThread()
|
||||
timing.Init()
|
||||
err := initSDL()
|
||||
|
||||
@ -124,16 +136,17 @@ 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) {
|
||||
|
||||
asserts.T(isInited, "engine.Init was not called!")
|
||||
if x == -1 && y == -1 {
|
||||
x = sdl.WINDOWPOS_CENTERED
|
||||
y = sdl.WINDOWPOS_CENTERED
|
||||
@ -143,7 +156,11 @@ func createWindow(title string, x, y, width, height int32, flags WindowFlags) (*
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
win := &Window{SDLWin: sdlWin, EventCallbacks: make([]func(sdl.Event), 0)}
|
||||
win := &Window{
|
||||
SDLWin: sdlWin,
|
||||
EventCallbacks: make([]func(sdl.Event), 0),
|
||||
Rend: rend,
|
||||
}
|
||||
|
||||
win.GlCtx, err = sdlWin.GLCreateContext()
|
||||
if err != nil {
|
||||
@ -169,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,29 +6,34 @@ import (
|
||||
"github.com/go-gl/gl/v4.1-core/gl"
|
||||
)
|
||||
|
||||
var (
|
||||
isRunning = false
|
||||
)
|
||||
|
||||
type Game interface {
|
||||
Init()
|
||||
|
||||
FrameStart()
|
||||
Update()
|
||||
Render()
|
||||
FrameEnd()
|
||||
ShouldRun() bool
|
||||
|
||||
GetWindow() *Window
|
||||
GetImGUI() nmageimgui.ImguiInfo
|
||||
|
||||
Deinit()
|
||||
DeInit()
|
||||
}
|
||||
|
||||
func Run(g Game) {
|
||||
func Run(g Game, w *Window, ui nmageimgui.ImguiInfo) {
|
||||
|
||||
isRunning = true
|
||||
g.Init()
|
||||
|
||||
w := g.GetWindow()
|
||||
ui := g.GetImGUI()
|
||||
for g.ShouldRun() {
|
||||
//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))
|
||||
ui.Render(float32(tempWidth), float32(tempHeight), tempFBWidth, tempFBHeight)
|
||||
|
||||
for isRunning {
|
||||
|
||||
//PERF: Cache these
|
||||
width, height := w.SDLWin.GetSize()
|
||||
fbWidth, fbHeight := w.SDLWin.GLGetDrawableSize()
|
||||
|
||||
@ -36,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)
|
||||
@ -46,8 +49,13 @@ func Run(g Game) {
|
||||
w.SDLWin.GLSwap()
|
||||
|
||||
g.FrameEnd()
|
||||
w.Rend.FrameEnd()
|
||||
timing.FrameEnded()
|
||||
}
|
||||
|
||||
g.Deinit()
|
||||
g.DeInit()
|
||||
}
|
||||
|
||||
func Quit() {
|
||||
isRunning = false
|
||||
}
|
||||
|
||||
89
main.go
89
main.go
@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"github.com/bloeys/assimp-go/asig"
|
||||
"github.com/bloeys/gglm/gglm"
|
||||
@ -12,28 +11,32 @@ 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:
|
||||
// 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?
|
||||
// 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
|
||||
window *engine.Window
|
||||
window *engine.Window
|
||||
|
||||
simpleMat *materials.Material
|
||||
cubeMesh *meshes.Mesh
|
||||
@ -50,7 +53,6 @@ var (
|
||||
type OurGame struct {
|
||||
Win *engine.Window
|
||||
ImGUIInfo nmageimgui.ImguiInfo
|
||||
Quitting bool
|
||||
}
|
||||
|
||||
func (g *OurGame) Init() {
|
||||
@ -66,19 +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)
|
||||
}
|
||||
cubeMesh.AddTexture(tex)
|
||||
|
||||
//Set mesh textures on material
|
||||
for _, v := range cubeMesh.TextureIDs {
|
||||
simpleMat.AddTextureID(v)
|
||||
}
|
||||
|
||||
//Enable vertex attributes
|
||||
simpleMat.SetAttribute(cubeMesh.Buf)
|
||||
//Configure material
|
||||
simpleMat.DiffuseTex = tex.TexID
|
||||
|
||||
//Movement, scale and rotation
|
||||
translationMat := gglm.NewTranslationMat(gglm.NewVec3(0, 0, 0))
|
||||
@ -100,13 +96,10 @@ func (g *OurGame) Init() {
|
||||
simpleMat.SetUnifVec3("lightColor1", lightColor1)
|
||||
}
|
||||
|
||||
func (g *OurGame) FrameStart() {
|
||||
}
|
||||
|
||||
func (g *OurGame) Update() {
|
||||
|
||||
if input.IsQuitClicked() {
|
||||
g.Quitting = true
|
||||
engine.Quit()
|
||||
}
|
||||
|
||||
winWidth, winHeight := g.Win.SDLWin.GetSize()
|
||||
@ -149,64 +142,31 @@ 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.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()
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
runtime.LockOSThread()
|
||||
|
||||
//Init engine
|
||||
err := engine.Init()
|
||||
if err != nil {
|
||||
@ -214,7 +174,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)
|
||||
}
|
||||
@ -227,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"
|
||||
@ -12,7 +11,8 @@ import (
|
||||
type Material struct {
|
||||
Name string
|
||||
ShaderProg shaders.ShaderProgram
|
||||
TexIDs []uint32
|
||||
|
||||
DiffuseTex uint32
|
||||
|
||||
UnifLocs map[string]int32
|
||||
AttribLocs map[string]int32
|
||||
@ -21,19 +21,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 {
|
||||
@ -62,27 +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) AddTextureID(texID uint32) {
|
||||
m.TexIDs = append(m.TexIDs, texID)
|
||||
}
|
||||
|
||||
func (m *Material) EnableAttribute(attribName string) {
|
||||
gl.EnableVertexAttribArray(uint32(m.GetAttribLoc(attribName)))
|
||||
}
|
||||
|
||||
@ -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
41
renderer/rend3dgl/rend3dgl.go
Executable 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
12
renderer/renderer.go
Executable 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()
|
||||
}
|
||||
@ -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.Seconds())
|
||||
}
|
||||
}
|
||||
|
||||
//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())
|
||||
|
||||
@ -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