move WorldRenderer to game package
This commit is contained in:
@ -1,470 +0,0 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"edgaru089.ml/go/gl01/internal/asset"
|
||||
"edgaru089.ml/go/gl01/internal/igwrap"
|
||||
"edgaru089.ml/go/gl01/internal/io"
|
||||
"edgaru089.ml/go/gl01/internal/util"
|
||||
"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/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
|
||||
)
|
||||
|
||||
// WorldRenderer holds texture/shader resource and viewport
|
||||
// information for world rendering.
|
||||
type WorldRenderer 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 *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 *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 *Shader // SSAO pass shader
|
||||
|
||||
// SSAO blur pass
|
||||
blur struct {
|
||||
fbo uint32
|
||||
output uint32
|
||||
shader *Shader
|
||||
}
|
||||
}
|
||||
|
||||
// Deferred lighting pass
|
||||
lighting struct {
|
||||
shader *Shader // Deferred lighting pass shaders
|
||||
}
|
||||
|
||||
// Semi-transparent pass
|
||||
water struct {
|
||||
shader *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 *Shader // Shader used to copy output.tex to back buffer.
|
||||
}
|
||||
|
||||
texture *Texture // World texture atlas
|
||||
}
|
||||
|
||||
// The default WorldRenderer.
|
||||
var DefaultWorldRenderer WorldRenderer
|
||||
|
||||
// Init initializes the WorldRenderer.
|
||||
func (r *WorldRenderer) Init(w *world.World) (err error) {
|
||||
|
||||
r.depthmap.shader, err = NewShader(asset.WorldShaderShadowmapVert, asset.WorldShaderShadowmapFrag)
|
||||
if err != nil {
|
||||
return errors.New("depthmap: " + err.Error())
|
||||
}
|
||||
r.gbuffer.shader, err = NewShader(asset.WorldShaderGeometryVert, asset.WorldShaderGeometryFrag)
|
||||
if err != nil {
|
||||
return errors.New("gbuffer: " + err.Error())
|
||||
}
|
||||
r.ssao.shader, err = NewShader(asset.WorldShaderSSAOVert, asset.WorldShaderSSAOFrag)
|
||||
if err != nil {
|
||||
return errors.New("ssao: " + err.Error())
|
||||
}
|
||||
r.ssao.blur.shader, err = NewShader(asset.WorldShaderSSAOBlurVert, asset.WorldShaderSSAOBlurFrag)
|
||||
if err != nil {
|
||||
return errors.New("ssao_blur: " + err.Error())
|
||||
}
|
||||
r.lighting.shader, err = NewShader(asset.WorldShaderLightingVert, asset.WorldShaderLightingFrag)
|
||||
if err != nil {
|
||||
return errors.New("lighting: " + err.Error())
|
||||
}
|
||||
r.water.shader, err = NewShader(asset.WorldShaderWaterVert, asset.WorldShaderWaterFrag)
|
||||
if err != nil {
|
||||
return errors.New("water: " + err.Error())
|
||||
}
|
||||
r.output.shader, err = 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 = 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.gbuffer.shader.SetUniformTexture("tex", r.texture)
|
||||
r.water.shader.SetUniformTexture("tex", r.texture)
|
||||
igwrap.SetTextureFlag(r.texture.Handle(), igwrap.TextureFlag_RGBA, 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()
|
||||
|
||||
initScreenQuad()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResizeDisplay resizes the size of the internal buffers dependent on the window size.
|
||||
// It is called automatically most of the time.
|
||||
func (r *WorldRenderer) ResizeDisplay(newSize itype.Vec2i) {
|
||||
gl.BindTexture(gl.TEXTURE_2D, r.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, r.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, r.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, r.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, r.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, r.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, r.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)
|
||||
r.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)
|
||||
)
|
||||
|
||||
func (r *WorldRenderer) Render(world *world.World, view *View) {
|
||||
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 r.lastDisplaySize != io.DisplaySize {
|
||||
r.ResizeDisplay(io.DisplaySize)
|
||||
}
|
||||
|
||||
imgui.SliderFloat3("Sun", &sun, -1, 1)
|
||||
normalSun := itype.Vec3f(sun).Normalize()
|
||||
imgui.SliderFloat("Water Alpha", &alpha, 0, 1)
|
||||
imgui.SliderFloat("Gamma", &gamma, 1.6, 2.8)
|
||||
imgui.SliderFloat("Exposure", &exposure, 0, 2)
|
||||
|
||||
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, r.depthmap.fbo)
|
||||
gl.Clear(gl.DEPTH_BUFFER_BIT)
|
||||
|
||||
lightPos := 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)
|
||||
lightProjection := mgl32.Ortho(-50, 50, -50, 50, 1, 100)
|
||||
lightspace := lightProjection.Mul4(lightView)
|
||||
|
||||
io.RenderPos = lightPos.ToFloat64()
|
||||
io.RenderDir = view.EyePos.Add(lightPos.Negative()).ToFloat64()
|
||||
|
||||
r.depthmap.shader.UseProgram()
|
||||
r.depthmap.shader.SetUniformMat4("lightspace", lightspace)
|
||||
|
||||
world.Render()
|
||||
world.RenderWater()
|
||||
|
||||
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(r.lastDisplaySize[0]), int32(r.lastDisplaySize[1]))
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, r.gbuffer.fbo)
|
||||
gl.ClearColor(0, 0, 0, 1)
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
|
||||
gl.Disable(gl.BLEND)
|
||||
|
||||
r.gbuffer.shader.UseProgram()
|
||||
r.gbuffer.shader.BindTextures()
|
||||
r.gbuffer.shader.SetUniformMat4("lightspace", lightspace)
|
||||
r.gbuffer.shader.SetUniformMat4("view", view.View())
|
||||
r.gbuffer.shader.SetUniformMat4("projection", view.Perspective())
|
||||
r.gbuffer.shader.SetUniformMat4("mvp", view.Perspective().Mul4(view.View()))
|
||||
r.gbuffer.shader.SetUniformVec3f("viewPos", view.EyePos)
|
||||
|
||||
world.Render()
|
||||
|
||||
io.Diagnostics.Times.RenderPasses.Geometry = lastclock.Restart()
|
||||
|
||||
// 3/1. SSAO pass
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, r.ssao.fbo)
|
||||
gl.ClearColor(1, 1, 1, 1)
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT)
|
||||
|
||||
r.ssao.shader.UseProgram()
|
||||
r.ssao.shader.BindTextures()
|
||||
r.ssao.shader.SetUniformMat4("view", view.View())
|
||||
r.ssao.shader.SetUniformMat4("projection", view.Perspective())
|
||||
r.ssao.shader.SetUniformVec3f("viewPos", view.EyePos)
|
||||
|
||||
DrawScreenQuad()
|
||||
|
||||
// 3/2. SSAO blur pass (TODO)
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, r.ssao.blur.fbo)
|
||||
r.ssao.blur.shader.UseProgram()
|
||||
r.ssao.blur.shader.BindTextures()
|
||||
r.ssao.blur.shader.SetUniformVec2f("screenSize", r.lastDisplaySize.ToFloat32())
|
||||
|
||||
DrawScreenQuad()
|
||||
|
||||
io.Diagnostics.Times.RenderPasses.SSAO = lastclock.Restart()
|
||||
|
||||
// 4. Render the actual output with deferred lighting
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, r.output.fbo)
|
||||
gl.ClearColor(0, 0, 0, 0)
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT)
|
||||
gl.Disable(gl.DEPTH_TEST)
|
||||
r.lighting.shader.UseProgram()
|
||||
r.lighting.shader.BindTextures()
|
||||
r.lighting.shader.SetUniformMat4("lightspace", lightspace)
|
||||
r.lighting.shader.SetUniformVec3f("viewPos", view.EyePos)
|
||||
r.lighting.shader.SetUniformVec4f("fogColor", io.FogColor)
|
||||
r.lighting.shader.SetUniformVec3f("sun", normalSun)
|
||||
|
||||
DrawScreenQuad()
|
||||
|
||||
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.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
|
||||
gl.BlendEquation(gl.FUNC_ADD)
|
||||
r.water.shader.UseProgram()
|
||||
r.water.shader.BindTextures()
|
||||
|
||||
r.water.shader.SetUniformMat4("lightspace", lightspace)
|
||||
r.water.shader.SetUniformMat4("view", view.View())
|
||||
r.water.shader.SetUniformMat4("projection", view.Perspective())
|
||||
r.water.shader.SetUniformVec3f("viewPos", view.EyePos)
|
||||
r.water.shader.SetUniformVec4f("fogColor", io.FogColor)
|
||||
r.water.shader.SetUniformVec3f("sun", normalSun)
|
||||
r.water.shader.SetUniformFloat("alpha", alpha)
|
||||
r.water.shader.SetUniformVec2f("screenSize", r.lastDisplaySize.ToFloat32())
|
||||
|
||||
world.RenderWater()
|
||||
|
||||
// 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)
|
||||
r.output.shader.UseProgram()
|
||||
r.output.shader.BindTextures()
|
||||
r.output.shader.SetUniformFloat("gamma", gamma)
|
||||
r.output.shader.SetUniformFloat("exposure", exposure)
|
||||
|
||||
DrawScreenQuad()
|
||||
|
||||
io.Diagnostics.Times.RenderPasses.Postfx = lastclock.Restart()
|
||||
io.Diagnostics.Times.Render = allclock.Elapsed()
|
||||
|
||||
// Show Information?
|
||||
if io.ShowDebugInfo {
|
||||
r.renderDebugInfo()
|
||||
}
|
||||
}
|
@ -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()
|
||||
}
|
@ -1,103 +0,0 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"edgaru089.ml/go/gl01/internal/asset"
|
||||
"edgaru089.ml/go/gl01/internal/igwrap"
|
||||
"edgaru089.ml/go/gl01/internal/io"
|
||||
"edgaru089.ml/go/gl01/internal/util"
|
||||
"edgaru089.ml/go/gl01/internal/util/itype"
|
||||
"github.com/inkyblackness/imgui-go/v4"
|
||||
)
|
||||
|
||||
const (
|
||||
timebarN = 700
|
||||
timebarScale = 160
|
||||
)
|
||||
|
||||
var (
|
||||
colorset = [...]imgui.PackedColor{4289753676, 4283598045, 4285048917, 4283584196, 4289950337, 4284512403, 4291005402, 4287401100, 4285839820, 4291671396}
|
||||
timebars [timebarN][]int // height of each bar set
|
||||
timebari int
|
||||
)
|
||||
|
||||
func (r *WorldRenderer) 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")
|
||||
|
||||
isize := asset.WorldTextureAtlas.ImageSize
|
||||
igwrap.TextBackground("Texture Atlas Size: (%dx%d)", isize[0], isize[1])
|
||||
imgui.SameLine()
|
||||
igwrap.TextBackground("[Hover]")
|
||||
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(r.texture.Handle(), isize.ToFloat32().Multiply(atlasScale), itype.Rectf{0, 0, 1, 1})
|
||||
imgui.EndTooltip()
|
||||
}
|
||||
|
||||
imgui.End()
|
||||
}
|
||||
|
||||
// Draw Textures
|
||||
imgui.SetNextWindowPosV(imgui.Vec2{X: float32(r.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 := r.lastDisplaySize.ToFloat32().Multiply(0.25)
|
||||
imageSize[1] -= imgui.CurrentStyle().WindowPadding().Y / 2
|
||||
imageSize[0] = imageSize[1] / float32(r.lastDisplaySize[1]) * float32(r.lastDisplaySize[0])
|
||||
|
||||
igwrap.Image(r.gbuffer.pos, imageSize, itype.Rectf{0, 0, 1, 1})
|
||||
igwrap.Image(r.gbuffer.norm, imageSize, itype.Rectf{0, 0, 1, 1})
|
||||
igwrap.Image(r.gbuffer.color, imageSize, itype.Rectf{0, 0, 1, 1})
|
||||
igwrap.Image(r.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() * timebarScale / 1000 / 1000),
|
||||
int(io.Diagnostics.Times.RenderPasses.Geometry.Nanoseconds() * timebarScale / 1000 / 1000),
|
||||
int(io.Diagnostics.Times.RenderPasses.SSAO.Nanoseconds() * timebarScale / 1000 / 1000),
|
||||
int(io.Diagnostics.Times.RenderPasses.Lighting.Nanoseconds() * timebarScale / 1000 / 1000),
|
||||
int(io.Diagnostics.Times.RenderPasses.Postfx.Nanoseconds() * timebarScale / 1000 / 1000),
|
||||
}
|
||||
timebari++
|
||||
if timebari >= len(timebars) {
|
||||
timebari = 0
|
||||
}
|
||||
|
||||
// Draw time bars
|
||||
size := r.lastDisplaySize
|
||||
dl := imgui.BackgroundDrawList()
|
||||
for i, l := range timebars {
|
||||
ex := 0
|
||||
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])
|
||||
ex += d
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user