move WorldRenderer to game package

This commit is contained in:
Edgaru089 2022-02-24 13:18:05 +08:00
parent 1ec4f9d281
commit 9195dd7c3f
5 changed files with 140 additions and 265 deletions

View File

@ -17,7 +17,7 @@ type Game struct {
player *entity.Entity player *entity.Entity
view *render.View view *render.View
worldrender *render.WorldRenderer render renderData
prevCursorPos itype.Vec2d prevCursorPos itype.Vec2d
cameraPos itype.Vec3d cameraPos itype.Vec3d
@ -38,7 +38,6 @@ func NewGame() (g *Game) {
return &Game{ return &Game{
world: world.NewWorld(), world: world.NewWorld(),
player: entity.NewEntity("player", itype.Vec3d{18, 80, 18}), player: entity.NewEntity("player", itype.Vec3d{18, 80, 18}),
worldrender: &render.WorldRenderer{},
cameraPos: itype.Vec3d{18, 80, 18}, cameraPos: itype.Vec3d{18, 80, 18},
rotY: 0, rotY: 0,
rotZ: 0, rotZ: 0,

View File

@ -6,7 +6,6 @@ import (
"os" "os"
"time" "time"
"edgaru089.ml/go/gl01/internal/igwrap"
"edgaru089.ml/go/gl01/internal/igwrap/backend" "edgaru089.ml/go/gl01/internal/igwrap/backend"
"edgaru089.ml/go/gl01/internal/io" "edgaru089.ml/go/gl01/internal/io"
"edgaru089.ml/go/gl01/internal/render" "edgaru089.ml/go/gl01/internal/render"
@ -14,7 +13,6 @@ import (
"edgaru089.ml/go/gl01/internal/util/itype" "edgaru089.ml/go/gl01/internal/util/itype"
"edgaru089.ml/go/gl01/internal/world" "edgaru089.ml/go/gl01/internal/world"
"edgaru089.ml/go/gl01/internal/world/worldgen" "edgaru089.ml/go/gl01/internal/world/worldgen"
"github.com/go-gl/gl/all-core/gl"
"github.com/go-gl/glfw/v3.3/glfw" "github.com/go-gl/glfw/v3.3/glfw"
"github.com/go-gl/mathgl/mgl64" "github.com/go-gl/mathgl/mgl64"
"github.com/inkyblackness/imgui-go/v4" "github.com/inkyblackness/imgui-go/v4"
@ -39,9 +37,8 @@ func (g *Game) Init(win *glfw.Window) {
g.world = world.NewWorld() g.world = world.NewWorld()
g.view = &render.View{} g.view = &render.View{}
g.worldrender = &render.WorldRenderer{}
err := g.worldrender.Init(g.world) err := g.initRender()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -249,19 +246,5 @@ func (g *Game) Update(win *glfw.Window, delta time.Duration) {
imgui.ShowDemoWindow(nil) imgui.ShowDemoWindow(nil)
g.imgui() g.imgui()
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])
}
io.Diagnostics.Times.GUI = clock.Restart() io.Diagnostics.Times.GUI = clock.Restart()
} }
// 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]))
g.worldrender.Render(g.world, g.view)
render.Framewire.Render(g.view)
backend.Render(win)
}

View File

@ -1,4 +1,4 @@
package render package game
import ( import (
"errors" "errors"
@ -8,11 +8,13 @@ import (
"edgaru089.ml/go/gl01/internal/asset" "edgaru089.ml/go/gl01/internal/asset"
"edgaru089.ml/go/gl01/internal/igwrap" "edgaru089.ml/go/gl01/internal/igwrap"
"edgaru089.ml/go/gl01/internal/igwrap/backend"
"edgaru089.ml/go/gl01/internal/io" "edgaru089.ml/go/gl01/internal/io"
"edgaru089.ml/go/gl01/internal/render"
"edgaru089.ml/go/gl01/internal/util" "edgaru089.ml/go/gl01/internal/util"
"edgaru089.ml/go/gl01/internal/util/itype" "edgaru089.ml/go/gl01/internal/util/itype"
"edgaru089.ml/go/gl01/internal/world"
"github.com/go-gl/gl/all-core/gl" "github.com/go-gl/gl/all-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/go-gl/mathgl/mgl32" "github.com/go-gl/mathgl/mgl32"
"github.com/inkyblackness/imgui-go/v4" "github.com/inkyblackness/imgui-go/v4"
) )
@ -26,9 +28,8 @@ var (
RandomSize = itype.Vec2i{32, 32} // Size of the random mapping RandomSize = itype.Vec2i{32, 32} // Size of the random mapping
) )
// WorldRenderer holds texture/shader resource and viewport // renderData holds OpenGL state used by the game renderer.
// information for world rendering. type renderData struct {
type WorldRenderer struct {
lastDisplaySize itype.Vec2i lastDisplaySize itype.Vec2i
startTime time.Time startTime time.Time
@ -37,7 +38,7 @@ type WorldRenderer struct {
// Depth mapping pass // Depth mapping pass
depthmap struct { depthmap struct {
fbo, tex uint32 // Framebuffer Object and Texture. fbo, tex uint32 // Framebuffer Object and Texture.
shader *Shader // Shader. shader *render.Shader // Shader.
} }
// Geometry pass // Geometry pass
@ -47,7 +48,7 @@ type WorldRenderer struct {
// Textures. Position/Depth(View Space); Normal/Lightspace Depth; Diffuse Color/Specular Intensity. // Textures. Position/Depth(View Space); Normal/Lightspace Depth; Diffuse Color/Specular Intensity.
pos, norm, color uint32 pos, norm, color uint32
depth uint32 // Depth renderbuffer. depth uint32 // Depth renderbuffer.
shader *Shader // Geometry pass shaders. shader *render.Shader // Geometry pass shaders.
} }
// Screen-Space Ambient Occlusion (SSAO) pass // Screen-Space Ambient Occlusion (SSAO) pass
@ -55,24 +56,24 @@ type WorldRenderer struct {
fbo uint32 // Framebuffer fbo uint32 // Framebuffer
ambient uint32 // Ambient strength output texture [0,1] (Red channel) ambient uint32 // Ambient strength output texture [0,1] (Red channel)
uboSamples uint32 // Uniform Buffer Object pointing to the array of samples uboSamples uint32 // Uniform Buffer Object pointing to the array of samples
shader *Shader // SSAO pass shader shader *render.Shader // SSAO pass shader
// SSAO blur pass // SSAO blur pass
blur struct { blur struct {
fbo uint32 fbo uint32
output uint32 output uint32
shader *Shader shader *render.Shader
} }
} }
// Deferred lighting pass // Deferred lighting pass
lighting struct { lighting struct {
shader *Shader // Deferred lighting pass shaders shader *render.Shader // Deferred lighting pass shaders
} }
// Semi-transparent pass // Semi-transparent pass
water struct { water struct {
shader *Shader shader *render.Shader
} }
// Output pass // Output pass
@ -80,43 +81,40 @@ type WorldRenderer struct {
fbo uint32 // Output framebuffer object, rendering to the output texture. fbo uint32 // Output framebuffer object, rendering to the output texture.
tex uint32 // Output texture, rendered to the back buffer at the end. tex uint32 // Output texture, rendered to the back buffer at the end.
//depth uint32 // Output depth renderbuffer, use gbuffer.depth //depth uint32 // Output depth renderbuffer, use gbuffer.depth
shader *Shader // Shader used to copy output.tex to back buffer. shader *render.Shader // Shader used to copy output.tex to back buffer.
} }
texture *Texture // World texture atlas texture *render.Texture // World texture atlas
} }
// The default WorldRenderer. func (g *Game) initRender() (err error) {
var DefaultWorldRenderer WorldRenderer r := &g.render
// Init initializes the WorldRenderer. r.depthmap.shader, err = render.NewShader(asset.WorldShaderShadowmapVert, asset.WorldShaderShadowmapFrag)
func (r *WorldRenderer) Init(w *world.World) (err error) {
r.depthmap.shader, err = NewShader(asset.WorldShaderShadowmapVert, asset.WorldShaderShadowmapFrag)
if err != nil { if err != nil {
return errors.New("depthmap: " + err.Error()) return errors.New("depthmap: " + err.Error())
} }
r.gbuffer.shader, err = NewShader(asset.WorldShaderGeometryVert, asset.WorldShaderGeometryFrag) r.gbuffer.shader, err = render.NewShader(asset.WorldShaderGeometryVert, asset.WorldShaderGeometryFrag)
if err != nil { if err != nil {
return errors.New("gbuffer: " + err.Error()) return errors.New("gbuffer: " + err.Error())
} }
r.ssao.shader, err = NewShader(asset.WorldShaderSSAOVert, asset.WorldShaderSSAOFrag) r.ssao.shader, err = render.NewShader(asset.WorldShaderSSAOVert, asset.WorldShaderSSAOFrag)
if err != nil { if err != nil {
return errors.New("ssao: " + err.Error()) return errors.New("ssao: " + err.Error())
} }
r.ssao.blur.shader, err = NewShader(asset.WorldShaderSSAOBlurVert, asset.WorldShaderSSAOBlurFrag) r.ssao.blur.shader, err = render.NewShader(asset.WorldShaderSSAOBlurVert, asset.WorldShaderSSAOBlurFrag)
if err != nil { if err != nil {
return errors.New("ssao_blur: " + err.Error()) return errors.New("ssao_blur: " + err.Error())
} }
r.lighting.shader, err = NewShader(asset.WorldShaderLightingVert, asset.WorldShaderLightingFrag) r.lighting.shader, err = render.NewShader(asset.WorldShaderLightingVert, asset.WorldShaderLightingFrag)
if err != nil { if err != nil {
return errors.New("lighting: " + err.Error()) return errors.New("lighting: " + err.Error())
} }
r.water.shader, err = NewShader(asset.WorldShaderWaterVert, asset.WorldShaderWaterFrag) r.water.shader, err = render.NewShader(asset.WorldShaderWaterVert, asset.WorldShaderWaterFrag)
if err != nil { if err != nil {
return errors.New("water: " + err.Error()) return errors.New("water: " + err.Error())
} }
r.output.shader, err = NewShader(asset.WorldShaderOutputVert, asset.WorldShaderOutputFrag) r.output.shader, err = render.NewShader(asset.WorldShaderOutputVert, asset.WorldShaderOutputFrag)
if err != nil { if err != nil {
return errors.New("output: " + err.Error()) return errors.New("output: " + err.Error())
} }
@ -126,14 +124,14 @@ func (r *WorldRenderer) Init(w *world.World) (err error) {
gl.GetFloatv(gl.MAX_TEXTURE_MAX_ANISOTROPY, &maxaf) gl.GetFloatv(gl.MAX_TEXTURE_MAX_ANISOTROPY, &maxaf)
asset.InitWorldTextureAtlas() asset.InitWorldTextureAtlas()
r.texture = NewTexture() r.texture = render.NewTexture()
r.texture.UpdatesRGB(asset.WorldTextureAtlas.Image) r.texture.UpdatesRGB(asset.WorldTextureAtlas.Image)
r.texture.GenerateMipMap() r.texture.GenerateMipMap()
gl.BindTexture(gl.TEXTURE_2D, r.texture.Handle()) gl.BindTexture(gl.TEXTURE_2D, r.texture.Handle())
gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAX_ANISOTROPY, maxaf) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAX_ANISOTROPY, maxaf)
r.gbuffer.shader.SetUniformTexture("tex", r.texture) r.gbuffer.shader.SetUniformTexture("tex", r.texture)
r.water.shader.SetUniformTexture("tex", r.texture) r.water.shader.SetUniformTexture("tex", r.texture)
igwrap.SetTextureFlag(r.texture.Handle(), igwrap.TextureFlag_RGBA, igwrap.TextureFlag_FlipY) igwrap.SetTextureFlag(r.texture.Handle(), igwrap.TextureFlag_Linear, igwrap.TextureFlag_FlipY)
r.depthmap.shader.SetUniformMat4("model", mgl32.Ident4()) r.depthmap.shader.SetUniformMat4("model", mgl32.Ident4())
r.gbuffer.shader.SetUniformMat4("model", mgl32.Ident4()) r.gbuffer.shader.SetUniformMat4("model", mgl32.Ident4())
@ -287,29 +285,28 @@ func (r *WorldRenderer) Init(w *world.World) (err error) {
r.lastDisplaySize = io.DisplaySize r.lastDisplaySize = io.DisplaySize
r.startTime = time.Now() r.startTime = time.Now()
initScreenQuad()
return nil return nil
} }
// ResizeDisplay resizes the size of the internal buffers dependent on the window size. // ResizeDisplay resizes the size of the internal buffers dependent on the window size.
// It is called automatically most of the time. // It is called automatically most of the time.
func (r *WorldRenderer) ResizeDisplay(newSize itype.Vec2i) { func (g *Game) ResizeDisplay(newSize itype.Vec2i) {
gl.BindTexture(gl.TEXTURE_2D, r.gbuffer.pos) // G-Buffer, Position 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.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, r.gbuffer.norm) // G-Buffer, Normal 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.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, r.gbuffer.color) // G-Buffer, Albedo Color 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.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, r.gbuffer.depth) // G-Buffer, Depth (Renderbuffer) 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.RenderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]))
gl.BindTexture(gl.TEXTURE_2D, r.ssao.ambient) // SSAO Output Ambient Strength 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.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, r.ssao.blur.output) // SSAO Blurred Output 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.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, r.output.tex) // Output Texture 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) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
r.lastDisplaySize = newSize g.render.lastDisplaySize = newSize
} }
var ( var (
@ -320,7 +317,12 @@ var (
atlasScale = float32(1) atlasScale = float32(1)
) )
func (r *WorldRenderer) Render(world *world.World, view *View) { // 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() allclock := util.NewClock()
lastclock := util.NewClock() lastclock := util.NewClock()
@ -328,8 +330,8 @@ func (r *WorldRenderer) Render(world *world.World, view *View) {
io.RenderDir = io.ViewDir io.RenderDir = io.ViewDir
// re-generate the G-buffers if the display size changed // re-generate the G-buffers if the display size changed
if r.lastDisplaySize != io.DisplaySize { if g.render.lastDisplaySize != io.DisplaySize {
r.ResizeDisplay(io.DisplaySize) g.ResizeDisplay(io.DisplaySize)
} }
imgui.SliderFloat3("Sun", &sun, -1, 1) imgui.SliderFloat3("Sun", &sun, -1, 1)
@ -346,84 +348,88 @@ func (r *WorldRenderer) Render(world *world.World, view *View) {
// 1. Render to depth map // 1. Render to depth map
gl.Viewport(0, 0, int32(ShadowmapSize[0]), int32(ShadowmapSize[1])) gl.Viewport(0, 0, int32(ShadowmapSize[0]), int32(ShadowmapSize[1]))
gl.BindFramebuffer(gl.FRAMEBUFFER, r.depthmap.fbo) gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.depthmap.fbo)
gl.Clear(gl.DEPTH_BUFFER_BIT) gl.Clear(gl.DEPTH_BUFFER_BIT)
lightPos := view.EyePos.Add(normalSun.Multiply(50)) lightPos := g.view.EyePos.Add(normalSun.Multiply(50))
lightView := mgl32.LookAt(lightPos[0], lightPos[1], lightPos[2], view.EyePos[0], view.EyePos[1], view.EyePos[2], 0, 1, 0) 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) lightProjection := mgl32.Ortho(-50, 50, -50, 50, 1, 100)
lightspace := lightProjection.Mul4(lightView) lightspace := lightProjection.Mul4(lightView)
io.RenderPos = lightPos.ToFloat64() io.RenderPos = lightPos.ToFloat64()
io.RenderDir = view.EyePos.Add(lightPos.Negative()).ToFloat64() io.RenderDir = g.view.EyePos.Add(lightPos.Negative()).ToFloat64()
r.depthmap.shader.UseProgram() g.render.depthmap.shader.UseProgram()
r.depthmap.shader.SetUniformMat4("lightspace", lightspace) g.render.depthmap.shader.SetUniformMat4("lightspace", lightspace)
world.Render() g.world.Render()
world.RenderWater() g.world.RenderWater()
gl.Flush()
io.Diagnostics.Times.RenderPasses.Depthmap = lastclock.Restart() io.Diagnostics.Times.RenderPasses.Depthmap = lastclock.Restart()
// 2. Geometry pass, render to G-buffer // 2. Geometry pass, render to G-buffer
io.RenderPos = io.ViewPos io.RenderPos = io.ViewPos
io.RenderDir = io.ViewDir io.RenderDir = io.ViewDir
gl.Viewport(0, 0, int32(r.lastDisplaySize[0]), int32(r.lastDisplaySize[1])) gl.Viewport(0, 0, int32(g.render.lastDisplaySize[0]), int32(g.render.lastDisplaySize[1]))
gl.BindFramebuffer(gl.FRAMEBUFFER, r.gbuffer.fbo) gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.gbuffer.fbo)
gl.ClearColor(0, 0, 0, 1) gl.ClearColor(0, 0, 0, 1)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.Disable(gl.BLEND) gl.Disable(gl.BLEND)
r.gbuffer.shader.UseProgram() g.render.gbuffer.shader.UseProgram()
r.gbuffer.shader.BindTextures() g.render.gbuffer.shader.BindTextures()
r.gbuffer.shader.SetUniformMat4("lightspace", lightspace) g.render.gbuffer.shader.SetUniformMat4("lightspace", lightspace)
r.gbuffer.shader.SetUniformMat4("view", view.View()) g.render.gbuffer.shader.SetUniformMat4("view", matv)
r.gbuffer.shader.SetUniformMat4("projection", view.Perspective()) g.render.gbuffer.shader.SetUniformMat4("projection", matp)
r.gbuffer.shader.SetUniformMat4("mvp", view.Perspective().Mul4(view.View())) g.render.gbuffer.shader.SetUniformMat4("mvp", matvp)
r.gbuffer.shader.SetUniformVec3f("viewPos", view.EyePos) g.render.gbuffer.shader.SetUniformVec3f("viewPos", g.view.EyePos)
world.Render() g.world.Render()
gl.Flush()
io.Diagnostics.Times.RenderPasses.Geometry = lastclock.Restart() io.Diagnostics.Times.RenderPasses.Geometry = lastclock.Restart()
// 3/1. SSAO pass // 3/1. SSAO pass
gl.BindFramebuffer(gl.FRAMEBUFFER, r.ssao.fbo) gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.ssao.fbo)
gl.ClearColor(1, 1, 1, 1) gl.ClearColor(1, 1, 1, 1)
gl.Clear(gl.COLOR_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT)
r.ssao.shader.UseProgram() g.render.ssao.shader.UseProgram()
r.ssao.shader.BindTextures() g.render.ssao.shader.BindTextures()
r.ssao.shader.SetUniformMat4("view", view.View()) g.render.ssao.shader.SetUniformMat4("view", matv)
r.ssao.shader.SetUniformMat4("projection", view.Perspective()) g.render.ssao.shader.SetUniformMat4("projection", matp)
r.ssao.shader.SetUniformVec3f("viewPos", view.EyePos) g.render.ssao.shader.SetUniformVec3f("viewPos", g.view.EyePos)
DrawScreenQuad() render.DrawScreenQuad()
// 3/2. SSAO blur pass (TODO) // 3/2. SSAO blur pass
gl.BindFramebuffer(gl.FRAMEBUFFER, r.ssao.blur.fbo) gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.ssao.blur.fbo)
r.ssao.blur.shader.UseProgram() g.render.ssao.blur.shader.UseProgram()
r.ssao.blur.shader.BindTextures() g.render.ssao.blur.shader.BindTextures()
r.ssao.blur.shader.SetUniformVec2f("screenSize", r.lastDisplaySize.ToFloat32()) g.render.ssao.blur.shader.SetUniformVec2f("screenSize", g.render.lastDisplaySize.ToFloat32())
DrawScreenQuad() render.DrawScreenQuad()
gl.Flush()
io.Diagnostics.Times.RenderPasses.SSAO = lastclock.Restart() io.Diagnostics.Times.RenderPasses.SSAO = lastclock.Restart()
// 4. Render the actual output with deferred lighting // 4. Render the actual output with deferred lighting
gl.BindFramebuffer(gl.FRAMEBUFFER, r.output.fbo) gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.output.fbo)
gl.ClearColor(0, 0, 0, 0) gl.ClearColor(0, 0, 0, 0)
gl.Clear(gl.COLOR_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT)
gl.Disable(gl.DEPTH_TEST) gl.Disable(gl.DEPTH_TEST)
r.lighting.shader.UseProgram() g.render.lighting.shader.UseProgram()
r.lighting.shader.BindTextures() g.render.lighting.shader.BindTextures()
r.lighting.shader.SetUniformMat4("lightspace", lightspace) g.render.lighting.shader.SetUniformMat4("lightspace", lightspace)
r.lighting.shader.SetUniformVec3f("viewPos", view.EyePos) g.render.lighting.shader.SetUniformVec3f("viewPos", g.view.EyePos)
r.lighting.shader.SetUniformVec4f("fogColor", io.FogColor) g.render.lighting.shader.SetUniformVec4f("fogColor", io.FogColor)
r.lighting.shader.SetUniformVec3f("sun", normalSun) g.render.lighting.shader.SetUniformVec3f("sun", normalSun)
DrawScreenQuad() render.DrawScreenQuad()
gl.Flush()
io.Diagnostics.Times.RenderPasses.Lighting = lastclock.Restart() io.Diagnostics.Times.RenderPasses.Lighting = lastclock.Restart()
// 5. Render water // 5. Render water
@ -433,19 +439,22 @@ func (r *WorldRenderer) Render(world *world.World, view *View) {
gl.Enable(gl.BLEND) gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.BlendEquation(gl.FUNC_ADD) gl.BlendEquation(gl.FUNC_ADD)
r.water.shader.UseProgram() g.render.water.shader.UseProgram()
r.water.shader.BindTextures() g.render.water.shader.BindTextures()
r.water.shader.SetUniformMat4("lightspace", lightspace) g.render.water.shader.SetUniformMat4("lightspace", lightspace)
r.water.shader.SetUniformMat4("view", view.View()) g.render.water.shader.SetUniformMat4("view", matv)
r.water.shader.SetUniformMat4("projection", view.Perspective()) g.render.water.shader.SetUniformMat4("projection", matp)
r.water.shader.SetUniformVec3f("viewPos", view.EyePos) g.render.water.shader.SetUniformVec3f("viewPos", g.view.EyePos)
r.water.shader.SetUniformVec4f("fogColor", io.FogColor) g.render.water.shader.SetUniformVec4f("fogColor", io.FogColor)
r.water.shader.SetUniformVec3f("sun", normalSun) g.render.water.shader.SetUniformVec3f("sun", normalSun)
r.water.shader.SetUniformFloat("alpha", alpha) g.render.water.shader.SetUniformFloat("alpha", alpha)
r.water.shader.SetUniformVec2f("screenSize", r.lastDisplaySize.ToFloat32()) g.render.water.shader.SetUniformVec2f("screenSize", g.render.lastDisplaySize.ToFloat32())
world.RenderWater() g.world.RenderWater()
// And render framewires
render.Framewire.Render(g.view)
// Finally. Copy the output texture to the back buffer // Finally. Copy the output texture to the back buffer
gl.BindFramebuffer(gl.FRAMEBUFFER, 0) gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
@ -453,18 +462,21 @@ func (r *WorldRenderer) Render(world *world.World, view *View) {
gl.Clear(gl.COLOR_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT)
gl.Disable(gl.DEPTH_TEST) gl.Disable(gl.DEPTH_TEST)
gl.Disable(gl.BLEND) gl.Disable(gl.BLEND)
r.output.shader.UseProgram() g.render.output.shader.UseProgram()
r.output.shader.BindTextures() g.render.output.shader.BindTextures()
r.output.shader.SetUniformFloat("gamma", gamma) g.render.output.shader.SetUniformFloat("gamma", gamma)
r.output.shader.SetUniformFloat("exposure", exposure) g.render.output.shader.SetUniformFloat("exposure", exposure)
DrawScreenQuad() render.DrawScreenQuad()
gl.Flush()
io.Diagnostics.Times.RenderPasses.Postfx = lastclock.Restart() io.Diagnostics.Times.RenderPasses.Postfx = lastclock.Restart()
io.Diagnostics.Times.Render = allclock.Elapsed() io.Diagnostics.Times.Render = allclock.Elapsed()
// Show Information? // Show Information?
if io.ShowDebugInfo { if io.ShowDebugInfo {
r.renderDebugInfo() g.renderDebugInfo()
} }
backend.Render(win)
} }

