Ensure renderer calls aren't virtual

This commit is contained in:
bloeys
2024-05-13 04:57:16 +04:00
parent 5aa0f41085
commit 3795a7123f
5 changed files with 32 additions and 32 deletions

View File

@ -9,7 +9,6 @@ import (
"github.com/bloeys/nmage/assert" "github.com/bloeys/nmage/assert"
"github.com/bloeys/nmage/assets" "github.com/bloeys/nmage/assets"
"github.com/bloeys/nmage/input" "github.com/bloeys/nmage/input"
"github.com/bloeys/nmage/renderer"
"github.com/bloeys/nmage/timing" "github.com/bloeys/nmage/timing"
nmageimgui "github.com/bloeys/nmage/ui/imgui" nmageimgui "github.com/bloeys/nmage/ui/imgui"
"github.com/go-gl/gl/v4.1-core/gl" "github.com/go-gl/gl/v4.1-core/gl"
@ -31,7 +30,6 @@ type Window struct {
SDLWin *sdl.Window SDLWin *sdl.Window
GlCtx sdl.GLContext GlCtx sdl.GLContext
EventCallbacks []func(sdl.Event) EventCallbacks []func(sdl.Event)
Rend renderer.Render
} }
func (w *Window) handleInputs() { func (w *Window) handleInputs() {
@ -170,22 +168,21 @@ func initSDL() error {
return nil return nil
} }
func CreateOpenGLWindow(title string, x, y, width, height int32, flags WindowFlags, rend renderer.Render) (Window, error) { func CreateOpenGLWindow(title string, x, y, width, height int32, flags WindowFlags) (Window, error) {
return createWindow(title, x, y, width, height, WindowFlags_OPENGL|flags, rend) return createWindow(title, x, y, width, height, WindowFlags_OPENGL|flags)
} }
func CreateOpenGLWindowCentered(title string, width, height int32, flags WindowFlags, rend renderer.Render) (Window, error) { func CreateOpenGLWindowCentered(title string, width, height int32, flags WindowFlags) (Window, error) {
return createWindow(title, sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, width, height, WindowFlags_OPENGL|flags, rend) return createWindow(title, sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, width, height, WindowFlags_OPENGL|flags)
} }
func createWindow(title string, x, y, width, height int32, flags WindowFlags, rend renderer.Render) (Window, error) { func createWindow(title string, x, y, width, height int32, flags WindowFlags) (Window, error) {
assert.T(isInited, "engine.Init() was not called!") assert.T(isInited, "engine.Init() was not called!")
win := Window{ win := Window{
SDLWin: nil, SDLWin: nil,
EventCallbacks: make([]func(sdl.Event), 0), EventCallbacks: make([]func(sdl.Event), 0),
Rend: rend,
} }
var err error var err error

View File

@ -1,6 +1,7 @@
package engine package engine
import ( import (
"github.com/bloeys/nmage/renderer"
"github.com/bloeys/nmage/timing" "github.com/bloeys/nmage/timing"
nmageimgui "github.com/bloeys/nmage/ui/imgui" nmageimgui "github.com/bloeys/nmage/ui/imgui"
"github.com/go-gl/gl/v4.1-core/gl" "github.com/go-gl/gl/v4.1-core/gl"
@ -20,7 +21,7 @@ type Game interface {
DeInit() DeInit()
} }
func Run(g Game, w *Window, ui nmageimgui.ImguiInfo) { func Run(g Game, w *Window, rend renderer.Render, ui nmageimgui.ImguiInfo) {
isRunning = true isRunning = true
@ -56,7 +57,7 @@ func Run(g Game, w *Window, ui nmageimgui.ImguiInfo) {
w.SDLWin.GLSwap() w.SDLWin.GLSwap()
g.FrameEnd() g.FrameEnd()
w.Rend.FrameEnd() rend.FrameEnd()
timing.FrameEnded() timing.FrameEnded()
} }

34
main.go
View File

@ -329,6 +329,7 @@ type Game struct {
WinWidth int32 WinWidth int32
WinHeight int32 WinHeight int32
Win *engine.Window Win *engine.Window
Rend *rend3dgl.Rend3DGL
ImGUIInfo nmageimgui.ImguiInfo ImGUIInfo nmageimgui.ImguiInfo
} }
@ -342,7 +343,7 @@ func main() {
//Create window //Create window
dpiScaling = getDpiScaling(unscaledWindowWidth, unscaledWindowHeight) dpiScaling = getDpiScaling(unscaledWindowWidth, unscaledWindowHeight)
window, err = engine.CreateOpenGLWindowCentered("nMage", int32(unscaledWindowWidth*dpiScaling), int32(unscaledWindowHeight*dpiScaling), engine.WindowFlags_RESIZABLE, rend3dgl.NewRend3DGL()) window, err = engine.CreateOpenGLWindowCentered("nMage", int32(unscaledWindowWidth*dpiScaling), int32(unscaledWindowHeight*dpiScaling), engine.WindowFlags_RESIZABLE)
if err != nil { if err != nil {
logging.ErrLog.Fatalln("Failed to create window. Err: ", err) logging.ErrLog.Fatalln("Failed to create window. Err: ", err)
} }
@ -356,6 +357,7 @@ func main() {
Win: &window, Win: &window,
WinWidth: int32(unscaledWindowWidth * dpiScaling), WinWidth: int32(unscaledWindowWidth * dpiScaling),
WinHeight: int32(unscaledWindowHeight * dpiScaling), WinHeight: int32(unscaledWindowHeight * dpiScaling),
Rend: rend3dgl.NewRend3DGL(),
ImGUIInfo: nmageimgui.NewImGui("./res/shaders/imgui.glsl"), ImGUIInfo: nmageimgui.NewImGui("./res/shaders/imgui.glsl"),
} }
window.EventCallbacks = append(window.EventCallbacks, game.handleWindowEvents) window.EventCallbacks = append(window.EventCallbacks, game.handleWindowEvents)
@ -372,7 +374,7 @@ func main() {
} }
window.SDLWin.SetTitle("nMage") window.SDLWin.SetTitle("nMage")
engine.Run(game, &window, game.ImGUIInfo) engine.Run(game, &window, game.Rend, game.ImGUIInfo)
if PROFILE_CPU { if PROFILE_CPU {
pprof.StopCPUProfile() pprof.StopCPUProfile()
@ -1202,7 +1204,7 @@ func (g *Game) renderDirectionalLightShadowmap() {
screenQuadMat.SetUnifVec2("offset", &dirLightDepthMapFboOffset) screenQuadMat.SetUnifVec2("offset", &dirLightDepthMapFboOffset)
screenQuadMat.SetUnifVec2("scale", &dirLightDepthMapFboScale) screenQuadMat.SetUnifVec2("scale", &dirLightDepthMapFboScale)
screenQuadMat.Bind() screenQuadMat.Bind()
window.Rend.DrawVertexArray(screenQuadMat, screenQuadVao, 0, 6) g.Rend.DrawVertexArray(&screenQuadMat, &screenQuadVao, 0, 6)
} }
} }
@ -1285,7 +1287,7 @@ func (g *Game) renderDemoFbo() {
screenQuadMat.SetUnifVec2("offset", &demoFboOffset) screenQuadMat.SetUnifVec2("offset", &demoFboOffset)
screenQuadMat.SetUnifVec2("scale", &demoFboScale) screenQuadMat.SetUnifVec2("scale", &demoFboScale)
window.Rend.DrawVertexArray(screenQuadMat, screenQuadVao, 0, 6) g.Rend.DrawVertexArray(&screenQuadMat, &screenQuadVao, 0, 6)
} }
func (g *Game) renderHdrFbo() { func (g *Game) renderHdrFbo() {
@ -1302,7 +1304,7 @@ func (g *Game) renderHdrFbo() {
hdrFbo.UnBind() hdrFbo.UnBind()
tonemappedScreenQuadMat.DiffuseTex = hdrFbo.Attachments[0].Id tonemappedScreenQuadMat.DiffuseTex = hdrFbo.Attachments[0].Id
window.Rend.DrawVertexArray(tonemappedScreenQuadMat, screenQuadVao, 0, 6) g.Rend.DrawVertexArray(&tonemappedScreenQuadMat, &screenQuadVao, 0, 6)
} }
func (g *Game) RenderScene(overrideMat *materials.Material) { func (g *Game) RenderScene(overrideMat *materials.Material) {
@ -1324,41 +1326,41 @@ func (g *Game) RenderScene(overrideMat *materials.Material) {
// Draw dir light // Draw dir light
dirLightTrMat := gglm.NewTrMatId() dirLightTrMat := gglm.NewTrMatId()
window.Rend.DrawMesh(sphereMesh, *dirLightTrMat.Translate(0, 10, 0).Scale(0.1, 0.1, 0.1), sunMat) g.Rend.DrawMesh(&sphereMesh, dirLightTrMat.Translate(0, 10, 0).Scale(0.1, 0.1, 0.1), &sunMat)
// Draw point lights // Draw point lights
for i := 0; i < len(pointLights); i++ { for i := 0; i < len(pointLights); i++ {
pl := &pointLights[i] pl := &pointLights[i]
plTrMat := gglm.NewTrMatId() plTrMat := gglm.NewTrMatId()
window.Rend.DrawMesh(cubeMesh, *plTrMat.TranslateVec(&pl.Pos).Scale(0.1, 0.1, 0.1), sunMat) g.Rend.DrawMesh(&cubeMesh, plTrMat.TranslateVec(&pl.Pos).Scale(0.1, 0.1, 0.1), &sunMat)
} }
// Chair // Chair
window.Rend.DrawMesh(chairMesh, tempModelMatrix, chairMat) g.Rend.DrawMesh(&chairMesh, &tempModelMatrix, &chairMat)
// Ground // Ground
groundTrMat := gglm.NewTrMatId() groundTrMat := gglm.NewTrMatId()
window.Rend.DrawMesh(cubeMesh, *groundTrMat.Translate(0, -3, 0).Scale(20, 1, 20), groundMat) g.Rend.DrawMesh(&cubeMesh, groundTrMat.Translate(0, -3, 0).Scale(20, 1, 20), &groundMat)
// Cubes // Cubes
tempModelMatrix.Translate(-6, 0, 0) tempModelMatrix.Translate(-6, 0, 0)
window.Rend.DrawMesh(cubeMesh, tempModelMatrix, cubeMat) g.Rend.DrawMesh(&cubeMesh, &tempModelMatrix, &cubeMat)
tempModelMatrix.Translate(0, -1, -4) tempModelMatrix.Translate(0, -1, -4)
window.Rend.DrawMesh(cubeMesh, tempModelMatrix, cubeMat) g.Rend.DrawMesh(&cubeMesh, &tempModelMatrix, &cubeMat)
// Rotating cubes // Rotating cubes
window.Rend.DrawMesh(cubeMesh, rotatingCubeTrMat1, cubeMat) g.Rend.DrawMesh(&cubeMesh, &rotatingCubeTrMat1, &cubeMat)
window.Rend.DrawMesh(cubeMesh, rotatingCubeTrMat2, cubeMat) g.Rend.DrawMesh(&cubeMesh, &rotatingCubeTrMat2, &cubeMat)
window.Rend.DrawMesh(cubeMesh, rotatingCubeTrMat3, cubeMat) g.Rend.DrawMesh(&cubeMesh, &rotatingCubeTrMat3, &cubeMat)
// Cubes generator // Cubes generator
// rowSize := 1 // rowSize := 1
// for y := 0; y < rowSize; y++ { // for y := 0; y < rowSize; y++ {
// for x := 0; x < rowSize; x++ { // for x := 0; x < rowSize; x++ {
// tempModelMatrix.Translate(gglm.NewVec3(-6, 0, 0)) // tempModelMatrix.Translate(gglm.NewVec3(-6, 0, 0))
// window.Rend.DrawMesh(cubeMesh, tempModelMatrix, cubeMat) // g.Rend.DrawMesh(cubeMesh, tempModelMatrix, cubeMat)
// } // }
// tempModelMatrix.Translate(gglm.NewVec3(float32(rowSize), -1, 0)) // tempModelMatrix.Translate(gglm.NewVec3(float32(rowSize), -1, 0))
// } // }
@ -1369,7 +1371,7 @@ func (g *Game) DrawSkybox() {
gl.Disable(gl.CULL_FACE) gl.Disable(gl.CULL_FACE)
gl.DepthFunc(gl.LEQUAL) gl.DepthFunc(gl.LEQUAL)
window.Rend.DrawCubemap(skyboxMesh, skyboxMat) g.Rend.DrawCubemap(&skyboxMesh, &skyboxMat)
gl.DepthFunc(gl.LESS) gl.DepthFunc(gl.LESS)
gl.Enable(gl.CULL_FACE) gl.Enable(gl.CULL_FACE)

View File

@ -17,7 +17,7 @@ type Rend3DGL struct {
BoundMeshVaoId uint32 BoundMeshVaoId uint32
} }
func (r *Rend3DGL) DrawMesh(mesh meshes.Mesh, modelMat gglm.TrMat, mat materials.Material) { func (r *Rend3DGL) DrawMesh(mesh *meshes.Mesh, modelMat *gglm.TrMat, mat *materials.Material) {
if mesh.Vao.Id != r.BoundMeshVaoId { if mesh.Vao.Id != r.BoundMeshVaoId {
mesh.Vao.Bind() mesh.Vao.Bind()
@ -43,7 +43,7 @@ func (r *Rend3DGL) DrawMesh(mesh meshes.Mesh, modelMat gglm.TrMat, mat materials
} }
} }
func (r *Rend3DGL) DrawVertexArray(mat materials.Material, vao buffers.VertexArray, firstElement int32, elementCount int32) { func (r *Rend3DGL) DrawVertexArray(mat *materials.Material, vao *buffers.VertexArray, firstElement int32, elementCount int32) {
if vao.Id != r.BoundVaoId { if vao.Id != r.BoundVaoId {
vao.Bind() vao.Bind()
@ -58,7 +58,7 @@ func (r *Rend3DGL) DrawVertexArray(mat materials.Material, vao buffers.VertexArr
gl.DrawArrays(gl.TRIANGLES, firstElement, elementCount) gl.DrawArrays(gl.TRIANGLES, firstElement, elementCount)
} }
func (r *Rend3DGL) DrawCubemap(mesh meshes.Mesh, mat materials.Material) { func (r *Rend3DGL) DrawCubemap(mesh *meshes.Mesh, mat *materials.Material) {
if mesh.Vao.Id != r.BoundMeshVaoId { if mesh.Vao.Id != r.BoundMeshVaoId {
mesh.Vao.Bind() mesh.Vao.Bind()

View File

@ -8,8 +8,8 @@ import (
) )
type Render interface { type Render interface {
DrawMesh(mesh meshes.Mesh, trMat gglm.TrMat, mat materials.Material) DrawMesh(mesh *meshes.Mesh, trMat *gglm.TrMat, mat *materials.Material)
DrawVertexArray(mat materials.Material, vao buffers.VertexArray, firstElement int32, count int32) DrawVertexArray(mat *materials.Material, vao *buffers.VertexArray, firstElement int32, count int32)
DrawCubemap(mesh meshes.Mesh, mat materials.Material) DrawCubemap(mesh *meshes.Mesh, mat *materials.Material)
FrameEnd() FrameEnd()
} }