mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 92855c52f9 | |||
| 594d342bf0 | |||
| bb1946b930 | |||
| ef2b01059a | |||
| 5fa6a06079 | |||
| b718611149 | |||
| be85e20024 | |||
| 040228319e | |||
| ddd8db3cb0 |
403
buffers/framebuffer.go
Executable file
403
buffers/framebuffer.go
Executable file
@ -0,0 +1,403 @@
|
||||
package buffers
|
||||
|
||||
import (
|
||||
"github.com/bloeys/nmage/logging"
|
||||
"github.com/go-gl/gl/v4.1-core/gl"
|
||||
)
|
||||
|
||||
type FramebufferAttachmentType int32
|
||||
|
||||
const (
|
||||
FramebufferAttachmentType_Unknown FramebufferAttachmentType = iota
|
||||
FramebufferAttachmentType_Texture
|
||||
FramebufferAttachmentType_Renderbuffer
|
||||
)
|
||||
|
||||
func (f FramebufferAttachmentType) IsValid() bool {
|
||||
|
||||
switch f {
|
||||
case FramebufferAttachmentType_Texture:
|
||||
fallthrough
|
||||
case FramebufferAttachmentType_Renderbuffer:
|
||||
return true
|
||||
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
type FramebufferAttachmentDataFormat int32
|
||||
|
||||
const (
|
||||
FramebufferAttachmentDataFormat_Unknown FramebufferAttachmentDataFormat = iota
|
||||
FramebufferAttachmentDataFormat_R32Int
|
||||
FramebufferAttachmentDataFormat_RGBA8
|
||||
FramebufferAttachmentDataFormat_SRGBA
|
||||
FramebufferAttachmentDataFormat_DepthF32
|
||||
FramebufferAttachmentDataFormat_Depth24Stencil8
|
||||
)
|
||||
|
||||
func (f FramebufferAttachmentDataFormat) IsColorFormat() bool {
|
||||
return f == FramebufferAttachmentDataFormat_R32Int ||
|
||||
f == FramebufferAttachmentDataFormat_RGBA8 ||
|
||||
f == FramebufferAttachmentDataFormat_SRGBA
|
||||
}
|
||||
|
||||
func (f FramebufferAttachmentDataFormat) IsDepthFormat() bool {
|
||||
return f == FramebufferAttachmentDataFormat_Depth24Stencil8 ||
|
||||
f == FramebufferAttachmentDataFormat_DepthF32
|
||||
}
|
||||
|
||||
func (f FramebufferAttachmentDataFormat) GlInternalFormat() int32 {
|
||||
|
||||
switch f {
|
||||
case FramebufferAttachmentDataFormat_R32Int:
|
||||
return gl.R32I
|
||||
case FramebufferAttachmentDataFormat_RGBA8:
|
||||
return gl.RGB8
|
||||
case FramebufferAttachmentDataFormat_SRGBA:
|
||||
return gl.SRGB_ALPHA
|
||||
case FramebufferAttachmentDataFormat_DepthF32:
|
||||
return gl.DEPTH_COMPONENT
|
||||
case FramebufferAttachmentDataFormat_Depth24Stencil8:
|
||||
return gl.DEPTH24_STENCIL8
|
||||
default:
|
||||
logging.ErrLog.Fatalf("unknown framebuffer attachment data format. Format=%d\n", f)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (f FramebufferAttachmentDataFormat) GlFormat() uint32 {
|
||||
|
||||
switch f {
|
||||
case FramebufferAttachmentDataFormat_R32Int:
|
||||
return gl.RED_INTEGER
|
||||
|
||||
case FramebufferAttachmentDataFormat_RGBA8:
|
||||
fallthrough
|
||||
case FramebufferAttachmentDataFormat_SRGBA:
|
||||
return gl.RGBA
|
||||
|
||||
case FramebufferAttachmentDataFormat_DepthF32:
|
||||
return gl.DEPTH_COMPONENT
|
||||
|
||||
case FramebufferAttachmentDataFormat_Depth24Stencil8:
|
||||
return gl.DEPTH_STENCIL
|
||||
|
||||
default:
|
||||
logging.ErrLog.Fatalf("unknown framebuffer attachment data format. Format=%d\n", f)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
type FramebufferAttachment struct {
|
||||
Id uint32
|
||||
Type FramebufferAttachmentType
|
||||
Format FramebufferAttachmentDataFormat
|
||||
}
|
||||
|
||||
type Framebuffer struct {
|
||||
Id uint32
|
||||
ClearFlags uint32
|
||||
Attachments []FramebufferAttachment
|
||||
ColorAttachmentsCount uint32
|
||||
Width uint32
|
||||
Height uint32
|
||||
}
|
||||
|
||||
func (fbo *Framebuffer) Bind() {
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, fbo.Id)
|
||||
}
|
||||
|
||||
func (fbo *Framebuffer) BindWithViewport() {
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, fbo.Id)
|
||||
gl.Viewport(0, 0, int32(fbo.Width), int32(fbo.Height))
|
||||
}
|
||||
|
||||
// Clear calls gl.Clear with the fob's clear flags.
|
||||
// Note that the fbo must be complete and bound.
|
||||
// Calling this without a bound fbo will clear something else, like your screen.
|
||||
func (fbo *Framebuffer) Clear() {
|
||||
gl.Clear(fbo.ClearFlags)
|
||||
}
|
||||
|
||||
func (fbo *Framebuffer) UnBind() {
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
|
||||
}
|
||||
|
||||
func (fbo *Framebuffer) UnBindWithViewport(width, height uint32) {
|
||||
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
|
||||
gl.Viewport(0, 0, int32(width), int32(height))
|
||||
}
|
||||
|
||||
// IsComplete returns true if OpenGL reports that the fbo is complete/usable.
|
||||
// Note that this function binds and then unbinds the fbo
|
||||
func (fbo *Framebuffer) IsComplete() bool {
|
||||
fbo.Bind()
|
||||
isComplete := gl.CheckFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE
|
||||
fbo.UnBind()
|
||||
return isComplete
|
||||
}
|
||||
|
||||
func (fbo *Framebuffer) HasColorAttachment() bool {
|
||||
return fbo.ColorAttachmentsCount > 0
|
||||
}
|
||||
|
||||
func (fbo *Framebuffer) HasDepthAttachment() bool {
|
||||
|
||||
for i := 0; i < len(fbo.Attachments); i++ {
|
||||
|
||||
a := &fbo.Attachments[i]
|
||||
if a.Format.IsDepthFormat() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (fbo *Framebuffer) NewColorAttachment(
|
||||
attachType FramebufferAttachmentType,
|
||||
attachFormat FramebufferAttachmentDataFormat,
|
||||
) {
|
||||
|
||||
if fbo.ColorAttachmentsCount == 8 {
|
||||
logging.ErrLog.Fatalf("failed creating color attachment for framebuffer due it already having %d attached\n", fbo.ColorAttachmentsCount)
|
||||
}
|
||||
|
||||
if !attachType.IsValid() {
|
||||
logging.ErrLog.Fatalf("failed creating color attachment for framebuffer due to unknown attachment type. Type=%d\n", attachType)
|
||||
}
|
||||
|
||||
if !attachFormat.IsColorFormat() {
|
||||
logging.ErrLog.Fatalf("failed creating color attachment for framebuffer due to attachment data format not being a valid color type. Data format=%d\n", attachFormat)
|
||||
}
|
||||
|
||||
a := FramebufferAttachment{
|
||||
Type: attachType,
|
||||
Format: attachFormat,
|
||||
}
|
||||
|
||||
fbo.Bind()
|
||||
|
||||
if attachType == FramebufferAttachmentType_Texture {
|
||||
|
||||
// Create texture
|
||||
gl.GenTextures(1, &a.Id)
|
||||
if a.Id == 0 {
|
||||
logging.ErrLog.Fatalf("failed to generate texture for framebuffer. GlError=%d\n", gl.GetError())
|
||||
}
|
||||
|
||||
gl.BindTexture(gl.TEXTURE_2D, a.Id)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, attachFormat.GlInternalFormat(), int32(fbo.Width), int32(fbo.Height), 0, attachFormat.GlFormat(), gl.UNSIGNED_BYTE, nil)
|
||||
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
|
||||
gl.BindTexture(gl.TEXTURE_2D, 0)
|
||||
|
||||
// Attach to fbo
|
||||
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0+fbo.ColorAttachmentsCount, gl.TEXTURE_2D, a.Id, 0)
|
||||
|
||||
} else if attachType == FramebufferAttachmentType_Renderbuffer {
|
||||
|
||||
// Create rbo
|
||||
gl.GenRenderbuffers(1, &a.Id)
|
||||
if a.Id == 0 {
|
||||
logging.ErrLog.Fatalf("failed to generate render buffer for framebuffer. GlError=%d\n", gl.GetError())
|
||||
}
|
||||
|
||||
gl.BindRenderbuffer(gl.RENDERBUFFER, a.Id)
|
||||
gl.RenderbufferStorage(gl.RENDERBUFFER, uint32(attachFormat.GlInternalFormat()), int32(fbo.Width), int32(fbo.Height))
|
||||
gl.BindRenderbuffer(gl.RENDERBUFFER, 0)
|
||||
|
||||
// Attach to fbo
|
||||
gl.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0+fbo.ColorAttachmentsCount, gl.RENDERBUFFER, a.Id)
|
||||
}
|
||||
|
||||
fbo.UnBind()
|
||||
fbo.ColorAttachmentsCount++
|
||||
fbo.ClearFlags |= gl.COLOR_BUFFER_BIT
|
||||
fbo.Attachments = append(fbo.Attachments, a)
|
||||
}
|
||||
|
||||
// SetNoColorBuffer sets the read and draw buffers of this fbo to 'NONE',
|
||||
// which tells the graphics driver that we don't want a color buffer for this fbo.
|
||||
//
|
||||
// This is required because normally an fbo must have a color buffer to be considered complete, but by
|
||||
// doing this we get marked as complete even without one.
|
||||
//
|
||||
// Usually used when you only care about some other buffer, like a depth buffer.
|
||||
func (fbo *Framebuffer) SetNoColorBuffer() {
|
||||
|
||||
if fbo.HasColorAttachment() {
|
||||
logging.ErrLog.Fatalf("failed SetNoColorBuffer because framebuffer already has a color attachment\n")
|
||||
}
|
||||
|
||||
fbo.Bind()
|
||||
gl.DrawBuffer(gl.NONE)
|
||||
gl.ReadBuffer(gl.NONE)
|
||||
fbo.UnBind()
|
||||
}
|
||||
|
||||
func (fbo *Framebuffer) NewDepthAttachment(
|
||||
attachType FramebufferAttachmentType,
|
||||
attachFormat FramebufferAttachmentDataFormat,
|
||||
) {
|
||||
|
||||
if fbo.HasDepthAttachment() {
|
||||
logging.ErrLog.Fatalf("failed creating depth attachment for framebuffer because a depth attachment already exists\n")
|
||||
}
|
||||
|
||||
if !attachType.IsValid() {
|
||||
logging.ErrLog.Fatalf("failed creating depth attachment for framebuffer due to unknown attachment type. Type=%d\n", attachType)
|
||||
}
|
||||
|
||||
if !attachFormat.IsDepthFormat() {
|
||||
logging.ErrLog.Fatalf("failed creating depth attachment for framebuffer due to attachment data format not being a valid depth-stencil type. Data format=%d\n", attachFormat)
|
||||
}
|
||||
|
||||
a := FramebufferAttachment{
|
||||
Type: attachType,
|
||||
Format: attachFormat,
|
||||
}
|
||||
|
||||
fbo.Bind()
|
||||
|
||||
if attachType == FramebufferAttachmentType_Texture {
|
||||
|
||||
// Create texture
|
||||
gl.GenTextures(1, &a.Id)
|
||||
if a.Id == 0 {
|
||||
logging.ErrLog.Fatalf("failed to generate texture for framebuffer. GlError=%d\n", gl.GetError())
|
||||
}
|
||||
|
||||
gl.BindTexture(gl.TEXTURE_2D, a.Id)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, attachFormat.GlInternalFormat(), int32(fbo.Width), int32(fbo.Height), 0, attachFormat.GlFormat(), 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)
|
||||
|
||||
// This is so that any sampling outside the depth map gives a full depth value.
|
||||
// Useful for example when doing shadow maps where we want things outside
|
||||
// the range of the texture to not show shadow
|
||||
borderColor := []float32{1, 1, 1, 1}
|
||||
gl.TexParameterfv(gl.TEXTURE_2D, gl.TEXTURE_BORDER_COLOR, &borderColor[0])
|
||||
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)
|
||||
|
||||
gl.BindTexture(gl.TEXTURE_2D, 0)
|
||||
|
||||
// Attach to fbo
|
||||
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, a.Id, 0)
|
||||
|
||||
} else if attachType == FramebufferAttachmentType_Renderbuffer {
|
||||
|
||||
// Create rbo
|
||||
gl.GenRenderbuffers(1, &a.Id)
|
||||
if a.Id == 0 {
|
||||
logging.ErrLog.Fatalf("failed to generate render buffer for framebuffer. GlError=%d\n", gl.GetError())
|
||||
}
|
||||
|
||||
gl.BindRenderbuffer(gl.RENDERBUFFER, a.Id)
|
||||
gl.RenderbufferStorage(gl.RENDERBUFFER, uint32(attachFormat.GlInternalFormat()), int32(fbo.Width), int32(fbo.Height))
|
||||
gl.BindRenderbuffer(gl.RENDERBUFFER, 0)
|
||||
|
||||
// Attach to fbo
|
||||
gl.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, a.Id)
|
||||
}
|
||||
|
||||
fbo.UnBind()
|
||||
fbo.ClearFlags |= gl.DEPTH_BUFFER_BIT
|
||||
fbo.Attachments = append(fbo.Attachments, a)
|
||||
}
|
||||
|
||||
func (fbo *Framebuffer) NewDepthStencilAttachment(
|
||||
attachType FramebufferAttachmentType,
|
||||
attachFormat FramebufferAttachmentDataFormat,
|
||||
) {
|
||||
|
||||
if fbo.HasDepthAttachment() {
|
||||
logging.ErrLog.Fatalf("failed creating depth-stencil attachment for framebuffer because a depth-stencil attachment already exists\n")
|
||||
}
|
||||
|
||||
if !attachType.IsValid() {
|
||||
logging.ErrLog.Fatalf("failed creating depth-stencil attachment for framebuffer due to unknown attachment type. Type=%d\n", attachType)
|
||||
}
|
||||
|
||||
if !attachFormat.IsDepthFormat() {
|
||||
logging.ErrLog.Fatalf("failed creating depth-stencil attachment for framebuffer due to attachment data format not being a valid depth-stencil type. Data format=%d\n", attachFormat)
|
||||
}
|
||||
|
||||
a := FramebufferAttachment{
|
||||
Type: attachType,
|
||||
Format: attachFormat,
|
||||
}
|
||||
|
||||
fbo.Bind()
|
||||
|
||||
if attachType == FramebufferAttachmentType_Texture {
|
||||
|
||||
// Create texture
|
||||
gl.GenTextures(1, &a.Id)
|
||||
if a.Id == 0 {
|
||||
logging.ErrLog.Fatalf("failed to generate texture for framebuffer. GlError=%d\n", gl.GetError())
|
||||
}
|
||||
|
||||
gl.BindTexture(gl.TEXTURE_2D, a.Id)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, attachFormat.GlInternalFormat(), int32(fbo.Width), int32(fbo.Height), 0, attachFormat.GlFormat(), gl.UNSIGNED_INT_24_8, nil)
|
||||
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
|
||||
gl.BindTexture(gl.TEXTURE_2D, 0)
|
||||
|
||||
// Attach to fbo
|
||||
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.TEXTURE_2D, a.Id, 0)
|
||||
|
||||
} else if attachType == FramebufferAttachmentType_Renderbuffer {
|
||||
|
||||
// Create rbo
|
||||
gl.GenRenderbuffers(1, &a.Id)
|
||||
if a.Id == 0 {
|
||||
logging.ErrLog.Fatalf("failed to generate render buffer for framebuffer. GlError=%d\n", gl.GetError())
|
||||
}
|
||||
|
||||
gl.BindRenderbuffer(gl.RENDERBUFFER, a.Id)
|
||||
gl.RenderbufferStorage(gl.RENDERBUFFER, uint32(attachFormat.GlInternalFormat()), int32(fbo.Width), int32(fbo.Height))
|
||||
gl.BindRenderbuffer(gl.RENDERBUFFER, 0)
|
||||
|
||||
// Attach to fbo
|
||||
gl.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, a.Id)
|
||||
}
|
||||
|
||||
fbo.UnBind()
|
||||
fbo.ClearFlags |= gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT
|
||||
fbo.Attachments = append(fbo.Attachments, a)
|
||||
}
|
||||
|
||||
func (fbo *Framebuffer) Delete() {
|
||||
|
||||
if fbo.Id == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
gl.DeleteFramebuffers(1, &fbo.Id)
|
||||
fbo.Id = 0
|
||||
}
|
||||
|
||||
func NewFramebuffer(width, height uint32) Framebuffer {
|
||||
|
||||
// It is allowed to have attachments of differnt sizes in one FBO,
|
||||
// but that complicates things (e.g. which size to use for gl.viewport) and I don't see much use
|
||||
// for it now, so we will have all attachments share size
|
||||
fbo := Framebuffer{
|
||||
Width: width,
|
||||
Height: height,
|
||||
}
|
||||
|
||||
gl.GenFramebuffers(1, &fbo.Id)
|
||||
if fbo.Id == 0 {
|
||||
logging.ErrLog.Fatalf("failed to generate framebuffer. GlError=%d\n", gl.GetError())
|
||||
}
|
||||
|
||||
return fbo
|
||||
}
|
||||
456
main.go
456
main.go
@ -7,15 +7,15 @@ import (
|
||||
|
||||
imgui "github.com/AllenDang/cimgui-go"
|
||||
"github.com/bloeys/gglm/gglm"
|
||||
"github.com/bloeys/nmage/assert"
|
||||
"github.com/bloeys/nmage/assets"
|
||||
"github.com/bloeys/nmage/buffers"
|
||||
"github.com/bloeys/nmage/camera"
|
||||
"github.com/bloeys/nmage/engine"
|
||||
"github.com/bloeys/nmage/entity"
|
||||
"github.com/bloeys/nmage/input"
|
||||
"github.com/bloeys/nmage/logging"
|
||||
"github.com/bloeys/nmage/materials"
|
||||
"github.com/bloeys/nmage/meshes"
|
||||
"github.com/bloeys/nmage/registry"
|
||||
"github.com/bloeys/nmage/renderer/rend3dgl"
|
||||
"github.com/bloeys/nmage/timing"
|
||||
nmageimgui "github.com/bloeys/nmage/ui/imgui"
|
||||
@ -50,6 +50,28 @@ type DirLight struct {
|
||||
SpecularColor gglm.Vec3
|
||||
}
|
||||
|
||||
var (
|
||||
dirLightSize float32 = 30
|
||||
dirLightNear float32 = 0.1
|
||||
dirLightFar float32 = 30
|
||||
dirLightPos = gglm.NewVec3(0, 10, 0)
|
||||
)
|
||||
|
||||
func (d *DirLight) GetProjViewMat() gglm.Mat4 {
|
||||
|
||||
// Some arbitrary position for the directional light
|
||||
pos := dirLightPos
|
||||
|
||||
size := dirLightSize
|
||||
nearClip := dirLightNear
|
||||
farClip := dirLightFar
|
||||
|
||||
projMat := gglm.Ortho(-size, size, -size, size, nearClip, farClip).Mat4
|
||||
viewMat := gglm.LookAtRH(pos, pos.Clone().Add(d.Dir.Clone().Scale(10)), gglm.NewVec3(0, 1, 0)).Mat4
|
||||
|
||||
return *projMat.Mul(&viewMat)
|
||||
}
|
||||
|
||||
// Check https://wiki.ogre3d.org/tiki-index.php?page=-Point+Light+Attenuation for values
|
||||
type PointLight struct {
|
||||
Pos gglm.Vec3
|
||||
@ -99,10 +121,26 @@ var (
|
||||
yaw float32 = -1.5
|
||||
cam *camera.Camera
|
||||
|
||||
renderToDemoFbo = true
|
||||
renderToBackBuffer = true
|
||||
demoFboScale = gglm.NewVec2(0.25, 0.25)
|
||||
demoFboOffset = gglm.NewVec2(0.75, -0.75)
|
||||
demoFbo buffers.Framebuffer
|
||||
|
||||
renderToDepthMapFbo = true
|
||||
depthMapFboScale = gglm.NewVec2(0.25, 0.25)
|
||||
depthMapFboOffset = gglm.NewVec2(0.75, -0.2)
|
||||
depthMapFbo buffers.Framebuffer
|
||||
|
||||
screenQuadVao buffers.VertexArray
|
||||
screenQuadMat *materials.Material
|
||||
|
||||
unlitMat *materials.Material
|
||||
whiteMat *materials.Material
|
||||
containerMat *materials.Material
|
||||
palleteMat *materials.Material
|
||||
skyboxMat *materials.Material
|
||||
depthMapMat *materials.Material
|
||||
debugDepthMat *materials.Material
|
||||
|
||||
cubeMesh *meshes.Mesh
|
||||
@ -112,8 +150,8 @@ var (
|
||||
|
||||
cubeModelMat = gglm.NewTrMatId()
|
||||
|
||||
drawSkybox = true
|
||||
debugDrawDepthBuffer bool
|
||||
renderSkybox = true
|
||||
renderDepthBuffer bool
|
||||
|
||||
skyboxCmap assets.Cubemap
|
||||
|
||||
@ -124,9 +162,9 @@ var (
|
||||
|
||||
// Lights
|
||||
dirLight = DirLight{
|
||||
Dir: *gglm.NewVec3(0, -0.8, 0.2).Normalize(),
|
||||
DiffuseColor: *gglm.NewVec3(0, 0, 0),
|
||||
SpecularColor: *gglm.NewVec3(0, 0, 0),
|
||||
Dir: *gglm.NewVec3(0, -0.5, -0.8).Normalize(),
|
||||
DiffuseColor: *gglm.NewVec3(1, 1, 1),
|
||||
SpecularColor: *gglm.NewVec3(1, 1, 1),
|
||||
}
|
||||
pointLights = [...]PointLight{
|
||||
{
|
||||
@ -177,64 +215,14 @@ var (
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
WinWidth int32
|
||||
WinHeight int32
|
||||
Win *engine.Window
|
||||
ImGUIInfo nmageimgui.ImguiInfo
|
||||
}
|
||||
|
||||
type TransformComp struct {
|
||||
entity.BaseComp
|
||||
|
||||
Pos *gglm.Vec3
|
||||
Rot *gglm.Quat
|
||||
Scale *gglm.Vec3
|
||||
}
|
||||
|
||||
func (t *TransformComp) Name() string {
|
||||
return "Transform Component"
|
||||
}
|
||||
|
||||
func Test() {
|
||||
|
||||
// lvl := level.NewLevel("test level")
|
||||
testRegistry := registry.NewRegistry[int](100)
|
||||
|
||||
e1, e1Handle := testRegistry.New()
|
||||
e1CompContainer := entity.NewCompContainer()
|
||||
fmt.Printf("Entity 1: %+v; Handle: %+v; Index: %+v; Gen: %+v; Flags: %+v\n", e1, e1Handle, e1Handle.Index(), e1Handle.Generation(), e1Handle.Flags())
|
||||
|
||||
trComp := entity.GetComp[*TransformComp](&e1CompContainer)
|
||||
fmt.Println("Get comp before adding any:", trComp)
|
||||
|
||||
entity.AddComp(e1Handle, &e1CompContainer, &TransformComp{
|
||||
Pos: gglm.NewVec3(0, 0, 0),
|
||||
Rot: gglm.NewQuatEulerXYZ(0, 0, 0),
|
||||
Scale: gglm.NewVec3(0, 0, 0),
|
||||
})
|
||||
trComp = entity.GetComp[*TransformComp](&e1CompContainer)
|
||||
fmt.Println("Get transform comp:", trComp)
|
||||
|
||||
e2, e2Handle := testRegistry.New()
|
||||
e3, e3Handle := testRegistry.New()
|
||||
e4, e4Handle := testRegistry.New()
|
||||
fmt.Printf("Entity 2: %+v; Handle: %+v; Index: %+v; Gen: %+v; Flags: %+v\n", e2, e2Handle, e2Handle.Index(), e2Handle.Generation(), e2Handle.Flags())
|
||||
fmt.Printf("Entity 3: %+v; Handle: %+v; Index: %+v; Gen: %+v; Flags: %+v\n", e3, e3Handle, e3Handle.Index(), e3Handle.Generation(), e3Handle.Flags())
|
||||
fmt.Printf("Entity 4: %+v; Handle: %+v; Index: %+v; Gen: %+v; Flags: %+v\n", e4, e4Handle, e4Handle.Index(), e4Handle.Generation(), e4Handle.Flags())
|
||||
|
||||
*e2 = 1000
|
||||
fmt.Printf("Entity 2 value after registry get: %+v\n", *testRegistry.Get(e2Handle))
|
||||
|
||||
testRegistry.Free(e2Handle)
|
||||
fmt.Printf("Entity 2 value after free: %+v\n", testRegistry.Get(e2Handle))
|
||||
|
||||
e5, e5Handle := testRegistry.New()
|
||||
fmt.Printf("Entity 5: %+v; Handle: %+v; Index: %+v; Gen: %+v; Flags: %+v\n", e5, e5Handle, e5Handle.Index(), e5Handle.Generation(), e5Handle.Flags())
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
// Test()
|
||||
// return
|
||||
|
||||
//Init engine
|
||||
err := engine.Init()
|
||||
if err != nil {
|
||||
@ -255,6 +243,8 @@ func main() {
|
||||
|
||||
game := &Game{
|
||||
Win: window,
|
||||
WinWidth: int32(unscaledWindowWidth * dpiScaling),
|
||||
WinHeight: int32(unscaledWindowHeight * dpiScaling),
|
||||
ImGUIInfo: nmageimgui.NewImGui("./res/shaders/imgui.glsl"),
|
||||
}
|
||||
window.EventCallbacks = append(window.EventCallbacks, game.handleWindowEvents)
|
||||
@ -268,13 +258,12 @@ func (g *Game) handleWindowEvents(e sdl.Event) {
|
||||
case *sdl.WindowEvent:
|
||||
if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED {
|
||||
|
||||
width := e.Data1
|
||||
height := e.Data2
|
||||
cam.AspectRatio = float32(width) / float32(height)
|
||||
cam.Update()
|
||||
g.WinWidth = e.Data1
|
||||
g.WinHeight = e.Data2
|
||||
cam.AspectRatio = float32(g.WinWidth) / float32(g.WinHeight)
|
||||
|
||||
palleteMat.SetUnifMat4("projMat", &cam.ProjMat)
|
||||
debugDepthMat.SetUnifMat4("projMat", &cam.ProjMat)
|
||||
cam.Update()
|
||||
updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -385,23 +374,33 @@ func (g *Game) Init() {
|
||||
logging.ErrLog.Fatalln("Failed to load cubemap. Err: ", err)
|
||||
}
|
||||
|
||||
//
|
||||
// Create materials and assign any unused texture slots to black
|
||||
//
|
||||
screenQuadMat = materials.NewMaterial("Screen Quad Mat", "./res/shaders/screen-quad.glsl")
|
||||
screenQuadMat.SetUnifVec2("scale", demoFboScale)
|
||||
screenQuadMat.SetUnifVec2("offset", demoFboOffset)
|
||||
screenQuadMat.SetUnifInt32("material.diffuse", int32(materials.TextureSlot_Diffuse))
|
||||
|
||||
unlitMat = materials.NewMaterial("Unlit mat", "./res/shaders/simple-unlit.glsl")
|
||||
unlitMat.SetUnifInt32("material.diffuse", int32(materials.TextureSlot_Diffuse))
|
||||
|
||||
whiteMat = materials.NewMaterial("White mat", "./res/shaders/simple.glsl")
|
||||
whiteMat.Shininess = 64
|
||||
whiteMat.DiffuseTex = whiteTex.TexID
|
||||
whiteMat.SpecularTex = blackTex.TexID
|
||||
whiteMat.NormalTex = blackTex.TexID
|
||||
whiteMat.EmissionTex = blackTex.TexID
|
||||
whiteMat.SetUnifInt32("material.diffuse", 0)
|
||||
whiteMat.SetUnifInt32("material.specular", 1)
|
||||
// whiteMat.SetUnifInt32("material.normal", 2)
|
||||
whiteMat.SetUnifInt32("material.emission", 3)
|
||||
whiteMat.SetUnifMat4("projMat", &cam.ProjMat)
|
||||
whiteMat.SetUnifInt32("material.diffuse", int32(materials.TextureSlot_Diffuse))
|
||||
whiteMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
|
||||
// whiteMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
|
||||
whiteMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
|
||||
whiteMat.SetUnifVec3("ambientColor", ambientColor)
|
||||
whiteMat.SetUnifFloat32("material.shininess", whiteMat.Shininess)
|
||||
whiteMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
|
||||
whiteMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
|
||||
whiteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
|
||||
whiteMat.SetUnifInt32("dirLight.shadowMap", int32(materials.TextureSlot_ShadowMap))
|
||||
|
||||
containerMat = materials.NewMaterial("Container mat", "./res/shaders/simple.glsl")
|
||||
containerMat.Shininess = 64
|
||||
@ -409,16 +408,16 @@ func (g *Game) Init() {
|
||||
containerMat.SpecularTex = containerSpecularTex.TexID
|
||||
containerMat.NormalTex = blackTex.TexID
|
||||
containerMat.EmissionTex = blackTex.TexID
|
||||
containerMat.SetUnifInt32("material.diffuse", 0)
|
||||
containerMat.SetUnifInt32("material.specular", 1)
|
||||
// containerMat.SetUnifInt32("material.normal", 2)
|
||||
containerMat.SetUnifInt32("material.emission", 3)
|
||||
containerMat.SetUnifMat4("projMat", &cam.ProjMat)
|
||||
containerMat.SetUnifInt32("material.diffuse", int32(materials.TextureSlot_Diffuse))
|
||||
containerMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
|
||||
// containerMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
|
||||
containerMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
|
||||
containerMat.SetUnifVec3("ambientColor", ambientColor)
|
||||
containerMat.SetUnifFloat32("material.shininess", containerMat.Shininess)
|
||||
containerMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
|
||||
containerMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
|
||||
containerMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
|
||||
containerMat.SetUnifInt32("dirLight.shadowMap", int32(materials.TextureSlot_ShadowMap))
|
||||
|
||||
palleteMat = materials.NewMaterial("Pallete mat", "./res/shaders/simple.glsl")
|
||||
palleteMat.Shininess = 64
|
||||
@ -426,33 +425,83 @@ func (g *Game) Init() {
|
||||
palleteMat.SpecularTex = blackTex.TexID
|
||||
palleteMat.NormalTex = blackTex.TexID
|
||||
palleteMat.EmissionTex = blackTex.TexID
|
||||
palleteMat.SetUnifInt32("material.diffuse", 0)
|
||||
palleteMat.SetUnifInt32("material.specular", 1)
|
||||
// palleteMat.SetUnifInt32("material.normal", 2)
|
||||
palleteMat.SetUnifInt32("material.emission", 3)
|
||||
palleteMat.SetUnifMat4("projMat", &cam.ProjMat)
|
||||
palleteMat.SetUnifInt32("material.diffuse", int32(materials.TextureSlot_Diffuse))
|
||||
palleteMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
|
||||
// palleteMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
|
||||
palleteMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
|
||||
palleteMat.SetUnifVec3("ambientColor", ambientColor)
|
||||
palleteMat.SetUnifFloat32("material.shininess", palleteMat.Shininess)
|
||||
palleteMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
|
||||
palleteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
|
||||
palleteMat.SetUnifInt32("dirLight.shadowMap", int32(materials.TextureSlot_ShadowMap))
|
||||
|
||||
debugDepthMat = materials.NewMaterial("Debug depth mat", "./res/shaders/debug-depth.glsl")
|
||||
debugDepthMat.SetUnifMat4("projMat", &cam.ProjMat)
|
||||
|
||||
depthMapMat = materials.NewMaterial("Depth Map mat", "./res/shaders/depth-map.glsl")
|
||||
|
||||
skyboxMat = materials.NewMaterial("Skybox mat", "./res/shaders/skybox.glsl")
|
||||
skyboxMat.CubemapTex = skyboxCmap.TexID
|
||||
skyboxMat.SetUnifInt32("skybox", int32(materials.TextureSlot_Cubemap))
|
||||
|
||||
// Movement, scale and rotation
|
||||
// Cube model mat
|
||||
translationMat := gglm.NewTranslationMat(gglm.NewVec3(0, 0, 0))
|
||||
scaleMat := gglm.NewScaleMat(gglm.NewVec3(1, 1, 1))
|
||||
rotMat := gglm.NewRotMat(gglm.NewQuatEuler(gglm.NewVec3(-90, -90, 0).AsRad()))
|
||||
cubeModelMat.Mul(translationMat.Mul(rotMat.Mul(scaleMat)))
|
||||
|
||||
// Screen quad vao setup.
|
||||
// We don't actually care about the values here because the quad is hardcoded in the shader,
|
||||
// but we just want to have a vao with 6 vertices and uv0 so opengl can be called properly
|
||||
screenQuadVbo := buffers.NewVertexBuffer(buffers.Element{ElementType: buffers.DataTypeVec3}, buffers.Element{ElementType: buffers.DataTypeVec2})
|
||||
screenQuadVbo.SetData(make([]float32, 6), buffers.BufUsage_Static)
|
||||
screenQuadVao = buffers.NewVertexArray()
|
||||
screenQuadVao.AddVertexBuffer(screenQuadVbo)
|
||||
|
||||
// Fbos and lights
|
||||
g.initFbos()
|
||||
g.updateLights()
|
||||
updateViewMat()
|
||||
|
||||
// Initial camera update
|
||||
cam.Update()
|
||||
updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
|
||||
}
|
||||
|
||||
func (g *Game) initFbos() {
|
||||
|
||||
// Demo fbo
|
||||
demoFbo = buffers.NewFramebuffer(uint32(g.WinWidth), uint32(g.WinHeight))
|
||||
|
||||
demoFbo.NewColorAttachment(
|
||||
buffers.FramebufferAttachmentType_Texture,
|
||||
buffers.FramebufferAttachmentDataFormat_SRGBA,
|
||||
)
|
||||
|
||||
demoFbo.NewDepthStencilAttachment(
|
||||
buffers.FramebufferAttachmentType_Renderbuffer,
|
||||
buffers.FramebufferAttachmentDataFormat_Depth24Stencil8,
|
||||
)
|
||||
|
||||
assert.T(demoFbo.IsComplete(), "Demo fbo is not complete after init")
|
||||
|
||||
// Depth map fbo
|
||||
depthMapFbo = buffers.NewFramebuffer(1024, 1024)
|
||||
depthMapFbo.SetNoColorBuffer()
|
||||
depthMapFbo.NewDepthAttachment(
|
||||
buffers.FramebufferAttachmentType_Texture,
|
||||
buffers.FramebufferAttachmentDataFormat_DepthF32,
|
||||
)
|
||||
|
||||
assert.T(depthMapFbo.IsComplete(), "Depth map fbo is not complete after init")
|
||||
}
|
||||
|
||||
func (g *Game) updateLights() {
|
||||
|
||||
// Directional light
|
||||
whiteMat.ShadowMap = depthMapFbo.Attachments[0].Id
|
||||
containerMat.ShadowMap = depthMapFbo.Attachments[0].Id
|
||||
palleteMat.ShadowMap = depthMapFbo.Attachments[0].Id
|
||||
|
||||
// Point lights
|
||||
for i := 0; i < len(pointLights); i++ {
|
||||
|
||||
pl := &pointLights[i]
|
||||
@ -483,6 +532,7 @@ func (g *Game) updateLights() {
|
||||
palleteMat.SetUnifFloat32(indexString+".quadratic", pl.Quadratic)
|
||||
}
|
||||
|
||||
// Spotlights
|
||||
for i := 0; i < len(spotLights); i++ {
|
||||
|
||||
l := &spotLights[i]
|
||||
@ -546,10 +596,12 @@ func (g *Game) showDebugWindow() {
|
||||
// Camera
|
||||
imgui.Text("Camera")
|
||||
if imgui.DragFloat3("Cam Pos", &cam.Pos.Data) {
|
||||
updateViewMat()
|
||||
cam.Update()
|
||||
updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
|
||||
}
|
||||
if imgui.DragFloat3("Cam Forward", &cam.Forward.Data) {
|
||||
updateViewMat()
|
||||
cam.Update()
|
||||
updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
|
||||
}
|
||||
|
||||
imgui.Spacing()
|
||||
@ -565,17 +617,6 @@ func (g *Game) showDebugWindow() {
|
||||
|
||||
imgui.Spacing()
|
||||
|
||||
// Specular
|
||||
imgui.Text("Specular Settings")
|
||||
|
||||
if imgui.DragFloat("Specular Shininess", &whiteMat.Shininess) {
|
||||
whiteMat.SetUnifFloat32("material.shininess", whiteMat.Shininess)
|
||||
containerMat.SetUnifFloat32("material.shininess", whiteMat.Shininess)
|
||||
palleteMat.SetUnifFloat32("material.shininess", whiteMat.Shininess)
|
||||
}
|
||||
|
||||
imgui.Spacing()
|
||||
|
||||
// Directional light
|
||||
imgui.Text("Directional Light")
|
||||
|
||||
@ -597,6 +638,22 @@ func (g *Game) showDebugWindow() {
|
||||
palleteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
|
||||
}
|
||||
|
||||
imgui.DragFloat3("dPos", &dirLightPos.Data)
|
||||
imgui.DragFloat("dSize", &dirLightSize)
|
||||
imgui.DragFloat("dNear", &dirLightNear)
|
||||
imgui.DragFloat("dFar", &dirLightFar)
|
||||
|
||||
imgui.Spacing()
|
||||
|
||||
// Specular
|
||||
imgui.Text("Specular Settings")
|
||||
|
||||
if imgui.DragFloat("Specular Shininess", &whiteMat.Shininess) {
|
||||
whiteMat.SetUnifFloat32("material.shininess", whiteMat.Shininess)
|
||||
containerMat.SetUnifFloat32("material.shininess", whiteMat.Shininess)
|
||||
palleteMat.SetUnifFloat32("material.shininess", whiteMat.Shininess)
|
||||
}
|
||||
|
||||
imgui.Spacing()
|
||||
|
||||
// Point lights
|
||||
@ -693,11 +750,24 @@ func (g *Game) showDebugWindow() {
|
||||
imgui.EndListBox()
|
||||
}
|
||||
|
||||
// Demo fbo
|
||||
imgui.Text("Demo Framebuffer")
|
||||
imgui.Checkbox("Render to demo FBO", &renderToDemoFbo)
|
||||
imgui.DragFloat2("Scale##0", &demoFboScale.Data)
|
||||
imgui.DragFloat2("Offset##0", &demoFboOffset.Data)
|
||||
|
||||
// Depth map fbo
|
||||
imgui.Text("Depth Map Framebuffer")
|
||||
imgui.Checkbox("Render to depth map FBO", &renderToDepthMapFbo)
|
||||
imgui.DragFloat2("Scale##1", &depthMapFboScale.Data)
|
||||
imgui.DragFloat2("Offset##1", &depthMapFboOffset.Data)
|
||||
|
||||
// Other
|
||||
imgui.Text("Other Settings")
|
||||
|
||||
imgui.Checkbox("Draw Skybox", &drawSkybox)
|
||||
imgui.Checkbox("Debug depth buffer", &debugDrawDepthBuffer)
|
||||
imgui.Checkbox("Render skybox", &renderSkybox)
|
||||
imgui.Checkbox("Render to back buffer", &renderToBackBuffer)
|
||||
imgui.Checkbox("Render depth buffer", &renderDepthBuffer)
|
||||
|
||||
imgui.End()
|
||||
}
|
||||
@ -725,7 +795,7 @@ func (g *Game) updateCameraLookAround() {
|
||||
// Update cam forward
|
||||
cam.UpdateRotation(pitch, yaw)
|
||||
|
||||
updateViewMat()
|
||||
updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
|
||||
}
|
||||
|
||||
func (g *Game) updateCameraPos() {
|
||||
@ -756,81 +826,159 @@ func (g *Game) updateCameraPos() {
|
||||
}
|
||||
|
||||
if update {
|
||||
updateViewMat()
|
||||
cam.Update()
|
||||
updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) Render() {
|
||||
|
||||
dirLightProjViewMat := dirLight.GetProjViewMat()
|
||||
|
||||
// Set some uniforms
|
||||
whiteMat.SetUnifVec3("camPos", &cam.Pos)
|
||||
whiteMat.SetUnifMat4("dirLightProjViewMat", &dirLightProjViewMat)
|
||||
|
||||
containerMat.SetUnifVec3("camPos", &cam.Pos)
|
||||
containerMat.SetUnifMat4("dirLightProjViewMat", &dirLightProjViewMat)
|
||||
|
||||
palleteMat.SetUnifVec3("camPos", &cam.Pos)
|
||||
palleteMat.SetUnifMat4("dirLightProjViewMat", &dirLightProjViewMat)
|
||||
|
||||
depthMapMat.SetUnifMat4("projViewMat", &dirLightProjViewMat)
|
||||
|
||||
//
|
||||
// Render depth map for shadows
|
||||
//
|
||||
depthMapFbo.BindWithViewport()
|
||||
depthMapFbo.Clear()
|
||||
|
||||
// Culling front faces helps 'peter panning' when
|
||||
// drawing shadow maps, but works only for solids with a back face (i.e. quads won't cast shadows).
|
||||
// Check more here: https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping
|
||||
//
|
||||
// Some note that this is too troublesome and fails in many cases. Might be better to remove.
|
||||
gl.CullFace(gl.FRONT)
|
||||
g.RenderScene(depthMapMat)
|
||||
gl.CullFace(gl.BACK)
|
||||
|
||||
depthMapFbo.UnBindWithViewport(uint32(g.WinWidth), uint32(g.WinHeight))
|
||||
|
||||
if renderToDepthMapFbo {
|
||||
screenQuadMat.DiffuseTex = depthMapFbo.Attachments[0].Id
|
||||
screenQuadMat.SetUnifVec2("offset", depthMapFboOffset)
|
||||
screenQuadMat.SetUnifVec2("scale", depthMapFboScale)
|
||||
screenQuadMat.Bind()
|
||||
window.Rend.DrawVertexArray(screenQuadMat, &screenQuadVao, 0, 6)
|
||||
}
|
||||
|
||||
if renderToBackBuffer {
|
||||
|
||||
if renderDepthBuffer {
|
||||
g.RenderScene(debugDepthMat)
|
||||
} else {
|
||||
g.RenderScene(nil)
|
||||
}
|
||||
}
|
||||
|
||||
if renderSkybox {
|
||||
g.DrawSkybox()
|
||||
}
|
||||
|
||||
if renderToDemoFbo {
|
||||
|
||||
demoFbo.Bind()
|
||||
demoFbo.Clear()
|
||||
|
||||
if renderDepthBuffer {
|
||||
g.RenderScene(debugDepthMat)
|
||||
} else {
|
||||
g.RenderScene(nil)
|
||||
}
|
||||
|
||||
if renderSkybox {
|
||||
g.DrawSkybox()
|
||||
}
|
||||
|
||||
demoFbo.UnBind()
|
||||
|
||||
screenQuadMat.DiffuseTex = demoFbo.Attachments[0].Id
|
||||
screenQuadMat.SetUnifVec2("offset", demoFboOffset)
|
||||
screenQuadMat.SetUnifVec2("scale", demoFboScale)
|
||||
|
||||
window.Rend.DrawVertexArray(screenQuadMat, &screenQuadVao, 0, 6)
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
rotatingCubeSpeedDeg1 float32 = 45
|
||||
rotatingCubeSpeedDeg2 float32 = 90
|
||||
rotatingCubeTrMat1 = *gglm.NewTrMatId().Translate(gglm.NewVec3(-4, -1, 4))
|
||||
rotatingCubeTrMat2 = *gglm.NewTrMatId().Translate(gglm.NewVec3(-1, 0.5, 4))
|
||||
)
|
||||
|
||||
func (g *Game) RenderScene(overrideMat *materials.Material) {
|
||||
|
||||
tempModelMatrix := cubeModelMat.Clone()
|
||||
|
||||
whiteMat.SetUnifVec3("camPos", &cam.Pos)
|
||||
containerMat.SetUnifVec3("camPos", &cam.Pos)
|
||||
palleteMat.SetUnifVec3("camPos", &cam.Pos)
|
||||
|
||||
// See if we need overrides
|
||||
sunMat := palleteMat
|
||||
chairMat := palleteMat
|
||||
cubeMat := containerMat
|
||||
if debugDrawDepthBuffer {
|
||||
sunMat = debugDepthMat
|
||||
chairMat = debugDepthMat
|
||||
cubeMat = debugDepthMat
|
||||
|
||||
if overrideMat != nil {
|
||||
sunMat = overrideMat
|
||||
chairMat = overrideMat
|
||||
cubeMat = overrideMat
|
||||
}
|
||||
|
||||
// Draw dir light
|
||||
window.Rend.Draw(sphereMesh, gglm.NewTrMatId().Translate(gglm.NewVec3(0, 10, 0)).Scale(gglm.NewVec3(0.1, 0.1, 0.1)), sunMat)
|
||||
window.Rend.DrawMesh(sphereMesh, gglm.NewTrMatId().Translate(gglm.NewVec3(0, 10, 0)).Scale(gglm.NewVec3(0.1, 0.1, 0.1)), sunMat)
|
||||
|
||||
// Draw point lights
|
||||
for i := 0; i < len(pointLights); i++ {
|
||||
|
||||
pl := &pointLights[i]
|
||||
window.Rend.Draw(cubeMesh, gglm.NewTrMatId().Translate(&pl.Pos).Scale(gglm.NewVec3(0.1, 0.1, 0.1)), sunMat)
|
||||
window.Rend.DrawMesh(cubeMesh, gglm.NewTrMatId().Translate(&pl.Pos).Scale(gglm.NewVec3(0.1, 0.1, 0.1)), sunMat)
|
||||
}
|
||||
|
||||
// Chair
|
||||
window.Rend.Draw(chairMesh, tempModelMatrix, chairMat)
|
||||
window.Rend.DrawMesh(chairMesh, tempModelMatrix, chairMat)
|
||||
|
||||
// Ground
|
||||
window.Rend.Draw(cubeMesh, gglm.NewTrMatId().Translate(gglm.NewVec3(0, -3, 0)).Scale(gglm.NewVec3(20, 1, 20)), cubeMat)
|
||||
window.Rend.DrawMesh(cubeMesh, gglm.NewTrMatId().Translate(gglm.NewVec3(0, -3, 0)).Scale(gglm.NewVec3(20, 1, 20)), cubeMat)
|
||||
|
||||
// Cubes
|
||||
rowSize := 1
|
||||
for y := 0; y < rowSize; y++ {
|
||||
for x := 0; x < rowSize; x++ {
|
||||
tempModelMatrix.Translate(gglm.NewVec3(-6, 0, 0))
|
||||
window.Rend.Draw(cubeMesh, tempModelMatrix, cubeMat)
|
||||
}
|
||||
tempModelMatrix.Translate(gglm.NewVec3(float32(rowSize), -1, 0))
|
||||
}
|
||||
tempModelMatrix.Translate(gglm.NewVec3(-6, 0, 0))
|
||||
window.Rend.DrawMesh(cubeMesh, tempModelMatrix, cubeMat)
|
||||
|
||||
if drawSkybox {
|
||||
g.DrawSkybox()
|
||||
}
|
||||
tempModelMatrix.Translate(gglm.NewVec3(0, -1, -4))
|
||||
window.Rend.DrawMesh(cubeMesh, tempModelMatrix, cubeMat)
|
||||
|
||||
// Rotating cubes
|
||||
rotatingCubeTrMat1.Rotate(rotatingCubeSpeedDeg1*gglm.Deg2Rad*timing.DT(), gglm.NewVec3(0, 1, 0))
|
||||
window.Rend.DrawMesh(cubeMesh, &rotatingCubeTrMat1, cubeMat)
|
||||
|
||||
rotatingCubeTrMat2.Rotate(rotatingCubeSpeedDeg2*gglm.Deg2Rad*timing.DT(), gglm.NewVec3(1, 1, 0))
|
||||
window.Rend.DrawMesh(cubeMesh, &rotatingCubeTrMat2, cubeMat)
|
||||
|
||||
// Cubes generator
|
||||
// rowSize := 1
|
||||
// for y := 0; y < rowSize; y++ {
|
||||
// for x := 0; x < rowSize; x++ {
|
||||
// tempModelMatrix.Translate(gglm.NewVec3(-6, 0, 0))
|
||||
// window.Rend.DrawMesh(cubeMesh, tempModelMatrix, cubeMat)
|
||||
// }
|
||||
// tempModelMatrix.Translate(gglm.NewVec3(float32(rowSize), -1, 0))
|
||||
// }
|
||||
}
|
||||
|
||||
func (g *Game) DrawSkybox() {
|
||||
|
||||
gl.Disable(gl.CULL_FACE)
|
||||
gl.DepthFunc(gl.LEQUAL)
|
||||
skyboxMesh.Vao.Bind()
|
||||
skyboxMat.Bind()
|
||||
gl.ActiveTexture(gl.TEXTURE0)
|
||||
gl.BindTexture(gl.TEXTURE_CUBE_MAP, skyboxCmap.TexID)
|
||||
|
||||
viewMat := cam.ViewMat.Clone()
|
||||
viewMat.Set(0, 3, 0)
|
||||
viewMat.Set(1, 3, 0)
|
||||
viewMat.Set(2, 3, 0)
|
||||
viewMat.Set(3, 0, 0)
|
||||
viewMat.Set(3, 1, 0)
|
||||
viewMat.Set(3, 2, 0)
|
||||
viewMat.Set(3, 3, 0)
|
||||
|
||||
skyboxMat.SetUnifMat4("viewMat", viewMat)
|
||||
skyboxMat.SetUnifMat4("projMat", &cam.ProjMat)
|
||||
for i := 0; i < len(skyboxMesh.SubMeshes); i++ {
|
||||
gl.DrawElementsBaseVertexWithOffset(gl.TRIANGLES, skyboxMesh.SubMeshes[i].IndexCount, gl.UNSIGNED_INT, uintptr(skyboxMesh.SubMeshes[i].BaseIndex), skyboxMesh.SubMeshes[i].BaseVertex)
|
||||
}
|
||||
window.Rend.DrawCubemap(skyboxMesh, skyboxMat)
|
||||
|
||||
gl.DepthFunc(gl.LESS)
|
||||
gl.Enable(gl.CULL_FACE)
|
||||
@ -843,10 +991,24 @@ func (g *Game) DeInit() {
|
||||
g.Win.Destroy()
|
||||
}
|
||||
|
||||
func updateViewMat() {
|
||||
cam.Update()
|
||||
whiteMat.SetUnifMat4("viewMat", &cam.ViewMat)
|
||||
containerMat.SetUnifMat4("viewMat", &cam.ViewMat)
|
||||
palleteMat.SetUnifMat4("viewMat", &cam.ViewMat)
|
||||
debugDepthMat.SetUnifMat4("viewMat", &cam.ViewMat)
|
||||
func updateAllProjViewMats(projMat, viewMat gglm.Mat4) {
|
||||
|
||||
projViewMat := projMat.Clone().Mul(&viewMat)
|
||||
|
||||
unlitMat.SetUnifMat4("projViewMat", projViewMat)
|
||||
whiteMat.SetUnifMat4("projViewMat", projViewMat)
|
||||
containerMat.SetUnifMat4("projViewMat", projViewMat)
|
||||
palleteMat.SetUnifMat4("projViewMat", projViewMat)
|
||||
debugDepthMat.SetUnifMat4("projViewMat", projViewMat)
|
||||
|
||||
// Update skybox projViewMat
|
||||
skyboxViewMat := viewMat.Clone()
|
||||
skyboxViewMat.Set(0, 3, 0)
|
||||
skyboxViewMat.Set(1, 3, 0)
|
||||
skyboxViewMat.Set(2, 3, 0)
|
||||
skyboxViewMat.Set(3, 0, 0)
|
||||
skyboxViewMat.Set(3, 1, 0)
|
||||
skyboxViewMat.Set(3, 2, 0)
|
||||
skyboxViewMat.Set(3, 3, 0)
|
||||
skyboxMat.SetUnifMat4("projViewMat", projMat.Clone().Mul(skyboxViewMat))
|
||||
}
|
||||
|
||||
@ -8,6 +8,17 @@ import (
|
||||
"github.com/go-gl/gl/v4.1-core/gl"
|
||||
)
|
||||
|
||||
type TextureSlot uint32
|
||||
|
||||
const (
|
||||
TextureSlot_Diffuse TextureSlot = 0
|
||||
TextureSlot_Specular TextureSlot = 1
|
||||
TextureSlot_Normal TextureSlot = 2
|
||||
TextureSlot_Emission TextureSlot = 3
|
||||
TextureSlot_Cubemap TextureSlot = 10
|
||||
TextureSlot_ShadowMap TextureSlot = 11
|
||||
)
|
||||
|
||||
type Material struct {
|
||||
Name string
|
||||
ShaderProg shaders.ShaderProgram
|
||||
@ -21,41 +32,53 @@ type Material struct {
|
||||
NormalTex uint32
|
||||
EmissionTex uint32
|
||||
|
||||
// Shininess of specular highlights
|
||||
Shininess float32
|
||||
|
||||
// Cubemap
|
||||
CubemapTex uint32
|
||||
|
||||
// Shadowmaps
|
||||
ShadowMap uint32
|
||||
}
|
||||
|
||||
func (m *Material) Bind() {
|
||||
|
||||
gl.UseProgram(m.ShaderProg.ID)
|
||||
|
||||
gl.ActiveTexture(gl.TEXTURE0)
|
||||
gl.BindTexture(gl.TEXTURE_2D, m.DiffuseTex)
|
||||
if m.DiffuseTex != 0 {
|
||||
gl.ActiveTexture(uint32(gl.TEXTURE0 + TextureSlot_Diffuse))
|
||||
gl.BindTexture(gl.TEXTURE_2D, m.DiffuseTex)
|
||||
}
|
||||
|
||||
gl.ActiveTexture(gl.TEXTURE1)
|
||||
gl.BindTexture(gl.TEXTURE_2D, m.SpecularTex)
|
||||
if m.SpecularTex != 0 {
|
||||
gl.ActiveTexture(uint32(gl.TEXTURE0 + TextureSlot_Specular))
|
||||
gl.BindTexture(gl.TEXTURE_2D, m.SpecularTex)
|
||||
}
|
||||
|
||||
gl.ActiveTexture(gl.TEXTURE2)
|
||||
gl.BindTexture(gl.TEXTURE_2D, m.NormalTex)
|
||||
if m.NormalTex != 0 {
|
||||
gl.ActiveTexture(uint32(gl.TEXTURE0 + TextureSlot_Normal))
|
||||
gl.BindTexture(gl.TEXTURE_2D, m.NormalTex)
|
||||
}
|
||||
|
||||
gl.ActiveTexture(gl.TEXTURE3)
|
||||
gl.BindTexture(gl.TEXTURE_2D, m.EmissionTex)
|
||||
if m.EmissionTex != 0 {
|
||||
gl.ActiveTexture(uint32(gl.TEXTURE0 + TextureSlot_Emission))
|
||||
gl.BindTexture(gl.TEXTURE_2D, m.EmissionTex)
|
||||
}
|
||||
|
||||
if m.CubemapTex != 0 {
|
||||
gl.ActiveTexture(uint32(gl.TEXTURE0 + TextureSlot_Cubemap))
|
||||
gl.BindTexture(gl.TEXTURE_CUBE_MAP, m.CubemapTex)
|
||||
}
|
||||
|
||||
if m.ShadowMap != 0 {
|
||||
gl.ActiveTexture(uint32(gl.TEXTURE0 + TextureSlot_ShadowMap))
|
||||
gl.BindTexture(gl.TEXTURE_2D, m.ShadowMap)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Material) UnBind() {
|
||||
gl.UseProgram(0)
|
||||
|
||||
//TODO: Should we unbind textures here? Are these two lines needed?
|
||||
// gl.ActiveTexture(gl.TEXTURE0)
|
||||
// gl.BindTexture(gl.TEXTURE_2D, 0)
|
||||
|
||||
// gl.ActiveTexture(gl.TEXTURE1)
|
||||
// gl.BindTexture(gl.TEXTURE_2D, 0)
|
||||
|
||||
// gl.ActiveTexture(gl.TEXTURE2)
|
||||
// gl.BindTexture(gl.TEXTURE_2D, 0)
|
||||
|
||||
// gl.ActiveTexture(gl.TEXTURE3)
|
||||
// gl.BindTexture(gl.TEXTURE_2D, 0)
|
||||
}
|
||||
|
||||
func (m *Material) GetAttribLoc(attribName string) int32 {
|
||||
@ -132,7 +155,7 @@ func NewMaterial(matName, shaderPath string) *Material {
|
||||
|
||||
shdrProg, err := shaders.LoadAndCompileCombinedShader(shaderPath)
|
||||
if err != nil {
|
||||
logging.ErrLog.Fatalln("Failed to create new material. Err: ", err)
|
||||
logging.ErrLog.Fatalf("Failed to create new material '%s'. Err: %s\n", matName, err.Error())
|
||||
}
|
||||
|
||||
return &Material{Name: matName, ShaderProg: shdrProg, UnifLocs: make(map[string]int32), AttribLocs: make(map[string]int32)}
|
||||
@ -142,7 +165,7 @@ func NewMaterialSrc(matName string, shaderSrc []byte) *Material {
|
||||
|
||||
shdrProg, err := shaders.LoadAndCompileCombinedShaderSrc(shaderSrc)
|
||||
if err != nil {
|
||||
logging.ErrLog.Fatalln("Failed to create new material. Err: ", err)
|
||||
logging.ErrLog.Fatalf("Failed to create new material '%s'. Err: %s\n", matName, err.Error())
|
||||
}
|
||||
|
||||
return &Material{Name: matName, ShaderProg: shdrProg, UnifLocs: make(map[string]int32), AttribLocs: make(map[string]int32)}
|
||||
|
||||
@ -2,6 +2,7 @@ package rend3dgl
|
||||
|
||||
import (
|
||||
"github.com/bloeys/gglm/gglm"
|
||||
"github.com/bloeys/nmage/buffers"
|
||||
"github.com/bloeys/nmage/materials"
|
||||
"github.com/bloeys/nmage/meshes"
|
||||
"github.com/bloeys/nmage/renderer"
|
||||
@ -11,23 +12,56 @@ import (
|
||||
var _ renderer.Render = &Rend3DGL{}
|
||||
|
||||
type Rend3DGL struct {
|
||||
BoundVao *buffers.VertexArray
|
||||
BoundMesh *meshes.Mesh
|
||||
BoundMat *materials.Material
|
||||
}
|
||||
|
||||
func (r3d *Rend3DGL) Draw(mesh *meshes.Mesh, trMat *gglm.TrMat, mat *materials.Material) {
|
||||
func (r *Rend3DGL) DrawMesh(mesh *meshes.Mesh, modelMat *gglm.TrMat, mat *materials.Material) {
|
||||
|
||||
if mesh != r3d.BoundMesh {
|
||||
if mesh != r.BoundMesh {
|
||||
mesh.Vao.Bind()
|
||||
r3d.BoundMesh = mesh
|
||||
r.BoundMesh = mesh
|
||||
}
|
||||
|
||||
if mat != r3d.BoundMat {
|
||||
if mat != r.BoundMat {
|
||||
mat.Bind()
|
||||
r3d.BoundMat = mat
|
||||
r.BoundMat = mat
|
||||
}
|
||||
|
||||
mat.SetUnifMat4("modelMat", &trMat.Mat4)
|
||||
mat.SetUnifMat4("modelMat", &modelMat.Mat4)
|
||||
|
||||
for i := 0; i < len(mesh.SubMeshes); i++ {
|
||||
gl.DrawElementsBaseVertexWithOffset(gl.TRIANGLES, mesh.SubMeshes[i].IndexCount, gl.UNSIGNED_INT, uintptr(mesh.SubMeshes[i].BaseIndex), mesh.SubMeshes[i].BaseVertex)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Rend3DGL) DrawVertexArray(mat *materials.Material, vao *buffers.VertexArray, firstElement int32, elementCount int32) {
|
||||
|
||||
if vao != r.BoundVao {
|
||||
vao.Bind()
|
||||
r.BoundVao = vao
|
||||
}
|
||||
|
||||
if mat != r.BoundMat {
|
||||
mat.Bind()
|
||||
r.BoundMat = mat
|
||||
}
|
||||
|
||||
gl.DrawArrays(gl.TRIANGLES, firstElement, elementCount)
|
||||
}
|
||||
|
||||
func (r *Rend3DGL) DrawCubemap(mesh *meshes.Mesh, mat *materials.Material) {
|
||||
|
||||
if mesh != r.BoundMesh {
|
||||
mesh.Vao.Bind()
|
||||
r.BoundMesh = mesh
|
||||
}
|
||||
|
||||
if mat != r.BoundMat {
|
||||
mat.Bind()
|
||||
r.BoundMat = mat
|
||||
}
|
||||
|
||||
for i := 0; i < len(mesh.SubMeshes); i++ {
|
||||
gl.DrawElementsBaseVertexWithOffset(gl.TRIANGLES, mesh.SubMeshes[i].IndexCount, gl.UNSIGNED_INT, uintptr(mesh.SubMeshes[i].BaseIndex), mesh.SubMeshes[i].BaseVertex)
|
||||
|
||||
@ -2,11 +2,14 @@ package renderer
|
||||
|
||||
import (
|
||||
"github.com/bloeys/gglm/gglm"
|
||||
"github.com/bloeys/nmage/buffers"
|
||||
"github.com/bloeys/nmage/materials"
|
||||
"github.com/bloeys/nmage/meshes"
|
||||
)
|
||||
|
||||
type Render interface {
|
||||
Draw(mesh *meshes.Mesh, trMat *gglm.TrMat, mat *materials.Material)
|
||||
DrawMesh(mesh *meshes.Mesh, trMat *gglm.TrMat, mat *materials.Material)
|
||||
DrawVertexArray(mat *materials.Material, vao *buffers.VertexArray, firstElement int32, count int32)
|
||||
DrawCubemap(mesh *meshes.Mesh, mat *materials.Material)
|
||||
FrameEnd()
|
||||
}
|
||||
|
||||
@ -13,17 +13,18 @@ out vec3 fragPos;
|
||||
|
||||
//MVP = Model View Projection
|
||||
uniform mat4 modelMat;
|
||||
uniform mat4 viewMat;
|
||||
uniform mat4 projMat;
|
||||
uniform mat4 projViewMat;
|
||||
|
||||
void main()
|
||||
{
|
||||
vertNormal = mat3(transpose(inverse(modelMat))) * vertNormalIn;
|
||||
vertUV0 = vertUV0In;
|
||||
vertColor = vertColorIn;
|
||||
fragPos = vec3(modelMat * vec4(vertPosIn, 1.0));
|
||||
|
||||
gl_Position = projMat * viewMat * modelMat * vec4(vertPosIn, 1.0);
|
||||
vec4 modelVert = modelMat * vec4(vertPosIn, 1);
|
||||
fragPos = modelVert.xyz;
|
||||
|
||||
gl_Position = projViewMat * modelVert;
|
||||
}
|
||||
|
||||
//shader:fragment
|
||||
|
||||
21
res/shaders/depth-map.glsl
Executable file
21
res/shaders/depth-map.glsl
Executable file
@ -0,0 +1,21 @@
|
||||
//shader:vertex
|
||||
#version 410
|
||||
|
||||
layout(location=0) in vec3 vertPosIn;
|
||||
|
||||
uniform mat4 modelMat;
|
||||
uniform mat4 projViewMat;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projViewMat * modelMat * vec4(vertPosIn, 1);
|
||||
}
|
||||
|
||||
//shader:fragment
|
||||
#version 410
|
||||
|
||||
void main()
|
||||
{
|
||||
// This implicitly writes to the depth buffer with no color operations
|
||||
// Equivalent: gl_FragDepth = gl_FragCoord.z;
|
||||
}
|
||||
45
res/shaders/screen-quad.glsl
Executable file
45
res/shaders/screen-quad.glsl
Executable file
@ -0,0 +1,45 @@
|
||||
//shader:vertex
|
||||
#version 410
|
||||
|
||||
out vec2 vertUV0;
|
||||
|
||||
// Hardcoded vertex positions for a fullscreen quad.
|
||||
// Format: vec4(pos.x, pos.y, uv0.x, uv0.y)
|
||||
vec4 quadData[6] = vec4[](
|
||||
vec4(-1.0, 1.0, 0.0, 1.0),
|
||||
vec4(-1.0, -1.0, 0.0, 0.0),
|
||||
vec4(1.0, -1.0, 1.0, 0.0),
|
||||
vec4(-1.0, 1.0, 0.0, 1.0),
|
||||
vec4(1.0, -1.0, 1.0, 0.0),
|
||||
vec4(1.0, 1.0, 1.0, 1.0)
|
||||
);
|
||||
|
||||
uniform vec2 scale = vec2(1, 1);
|
||||
uniform vec2 offset = vec2(0, 0);
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 vertData = quadData[gl_VertexID];
|
||||
|
||||
vertUV0 = vertData.zw;
|
||||
gl_Position = vec4((vertData.xy * scale) + offset, 0.0, 1.0);
|
||||
}
|
||||
|
||||
//shader:fragment
|
||||
#version 410
|
||||
|
||||
struct Material {
|
||||
sampler2D diffuse;
|
||||
};
|
||||
|
||||
uniform Material material;
|
||||
|
||||
in vec2 vertUV0;
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 diffuseTexColor = texture(material.diffuse, vertUV0);
|
||||
fragColor = vec4(diffuseTexColor.rgb, 1);
|
||||
}
|
||||
54
res/shaders/simple-unlit.glsl
Executable file
54
res/shaders/simple-unlit.glsl
Executable file
@ -0,0 +1,54 @@
|
||||
//shader:vertex
|
||||
#version 410
|
||||
|
||||
layout(location=0) in vec3 vertPosIn;
|
||||
layout(location=1) in vec3 vertNormalIn;
|
||||
layout(location=2) in vec2 vertUV0In;
|
||||
layout(location=3) in vec3 vertColorIn;
|
||||
|
||||
out vec3 vertNormal;
|
||||
out vec2 vertUV0;
|
||||
out vec3 vertColor;
|
||||
out vec3 fragPos;
|
||||
|
||||
//MVP = Model View Projection
|
||||
uniform mat4 modelMat;
|
||||
uniform mat4 projViewMat;
|
||||
|
||||
void main()
|
||||
{
|
||||
// @TODO: Calculate this on the CPU and send it as a uniform
|
||||
//
|
||||
// This produces the normal matrix that multiplies with the model normal to produce the
|
||||
// world space normal. Based on 'One last thing' section from: https://learnopengl.com/Lighting/Basic-Lighting
|
||||
vertNormal = mat3(transpose(inverse(modelMat))) * vertNormalIn;
|
||||
|
||||
vertUV0 = vertUV0In;
|
||||
vertColor = vertColorIn;
|
||||
|
||||
vec4 modelVert = modelMat * vec4(vertPosIn, 1);
|
||||
fragPos = modelVert.xyz;
|
||||
gl_Position = projViewMat * modelVert;
|
||||
}
|
||||
|
||||
//shader:fragment
|
||||
#version 410
|
||||
|
||||
struct Material {
|
||||
sampler2D diffuse;
|
||||
};
|
||||
|
||||
uniform Material material;
|
||||
|
||||
in vec3 vertColor;
|
||||
in vec3 vertNormal;
|
||||
in vec2 vertUV0;
|
||||
in vec3 fragPos;
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 diffuseTexColor = texture(material.diffuse, vertUV0);
|
||||
fragColor = vec4(diffuseTexColor.rgb, 1);
|
||||
}
|
||||
@ -10,25 +10,28 @@ out vec3 vertNormal;
|
||||
out vec2 vertUV0;
|
||||
out vec3 vertColor;
|
||||
out vec3 fragPos;
|
||||
out vec4 fragPosDirLight;
|
||||
|
||||
//MVP = Model View Projection
|
||||
uniform mat4 modelMat;
|
||||
uniform mat4 viewMat;
|
||||
uniform mat4 projMat;
|
||||
uniform mat4 projViewMat;
|
||||
uniform mat4 dirLightProjViewMat;
|
||||
|
||||
void main()
|
||||
{
|
||||
// @TODO: Calculate this on the CPU and send it as a uniform
|
||||
|
||||
//
|
||||
// This produces the normal matrix that multiplies with the model normal to produce the
|
||||
// world space normal. Based on 'One last thing' section from: https://learnopengl.com/Lighting/Basic-Lighting
|
||||
vertNormal = mat3(transpose(inverse(modelMat))) * vertNormalIn;
|
||||
|
||||
vertUV0 = vertUV0In;
|
||||
vertColor = vertColorIn;
|
||||
fragPos = vec3(modelMat * vec4(vertPosIn, 1.0));
|
||||
|
||||
gl_Position = projMat * viewMat * modelMat * vec4(vertPosIn, 1.0);
|
||||
vec4 modelVert = modelMat * vec4(vertPosIn, 1);
|
||||
fragPos = modelVert.xyz;
|
||||
fragPosDirLight = dirLightProjViewMat * vec4(fragPos, 1);
|
||||
|
||||
gl_Position = projViewMat * modelVert;
|
||||
}
|
||||
|
||||
//shader:fragment
|
||||
@ -48,6 +51,7 @@ struct DirLight {
|
||||
vec3 dir;
|
||||
vec3 diffuseColor;
|
||||
vec3 specularColor;
|
||||
sampler2D shadowMap;
|
||||
};
|
||||
|
||||
uniform DirLight dirLight;
|
||||
@ -83,6 +87,7 @@ in vec3 vertColor;
|
||||
in vec3 vertNormal;
|
||||
in vec2 vertUV0;
|
||||
in vec3 fragPos;
|
||||
in vec4 fragPosDirLight;
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
@ -93,6 +98,46 @@ vec4 emissionTexColor;
|
||||
vec3 normalizedVertNorm;
|
||||
vec3 viewDir;
|
||||
|
||||
float CalcShadow(sampler2D shadowMap, vec3 lightDir)
|
||||
{
|
||||
// Move from clip space to NDC
|
||||
vec3 projCoords = fragPosDirLight.xyz / fragPosDirLight.w;
|
||||
|
||||
// Move from [-1,1] to [0, 1]
|
||||
projCoords = projCoords * 0.5 + 0.5;
|
||||
|
||||
// If sampling outside the depth texture then force 'no shadow'
|
||||
if(projCoords.z > 1)
|
||||
return 0;
|
||||
|
||||
// currentDepth is the fragment depth from the light's perspective
|
||||
float currentDepth = projCoords.z;
|
||||
|
||||
// Bias in the range [0.005, 0.05] depending on the angle, where a higher
|
||||
// angle gives a higher bias, as shadow acne gets worse with angle
|
||||
float bias = max(0.05 * (1 - dot(normalizedVertNorm, lightDir)), 0.005);
|
||||
|
||||
// 'Percentage Close Filtering'. B
|
||||
// Basically get soft shadows by averaging this texel and surrounding ones
|
||||
float shadow = 0;
|
||||
vec2 texelSize = 1 / textureSize(shadowMap, 0);
|
||||
for(int x = -1; x <= 1; ++x)
|
||||
{
|
||||
for(int y = -1; y <= 1; ++y)
|
||||
{
|
||||
float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
|
||||
|
||||
// If our depth is larger than the lights closest depth at the texel we checked (projCoords),
|
||||
// then there is something closer to the light than us, and so we are in shadow
|
||||
shadow += currentDepth - bias > pcfDepth ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
shadow /= 9;
|
||||
|
||||
return shadow;
|
||||
}
|
||||
|
||||
vec3 CalcDirLight()
|
||||
{
|
||||
vec3 lightDir = normalize(-dirLight.dir);
|
||||
@ -106,7 +151,10 @@ vec3 CalcDirLight()
|
||||
float specularAmount = pow(max(dot(normalizedVertNorm, halfwayDir), 0.0), material.shininess);
|
||||
vec3 finalSpecular = specularAmount * dirLight.specularColor * specularTexColor.rgb;
|
||||
|
||||
return finalDiffuse + finalSpecular;
|
||||
// Shadow
|
||||
float shadow = CalcShadow(dirLight.shadowMap, lightDir);
|
||||
|
||||
return (finalDiffuse + finalSpecular) * (1 - shadow);
|
||||
}
|
||||
|
||||
vec3 CalcPointLight(PointLight pointLight)
|
||||
@ -129,7 +177,7 @@ vec3 CalcPointLight(PointLight pointLight)
|
||||
|
||||
// attenuation
|
||||
float distToLight = length(pointLight.pos - fragPos);
|
||||
float attenuation = 1.0 / (pointLight.constant + pointLight.linear * distToLight + pointLight.quadratic * (distToLight * distToLight));
|
||||
float attenuation = 1 / (pointLight.constant + pointLight.linear * distToLight + pointLight.quadratic * (distToLight * distToLight));
|
||||
|
||||
return (finalDiffuse + finalSpecular) * attenuation;
|
||||
}
|
||||
@ -146,7 +194,7 @@ vec3 CalcSpotLight(SpotLight light)
|
||||
// light after outer cutoff
|
||||
float theta = dot(fragToLightDir, normalize(-light.dir));
|
||||
float epsilon = (light.innerCutoff - light.outerCutoff);
|
||||
float intensity = clamp((theta - light.outerCutoff) / epsilon, 0.0, 1.0);
|
||||
float intensity = clamp((theta - light.outerCutoff) / epsilon, float(0), float(1));
|
||||
|
||||
if (intensity == 0)
|
||||
return vec3(0);
|
||||
|
||||
@ -8,13 +8,12 @@ layout(location=3) in vec3 vertColorIn;
|
||||
|
||||
out vec3 vertUV0;
|
||||
|
||||
uniform mat4 viewMat;
|
||||
uniform mat4 projMat;
|
||||
uniform mat4 projViewMat;
|
||||
|
||||
void main()
|
||||
{
|
||||
vertUV0 = vec3(vertPosIn.x, vertPosIn.y, -vertPosIn.z);
|
||||
vec4 pos = projMat * viewMat * vec4(vertPosIn, 1.0);
|
||||
vec4 pos = projViewMat * vec4(vertPosIn, 1.0);
|
||||
gl_Position = pos.xyww;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user