View File

@ -1,4 +1,4 @@
package render package game
import ( import (
"math" "math"
@ -14,7 +14,7 @@ import (
const ( const (
timebarN = 700 timebarN = 700
timebarScale = 160 timebarScale = 256
) )
var ( var (
@ -23,7 +23,7 @@ var (
timebari int timebari int
) )
func (r *WorldRenderer) renderDebugInfo() { func (g *Game) renderDebugInfo() {
// Render information // Render information
if igwrap.Begin("F3", nil, 0) { if igwrap.Begin("F3", nil, 0) {
igwrap.TextBlank() igwrap.TextBlank()
@ -52,7 +52,7 @@ func (r *WorldRenderer) renderDebugInfo() {
atlasScale = util.Maxf(1, atlasScale+wheely) atlasScale = util.Maxf(1, atlasScale+wheely)
} }
imgui.BeginTooltip() imgui.BeginTooltip()
igwrap.Image(r.texture.Handle(), isize.ToFloat32().Multiply(atlasScale), itype.Rectf{0, 0, 1, 1}) igwrap.Image(g.render.texture.Handle(), isize.ToFloat32().Multiply(atlasScale), itype.Rectf{0, 0, 1, 1})
imgui.EndTooltip() imgui.EndTooltip()
} }
@ -60,18 +60,18 @@ func (r *WorldRenderer) renderDebugInfo() {
} }
// Draw Textures // Draw Textures
imgui.SetNextWindowPosV(imgui.Vec2{X: float32(r.lastDisplaySize[0]), Y: 0}, imgui.ConditionAlways, imgui.Vec2{X: 1, Y: 0}) 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) { if igwrap.Begin("Renderer Textures/Outputs", nil, igwrap.WindowFlagsOverlay) {
imgui.PushStyleVarVec2(imgui.StyleVarItemSpacing, imgui.Vec2{}) imgui.PushStyleVarVec2(imgui.StyleVarItemSpacing, imgui.Vec2{})
imageSize := r.lastDisplaySize.ToFloat32().Multiply(0.25) imageSize := g.render.lastDisplaySize.ToFloat32().Multiply(0.25)
imageSize[1] -= imgui.CurrentStyle().WindowPadding().Y / 2 imageSize[1] -= imgui.CurrentStyle().WindowPadding().Y / 2
imageSize[0] = imageSize[1] / float32(r.lastDisplaySize[1]) * float32(r.lastDisplaySize[0]) imageSize[0] = imageSize[1] / float32(g.render.lastDisplaySize[1]) * float32(g.render.lastDisplaySize[0])
igwrap.Image(r.gbuffer.pos, imageSize, itype.Rectf{0, 0, 1, 1}) igwrap.Image(g.render.gbuffer.pos, imageSize, itype.Rectf{0, 0, 1, 1})
igwrap.Image(r.gbuffer.norm, imageSize, itype.Rectf{0, 0, 1, 1}) igwrap.Image(g.render.gbuffer.norm, imageSize, itype.Rectf{0, 0, 1, 1})
igwrap.Image(r.gbuffer.color, imageSize, itype.Rectf{0, 0, 1, 1}) igwrap.Image(g.render.gbuffer.color, imageSize, itype.Rectf{0, 0, 1, 1})
igwrap.Image(r.ssao.ambient, imageSize, itype.Rectf{0, 0, 1, 1}) igwrap.Image(g.render.ssao.ambient, imageSize, itype.Rectf{0, 0, 1, 1})
imgui.PopStyleVar() imgui.PopStyleVar()
imgui.End() imgui.End()
@ -79,11 +79,11 @@ func (r *WorldRenderer) renderDebugInfo() {
// Push the next bar // Push the next bar
timebars[timebari] = []int{ timebars[timebari] = []int{
int(io.Diagnostics.Times.RenderPasses.Depthmap.Nanoseconds() * timebarScale / 1000 / 1000), int(io.Diagnostics.Times.RenderPasses.Depthmap.Nanoseconds() * timebarScale / 1024 / 1024),
int(io.Diagnostics.Times.RenderPasses.Geometry.Nanoseconds() * timebarScale / 1000 / 1000), int(io.Diagnostics.Times.RenderPasses.Geometry.Nanoseconds() * timebarScale / 1024 / 1024),
int(io.Diagnostics.Times.RenderPasses.SSAO.Nanoseconds() * timebarScale / 1000 / 1000), int(io.Diagnostics.Times.RenderPasses.SSAO.Nanoseconds() * timebarScale / 1024 / 1024),
int(io.Diagnostics.Times.RenderPasses.Lighting.Nanoseconds() * timebarScale / 1000 / 1000), int(io.Diagnostics.Times.RenderPasses.Lighting.Nanoseconds() * timebarScale / 1024 / 1024),
int(io.Diagnostics.Times.RenderPasses.Postfx.Nanoseconds() * timebarScale / 1000 / 1000), int(io.Diagnostics.Times.RenderPasses.Postfx.Nanoseconds() * timebarScale / 1024 / 1024),
} }
timebari++ timebari++
if timebari >= len(timebars) { if timebari >= len(timebars) {
@ -91,12 +91,12 @@ func (r *WorldRenderer) renderDebugInfo() {
} }
// Draw time bars // Draw time bars
size := r.lastDisplaySize size := g.render.lastDisplaySize
dl := imgui.BackgroundDrawList() dl := imgui.BackgroundDrawList()
for i, l := range timebars { for i, l := range timebars {
ex := 0 ex := 0
for j, d := range l { for j, d := range l {
dl.AddLine(imgui.Vec2{X: float32(i), Y: float32(size[1] - ex - 1)}, imgui.Vec2{X: float32(i), Y: float32(size[1] - ex - d)}, colorset[j]) 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 ex += d
} }
} }

View File

@ -1,119 +0,0 @@
package render
import (
"time"
"unsafe"
"github.com/Edgaru089/gl01/internal/asset"
"github.com/Edgaru089/gl01/internal/util/itype"
"github.com/Edgaru089/gl01/internal/world"
"github.com/go-gl/gl/all-core/gl"
"github.com/go-gl/mathgl/mgl32"
)
// WorldRenderer holds texture/shader resource and viewport
// information for world rendering.
type WorldRenderer struct {
shader *Shader
texture *Texture
vao, vbo uint32
vbolen int
vertex []world.Vertex
// for testing!
initTime time.Time
}
// The default WorldRenderer.
var DefaultWorldRenderer WorldRenderer
// Init initializes the WorldRenderer.
func (r *WorldRenderer) Init(w *world.World) (err error) {
r.shader, err = NewShader(asset.WorldShaderVert, asset.WorldShaderFrag)
if err != nil {
return err
}
asset.InitWorldTextureAtlas()
r.texture = NewTextureRGBA(asset.WorldTextureAtlas.Image)
r.shader.SetUniformTexture("tex", r.texture)
r.shader.SetUniformMat4("model", mgl32.Ident4())
// and view and projection uniforms not yet set
gl.BindFragDataLocation(r.shader.Handle(), 0, gl.Str("outputColor\x00"))
gl.GenVertexArrays(1, &r.vao)
gl.BindVertexArray(r.vao)
gl.GenBuffers(1, &r.vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, r.vbo)
gensync := make(chan []world.Vertex, len(w.Chunks))
for _, c := range w.Chunks {
chunk := c
go func() {
arr := chunk.AppendVertex([]world.Vertex{})
gensync <- arr
}()
}
for range w.Chunks {
r.vertex = append(r.vertex, (<-gensync)...)
}
close(gensync)
gl.BufferData(gl.ARRAY_BUFFER, int(unsafe.Sizeof(world.Vertex{}))*len(r.vertex), gl.Ptr(r.vertex), gl.DYNAMIC_DRAW)
vertAttrib := uint32(gl.GetAttribLocation(r.shader.Handle(), gl.Str("vert\x00")))
gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, int32(unsafe.Sizeof(world.Vertex{})), gl.PtrOffset(int(unsafe.Offsetof(world.Vertex{}.World))))
normalAttrib := uint32(gl.GetAttribLocation(r.shader.Handle(), gl.Str("normal\x00")))
gl.VertexAttribPointer(normalAttrib, 3, gl.FLOAT, false, int32(unsafe.Sizeof(world.Vertex{})), gl.PtrOffset(int(unsafe.Offsetof(world.Vertex{}.Normal))))
texCoordAttrib := uint32(gl.GetAttribLocation(r.shader.Handle(), gl.Str("vertTexCoord\x00")))
gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, int32(unsafe.Sizeof(world.Vertex{})), gl.PtrOffset(int(unsafe.Offsetof(world.Vertex{}.Texture))))
lightAttrib := uint32(gl.GetAttribLocation(r.shader.Handle(), gl.Str("light\x00")))
gl.VertexAttribPointer(lightAttrib, 1, gl.FLOAT, false, int32(unsafe.Sizeof(world.Vertex{})), gl.PtrOffset(int(unsafe.Offsetof(world.Vertex{}.Light))))
gl.EnableVertexAttribArray(vertAttrib)
gl.EnableVertexAttribArray(normalAttrib)
gl.EnableVertexAttribArray(texCoordAttrib)
gl.EnableVertexAttribArray(lightAttrib)
w.EnableVertexArrayAttrib(vertAttrib)
w.EnableVertexArrayAttrib(normalAttrib)
w.EnableVertexArrayAttrib(texCoordAttrib)
w.EnableVertexArrayAttrib(lightAttrib)
gl.Enable(gl.CULL_FACE)
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LESS)
r.initTime = time.Now()
return nil
}
func (r *WorldRenderer) Render(world *world.World, view *View) {
r.shader.UseProgram()
r.shader.BindTextures()
r.shader.SetUniformMat4("view", view.view)
r.shader.SetUniformMat4("projection", mgl32.Perspective(view.fovy, view.aspect, 0.1, 200))
r.shader.SetUniformVec3f("sun", itype.Vec3f{-0.2, 1, 0.8}.Normalize())
// for testing!
//model := mgl32.HomogRotate3D(float32(time.Since(r.initTime).Seconds()), mgl32.Vec3{0, 1, 0})
//r.shader.SetUniformMat4("model", model)
//r.shader.SetUniformMat4("view", r.view)
gl.BindVertexArray(r.vao)
gl.BindBuffer(gl.ARRAY_BUFFER, r.vbo)
gl.DrawArrays(gl.TRIANGLES, 0, int32(len(r.vertex)))
world.Render()
//world.Chunk(0, 0).Render()
}