cleanup
This commit is contained in:
@@ -3,30 +3,12 @@ package game
|
||||
import (
|
||||
"time"
|
||||
|
||||
"edgaru089.ink/go/gl01/internal/entity"
|
||||
"edgaru089.ink/go/gl01/internal/render"
|
||||
"edgaru089.ink/go/gl01/internal/util/itype"
|
||||
"edgaru089.ink/go/gl01/internal/world"
|
||||
"github.com/inkyblackness/imgui-go/v4"
|
||||
|
||||
_ "edgaru089.ink/go/gl01/internal/entity/entities"
|
||||
)
|
||||
|
||||
// Game holds a game scene.
|
||||
type Game struct {
|
||||
world *world.World
|
||||
|
||||
player *entity.Entity
|
||||
|
||||
view *render.View
|
||||
render renderData
|
||||
|
||||
prevCursorPos itype.Vec2d
|
||||
cameraPos itype.Vec3d
|
||||
rotY itype.Angle
|
||||
rotZ float32 // Degrees in range (-90, 90)
|
||||
|
||||
fbSize itype.Vec2i
|
||||
fullscreen bool
|
||||
lastPos, lastSize itype.Vec2i // Window size before entering fullscreen
|
||||
|
||||
@@ -39,17 +21,5 @@ type Game struct {
|
||||
|
||||
// NewGame creates a new, empty Game.
|
||||
func NewGame() (g *Game) {
|
||||
return &Game{
|
||||
world: world.NewWorld(),
|
||||
player: entity.NewEntity("player", itype.Vec3d{18, 80, 18}),
|
||||
cameraPos: itype.Vec3d{18, 80, 18},
|
||||
rotY: 0,
|
||||
rotZ: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// LoadGameFromPath loads a saved game from a filesystem folder in a read-write fashion.
|
||||
func LoadGameFromPath(path string) (g *Game, err error) {
|
||||
|
||||
return
|
||||
return &Game{}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@ package game
|
||||
import (
|
||||
"image/color"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"edgaru089.ink/go/gl01/internal/asset"
|
||||
@@ -12,8 +10,6 @@ import (
|
||||
"edgaru089.ink/go/gl01/internal/igwrap/backend"
|
||||
"edgaru089.ink/go/gl01/internal/io"
|
||||
"edgaru089.ink/go/gl01/internal/util"
|
||||
"edgaru089.ink/go/gl01/internal/util/itype"
|
||||
"edgaru089.ink/go/gl01/internal/world"
|
||||
"github.com/go-gl/glfw/v3.3/glfw"
|
||||
"github.com/inkyblackness/imgui-go/v4"
|
||||
)
|
||||
@@ -73,13 +69,6 @@ func (g *Game) imgui() {
|
||||
igwrap.TextBackground("CgoCalls:%d (%d lastframe), Goroutines:%d", g.gui.lastframeCgoCalls, io.Diagnostics.CgoCalls, runtime.NumGoroutine())
|
||||
igwrap.TextBlank()
|
||||
|
||||
pos := g.player.Position()
|
||||
igwrap.TextBackground("Player: (%.3f, %.5f, %.3f) (Y%.2f, Z%.2f)", pos[0], pos[1], pos[2], g.rotY.Degrees(), g.rotZ)
|
||||
|
||||
if ok, bc, face, _, _ := g.world.CastViewRay(io.ViewPos, io.ViewDir.Normalize(), 10); ok {
|
||||
igwrap.TextBackground("Looking At: (%d %d %d) facing %s", bc[0], bc[1], bc[2], itype.DirectionName[face])
|
||||
}
|
||||
|
||||
imgui.End()
|
||||
}
|
||||
}
|
||||
@@ -87,14 +76,6 @@ func (g *Game) imgui() {
|
||||
if g.paused {
|
||||
imgui.ShowDemoWindow(nil)
|
||||
|
||||
if imgui.BeginV("Player", nil, imgui.WindowFlagsAlwaysAutoResize) {
|
||||
pos := g.player.Position()
|
||||
vel := g.player.Speed()
|
||||
igwrap.Text("Pos: (%.5f, %.5f, %.5f), Vel: (%.5f, %.5f, %.5f)", pos[0], pos[1], pos[2], vel[0], vel[1], vel[2])
|
||||
igwrap.Text("VelXZ=%.5f, VelXYZ=%.5f", math.Sqrt(vel[0]*vel[0]+vel[2]*vel[2]), vel.Length())
|
||||
}
|
||||
imgui.End()
|
||||
|
||||
if igwrap.Begin("Logs", &g.gui.showLog, imgui.WindowFlagsMenuBar) {
|
||||
if imgui.BeginMenuBar() {
|
||||
if imgui.Button("Clear") {
|
||||
@@ -118,76 +99,9 @@ func (g *Game) imgui() {
|
||||
imgui.End()
|
||||
}
|
||||
|
||||
if imgui.Begin("Actions") {
|
||||
|
||||
imgui.Text("Chunks")
|
||||
imgui.Separator()
|
||||
imgui.InputText("Load Filename", &g.gui.loadChunkFile)
|
||||
imgui.SliderInt2("Load ID", &g.gui.loadChunkID, -10, 10)
|
||||
if imgui.ButtonV("Load", imgui.Vec2{X: -2, Y: 0}) {
|
||||
c := &world.Chunk{}
|
||||
f, err := os.Open(g.gui.loadChunkFile)
|
||||
if err != nil {
|
||||
log.Print("LoadChunk: ", err)
|
||||
} else {
|
||||
c.LoadFromGobIndexed(f, int(g.gui.loadChunkID[0]), int(g.gui.loadChunkID[1]))
|
||||
g.world.SetChunk(int(g.gui.loadChunkID[0]), int(g.gui.loadChunkID[1]), c)
|
||||
}
|
||||
}
|
||||
imgui.Separator()
|
||||
imgui.InputText("Save Filename", &g.gui.saveChunkFile)
|
||||
imgui.SliderInt2("Save ID", &g.gui.saveChunkID, -10, 10)
|
||||
if imgui.ButtonV("Save", imgui.Vec2{X: -2, Y: 0}) {
|
||||
c := g.world.Chunks[itype.Vec2i{int(g.gui.saveChunkID[0]), int(g.gui.saveChunkID[1])}]
|
||||
f, _ := os.Create(g.gui.saveChunkFile)
|
||||
c.WriteToGob(f)
|
||||
f.Close()
|
||||
}
|
||||
imgui.Separator()
|
||||
|
||||
}
|
||||
imgui.End()
|
||||
}
|
||||
|
||||
imgui.BackgroundDrawList().AddRectFilledV(imgui.Vec2{X: float32(io.DisplaySize[0]/2 - 12), Y: float32(io.DisplaySize[1]/2 - 1)}, imgui.Vec2{X: float32(io.DisplaySize[0]/2 + 12), Y: float32(io.DisplaySize[1]/2 + 1)}, imgui.Packed(color.White), 0, 0)
|
||||
imgui.BackgroundDrawList().AddRectFilledV(imgui.Vec2{X: float32(io.DisplaySize[0]/2 - 1), Y: float32(io.DisplaySize[1]/2 - 12)}, imgui.Vec2{X: float32(io.DisplaySize[0]/2 + 1), Y: float32(io.DisplaySize[1]/2 + 12)}, imgui.Packed(color.White), 0, 0)
|
||||
|
||||
imgui.SetNextWindowPosV(imgui.Vec2{float32(io.DisplaySize[0] / 2), float32(io.DisplaySize[1]) + 1}, imgui.ConditionAlways, imgui.Vec2{0.5, 1})
|
||||
if igwrap.Begin("InventoryBar", nil, imgui.WindowFlagsAlwaysAutoResize|imgui.WindowFlagsNoNavFocus|imgui.WindowFlagsNoNavInputs|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoBringToFrontOnFocus|imgui.WindowFlagsNoSavedSettings|imgui.WindowFlagsNoFocusOnAppearing) {
|
||||
|
||||
imgui.PushStyleColor(imgui.StyleColorBorder, imgui.Vec4{0.8, 0.8, 0.8, 1})
|
||||
for i, id := range placeId {
|
||||
if i != 0 {
|
||||
imgui.SameLineV(0, 1)
|
||||
}
|
||||
|
||||
if i == placei {
|
||||
imgui.PushStyleVarFloat(imgui.StyleVarFrameBorderSize, 2)
|
||||
}
|
||||
|
||||
app := world.GetBlockBehaviour(id).Appearance(itype.Vec3i{}, 0, nil, g.world)
|
||||
var name string
|
||||
switch app.RenderType {
|
||||
case world.OneTexture:
|
||||
name = app.Name + ".png"
|
||||
case world.ThreeTexture:
|
||||
name = app.Name + "_top.png"
|
||||
case world.SixTexture:
|
||||
name = app.Name + "_y+.png"
|
||||
}
|
||||
igwrap.ImageButtonV(
|
||||
g.render.texture.Handle(),
|
||||
itype.Vec2f{32, 32},
|
||||
asset.WorldTextureAtlas.RectNormalized(name),
|
||||
0, itype.Vec4f{}, itype.Vec4f{1, 1, 1, 1},
|
||||
)
|
||||
|
||||
if i == placei {
|
||||
imgui.PopStyleVar()
|
||||
}
|
||||
}
|
||||
imgui.PopStyleColor()
|
||||
|
||||
imgui.End()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,15 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"edgaru089.ink/go/gl01/internal/igwrap/backend"
|
||||
"edgaru089.ink/go/gl01/internal/io"
|
||||
"edgaru089.ink/go/gl01/internal/render"
|
||||
"edgaru089.ink/go/gl01/internal/util"
|
||||
"edgaru089.ink/go/gl01/internal/util/itype"
|
||||
"edgaru089.ink/go/gl01/internal/world"
|
||||
"edgaru089.ink/go/gl01/internal/world/blocks"
|
||||
"edgaru089.ink/go/gl01/internal/world/worldgen"
|
||||
"github.com/go-gl/glfw/v3.3/glfw"
|
||||
"github.com/go-gl/mathgl/mgl64"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -23,10 +17,6 @@ const (
|
||||
PlayerPlaceCooldown = 200 * time.Millisecond
|
||||
)
|
||||
|
||||
var placeId = [10]int{blocks.Stone, blocks.Slab, blocks.Grass, blocks.LogOak, blocks.PlanksOak, blocks.LeavesOak, blocks.Glass, blocks.DebugDir, blocks.Bedrock, blocks.Water}
|
||||
var placeAux = [10]int{0, blocks.PlanksOak, 0, 0, 0, 0, 0, 0, 0, 0}
|
||||
var placei = 0
|
||||
|
||||
var logs string
|
||||
|
||||
type logger struct{}
|
||||
@@ -44,51 +34,14 @@ func init() {
|
||||
// Init initializes the game.
|
||||
func (g *Game) Init(win *glfw.Window) {
|
||||
|
||||
g.world = world.NewWorld()
|
||||
g.view = &render.View{}
|
||||
|
||||
err := g.initRender()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
g.player.SetDatasetI("LastBreak", 0)
|
||||
g.player.SetDatasetI("LastPlace", 0)
|
||||
|
||||
var seed int64 = time.Now().Unix()
|
||||
gensync := make(chan struct{})
|
||||
gensynccnt := 0
|
||||
for i := -4; i <= 4; i++ {
|
||||
for j := -4; j <= 4; j++ {
|
||||
c := &world.Chunk{}
|
||||
g.world.SetChunk(i, j, c)
|
||||
go func() {
|
||||
worldgen.Chunk(c, g.world, seed)
|
||||
gensync <- struct{}{}
|
||||
}()
|
||||
gensynccnt++
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < gensynccnt; i++ {
|
||||
<-gensync
|
||||
}
|
||||
|
||||
width, height := win.GetSize()
|
||||
g.view.Aspect(float32(width)/float32(height)).FovY(itype.Degrees(60)).LookAt(g.cameraPos.ToFloat32(), g.rotY, itype.Degrees(g.rotZ))
|
||||
|
||||
io.DisplaySize[0], io.DisplaySize[1] = win.GetFramebufferSize()
|
||||
win.SetSizeCallback(func(w *glfw.Window, width, height int) {
|
||||
//win.SetCursorPos(float64(width)/2, float64(height)/2)
|
||||
g.view.Aspect(float32(width) / float32(height))
|
||||
io.DisplaySize = itype.Vec2i{width, height}
|
||||
})
|
||||
|
||||
err = render.Framewire.Init()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
g.initImgui(win)
|
||||
|
||||
win.SetCursorPos(float64(width)/2, float64(height)/2)
|
||||
@@ -102,15 +55,8 @@ func (g *Game) Init(win *glfw.Window) {
|
||||
|
||||
width, height := w.GetSize()
|
||||
centerX, centerY := float64(width)/2, float64(height)/2
|
||||
deltaX, deltaY := xpos-centerX, ypos-centerY
|
||||
g.rotY -= itype.Degrees(float32(deltaX) / 10)
|
||||
g.rotZ -= float32(deltaY) / 10
|
||||
if g.rotZ > 89.99 {
|
||||
g.rotZ = 89.99
|
||||
}
|
||||
if g.rotZ < -89.99 {
|
||||
g.rotZ = -89.99
|
||||
}
|
||||
//deltaX, deltaY := xpos-centerX, ypos-centerY
|
||||
_, _ = xpos-centerX, ypos-centerY
|
||||
win.SetCursorPos(centerX, centerY)
|
||||
})
|
||||
|
||||
@@ -125,21 +71,6 @@ func (g *Game) Init(win *glfw.Window) {
|
||||
backend.KeyCallback(key, action)
|
||||
}
|
||||
if action == glfw.Press {
|
||||
if g.paused {
|
||||
if key == glfw.KeyEscape && !g.io.WantCaptureKeyboard() {
|
||||
g.paused = false
|
||||
win.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
|
||||
width, height := w.GetSize()
|
||||
win.SetCursorPos(float64(width)/2, float64(height)/2)
|
||||
}
|
||||
} else {
|
||||
if key == glfw.KeyEscape {
|
||||
g.paused = true
|
||||
win.SetInputMode(glfw.CursorMode, glfw.CursorNormal)
|
||||
width, height := w.GetSize()
|
||||
win.SetCursorPos(float64(width)/2, float64(height)/2)
|
||||
}
|
||||
}
|
||||
|
||||
if key == glfw.KeyF11 {
|
||||
if g.fullscreen {
|
||||
@@ -175,18 +106,6 @@ func (g *Game) Init(win *glfw.Window) {
|
||||
win.SetScrollCallback(func(w *glfw.Window, xpos, ypos float64) {
|
||||
if g.paused {
|
||||
backend.MouseScrollCallback(xpos, ypos)
|
||||
} else {
|
||||
if ypos > 0 {
|
||||
placei--
|
||||
} else if ypos < 0 {
|
||||
placei++
|
||||
}
|
||||
if placei < 0 {
|
||||
placei += len(placeId)
|
||||
}
|
||||
if placei >= len(placeId) {
|
||||
placei -= len(placeId)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -201,114 +120,6 @@ func (g *Game) Update(win *glfw.Window, delta time.Duration) {
|
||||
|
||||
clock := util.NewClock()
|
||||
|
||||
if !g.paused {
|
||||
|
||||
if win.GetKey(glfw.KeyLeft) == glfw.Press {
|
||||
g.rotY += itype.Degrees(float32(delta.Seconds()) * 100)
|
||||
}
|
||||
if win.GetKey(glfw.KeyRight) == glfw.Press {
|
||||
g.rotY -= itype.Degrees(float32(delta.Seconds()) * 100)
|
||||
}
|
||||
if win.GetKey(glfw.KeyUp) == glfw.Press {
|
||||
g.rotZ += float32(delta.Seconds()) * 100
|
||||
if g.rotZ > 89.99 {
|
||||
g.rotZ = 89.99
|
||||
}
|
||||
}
|
||||
if win.GetKey(glfw.KeyDown) == glfw.Press {
|
||||
g.rotZ -= float32(delta.Seconds()) * 100
|
||||
if g.rotZ < -89.99 {
|
||||
g.rotZ = -89.99
|
||||
}
|
||||
}
|
||||
|
||||
//forward := itype.Vec3f(mgl32.Rotate3DY(mgl32.DegToRad(g.rotY)).Mul3(mgl32.Rotate3DZ(mgl32.DegToRad(g.rotZ))).Mul3x1(mgl32.Vec3{1, 0, 0})).ToFloat64().Multiply(delta.Seconds()*8)
|
||||
var walkaccel float64
|
||||
if g.player.OnGround() {
|
||||
walkaccel = 48
|
||||
} else {
|
||||
walkaccel = 16
|
||||
}
|
||||
|
||||
forward := itype.Vec3d(mgl64.Rotate3DY(float64(g.rotY.Radians())).Mul3x1(mgl64.Vec3{1, 0, 0}))
|
||||
right := forward.Cross(itype.Vec3d{0, 1, 0})
|
||||
accel := itype.Vec3d{0, 0, 0}
|
||||
if win.GetKey(glfw.KeyW) == glfw.Press {
|
||||
accel = accel.Add(forward.Multiply(walkaccel * delta.Seconds()))
|
||||
}
|
||||
if win.GetKey(glfw.KeyS) == glfw.Press {
|
||||
accel = accel.Add(forward.Multiply(walkaccel * delta.Seconds()).Negative())
|
||||
}
|
||||
if win.GetKey(glfw.KeyD) == glfw.Press {
|
||||
accel = accel.Add(right.Multiply(walkaccel * delta.Seconds()))
|
||||
}
|
||||
if win.GetKey(glfw.KeyA) == glfw.Press {
|
||||
accel = accel.Add(right.Multiply(walkaccel * delta.Seconds()).Negative())
|
||||
}
|
||||
|
||||
if win.GetKey(glfw.KeySpace) == glfw.Press && g.player.OnGround() {
|
||||
//log.Print("Jump!")
|
||||
accel = accel.Addv(0, 8, 0)
|
||||
}
|
||||
|
||||
g.player.Accelerate(accel[0], accel[1], accel[2])
|
||||
}
|
||||
|
||||
g.player.Update(g.world, delta)
|
||||
|
||||
g.view.LookAt(g.player.EyePosition().ToFloat32(), g.rotY, itype.Degrees(g.rotZ))
|
||||
io.ViewPos = g.player.EyePosition()
|
||||
io.ViewDir = itype.Vec3d(mgl64.Rotate3DY(float64(g.rotY.Radians())).Mul3(mgl64.Rotate3DZ(float64(itype.Degrees(g.rotZ)))).Mul3x1(mgl64.Vec3{1, 0, 0}))
|
||||
|
||||
render.Framewire.PushBox(g.player.WorldHitbox()[0].ToFloat32(), color.White)
|
||||
|
||||
if g.player.Position()[1] < -100 {
|
||||
g.player.SetPosition(itype.Vec3d{18, 80, 18})
|
||||
g.player.SetSpeed(itype.Vec3d{})
|
||||
}
|
||||
|
||||
if ok, bc, dir, _, _ := g.world.CastViewRay(io.ViewPos, io.ViewDir.Normalize(), 6); ok {
|
||||
ba := g.world.Block(bc).Appearance(bc)
|
||||
for _, r := range ba.Lookbox {
|
||||
render.Framewire.PushBox(r.GrowEven(itype.Vec3d{0.03125, 0.03125, 0.03125}).Offset(bc.ToFloat64()).ToFloat32(), color.White)
|
||||
}
|
||||
|
||||
if !g.paused {
|
||||
// Break/Place block
|
||||
if win.GetMouseButton(glfw.MouseButtonLeft) == glfw.Press && g.runtime-time.Duration(g.player.DatasetI("LastBreak")) >= PlayerBreakCooldown {
|
||||
// Break
|
||||
g.world.Break(bc)
|
||||
g.player.SetDatasetI("LastBreak", int64(g.runtime))
|
||||
} else if win.GetMouseButton(glfw.MouseButtonRight) == glfw.Press && g.runtime-time.Duration(g.player.DatasetI("LastPlace")) >= PlayerBreakCooldown {
|
||||
// Place
|
||||
// Check hitbox
|
||||
tobehit := world.GetBlockBehaviour(placeId[placei]).Appearance(bc.Add(itype.DirectionVeci[dir]), 0, nil, g.world).Hitbox
|
||||
if len(tobehit) == 0 {
|
||||
tobehit = []itype.Boxd{{OffX: 0, OffY: 0, OffZ: 0, SizeX: 1, SizeY: 1, SizeZ: 1}}
|
||||
}
|
||||
canplace := true
|
||||
outer:
|
||||
for _, pb := range g.player.WorldHitbox() {
|
||||
for _, b := range tobehit {
|
||||
if ok, _ := b.Offset(bc.Add(itype.DirectionVeci[dir]).ToFloat64()).Intersect(pb); ok {
|
||||
canplace = false
|
||||
break outer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if canplace {
|
||||
aux := placeAux[placei]
|
||||
if win.GetKey(glfw.KeyLeftShift) == glfw.Press {
|
||||
aux = -aux
|
||||
}
|
||||
g.world.SetBlock(bc.Add(itype.DirectionVeci[dir]), placeId[placei], aux, nil)
|
||||
g.player.SetDatasetI("LastPlace", int64(g.runtime))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
io.Diagnostics.Times.Logic = clock.Restart()
|
||||
|
||||
g.imgui()
|
||||
|
||||
@@ -1,488 +1,19 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"edgaru089.ink/go/gl01/internal/asset"
|
||||
"edgaru089.ink/go/gl01/internal/igwrap"
|
||||
"edgaru089.ink/go/gl01/internal/igwrap/backend"
|
||||
"edgaru089.ink/go/gl01/internal/io"
|
||||
"edgaru089.ink/go/gl01/internal/render"
|
||||
"edgaru089.ink/go/gl01/internal/util"
|
||||
"edgaru089.ink/go/gl01/internal/util/itype"
|
||||
"github.com/go-gl/gl/all-core/gl"
|
||||
"github.com/go-gl/glfw/v3.3/glfw"
|
||||
"github.com/go-gl/mathgl/mgl32"
|
||||
"github.com/inkyblackness/imgui-go/v4"
|
||||
)
|
||||
|
||||
const (
|
||||
SSAOSampleCount = 32 // Number of samples in the SSAO pass. Must stay the same with ssao.frag
|
||||
)
|
||||
|
||||
var (
|
||||
ShadowmapSize = itype.Vec2i{6144, 6144} // Size of the shadow mapping
|
||||
RandomSize = itype.Vec2i{32, 32} // Size of the random mapping
|
||||
)
|
||||
|
||||
// renderData holds OpenGL state used by the game renderer.
|
||||
type renderData struct {
|
||||
lastDisplaySize itype.Vec2i
|
||||
startTime time.Time
|
||||
|
||||
texRand uint32 // random texture mapping, RGBA8 (0~1)
|
||||
|
||||
// Depth mapping pass
|
||||
depthmap struct {
|
||||
fbo, tex uint32 // Framebuffer Object and Texture.
|
||||
shader *render.Shader // Shader.
|
||||
}
|
||||
|
||||
// Geometry pass
|
||||
gbuffer struct {
|
||||
fbo uint32 // The Framebuffer object.
|
||||
|
||||
// Textures. Position/Depth(View Space); Normal/Lightspace Depth; Diffuse Color/Specular Intensity.
|
||||
pos, norm, color uint32
|
||||
depth uint32 // Depth renderbuffer.
|
||||
shader *render.Shader // Geometry pass shaders.
|
||||
}
|
||||
|
||||
// Screen-Space Ambient Occlusion (SSAO) pass
|
||||
ssao struct {
|
||||
fbo uint32 // Framebuffer
|
||||
ambient uint32 // Ambient strength output texture [0,1] (Red channel)
|
||||
uboSamples uint32 // Uniform Buffer Object pointing to the array of samples
|
||||
shader *render.Shader // SSAO pass shader
|
||||
|
||||
// SSAO blur pass
|
||||
blur struct {
|
||||
fbo uint32
|
||||
output uint32
|
||||
shader *render.Shader
|
||||
}
|
||||
}
|
||||
|
||||
// Deferred lighting pass
|
||||
lighting struct {
|
||||
shader *render.Shader // Deferred lighting pass shaders
|
||||
}
|
||||
|
||||
// Semi-transparent pass
|
||||
water struct {
|
||||
shader *render.Shader
|
||||
}
|
||||
|
||||
// Output pass
|
||||
output struct {
|
||||
fbo uint32 // Output framebuffer object, rendering to the output texture.
|
||||
tex uint32 // Output texture, rendered to the back buffer at the end.
|
||||
//depth uint32 // Output depth renderbuffer, use gbuffer.depth
|
||||
shader *render.Shader // Shader used to copy output.tex to back buffer.
|
||||
}
|
||||
|
||||
texture *render.Texture // World texture atlas
|
||||
}
|
||||
|
||||
func (g *Game) initRender() (err error) {
|
||||
r := &g.render
|
||||
|
||||
r.depthmap.shader, err = render.NewShader(asset.WorldShaderShadowmapVert, asset.WorldShaderShadowmapFrag)
|
||||
if err != nil {
|
||||
return errors.New("depthmap: " + err.Error())
|
||||
}
|
||||
r.gbuffer.shader, err = render.NewShader(asset.WorldShaderGeometryVert, asset.WorldShaderGeometryFrag)
|
||||
if err != nil {
|
||||
return errors.New("gbuffer: " + err.Error())
|
||||
}
|
||||
r.ssao.shader, err = render.NewShader(asset.WorldShaderSSAOVert, asset.WorldShaderSSAOFrag)
|
||||
if err != nil {
|
||||
return errors.New("ssao: " + err.Error())
|
||||
}
|
||||
r.ssao.blur.shader, err = render.NewShader(asset.WorldShaderSSAOBlurVert, asset.WorldShaderSSAOBlurFrag)
|
||||
if err != nil {
|
||||
return errors.New("ssao_blur: " + err.Error())
|
||||
}
|
||||
r.lighting.shader, err = render.NewShader(asset.WorldShaderLightingVert, asset.WorldShaderLightingFrag)
|
||||
if err != nil {
|
||||
return errors.New("lighting: " + err.Error())
|
||||
}
|
||||
r.water.shader, err = render.NewShader(asset.WorldShaderWaterVert, asset.WorldShaderWaterFrag)
|
||||
if err != nil {
|
||||
return errors.New("water: " + err.Error())
|
||||
}
|
||||
r.output.shader, err = render.NewShader(asset.WorldShaderOutputVert, asset.WorldShaderOutputFrag)
|
||||
if err != nil {
|
||||
return errors.New("output: " + err.Error())
|
||||
}
|
||||
|
||||
// get the maximum anisotropic filtering level
|
||||
var maxaf float32
|
||||
gl.GetFloatv(gl.MAX_TEXTURE_MAX_ANISOTROPY, &maxaf)
|
||||
|
||||
asset.InitWorldTextureAtlas()
|
||||
r.texture = render.NewTexture()
|
||||
r.texture.UpdatesRGB(asset.WorldTextureAtlas.Image)
|
||||
r.texture.GenerateMipMap()
|
||||
gl.BindTexture(gl.TEXTURE_2D, r.texture.Handle())
|
||||
gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAX_ANISOTROPY, maxaf)
|
||||
r.depthmap.shader.SetUniformTexture("tex", r.texture)
|
||||
r.gbuffer.shader.SetUniformTexture("tex", r.texture)
|
||||
r.water.shader.SetUniformTexture("tex", r.texture)
|
||||
igwrap.SetTextureFlag(r.texture.Handle(), igwrap.TextureFlag_Linear, igwrap.TextureFlag_FlipY)
|
||||
|
||||
r.depthmap.shader.SetUniformMat4("model", mgl32.Ident4())
|
||||
r.gbuffer.shader.SetUniformMat4("model", mgl32.Ident4())
|
||||
r.water.shader.SetUniformMat4("model", mgl32.Ident4())
|
||||
// and view and projection uniforms not yet set
|
||||
gl.BindFragDataLocation(r.lighting.shader.Handle(), 0, gl.Str("outputColor\x00"))
|
||||
gl.BindFragDataLocation(r.water.shader.Handle(), 0, gl.Str("outputColor\x00"))
|
||||
gl.BindFragDataLocation(r.output.shader.Handle(), 0, gl.Str("outputColor\x00"))
|
||||
|
||||
// generate random mapping
|
||||
{
|
||||
data := make([]uint32, RandomSize[0]*RandomSize[1])
|
||||
for i := range data {
|
||||
data[i] = rand.Uint32()
|
||||
}
|
||||
gl.GenTextures(1, &r.texRand)
|
||||
gl.BindTexture(gl.TEXTURE_2D, r.texRand)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(RandomSize[0]), int32(RandomSize[1]), 0, gl.RGBA, gl.UNSIGNED_BYTE, util.Ptr(data))
|
||||
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_WRAP_S, gl.REPEAT)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
|
||||
}
|
||||
r.ssao.shader.SetUniformTextureHandle("rand", r.texRand)
|
||||
r.ssao.shader.SetUniformVec2f("randSize", RandomSize.ToFloat32())
|
||||
|
||||
// generate the depthmap and depthmap FBO
|
||||
gl.GenFramebuffers(1, &r.depthmap.fbo)
|
||||
gl.GenTextures(1, &r.depthmap.tex)
|
||||
gl.BindTexture(gl.TEXTURE_2D, r.depthmap.tex)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT, int32(ShadowmapSize[0]), int32(ShadowmapSize[1]), 0, gl.DEPTH_COMPONENT, gl.FLOAT, nil)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_BORDER)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_BORDER)
|
||||
borderColor := []float32{1, 1, 1, 1}
|
||||
gl.TexParameterfv(gl.TEXTURE_2D, gl.TEXTURE_BORDER_COLOR, &borderColor[0])
|
||||
// attach depth texture as FBO's depth buffer
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, r.depthmap.fbo)
|
||||
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, r.depthmap.tex, 0)
|
||||
gl.DrawBuffer(gl.NONE)
|
||||
gl.ReadBuffer(gl.NONE)
|
||||
// attach the shadowmap to the shader
|
||||
r.lighting.shader.SetUniformTextureHandle("shadowmap", r.depthmap.tex)
|
||||
r.water.shader.SetUniformTextureHandle("shadowmap", r.depthmap.tex)
|
||||
|
||||
// generate G-buffer and friends
|
||||
gl.GenFramebuffers(1, &r.gbuffer.fbo)
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, r.gbuffer.fbo)
|
||||
// position
|
||||
gl.GenTextures(1, &r.gbuffer.pos)
|
||||
gl.BindTexture(gl.TEXTURE_2D, r.gbuffer.pos)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.FLOAT, nil)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_BORDER)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_BORDER)
|
||||
borderColor = []float32{1, 1, 1, 1}
|
||||
gl.TexParameterfv(gl.TEXTURE_2D, gl.TEXTURE_BORDER_COLOR, &borderColor[0])
|
||||
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, r.gbuffer.pos, 0)
|
||||
// normal
|
||||
gl.GenTextures(1, &r.gbuffer.norm)
|
||||
gl.BindTexture(gl.TEXTURE_2D, r.gbuffer.norm)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.FLOAT, nil)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
|
||||
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT1, gl.TEXTURE_2D, r.gbuffer.norm, 0)
|
||||
// diffuse color
|
||||
gl.GenTextures(1, &r.gbuffer.color)
|
||||
gl.BindTexture(gl.TEXTURE_2D, r.gbuffer.color)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
|
||||
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT2, gl.TEXTURE_2D, r.gbuffer.color, 0)
|
||||
// depth
|
||||
gl.GenRenderbuffers(1, &r.gbuffer.depth)
|
||||
gl.BindRenderbuffer(gl.RENDERBUFFER, r.gbuffer.depth)
|
||||
gl.RenderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]))
|
||||
gl.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, r.gbuffer.depth)
|
||||
// tell OpenGL which color attachments we'll use (of this framebuffer)
|
||||
attachments := [...]uint32{gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1, gl.COLOR_ATTACHMENT2}
|
||||
gl.DrawBuffers(int32(len(attachments)), &attachments[0])
|
||||
// attach the textures
|
||||
r.lighting.shader.SetUniformTextureHandle("gPos", r.gbuffer.pos)
|
||||
r.lighting.shader.SetUniformTextureHandle("gNorm", r.gbuffer.norm)
|
||||
r.lighting.shader.SetUniformTextureHandle("gColor", r.gbuffer.color)
|
||||
r.ssao.shader.SetUniformTextureHandle("gPos", r.gbuffer.pos)
|
||||
r.ssao.shader.SetUniformTextureHandle("gNorm", r.gbuffer.norm)
|
||||
r.water.shader.SetUniformTextureHandle("gPos", r.gbuffer.pos)
|
||||
|
||||
// generate SSAO friends
|
||||
gl.GenFramebuffers(1, &r.ssao.fbo)
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, r.ssao.fbo)
|
||||
// ambient strength texture
|
||||
gl.GenTextures(1, &r.ssao.ambient)
|
||||
gl.BindTexture(gl.TEXTURE_2D, r.ssao.ambient)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RED, gl.UNSIGNED_BYTE, nil)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
|
||||
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, r.ssao.ambient, 0)
|
||||
r.ssao.blur.shader.SetUniformTextureHandle("tex", r.ssao.ambient)
|
||||
// uniform buffer object for samples
|
||||
ssaoSamples := make([]itype.Vec4f, SSAOSampleCount)
|
||||
for i := range ssaoSamples {
|
||||
sample := itype.Vec3d{rand.Float64()*2 - 1, rand.Float64()*2 - 1, rand.Float64() + 0.05}
|
||||
sample = sample.Normalize().Multiply(rand.Float64()).Multiply(rand.Float64())
|
||||
ssaoSamples[i] = itype.Vec4f{
|
||||
float32(sample[0]),
|
||||
float32(sample[1]),
|
||||
float32(sample[2]),
|
||||
0,
|
||||
}
|
||||
}
|
||||
gl.GenBuffers(1, &r.ssao.uboSamples)
|
||||
gl.BindBuffer(gl.UNIFORM_BUFFER, r.ssao.uboSamples)
|
||||
gl.BufferData(gl.UNIFORM_BUFFER, int(unsafe.Sizeof(float32(0))*4*SSAOSampleCount), util.Ptr(ssaoSamples), gl.STATIC_DRAW)
|
||||
// bind UBO (onto binding 0 for now)
|
||||
gl.BindBufferBase(gl.UNIFORM_BUFFER, 0, r.ssao.uboSamples)
|
||||
gl.UniformBlockBinding(r.ssao.shader.Handle(), gl.GetUniformBlockIndex(r.ssao.shader.Handle(), gl.Str("uboSamples\x00")), 0)
|
||||
// SSAO blur pass
|
||||
gl.GenFramebuffers(1, &r.ssao.blur.fbo)
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, r.ssao.blur.fbo)
|
||||
gl.GenTextures(1, &r.ssao.blur.output)
|
||||
gl.BindTexture(gl.TEXTURE_2D, r.ssao.blur.output)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RED, gl.UNSIGNED_BYTE, nil)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
|
||||
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, r.ssao.blur.output, 0)
|
||||
r.lighting.shader.SetUniformTextureHandle("ssaoAmbient", r.ssao.blur.output)
|
||||
|
||||
// generate the output texture and friends
|
||||
gl.GenFramebuffers(1, &r.output.fbo)
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, r.output.fbo)
|
||||
// output
|
||||
gl.GenTextures(1, &r.output.tex)
|
||||
gl.BindTexture(gl.TEXTURE_2D, r.output.tex)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.FLOAT, nil)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
|
||||
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, r.output.tex, 0)
|
||||
// depth
|
||||
gl.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, r.gbuffer.depth)
|
||||
// attach textures
|
||||
r.output.shader.SetUniformTextureHandle("tex", r.output.tex)
|
||||
|
||||
// set the texture flags for ImGUI
|
||||
igwrap.SetTextureFlag(r.gbuffer.color, igwrap.TextureFlag_Linear)
|
||||
igwrap.SetTextureFlag(r.ssao.ambient, igwrap.TextureFlag_Red)
|
||||
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
|
||||
r.lastDisplaySize = io.DisplaySize
|
||||
r.startTime = time.Now()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResizeDisplay resizes the size of the internal buffers dependent on the window size.
|
||||
// It is called automatically most of the time.
|
||||
func (g *Game) ResizeDisplay(newSize itype.Vec2i) {
|
||||
gl.BindTexture(gl.TEXTURE_2D, g.render.gbuffer.pos) // G-Buffer, Position
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.FLOAT, nil)
|
||||
gl.BindTexture(gl.TEXTURE_2D, g.render.gbuffer.norm) // G-Buffer, Normal
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.FLOAT, nil)
|
||||
gl.BindTexture(gl.TEXTURE_2D, g.render.gbuffer.color) // G-Buffer, Albedo Color
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
|
||||
gl.BindRenderbuffer(gl.RENDERBUFFER, g.render.gbuffer.depth) // G-Buffer, Depth (Renderbuffer)
|
||||
gl.RenderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]))
|
||||
gl.BindTexture(gl.TEXTURE_2D, g.render.ssao.ambient) // SSAO Output Ambient Strength
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RED, gl.UNSIGNED_BYTE, nil)
|
||||
gl.BindTexture(gl.TEXTURE_2D, g.render.ssao.blur.output) // SSAO Blurred Output
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RED, gl.UNSIGNED_BYTE, nil)
|
||||
gl.BindTexture(gl.TEXTURE_2D, g.render.output.tex) // Output Texture
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
|
||||
g.render.lastDisplaySize = newSize
|
||||
|
||||
}
|
||||
|
||||
var (
|
||||
sun = [3]float32{0.2, 0.4, 0.3}
|
||||
alpha = float32(0.55)
|
||||
gamma = float32(2.2)
|
||||
exposure = float32(1)
|
||||
atlasScale = float32(1)
|
||||
)
|
||||
|
||||
// Render, called with a OpenGL context, renders the game.
|
||||
func (g *Game) Render(win *glfw.Window) {
|
||||
gl.Viewport(0, 0, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]))
|
||||
matv, matp := g.view.View(), g.view.Perspective()
|
||||
matvp := matp.Mul4(matv)
|
||||
|
||||
allclock := util.NewClock()
|
||||
lastclock := util.NewClock()
|
||||
|
||||
io.RenderPos = io.ViewPos
|
||||
io.RenderDir = io.ViewDir
|
||||
|
||||
// re-generate the G-buffers if the display size changed
|
||||
if g.render.lastDisplaySize != io.DisplaySize {
|
||||
g.ResizeDisplay(io.DisplaySize)
|
||||
}
|
||||
|
||||
if g.paused {
|
||||
imgui.SliderFloat3("Sun", &sun, -1, 1)
|
||||
imgui.SliderFloat("Water Alpha", &alpha, 0, 1)
|
||||
imgui.SliderFloat("Gamma", &gamma, 1.6, 2.8)
|
||||
imgui.SliderFloat("Exposure", &exposure, 0, 2)
|
||||
}
|
||||
normalSun := itype.Vec3f(sun).Normalize()
|
||||
|
||||
gl.Enable(gl.CULL_FACE)
|
||||
gl.Enable(gl.DEPTH_TEST)
|
||||
gl.DepthFunc(gl.LESS)
|
||||
|
||||
lastclock.Restart()
|
||||
|
||||
// 1. Render to depth map
|
||||
gl.Viewport(0, 0, int32(ShadowmapSize[0]), int32(ShadowmapSize[1]))
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.depthmap.fbo)
|
||||
gl.Clear(gl.DEPTH_BUFFER_BIT)
|
||||
gl.Disable(gl.CULL_FACE)
|
||||
|
||||
lightPos := g.view.EyePos.Add(normalSun.Multiply(50))
|
||||
lightView := mgl32.LookAt(lightPos[0], lightPos[1], lightPos[2], g.view.EyePos[0], g.view.EyePos[1], g.view.EyePos[2], 0, 1, 0)
|
||||
lightProjection := mgl32.Ortho(-50, 50, -50, 50, 1, 100)
|
||||
lightspace := lightProjection.Mul4(lightView)
|
||||
|
||||
io.RenderPos = lightPos.ToFloat64()
|
||||
io.RenderDir = g.view.EyePos.Add(lightPos.Negative()).ToFloat64()
|
||||
|
||||
g.render.depthmap.shader.UseProgram()
|
||||
g.render.depthmap.shader.BindTextures()
|
||||
g.render.depthmap.shader.SetUniformMat4("lightspace", lightspace)
|
||||
|
||||
g.world.Render()
|
||||
g.world.RenderWater()
|
||||
|
||||
gl.Flush()
|
||||
io.Diagnostics.Times.RenderPasses.Depthmap = lastclock.Restart()
|
||||
|
||||
// 2. Geometry pass, render to G-buffer
|
||||
io.RenderPos = io.ViewPos
|
||||
io.RenderDir = io.ViewDir
|
||||
|
||||
gl.Viewport(0, 0, int32(g.render.lastDisplaySize[0]), int32(g.render.lastDisplaySize[1]))
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.gbuffer.fbo)
|
||||
gl.ClearColor(0, 0, 0, 1)
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
|
||||
gl.Enable(gl.CULL_FACE)
|
||||
gl.Disable(gl.BLEND)
|
||||
|
||||
g.render.gbuffer.shader.UseProgram()
|
||||
g.render.gbuffer.shader.BindTextures()
|
||||
g.render.gbuffer.shader.SetUniformMat4("lightspace", lightspace)
|
||||
g.render.gbuffer.shader.SetUniformMat4("view", matv)
|
||||
g.render.gbuffer.shader.SetUniformMat4("projection", matp)
|
||||
g.render.gbuffer.shader.SetUniformMat4("mvp", matvp)
|
||||
g.render.gbuffer.shader.SetUniformVec3f("viewPos", g.view.EyePos)
|
||||
|
||||
g.world.Render()
|
||||
|
||||
gl.Flush()
|
||||
io.Diagnostics.Times.RenderPasses.Geometry = lastclock.Restart()
|
||||
|
||||
// 3/1. SSAO pass
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.ssao.fbo)
|
||||
gl.ClearColor(1, 1, 1, 1)
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT)
|
||||
|
||||
g.render.ssao.shader.UseProgram()
|
||||
g.render.ssao.shader.BindTextures()
|
||||
g.render.ssao.shader.SetUniformMat4("view", matv)
|
||||
g.render.ssao.shader.SetUniformMat4("projection", matp)
|
||||
g.render.ssao.shader.SetUniformVec3f("viewPos", g.view.EyePos)
|
||||
|
||||
render.DrawScreenQuad()
|
||||
|
||||
// 3/2. SSAO blur pass
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.ssao.blur.fbo)
|
||||
g.render.ssao.blur.shader.UseProgram()
|
||||
g.render.ssao.blur.shader.BindTextures()
|
||||
g.render.ssao.blur.shader.SetUniformVec2f("screenSize", g.render.lastDisplaySize.ToFloat32())
|
||||
|
||||
render.DrawScreenQuad()
|
||||
|
||||
gl.Flush()
|
||||
io.Diagnostics.Times.RenderPasses.SSAO = lastclock.Restart()
|
||||
|
||||
// 4. Render the actual output with deferred lighting
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.output.fbo)
|
||||
gl.ClearColor(0, 0, 0, 0)
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT)
|
||||
gl.Disable(gl.DEPTH_TEST)
|
||||
g.render.lighting.shader.UseProgram()
|
||||
g.render.lighting.shader.BindTextures()
|
||||
g.render.lighting.shader.SetUniformMat4("lightspace", lightspace)
|
||||
g.render.lighting.shader.SetUniformVec3f("viewPos", g.view.EyePos)
|
||||
g.render.lighting.shader.SetUniformVec4f("fogColor", io.FogColor)
|
||||
g.render.lighting.shader.SetUniformVec3f("sun", normalSun)
|
||||
|
||||
render.DrawScreenQuad()
|
||||
|
||||
gl.Flush()
|
||||
io.Diagnostics.Times.RenderPasses.Lighting = lastclock.Restart()
|
||||
|
||||
// 5. Render water
|
||||
gl.Enable(gl.DEPTH_TEST)
|
||||
gl.DepthFunc(gl.LESS)
|
||||
gl.Enable(gl.CULL_FACE)
|
||||
gl.Enable(gl.BLEND)
|
||||
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
|
||||
gl.BlendEquation(gl.FUNC_ADD)
|
||||
g.render.water.shader.UseProgram()
|
||||
g.render.water.shader.BindTextures()
|
||||
|
||||
g.render.water.shader.SetUniformMat4("lightspace", lightspace)
|
||||
g.render.water.shader.SetUniformMat4("view", matv)
|
||||
g.render.water.shader.SetUniformMat4("projection", matp)
|
||||
g.render.water.shader.SetUniformVec3f("viewPos", g.view.EyePos)
|
||||
g.render.water.shader.SetUniformVec4f("fogColor", io.FogColor)
|
||||
g.render.water.shader.SetUniformVec3f("sun", normalSun)
|
||||
g.render.water.shader.SetUniformFloat("alpha", alpha)
|
||||
g.render.water.shader.SetUniformVec2f("screenSize", g.render.lastDisplaySize.ToFloat32())
|
||||
|
||||
g.world.RenderWater()
|
||||
|
||||
// And render framewires
|
||||
render.Framewire.Render(g.view)
|
||||
|
||||
// Finally. Copy the output texture to the back buffer
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
|
||||
gl.ClearColor(io.ClearColor[0], io.ClearColor[1], io.ClearColor[2], io.ClearColor[3])
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT)
|
||||
gl.Disable(gl.DEPTH_TEST)
|
||||
gl.Disable(gl.BLEND)
|
||||
g.render.output.shader.UseProgram()
|
||||
g.render.output.shader.BindTextures()
|
||||
g.render.output.shader.SetUniformFloat("gamma", gamma)
|
||||
g.render.output.shader.SetUniformFloat("exposure", exposure)
|
||||
|
||||
render.DrawScreenQuad()
|
||||
|
||||
gl.Flush()
|
||||
io.Diagnostics.Times.RenderPasses.Postfx = lastclock.Restart()
|
||||
io.Diagnostics.Times.Render = allclock.Elapsed()
|
||||
|
||||
// Show Information?
|
||||
if io.ShowDebugInfo {
|
||||
g.renderDebugInfo()
|
||||
}
|
||||
|
||||
backend.Render(win)
|
||||
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"edgaru089.ink/go/gl01/internal/asset"
|
||||
"edgaru089.ink/go/gl01/internal/igwrap"
|
||||
"edgaru089.ink/go/gl01/internal/io"
|
||||
"edgaru089.ink/go/gl01/internal/util"
|
||||
"edgaru089.ink/go/gl01/internal/util/itype"
|
||||
"github.com/inkyblackness/imgui-go/v4"
|
||||
)
|
||||
|
||||
const (
|
||||
timebarN = 700
|
||||
)
|
||||
|
||||
var (
|
||||
colorset = [...]imgui.PackedColor{4289753676, 4283598045, 4285048917, 4283584196, 4289950337, 4284512403, 4291005402, 4287401100, 4285839820, 4291671396}
|
||||
timebars [timebarN][]int // height of each bar set
|
||||
timebari int
|
||||
timebarScale int = 64
|
||||
)
|
||||
|
||||
func (g *Game) renderDebugInfo() {
|
||||
// Render information
|
||||
if igwrap.Begin("F3", nil, 0) {
|
||||
igwrap.TextBlank()
|
||||
|
||||
igwrap.TextBackground("WorldRender: lastframe %.3fms", float64(io.Diagnostics.Times.Render.Nanoseconds())/float64(time.Millisecond))
|
||||
igwrap.TextBackground("TimeBars:")
|
||||
pad := igwrap.Vec2f(imgui.CurrentStyle().ItemSpacing())
|
||||
imgui.SameLine()
|
||||
igwrap.TextBackgroundV(colorset[0], pad, "Depthmap")
|
||||
imgui.SameLine()
|
||||
igwrap.TextBackgroundV(colorset[1], pad, "Geometry")
|
||||
imgui.SameLine()
|
||||
igwrap.TextBackgroundV(colorset[2], pad, "SSAO")
|
||||
imgui.SameLine()
|
||||
igwrap.TextBackgroundV(colorset[3], pad, "Lighting")
|
||||
imgui.SameLine()
|
||||
igwrap.TextBackgroundV(colorset[4], pad, "Postfx")
|
||||
imgui.SameLine()
|
||||
igwrap.TextBackground("[Hover]")
|
||||
if imgui.IsItemHoveredV(imgui.HoveredFlagsAllowWhenDisabled) {
|
||||
_, wheely := imgui.CurrentIO().MouseWheel()
|
||||
if math.Abs(float64(wheely)) > 1e-3 {
|
||||
if wheely > 0 {
|
||||
timebarScale = util.Mini(timebarScale*2, 128)
|
||||
} else { // < 0
|
||||
timebarScale = util.Maxi(timebarScale/2, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isize := asset.WorldTextureAtlas.ImageSize
|
||||
igwrap.TextBackground("Texture Atlas Size: (%dx%d)", isize[0], isize[1])
|
||||
imgui.SameLine()
|
||||
igwrap.TextBackground("[Scroll]")
|
||||
if imgui.IsItemHoveredV(imgui.HoveredFlagsAllowWhenDisabled) {
|
||||
_, wheely := imgui.CurrentIO().MouseWheel()
|
||||
if math.Abs(float64(wheely)) > 1e-3 {
|
||||
atlasScale = util.Maxf(1, atlasScale+wheely)
|
||||
}
|
||||
imgui.BeginTooltip()
|
||||
igwrap.Image(g.render.texture.Handle(), isize.ToFloat32().Multiply(atlasScale), itype.Rectf{0, 0, 1, 1})
|
||||
imgui.EndTooltip()
|
||||
}
|
||||
|
||||
imgui.End()
|
||||
}
|
||||
|
||||
// Draw Textures
|
||||
imgui.SetNextWindowPosV(imgui.Vec2{X: float32(g.render.lastDisplaySize[0]), Y: 0}, imgui.ConditionAlways, imgui.Vec2{X: 1, Y: 0})
|
||||
if igwrap.Begin("Renderer Textures/Outputs", nil, igwrap.WindowFlagsOverlay) {
|
||||
imgui.PushStyleVarVec2(imgui.StyleVarItemSpacing, imgui.Vec2{})
|
||||
|
||||
imageSize := g.render.lastDisplaySize.ToFloat32().Multiply(0.25)
|
||||
imageSize[1] -= imgui.CurrentStyle().WindowPadding().Y / 2
|
||||
imageSize[0] = imageSize[1] / float32(g.render.lastDisplaySize[1]) * float32(g.render.lastDisplaySize[0])
|
||||
|
||||
igwrap.Image(g.render.gbuffer.pos, imageSize, itype.Rectf{0, 0, 1, 1})
|
||||
igwrap.Image(g.render.gbuffer.norm, imageSize, itype.Rectf{0, 0, 1, 1})
|
||||
igwrap.Image(g.render.gbuffer.color, imageSize, itype.Rectf{0, 0, 1, 1})
|
||||
igwrap.Image(g.render.ssao.ambient, imageSize, itype.Rectf{0, 0, 1, 1})
|
||||
|
||||
imgui.PopStyleVar()
|
||||
imgui.End()
|
||||
}
|
||||
|
||||
// Push the next bar
|
||||
timebars[timebari] = []int{
|
||||
int(io.Diagnostics.Times.RenderPasses.Depthmap.Nanoseconds()),
|
||||
int(io.Diagnostics.Times.RenderPasses.Geometry.Nanoseconds()),
|
||||
int(io.Diagnostics.Times.RenderPasses.SSAO.Nanoseconds()),
|
||||
int(io.Diagnostics.Times.RenderPasses.Lighting.Nanoseconds()),
|
||||
int(io.Diagnostics.Times.RenderPasses.Postfx.Nanoseconds()),
|
||||
}
|
||||
timebari++
|
||||
if timebari >= len(timebars) {
|
||||
timebari = 0
|
||||
}
|
||||
|
||||
// Draw time bars
|
||||
size := g.render.lastDisplaySize
|
||||
dl := imgui.BackgroundDrawList()
|
||||
for i, l := range timebars {
|
||||
ex := 0
|
||||
for j, d := range l {
|
||||
d = d * timebarScale / 1024 / 1024
|
||||
dl.AddLine(imgui.Vec2{X: float32(i), Y: float32(size[1] - ex)}, imgui.Vec2{X: float32(i), Y: float32(size[1] - ex - d)}, colorset[j])
|
||||
ex += d
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user