Compare commits

...

20 Commits

Author SHA1 Message Date
fbfcbaa156 Point light shadows+cubemap array fbo+cleanup 2024-04-15 10:49:18 +04:00
c4b1dd1b3d Geometry shader support+omnidirectional depth map shader+improvements 2024-04-15 05:09:07 +04:00
a5bea5a661 Cubemap depth fbo attachments 2024-04-15 04:16:44 +04:00
22ba9ca891 Fix comment 2024-04-15 03:22:25 +04:00
92855c52f9 Shader fixes 2024-04-15 03:19:40 +04:00
594d342bf0 Remove some stuff 2024-04-14 07:59:40 +04:00
bb1946b930 Simple percentage closer filtering for shadows 2024-04-14 07:58:47 +04:00
ef2b01059a Update calcshadow 2024-04-14 06:18:41 +04:00
5fa6a06079 Clamp to border for depth map+rotating cubes 2024-04-14 06:14:10 +04:00
b718611149 Improving shadows 2024-04-14 03:45:48 +04:00
be85e20024 Basic directional shadows 2024-04-14 02:50:40 +04:00
040228319e Depth fbo+renderer work+texture slots enum+optimize shaders+more 2024-04-13 23:55:52 +04:00
ddd8db3cb0 Framebuffers+unlit shader+screen quad shader 2024-04-13 08:03:17 +04:00
692167ada2 Split buffer struct into VAO+VBO+IBO structs 2024-04-13 02:59:31 +04:00
524ef068f0 Add comment 2024-04-12 23:57:24 +04:00
b060dcdbe9 Go 1.22+fix input bug 2024-04-12 23:55:21 +04:00
e22525e2ee Default to srgba textures 2024-04-12 23:38:51 +04:00
ee61373069 Blinn-phong 2024-04-12 23:28:59 +04:00
1f922b6a47 Enable stencil test 2024-04-12 23:02:27 +04:00
9d7bdc0196 Improve error messages 2024-04-12 22:40:08 +04:00
27 changed files with 1715 additions and 470 deletions

View File

@ -12,7 +12,7 @@ jobs:
- name: Install golang - name: Install golang
uses: actions/setup-go@v3 uses: actions/setup-go@v3
with: with:
go-version: '>=1.18' go-version: '>=1.22'
- name: Install assimp-go dylib - name: Install assimp-go dylib
run: sudo mkdir -p /usr/local/lib && sudo wget https://github.com/bloeys/assimp-go/releases/download/v0.4.2/libassimp_darwin_amd64.dylib -O /usr/local/lib/libassimp.5.dylib run: sudo mkdir -p /usr/local/lib && sudo wget https://github.com/bloeys/assimp-go/releases/download/v0.4.2/libassimp_darwin_amd64.dylib -O /usr/local/lib/libassimp.5.dylib

View File

@ -19,7 +19,8 @@ import (
type ColorFormat int type ColorFormat int
const ( const (
ColorFormat_RGBA8 ColorFormat = iota ColorFormat_Unknown ColorFormat = iota
ColorFormat_RGBA8
) )
type Texture struct { type Texture struct {
@ -45,7 +46,7 @@ type TextureLoadOptions struct {
WriteToCache bool WriteToCache bool
GenMipMaps bool GenMipMaps bool
KeepPixelsInMem bool KeepPixelsInMem bool
TextureIsSrgba bool NoSrgba bool
} }
type Cubemap struct { type Cubemap struct {
@ -103,9 +104,9 @@ func LoadTexturePNG(file string, loadOptions *TextureLoadOptions) (Texture, erro
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
// load and generate the texture // load and generate the texture
internalFormat := int32(gl.RGBA8) internalFormat := int32(gl.SRGB_ALPHA)
if loadOptions.TextureIsSrgba { if loadOptions.NoSrgba {
internalFormat = gl.SRGB_ALPHA internalFormat = gl.RGBA8
} }
gl.TexImage2D(gl.TEXTURE_2D, 0, internalFormat, tex.Width, tex.Height, 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tex.Pixels[0])) gl.TexImage2D(gl.TEXTURE_2D, 0, internalFormat, tex.Width, tex.Height, 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tex.Pixels[0]))
@ -151,9 +152,9 @@ func LoadTextureInMemPngImg(img image.Image, loadOptions *TextureLoadOptions) (T
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
// load and generate the texture // load and generate the texture
internalFormat := int32(gl.RGBA8) internalFormat := int32(gl.SRGB_ALPHA)
if loadOptions.TextureIsSrgba { if loadOptions.NoSrgba {
internalFormat = gl.SRGB_ALPHA internalFormat = gl.RGBA8
} }
gl.TexImage2D(gl.TEXTURE_2D, 0, internalFormat, tex.Width, tex.Height, 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tex.Pixels[0])) gl.TexImage2D(gl.TEXTURE_2D, 0, internalFormat, tex.Width, tex.Height, 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tex.Pixels[0]))
@ -216,9 +217,9 @@ func LoadTextureJpeg(file string, loadOptions *TextureLoadOptions) (Texture, err
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
// load and generate the texture // load and generate the texture
internalFormat := int32(gl.RGBA8) internalFormat := int32(gl.SRGB_ALPHA)
if loadOptions.TextureIsSrgba { if loadOptions.NoSrgba {
internalFormat = gl.SRGB_ALPHA internalFormat = gl.RGBA8
} }
gl.TexImage2D(gl.TEXTURE_2D, 0, internalFormat, tex.Width, tex.Height, 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tex.Pixels[0])) gl.TexImage2D(gl.TEXTURE_2D, 0, internalFormat, tex.Width, tex.Height, 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tex.Pixels[0]))
@ -288,9 +289,9 @@ func LoadCubemapTextures(rightTex, leftTex, topTex, botTex, frontTex, backTex st
height := int32(nrgbaImg.Bounds().Dy()) height := int32(nrgbaImg.Bounds().Dy())
width := int32(nrgbaImg.Bounds().Dx()) width := int32(nrgbaImg.Bounds().Dx())
internalFormat := int32(gl.RGBA8) internalFormat := int32(gl.SRGB_ALPHA)
if loadOptions.TextureIsSrgba { if loadOptions.NoSrgba {
internalFormat = gl.SRGB_ALPHA internalFormat = gl.RGBA8
} }
gl.TexImage2D(uint32(gl.TEXTURE_CUBE_MAP_POSITIVE_X)+i, 0, internalFormat, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&nrgbaImg.Pix[0])) gl.TexImage2D(uint32(gl.TEXTURE_CUBE_MAP_POSITIVE_X)+i, 0, internalFormat, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&nrgbaImg.Pix[0]))

View File

@ -10,8 +10,10 @@ import (
type BufUsage int type BufUsage int
const ( const (
BufUsage_Unknown BufUsage = iota
//Buffer is set only once and used many times //Buffer is set only once and used many times
BufUsage_Static BufUsage = iota BufUsage_Static
//Buffer is changed a lot and used many times //Buffer is changed a lot and used many times
BufUsage_Dynamic BufUsage_Dynamic
//Buffer is set only once and used by the GPU at most a few times //Buffer is set only once and used by the GPU at most a few times

View File

@ -1,134 +0,0 @@
package buffers
import (
"github.com/bloeys/nmage/logging"
"github.com/go-gl/gl/v4.1-core/gl"
)
type Buffer struct {
VAOID uint32
// BufID is the ID of the VBO
BufID uint32
// IndexBufID is the ID of the index/element buffer
IndexBufID uint32
// IndexBufCount is the number of elements in the index buffer
// Updated on SetIndexBufData
IndexBufCount int32
// IndexBufCount int32
Stride int32
layout []Element
}
func (b *Buffer) Bind() {
gl.BindVertexArray(b.VAOID)
}
func (b *Buffer) UnBind() {
gl.BindVertexArray(0)
}
func (b *Buffer) SetData(values []float32) {
gl.BindVertexArray(b.VAOID)
gl.BindBuffer(gl.ARRAY_BUFFER, b.BufID)
sizeInBytes := len(values) * 4
if sizeInBytes == 0 {
gl.BufferData(gl.ARRAY_BUFFER, 0, gl.Ptr(nil), BufUsage_Static.ToGL())
} else {
gl.BufferData(gl.ARRAY_BUFFER, sizeInBytes, gl.Ptr(&values[0]), BufUsage_Static.ToGL())
}
gl.BindVertexArray(0)
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
func (b *Buffer) SetDataWithUsage(values []float32, usage BufUsage) {
gl.BindVertexArray(b.VAOID)
gl.BindBuffer(gl.ARRAY_BUFFER, b.BufID)
sizeInBytes := len(values) * 4
if sizeInBytes == 0 {
gl.BufferData(gl.ARRAY_BUFFER, 0, gl.Ptr(nil), usage.ToGL())
} else {
gl.BufferData(gl.ARRAY_BUFFER, sizeInBytes, gl.Ptr(&values[0]), usage.ToGL())
}
gl.BindVertexArray(0)
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
func (b *Buffer) SetIndexBufData(values []uint32) {
b.IndexBufCount = int32(len(values))
gl.BindVertexArray(b.VAOID)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, b.IndexBufID)
sizeInBytes := len(values) * 4
if sizeInBytes == 0 {
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, 0, gl.Ptr(nil), BufUsage_Static.ToGL())
} else {
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, sizeInBytes, gl.Ptr(&values[0]), BufUsage_Static.ToGL())
}
gl.BindVertexArray(0)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0)
}
func (b *Buffer) GetLayout() []Element {
e := make([]Element, len(b.layout))
copy(e, b.layout)
return e
}
// SetLayout updates the layout object and the corresponding vertex attributes.
// Vertex attributes are also enabled.
func (b *Buffer) SetLayout(layout ...Element) {
b.layout = layout
b.Stride = 0
for i := 0; i < len(b.layout); i++ {
b.layout[i].Offset = int(b.Stride)
b.Stride += b.layout[i].Size()
}
//Set opengl stuff
b.Bind()
//NOTE: VBOs are only bound at 'VertexAttribPointer', not BindBUffer, so we need to bind the buffer and vao here
gl.BindBuffer(gl.ARRAY_BUFFER, b.BufID)
for i := 0; i < len(layout); i++ {
gl.EnableVertexAttribArray(uint32(i))
gl.VertexAttribPointerWithOffset(uint32(i), layout[i].ElementType.CompCount(), layout[i].ElementType.GLType(), false, b.Stride, uintptr(layout[i].Offset))
}
b.UnBind()
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
func NewBuffer(layout ...Element) Buffer {
b := Buffer{}
gl.GenVertexArrays(1, &b.VAOID)
if b.VAOID == 0 {
logging.ErrLog.Println("Failed to create openGL vertex array object")
}
gl.GenBuffers(1, &b.BufID)
if b.BufID == 0 {
logging.ErrLog.Println("Failed to create openGL buffer")
}
gl.GenBuffers(1, &b.IndexBufID)
if b.IndexBufID == 0 {
logging.ErrLog.Println("Failed to create openGL buffer")
}
b.SetLayout(layout...)
return b
}

521
buffers/framebuffer.go Executable file
View File

@ -0,0 +1,521 @@
package buffers
import (
"github.com/bloeys/nmage/assert"
"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
FramebufferAttachmentType_Cubemap
FramebufferAttachmentType_Cubemap_Array
)
func (f FramebufferAttachmentType) IsValid() bool {
switch f {
case FramebufferAttachmentType_Texture:
fallthrough
case FramebufferAttachmentType_Renderbuffer:
fallthrough
case FramebufferAttachmentType_Cubemap:
fallthrough
case FramebufferAttachmentType_Cubemap_Array:
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 attachType == FramebufferAttachmentType_Cubemap || attachType == FramebufferAttachmentType_Cubemap_Array {
logging.ErrLog.Fatalf("failed creating color attachment because cubemaps can not be color attachments (at least in this implementation. You might be able to do it manually)\n")
}
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)
}
if attachType == FramebufferAttachmentType_Cubemap_Array {
logging.ErrLog.Fatalf("failed creating cubemap array depth attachment because 'NewDepthCubemapArrayAttachment' must be used for that\n")
}
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)
} else if attachType == FramebufferAttachmentType_Cubemap {
// Create cubemap
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_CUBE_MAP, a.Id)
for i := 0; i < 6; i++ {
gl.TexImage2D(uint32(gl.TEXTURE_CUBE_MAP_POSITIVE_X+i), 0, attachFormat.GlInternalFormat(), int32(fbo.Width), int32(fbo.Height), 0, attachFormat.GlFormat(), gl.FLOAT, nil)
}
gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE)
gl.BindTexture(gl.TEXTURE_2D, 0)
// Attach to fbo
gl.FramebufferTexture(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, a.Id, 0)
}
fbo.UnBind()
fbo.ClearFlags |= gl.DEPTH_BUFFER_BIT
fbo.Attachments = append(fbo.Attachments, a)
}
func (fbo *Framebuffer) NewDepthCubemapArrayAttachment(
attachFormat FramebufferAttachmentDataFormat,
numCubemaps int32,
) {
if fbo.HasDepthAttachment() {
logging.ErrLog.Fatalf("failed creating cubemap array depth attachment for framebuffer because a depth attachment already exists\n")
}
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: FramebufferAttachmentType_Cubemap_Array,
Format: attachFormat,
}
fbo.Bind()
// Create cubemap array
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_CUBE_MAP_ARRAY, a.Id)
gl.TexImage3D(
gl.TEXTURE_CUBE_MAP_ARRAY,
0,
attachFormat.GlInternalFormat(),
int32(fbo.Width),
int32(fbo.Height),
6*numCubemaps,
0,
attachFormat.GlFormat(),
gl.FLOAT,
nil,
)
gl.TexParameteri(gl.TEXTURE_CUBE_MAP_ARRAY, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_CUBE_MAP_ARRAY, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_CUBE_MAP_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_CUBE_MAP_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_CUBE_MAP_ARRAY, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE)
gl.BindTexture(gl.TEXTURE_2D, 0)
// Attach to fbo
gl.FramebufferTexture(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, a.Id, 0)
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)
}
// SetCubemapArrayLayerFace 'binds' a single face of a cubemap from the cubemap
// array to the fbo, such that rendering only affects that one face and the others inaccessible.
//
// If this is not called, the default is that the entire cubemap array and all the faces in it
// are bound and available for use when binding the fbo.
func (fbo *Framebuffer) SetCubemapArrayLayerFace(layerFace int32) {
for i := 0; i < len(fbo.Attachments); i++ {
a := &fbo.Attachments[i]
if a.Type != FramebufferAttachmentType_Cubemap_Array {
continue
}
assert.T(a.Format.IsDepthFormat(), "SetCubemapFromArray called but a cubemap array is set on a color attachment, which is not currently handled. Code must be updated!")
gl.FramebufferTextureLayer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, a.Id, 0, layerFace)
return
}
logging.ErrLog.Fatalf("SetCubemapFromArray failed because no cubemap array attachment was found on fbo. Fbo=%+v\n", *fbo)
}
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
}

46
buffers/index_buffer.go Executable file
View File

@ -0,0 +1,46 @@
package buffers
import (
"github.com/bloeys/nmage/logging"
"github.com/go-gl/gl/v4.1-core/gl"
)
type IndexBuffer struct {
Id uint32
// IndexBufCount is the number of elements in the index buffer. Updated in IndexBuffer.SetData
IndexBufCount int32
}
func (ib *IndexBuffer) Bind() {
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ib.Id)
}
func (ib *IndexBuffer) UnBind() {
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0)
}
func (ib *IndexBuffer) SetData(values []uint32) {
ib.Bind()
sizeInBytes := len(values) * 4
ib.IndexBufCount = int32(len(values))
if sizeInBytes == 0 {
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, 0, gl.Ptr(nil), BufUsage_Static.ToGL())
} else {
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, sizeInBytes, gl.Ptr(&values[0]), BufUsage_Static.ToGL())
}
}
func NewIndexBuffer() IndexBuffer {
ib := IndexBuffer{}
gl.GenBuffers(1, &ib.Id)
if ib.Id == 0 {
logging.ErrLog.Println("Failed to create OpenGL buffer")
}
return ib
}

54
buffers/vertex_array.go Executable file
View File

@ -0,0 +1,54 @@
package buffers
import (
"github.com/bloeys/nmage/logging"
"github.com/go-gl/gl/v4.1-core/gl"
)
type VertexArray struct {
Id uint32
Vbos []VertexBuffer
IndexBuffer IndexBuffer
}
func (va *VertexArray) Bind() {
gl.BindVertexArray(va.Id)
}
func (va *VertexArray) UnBind() {
gl.BindVertexArray(0)
}
func (va *VertexArray) AddVertexBuffer(vbo VertexBuffer) {
// NOTE: VBOs are only bound at 'VertexAttribPointer' (and related) calls
va.Bind()
vbo.Bind()
for i := 0; i < len(vbo.layout); i++ {
l := &vbo.layout[i]
gl.EnableVertexAttribArray(uint32(i))
gl.VertexAttribPointerWithOffset(uint32(i), l.ElementType.CompCount(), l.ElementType.GLType(), false, vbo.Stride, uintptr(l.Offset))
}
}
func (va *VertexArray) SetIndexBuffer(ib IndexBuffer) {
va.Bind()
ib.Bind()
va.IndexBuffer = ib
}
func NewVertexArray() VertexArray {
vao := VertexArray{}
gl.GenVertexArrays(1, &vao.Id)
if vao.Id == 0 {
logging.ErrLog.Println("Failed to create OpenGL vertex array object")
}
return vao
}

63
buffers/vertex_buffer.go Executable file
View File

@ -0,0 +1,63 @@
package buffers
import (
"github.com/bloeys/nmage/logging"
"github.com/go-gl/gl/v4.1-core/gl"
)
type VertexBuffer struct {
Id uint32
Stride int32
layout []Element
}
func (vb *VertexBuffer) Bind() {
gl.BindBuffer(gl.ARRAY_BUFFER, vb.Id)
}
func (vb *VertexBuffer) UnBind() {
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
func (vb *VertexBuffer) SetData(values []float32, usage BufUsage) {
vb.Bind()
sizeInBytes := len(values) * 4
if sizeInBytes == 0 {
gl.BufferData(gl.ARRAY_BUFFER, 0, gl.Ptr(nil), usage.ToGL())
} else {
gl.BufferData(gl.ARRAY_BUFFER, sizeInBytes, gl.Ptr(&values[0]), usage.ToGL())
}
}
func (vb *VertexBuffer) GetLayout() []Element {
e := make([]Element, len(vb.layout))
copy(e, vb.layout)
return e
}
func (vb *VertexBuffer) SetLayout(layout ...Element) {
vb.Stride = 0
vb.layout = layout
for i := 0; i < len(vb.layout); i++ {
vb.layout[i].Offset = int(vb.Stride)
vb.Stride += vb.layout[i].Size()
}
}
func NewVertexBuffer(layout ...Element) VertexBuffer {
vb := VertexBuffer{}
gl.GenBuffers(1, &vb.Id)
if vb.Id == 0 {
logging.ErrLog.Println("Failed to create OpenGL buffer")
}
vb.SetLayout(layout...)
return vb
}

View File

@ -39,6 +39,20 @@ func (w *Window) handleInputs() {
imguiCaptureMouse := imIo.WantCaptureMouse() imguiCaptureMouse := imIo.WantCaptureMouse()
imguiCaptureKeyboard := imIo.WantCaptureKeyboard() imguiCaptureKeyboard := imIo.WantCaptureKeyboard()
// These two are to fix a bug where state isn't cleared
// even after imgui captures the keyboard/mouse.
//
// For example, if player is moving due to key held and then imgui captures the keyboard,
// the player keeps moving even when the key is no longer pressed because the input system never
// receives the key up event.
if imguiCaptureMouse {
input.ClearMouseState()
}
if imguiCaptureKeyboard {
input.ClearKeyboardState()
}
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
//Fire callbacks //Fire callbacks
@ -203,6 +217,7 @@ func createWindow(title string, x, y, width, height int32, flags WindowFlags, re
if err != nil { if err != nil {
return nil, err return nil, err
} }
win := &Window{ win := &Window{
SDLWin: sdlWin, SDLWin: sdlWin,
EventCallbacks: make([]func(sdl.Event), 0), EventCallbacks: make([]func(sdl.Event), 0),
@ -219,6 +234,10 @@ func createWindow(title string, x, y, width, height int32, flags WindowFlags, re
return nil, err return nil, err
} }
// Get rid of the blinding white startup screen (unfortunately there is still one frame of white)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT)
sdlWin.GLSwap()
return win, err return win, err
} }
@ -229,6 +248,7 @@ func initOpenGL() error {
} }
gl.Enable(gl.DEPTH_TEST) gl.Enable(gl.DEPTH_TEST)
gl.Enable(gl.STENCIL_TEST)
gl.Enable(gl.CULL_FACE) gl.Enable(gl.CULL_FACE)
gl.CullFace(gl.BACK) gl.CullFace(gl.BACK)
gl.FrontFace(gl.CCW) gl.FrontFace(gl.CCW)
@ -239,6 +259,7 @@ func initOpenGL() error {
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.ClearColor(0, 0, 0, 1) gl.ClearColor(0, 0, 0, 1)
return nil return nil
} }
@ -252,7 +273,6 @@ func SetSrgbFramebuffer(isEnabled bool) {
} }
func SetVSync(enabled bool) { func SetVSync(enabled bool) {
assert.T(isInited, "engine.Init was not called!")
if enabled { if enabled {
sdl.GLSetSwapInterval(1) sdl.GLSetSwapInterval(1)

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/bloeys/nmage module github.com/bloeys/nmage
go 1.18 go 1.22
require github.com/veandco/go-sdl2 v0.4.35 require github.com/veandco/go-sdl2 v0.4.35

View File

@ -40,15 +40,17 @@ var (
func EventLoopStart() { func EventLoopStart() {
for _, v := range keyMap { for k, v := range keyMap {
v.IsPressedThisFrame = false v.IsPressedThisFrame = false
v.IsReleasedThisFrame = false v.IsReleasedThisFrame = false
keyMap[k] = v
} }
for _, v := range mouseBtnMap { for k, v := range mouseBtnMap {
v.IsPressedThisFrame = false v.IsPressedThisFrame = false
v.IsReleasedThisFrame = false v.IsReleasedThisFrame = false
v.IsDoubleClicked = false v.IsDoubleClicked = false
mouseBtnMap[k] = v
} }
mouseMotion.XDelta = 0 mouseMotion.XDelta = 0
@ -60,6 +62,16 @@ func EventLoopStart() {
quitRequested = false quitRequested = false
} }
func ClearKeyboardState() {
clear(keyMap)
}
func ClearMouseState() {
clear(mouseBtnMap)
mouseMotion = mouseMotionState{}
mouseWheel = mouseWheelState{}
}
func HandleQuitEvent(e *sdl.QuitEvent) { func HandleQuitEvent(e *sdl.QuitEvent) {
quitRequested = true quitRequested = true
} }

640
main.go
View File

@ -7,15 +7,15 @@ import (
imgui "github.com/AllenDang/cimgui-go" imgui "github.com/AllenDang/cimgui-go"
"github.com/bloeys/gglm/gglm" "github.com/bloeys/gglm/gglm"
"github.com/bloeys/nmage/assert"
"github.com/bloeys/nmage/assets" "github.com/bloeys/nmage/assets"
"github.com/bloeys/nmage/buffers"
"github.com/bloeys/nmage/camera" "github.com/bloeys/nmage/camera"
"github.com/bloeys/nmage/engine" "github.com/bloeys/nmage/engine"
"github.com/bloeys/nmage/entity"
"github.com/bloeys/nmage/input" "github.com/bloeys/nmage/input"
"github.com/bloeys/nmage/logging" "github.com/bloeys/nmage/logging"
"github.com/bloeys/nmage/materials" "github.com/bloeys/nmage/materials"
"github.com/bloeys/nmage/meshes" "github.com/bloeys/nmage/meshes"
"github.com/bloeys/nmage/registry"
"github.com/bloeys/nmage/renderer/rend3dgl" "github.com/bloeys/nmage/renderer/rend3dgl"
"github.com/bloeys/nmage/timing" "github.com/bloeys/nmage/timing"
nmageimgui "github.com/bloeys/nmage/ui/imgui" nmageimgui "github.com/bloeys/nmage/ui/imgui"
@ -26,14 +26,18 @@ import (
/* /*
@TODO: @TODO:
- Rendering: - Rendering:
- Phong lighting model ✅ - Blinn-Phong lighting model ✅
- Directional lights ✅ - Directional lights ✅
- Point lights ✅ - Point lights ✅
- Spotlights ✅ - Spotlights ✅
- Directional light shadows ✅
- Point light shadows ✅
- Spotlight shadows
- HDR - HDR
- Cascaded shadow mapping - Cascaded shadow mapping
- Skeletal animations - Skeletal animations
- Create VAO struct independent from VBO to support multi-VBO use cases (e.g. instancing) - Proper model loading (i.e. load model by reading all its meshes, textures, and so on together)
- Create VAO struct independent from VBO to support multi-VBO use cases (e.g. instancing) ✅
- Renderer batching - Renderer batching
- Scene graph - Scene graph
- Separate engine loop from rendering loop? or leave it to the user? - Separate engine loop from rendering loop? or leave it to the user?
@ -49,17 +53,67 @@ type DirLight struct {
SpecularColor gglm.Vec3 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 // Check https://wiki.ogre3d.org/tiki-index.php?page=-Point+Light+Attenuation for values
type PointLight struct { type PointLight struct {
Pos gglm.Vec3 Pos gglm.Vec3
DiffuseColor gglm.Vec3 DiffuseColor gglm.Vec3
SpecularColor gglm.Vec3 SpecularColor gglm.Vec3
// @TODO
Radius float32 Radius float32
Constant float32 Constant float32
Linear float32 Linear float32
Quadratic float32 Quadratic float32
FarPlane float32
}
const (
MaxPointLights = 8
)
var (
pointLightNear float32 = 1
)
func (p *PointLight) GetProjViewMats(shadowMapWidth, shadowMapHeight float32) [6]gglm.Mat4 {
aspect := float32(shadowMapWidth) / float32(shadowMapHeight)
projMat := gglm.Perspective(90*gglm.Deg2Rad, aspect, pointLightNear, p.FarPlane)
projViewMats := [6]gglm.Mat4{
*projMat.Clone().Mul(&gglm.LookAtRH(&p.Pos, gglm.NewVec3(1, 0, 0).Add(&p.Pos), gglm.NewVec3(0, -1, 0)).Mat4),
*projMat.Clone().Mul(&gglm.LookAtRH(&p.Pos, gglm.NewVec3(-1, 0, 0).Add(&p.Pos), gglm.NewVec3(0, -1, 0)).Mat4),
*projMat.Clone().Mul(&gglm.LookAtRH(&p.Pos, gglm.NewVec3(0, 1, 0).Add(&p.Pos), gglm.NewVec3(0, 0, 1)).Mat4),
*projMat.Clone().Mul(&gglm.LookAtRH(&p.Pos, gglm.NewVec3(0, -1, 0).Add(&p.Pos), gglm.NewVec3(0, 0, -1)).Mat4),
*projMat.Clone().Mul(&gglm.LookAtRH(&p.Pos, gglm.NewVec3(0, 0, 1).Add(&p.Pos), gglm.NewVec3(0, -1, 0)).Mat4),
*projMat.Clone().Mul(&gglm.LookAtRH(&p.Pos, gglm.NewVec3(0, 0, -1).Add(&p.Pos), gglm.NewVec3(0, -1, 0)).Mat4),
}
return projViewMats
} }
type SpotLight struct { type SpotLight struct {
@ -98,11 +152,33 @@ var (
yaw float32 = -1.5 yaw float32 = -1.5
cam *camera.Camera cam *camera.Camera
whiteMat *materials.Material // Demo fbo
containerMat *materials.Material renderToDemoFbo = true
palleteMat *materials.Material renderToBackBuffer = true
skyboxMat *materials.Material demoFboScale = gglm.NewVec2(0.25, 0.25)
debugDepthMat *materials.Material demoFboOffset = gglm.NewVec2(0.75, -0.75)
demoFbo buffers.Framebuffer
// Dir light fbo
showDirLightDepthMapFbo = true
dirLightDepthMapFboScale = gglm.NewVec2(0.25, 0.25)
dirLightDepthMapFboOffset = gglm.NewVec2(0.75, -0.2)
dirLightDepthMapFbo buffers.Framebuffer
// Point light fbo
omnidirDepthMapFbo buffers.Framebuffer
screenQuadVao buffers.VertexArray
screenQuadMat *materials.Material
unlitMat *materials.Material
whiteMat *materials.Material
containerMat *materials.Material
palleteMat *materials.Material
skyboxMat *materials.Material
dirLightDepthMapMat *materials.Material
omnidirDepthMapMat *materials.Material
debugDepthMat *materials.Material
cubeMesh *meshes.Mesh cubeMesh *meshes.Mesh
sphereMesh *meshes.Mesh sphereMesh *meshes.Mesh
@ -111,8 +187,8 @@ var (
cubeModelMat = gglm.NewTrMatId() cubeModelMat = gglm.NewTrMatId()
drawSkybox = true renderSkybox = true
debugDrawDepthBuffer bool renderDepthBuffer bool
skyboxCmap assets.Cubemap skyboxCmap assets.Cubemap
@ -123,19 +199,21 @@ var (
// Lights // Lights
dirLight = DirLight{ dirLight = DirLight{
Dir: *gglm.NewVec3(0, -0.8, 0.2).Normalize(), Dir: *gglm.NewVec3(0, -0.5, -0.8).Normalize(),
DiffuseColor: *gglm.NewVec3(0, 0, 0), DiffuseColor: *gglm.NewVec3(1, 1, 1),
SpecularColor: *gglm.NewVec3(0, 0, 0), SpecularColor: *gglm.NewVec3(1, 1, 1),
} }
pointLights = [...]PointLight{ pointLights = [...]PointLight{
{ {
Pos: *gglm.NewVec3(0, 5, 0), Pos: *gglm.NewVec3(0, 2, -2),
DiffuseColor: *gglm.NewVec3(1, 0, 0), DiffuseColor: *gglm.NewVec3(1, 0, 0),
SpecularColor: *gglm.NewVec3(1, 1, 1), SpecularColor: *gglm.NewVec3(1, 1, 1),
// These values are for 50m range // These values are for 50m range
Constant: 1.0, Constant: 1.0,
Linear: 0.09, Linear: 0.09,
Quadratic: 0.032, Quadratic: 0.032,
FarPlane: 25,
}, },
{ {
Pos: *gglm.NewVec3(0, -5, 0), Pos: *gglm.NewVec3(0, -5, 0),
@ -144,6 +222,7 @@ var (
Constant: 1.0, Constant: 1.0,
Linear: 0.09, Linear: 0.09,
Quadratic: 0.032, Quadratic: 0.032,
FarPlane: 25,
}, },
{ {
Pos: *gglm.NewVec3(5, 0, 0), Pos: *gglm.NewVec3(5, 0, 0),
@ -152,19 +231,21 @@ var (
Constant: 1.0, Constant: 1.0,
Linear: 0.09, Linear: 0.09,
Quadratic: 0.032, Quadratic: 0.032,
FarPlane: 25,
}, },
{ {
Pos: *gglm.NewVec3(-4, 0, 0), Pos: *gglm.NewVec3(-3, 4, 3),
DiffuseColor: *gglm.NewVec3(0, 0, 1), DiffuseColor: *gglm.NewVec3(1, 1, 1),
SpecularColor: *gglm.NewVec3(1, 1, 1), SpecularColor: *gglm.NewVec3(1, 1, 1),
Constant: 1.0, Constant: 1.0,
Linear: 0.09, Linear: 0.09,
Quadratic: 0.032, Quadratic: 0.032,
FarPlane: 25,
}, },
} }
spotLights = [...]SpotLight{ spotLights = [...]SpotLight{
{ {
Pos: *gglm.NewVec3(0, 5, 0), Pos: *gglm.NewVec3(2, 5, 5),
Dir: *gglm.NewVec3(0, -1, 0), Dir: *gglm.NewVec3(0, -1, 0),
DiffuseColor: *gglm.NewVec3(0, 1, 1), DiffuseColor: *gglm.NewVec3(0, 1, 1),
SpecularColor: *gglm.NewVec3(1, 1, 1), SpecularColor: *gglm.NewVec3(1, 1, 1),
@ -176,64 +257,14 @@ var (
) )
type Game struct { type Game struct {
WinWidth int32
WinHeight int32
Win *engine.Window Win *engine.Window
ImGUIInfo nmageimgui.ImguiInfo 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() { func main() {
// Test()
// return
//Init engine //Init engine
err := engine.Init() err := engine.Init()
if err != nil { if err != nil {
@ -254,6 +285,8 @@ func main() {
game := &Game{ game := &Game{
Win: window, Win: window,
WinWidth: int32(unscaledWindowWidth * dpiScaling),
WinHeight: int32(unscaledWindowHeight * dpiScaling),
ImGUIInfo: nmageimgui.NewImGui("./res/shaders/imgui.glsl"), ImGUIInfo: nmageimgui.NewImGui("./res/shaders/imgui.glsl"),
} }
window.EventCallbacks = append(window.EventCallbacks, game.handleWindowEvents) window.EventCallbacks = append(window.EventCallbacks, game.handleWindowEvents)
@ -267,13 +300,12 @@ func (g *Game) handleWindowEvents(e sdl.Event) {
case *sdl.WindowEvent: case *sdl.WindowEvent:
if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED { if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED {
width := e.Data1 g.WinWidth = e.Data1
height := e.Data2 g.WinHeight = e.Data2
cam.AspectRatio = float32(width) / float32(height) cam.AspectRatio = float32(g.WinWidth) / float32(g.WinHeight)
cam.Update()
palleteMat.SetUnifMat4("projMat", &cam.ProjMat) cam.Update()
debugDepthMat.SetUnifMat4("projMat", &cam.ProjMat) updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
} }
} }
} }
@ -349,27 +381,27 @@ func (g *Game) Init() {
} }
//Load textures //Load textures
whiteTex, err := assets.LoadTexturePNG("./res/textures/white.png", &assets.TextureLoadOptions{TextureIsSrgba: true}) whiteTex, err := assets.LoadTexturePNG("./res/textures/white.png", &assets.TextureLoadOptions{})
if err != nil { if err != nil {
logging.ErrLog.Fatalln("Failed to load texture. Err: ", err) logging.ErrLog.Fatalln("Failed to load texture. Err: ", err)
} }
blackTex, err := assets.LoadTexturePNG("./res/textures/black.png", &assets.TextureLoadOptions{TextureIsSrgba: true}) blackTex, err := assets.LoadTexturePNG("./res/textures/black.png", &assets.TextureLoadOptions{})
if err != nil { if err != nil {
logging.ErrLog.Fatalln("Failed to load texture. Err: ", err) logging.ErrLog.Fatalln("Failed to load texture. Err: ", err)
} }
containerDiffuseTex, err := assets.LoadTexturePNG("./res/textures/container-diffuse.png", &assets.TextureLoadOptions{TextureIsSrgba: true}) containerDiffuseTex, err := assets.LoadTexturePNG("./res/textures/container-diffuse.png", &assets.TextureLoadOptions{})
if err != nil { if err != nil {
logging.ErrLog.Fatalln("Failed to load texture. Err: ", err) logging.ErrLog.Fatalln("Failed to load texture. Err: ", err)
} }
containerSpecularTex, err := assets.LoadTexturePNG("./res/textures/container-specular.png", &assets.TextureLoadOptions{TextureIsSrgba: true}) containerSpecularTex, err := assets.LoadTexturePNG("./res/textures/container-specular.png", &assets.TextureLoadOptions{})
if err != nil { if err != nil {
logging.ErrLog.Fatalln("Failed to load texture. Err: ", err) logging.ErrLog.Fatalln("Failed to load texture. Err: ", err)
} }
palleteTex, err := assets.LoadTexturePNG("./res/textures/pallete-endesga-64-1x.png", &assets.TextureLoadOptions{TextureIsSrgba: true}) palleteTex, err := assets.LoadTexturePNG("./res/textures/pallete-endesga-64-1x.png", &assets.TextureLoadOptions{})
if err != nil { if err != nil {
logging.ErrLog.Fatalln("Failed to load texture. Err: ", err) logging.ErrLog.Fatalln("Failed to load texture. Err: ", err)
} }
@ -378,29 +410,40 @@ func (g *Game) Init() {
"./res/textures/sb-right.jpg", "./res/textures/sb-left.jpg", "./res/textures/sb-right.jpg", "./res/textures/sb-left.jpg",
"./res/textures/sb-top.jpg", "./res/textures/sb-bottom.jpg", "./res/textures/sb-top.jpg", "./res/textures/sb-bottom.jpg",
"./res/textures/sb-front.jpg", "./res/textures/sb-back.jpg", "./res/textures/sb-front.jpg", "./res/textures/sb-back.jpg",
&assets.TextureLoadOptions{TextureIsSrgba: true}, &assets.TextureLoadOptions{},
) )
if err != nil { if err != nil {
logging.ErrLog.Fatalln("Failed to load cubemap. Err: ", err) logging.ErrLog.Fatalln("Failed to load cubemap. Err: ", err)
} }
//
// Create materials and assign any unused texture slots to black // 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 = materials.NewMaterial("White mat", "./res/shaders/simple.glsl")
whiteMat.Shininess = 64 whiteMat.Shininess = 64
whiteMat.DiffuseTex = whiteTex.TexID whiteMat.DiffuseTex = whiteTex.TexID
whiteMat.SpecularTex = blackTex.TexID whiteMat.SpecularTex = blackTex.TexID
whiteMat.NormalTex = blackTex.TexID whiteMat.NormalTex = blackTex.TexID
whiteMat.EmissionTex = blackTex.TexID whiteMat.EmissionTex = blackTex.TexID
whiteMat.SetUnifInt32("material.diffuse", 0) whiteMat.SetUnifInt32("material.diffuse", int32(materials.TextureSlot_Diffuse))
whiteMat.SetUnifInt32("material.specular", 1) whiteMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
// whiteMat.SetUnifInt32("material.normal", 2) // whiteMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
whiteMat.SetUnifInt32("material.emission", 3) whiteMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
whiteMat.SetUnifMat4("projMat", &cam.ProjMat)
whiteMat.SetUnifVec3("ambientColor", ambientColor) whiteMat.SetUnifVec3("ambientColor", ambientColor)
whiteMat.SetUnifFloat32("material.shininess", whiteMat.Shininess) whiteMat.SetUnifFloat32("material.shininess", whiteMat.Shininess)
whiteMat.SetUnifVec3("dirLight.dir", &dirLight.Dir) whiteMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
whiteMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor) whiteMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
whiteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor) whiteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
whiteMat.SetUnifInt32("dirLight.shadowMap", int32(materials.TextureSlot_ShadowMap))
whiteMat.SetUnifInt32("pointLightCubeShadowMaps", int32(materials.TextureSlot_Cubemap_Array))
containerMat = materials.NewMaterial("Container mat", "./res/shaders/simple.glsl") containerMat = materials.NewMaterial("Container mat", "./res/shaders/simple.glsl")
containerMat.Shininess = 64 containerMat.Shininess = 64
@ -408,16 +451,17 @@ func (g *Game) Init() {
containerMat.SpecularTex = containerSpecularTex.TexID containerMat.SpecularTex = containerSpecularTex.TexID
containerMat.NormalTex = blackTex.TexID containerMat.NormalTex = blackTex.TexID
containerMat.EmissionTex = blackTex.TexID containerMat.EmissionTex = blackTex.TexID
containerMat.SetUnifInt32("material.diffuse", 0) containerMat.SetUnifInt32("material.diffuse", int32(materials.TextureSlot_Diffuse))
containerMat.SetUnifInt32("material.specular", 1) containerMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
// containerMat.SetUnifInt32("material.normal", 2) // containerMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
containerMat.SetUnifInt32("material.emission", 3) containerMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
containerMat.SetUnifMat4("projMat", &cam.ProjMat)
containerMat.SetUnifVec3("ambientColor", ambientColor) containerMat.SetUnifVec3("ambientColor", ambientColor)
containerMat.SetUnifFloat32("material.shininess", containerMat.Shininess) containerMat.SetUnifFloat32("material.shininess", containerMat.Shininess)
containerMat.SetUnifVec3("dirLight.dir", &dirLight.Dir) containerMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
containerMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor) containerMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
containerMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor) containerMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
containerMat.SetUnifInt32("dirLight.shadowMap", int32(materials.TextureSlot_ShadowMap))
containerMat.SetUnifInt32("pointLightCubeShadowMaps", int32(materials.TextureSlot_Cubemap_Array))
palleteMat = materials.NewMaterial("Pallete mat", "./res/shaders/simple.glsl") palleteMat = materials.NewMaterial("Pallete mat", "./res/shaders/simple.glsl")
palleteMat.Shininess = 64 palleteMat.Shininess = 64
@ -425,63 +469,135 @@ func (g *Game) Init() {
palleteMat.SpecularTex = blackTex.TexID palleteMat.SpecularTex = blackTex.TexID
palleteMat.NormalTex = blackTex.TexID palleteMat.NormalTex = blackTex.TexID
palleteMat.EmissionTex = blackTex.TexID palleteMat.EmissionTex = blackTex.TexID
palleteMat.SetUnifInt32("material.diffuse", 0) palleteMat.SetUnifInt32("material.diffuse", int32(materials.TextureSlot_Diffuse))
palleteMat.SetUnifInt32("material.specular", 1) palleteMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
// palleteMat.SetUnifInt32("material.normal", 2) // palleteMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
palleteMat.SetUnifInt32("material.emission", 3) palleteMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
palleteMat.SetUnifMat4("projMat", &cam.ProjMat)
palleteMat.SetUnifVec3("ambientColor", ambientColor) palleteMat.SetUnifVec3("ambientColor", ambientColor)
palleteMat.SetUnifFloat32("material.shininess", palleteMat.Shininess) palleteMat.SetUnifFloat32("material.shininess", palleteMat.Shininess)
palleteMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor) palleteMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
palleteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor) palleteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
palleteMat.SetUnifInt32("dirLight.shadowMap", int32(materials.TextureSlot_ShadowMap))
palleteMat.SetUnifInt32("pointLightCubeShadowMaps", int32(materials.TextureSlot_Cubemap_Array))
debugDepthMat = materials.NewMaterial("Debug depth mat", "./res/shaders/debug-depth.glsl") debugDepthMat = materials.NewMaterial("Debug depth mat", "./res/shaders/debug-depth.glsl")
debugDepthMat.SetUnifMat4("projMat", &cam.ProjMat)
dirLightDepthMapMat = materials.NewMaterial("Directional Depth Map mat", "./res/shaders/directional-depth-map.glsl")
omnidirDepthMapMat = materials.NewMaterial("Omnidirectional Depth Map mat", "./res/shaders/omnidirectional-depth-map.glsl")
skyboxMat = materials.NewMaterial("Skybox mat", "./res/shaders/skybox.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)) translationMat := gglm.NewTranslationMat(gglm.NewVec3(0, 0, 0))
scaleMat := gglm.NewScaleMat(gglm.NewVec3(1, 1, 1)) scaleMat := gglm.NewScaleMat(gglm.NewVec3(1, 1, 1))
rotMat := gglm.NewRotMat(gglm.NewQuatEuler(gglm.NewVec3(-90, -90, 0).AsRad())) rotMat := gglm.NewRotMat(gglm.NewQuatEuler(gglm.NewVec3(-90, -90, 0).AsRad()))
cubeModelMat.Mul(translationMat.Mul(rotMat.Mul(scaleMat))) 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() 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
dirLightDepthMapFbo = buffers.NewFramebuffer(1024, 1024)
dirLightDepthMapFbo.SetNoColorBuffer()
dirLightDepthMapFbo.NewDepthAttachment(
buffers.FramebufferAttachmentType_Texture,
buffers.FramebufferAttachmentDataFormat_DepthF32,
)
assert.T(dirLightDepthMapFbo.IsComplete(), "Depth map fbo is not complete after init")
// Cubemap fbo
omnidirDepthMapFbo = buffers.NewFramebuffer(1024, 1024)
omnidirDepthMapFbo.SetNoColorBuffer()
omnidirDepthMapFbo.NewDepthCubemapArrayAttachment(
buffers.FramebufferAttachmentDataFormat_DepthF32,
MaxPointLights,
)
assert.T(omnidirDepthMapFbo.IsComplete(), "Cubemap fbo is not complete after init")
} }
func (g *Game) updateLights() { func (g *Game) updateLights() {
// Directional light
whiteMat.ShadowMapTex1 = dirLightDepthMapFbo.Attachments[0].Id
containerMat.ShadowMapTex1 = dirLightDepthMapFbo.Attachments[0].Id
palleteMat.ShadowMapTex1 = dirLightDepthMapFbo.Attachments[0].Id
// Point lights
for i := 0; i < len(pointLights); i++ { for i := 0; i < len(pointLights); i++ {
pl := &pointLights[i] p := &pointLights[i]
indexString := "pointLights[" + strconv.Itoa(i) + "]" indexString := "pointLights[" + strconv.Itoa(i) + "]"
whiteMat.SetUnifVec3(indexString+".pos", &pl.Pos) whiteMat.SetUnifVec3(indexString+".pos", &p.Pos)
containerMat.SetUnifVec3(indexString+".pos", &pl.Pos) containerMat.SetUnifVec3(indexString+".pos", &p.Pos)
palleteMat.SetUnifVec3(indexString+".pos", &pl.Pos) palleteMat.SetUnifVec3(indexString+".pos", &p.Pos)
whiteMat.SetUnifVec3(indexString+".diffuseColor", &pl.DiffuseColor) whiteMat.SetUnifVec3(indexString+".diffuseColor", &p.DiffuseColor)
containerMat.SetUnifVec3(indexString+".diffuseColor", &pl.DiffuseColor) containerMat.SetUnifVec3(indexString+".diffuseColor", &p.DiffuseColor)
palleteMat.SetUnifVec3(indexString+".diffuseColor", &pl.DiffuseColor) palleteMat.SetUnifVec3(indexString+".diffuseColor", &p.DiffuseColor)
whiteMat.SetUnifVec3(indexString+".specularColor", &pl.SpecularColor) whiteMat.SetUnifVec3(indexString+".specularColor", &p.SpecularColor)
containerMat.SetUnifVec3(indexString+".specularColor", &pl.SpecularColor) containerMat.SetUnifVec3(indexString+".specularColor", &p.SpecularColor)
palleteMat.SetUnifVec3(indexString+".specularColor", &pl.SpecularColor) palleteMat.SetUnifVec3(indexString+".specularColor", &p.SpecularColor)
whiteMat.SetUnifFloat32(indexString+".constant", pl.Constant) whiteMat.SetUnifFloat32(indexString+".constant", p.Constant)
containerMat.SetUnifFloat32(indexString+".constant", pl.Constant) containerMat.SetUnifFloat32(indexString+".constant", p.Constant)
palleteMat.SetUnifFloat32(indexString+".constant", pl.Constant) palleteMat.SetUnifFloat32(indexString+".constant", p.Constant)
whiteMat.SetUnifFloat32(indexString+".linear", pl.Linear) whiteMat.SetUnifFloat32(indexString+".linear", p.Linear)
containerMat.SetUnifFloat32(indexString+".linear", pl.Linear) containerMat.SetUnifFloat32(indexString+".linear", p.Linear)
palleteMat.SetUnifFloat32(indexString+".linear", pl.Linear) palleteMat.SetUnifFloat32(indexString+".linear", p.Linear)
whiteMat.SetUnifFloat32(indexString+".quadratic", pl.Quadratic) whiteMat.SetUnifFloat32(indexString+".quadratic", p.Quadratic)
containerMat.SetUnifFloat32(indexString+".quadratic", pl.Quadratic) containerMat.SetUnifFloat32(indexString+".quadratic", p.Quadratic)
palleteMat.SetUnifFloat32(indexString+".quadratic", pl.Quadratic) palleteMat.SetUnifFloat32(indexString+".quadratic", p.Quadratic)
whiteMat.SetUnifFloat32(indexString+".farPlane", p.FarPlane)
containerMat.SetUnifFloat32(indexString+".farPlane", p.FarPlane)
palleteMat.SetUnifFloat32(indexString+".farPlane", p.FarPlane)
} }
whiteMat.CubemapArrayTex = omnidirDepthMapFbo.Attachments[0].Id
containerMat.CubemapArrayTex = omnidirDepthMapFbo.Attachments[0].Id
palleteMat.CubemapArrayTex = omnidirDepthMapFbo.Attachments[0].Id
// Spotlights
for i := 0; i < len(spotLights); i++ { for i := 0; i < len(spotLights); i++ {
l := &spotLights[i] l := &spotLights[i]
@ -545,10 +661,12 @@ func (g *Game) showDebugWindow() {
// Camera // Camera
imgui.Text("Camera") imgui.Text("Camera")
if imgui.DragFloat3("Cam Pos", &cam.Pos.Data) { if imgui.DragFloat3("Cam Pos", &cam.Pos.Data) {
updateViewMat() cam.Update()
updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
} }
if imgui.DragFloat3("Cam Forward", &cam.Forward.Data) { if imgui.DragFloat3("Cam Forward", &cam.Forward.Data) {
updateViewMat() cam.Update()
updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
} }
imgui.Spacing() imgui.Spacing()
@ -564,20 +682,11 @@ func (g *Game) showDebugWindow() {
imgui.Spacing() 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 // Directional light
imgui.Text("Directional Light") imgui.Text("Directional Light")
imgui.Checkbox("Render Directional Light Shadows", &renderDirLightShadows)
if imgui.DragFloat3("Direction", &dirLight.Dir.Data) { if imgui.DragFloat3("Direction", &dirLight.Dir.Data) {
whiteMat.SetUnifVec3("dirLight.dir", &dirLight.Dir) whiteMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
containerMat.SetUnifVec3("dirLight.dir", &dirLight.Dir) containerMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
@ -596,9 +705,26 @@ func (g *Game) showDebugWindow() {
palleteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor) 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() imgui.Spacing()
// Point lights // Point lights
imgui.Checkbox("Render Point Light Shadows", &renderPointLightShadows)
if imgui.BeginListBoxV("Point Lights", imgui.Vec2{Y: 200}) { if imgui.BeginListBoxV("Point Lights", imgui.Vec2{Y: 200}) {
for i := 0; i < len(pointLights); i++ { for i := 0; i < len(pointLights); i++ {
@ -692,11 +818,24 @@ func (g *Game) showDebugWindow() {
imgui.EndListBox() imgui.EndListBox()
} }
// Demo fbo
imgui.Text("Demo Framebuffer")
imgui.Checkbox("Show FBO##0", &renderToDemoFbo)
imgui.DragFloat2("Scale##0", &demoFboScale.Data)
imgui.DragFloat2("Offset##0", &demoFboOffset.Data)
// Depth map fbo
imgui.Text("Directional Light Depth Map Framebuffer")
imgui.Checkbox("Show FBO##1", &showDirLightDepthMapFbo)
imgui.DragFloat2("Scale##1", &dirLightDepthMapFboScale.Data)
imgui.DragFloat2("Offset##1", &dirLightDepthMapFboOffset.Data)
// Other // Other
imgui.Text("Other Settings") imgui.Text("Other Settings")
imgui.Checkbox("Draw Skybox", &drawSkybox) imgui.Checkbox("Render skybox", &renderSkybox)
imgui.Checkbox("Debug depth buffer", &debugDrawDepthBuffer) imgui.Checkbox("Render to back buffer", &renderToBackBuffer)
imgui.Checkbox("Render depth buffer", &renderDepthBuffer)
imgui.End() imgui.End()
} }
@ -724,7 +863,7 @@ func (g *Game) updateCameraLookAround() {
// Update cam forward // Update cam forward
cam.UpdateRotation(pitch, yaw) cam.UpdateRotation(pitch, yaw)
updateViewMat() updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
} }
func (g *Game) updateCameraPos() { func (g *Game) updateCameraPos() {
@ -755,81 +894,202 @@ func (g *Game) updateCameraPos() {
} }
if update { if update {
updateViewMat() cam.Update()
updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
} }
} }
var (
renderDirLightShadows = true
renderPointLightShadows = true
rotatingCubeSpeedDeg1 float32 = 45
rotatingCubeSpeedDeg2 float32 = 120
rotatingCubeTrMat1 = *gglm.NewTrMatId().Translate(gglm.NewVec3(-4, -1, 4))
rotatingCubeTrMat2 = *gglm.NewTrMatId().Translate(gglm.NewVec3(-1, 0.5, 4))
)
func (g *Game) Render() { func (g *Game) Render() {
rotatingCubeTrMat1.Rotate(rotatingCubeSpeedDeg1*gglm.Deg2Rad*timing.DT(), gglm.NewVec3(0, 1, 0))
rotatingCubeTrMat2.Rotate(rotatingCubeSpeedDeg2*gglm.Deg2Rad*timing.DT(), gglm.NewVec3(1, 1, 0))
if renderDirLightShadows {
g.renderDirectionalShadowmap()
}
if renderPointLightShadows {
g.renderOmnidirectionalShadowmap()
}
if renderToBackBuffer {
if renderDepthBuffer {
g.RenderScene(debugDepthMat)
} else {
g.RenderScene(nil)
}
}
if renderSkybox {
g.DrawSkybox()
}
if renderToDemoFbo {
g.renderDemoFob()
}
}
func (g *Game) renderDirectionalShadowmap() {
// Set some uniforms
dirLightProjViewMat := dirLight.GetProjViewMat()
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)
dirLightDepthMapMat.SetUnifMat4("projViewMat", &dirLightProjViewMat)
// Start rendering
dirLightDepthMapFbo.BindWithViewport()
dirLightDepthMapFbo.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(dirLightDepthMapMat)
gl.CullFace(gl.BACK)
dirLightDepthMapFbo.UnBindWithViewport(uint32(g.WinWidth), uint32(g.WinHeight))
if showDirLightDepthMapFbo {
screenQuadMat.DiffuseTex = dirLightDepthMapFbo.Attachments[0].Id
screenQuadMat.SetUnifVec2("offset", dirLightDepthMapFboOffset)
screenQuadMat.SetUnifVec2("scale", dirLightDepthMapFboScale)
screenQuadMat.Bind()
window.Rend.DrawVertexArray(screenQuadMat, &screenQuadVao, 0, 6)
}
}
func (g *Game) renderOmnidirectionalShadowmap() {
omnidirDepthMapFbo.BindWithViewport()
omnidirDepthMapFbo.Clear()
for i := 0; i < len(pointLights); i++ {
p := &pointLights[i]
// Generic uniforms
omnidirDepthMapMat.SetUnifVec3("lightPos", &p.Pos)
omnidirDepthMapMat.SetUnifInt32("cubemapIndex", int32(i))
omnidirDepthMapMat.SetUnifFloat32("farPlane", p.FarPlane)
// Set projView matrices
projViewMats := p.GetProjViewMats(float32(omnidirDepthMapFbo.Width), float32(omnidirDepthMapFbo.Height))
for j := 0; j < len(projViewMats); j++ {
omnidirDepthMapMat.SetUnifMat4("cubemapProjViewMats["+strconv.Itoa(j)+"]", &projViewMats[j])
}
g.RenderScene(omnidirDepthMapMat)
}
omnidirDepthMapFbo.UnBindWithViewport(uint32(g.WinWidth), uint32(g.WinHeight))
}
func (g *Game) renderDemoFob() {
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)
}
func (g *Game) RenderScene(overrideMat *materials.Material) {
tempModelMatrix := cubeModelMat.Clone() tempModelMatrix := cubeModelMat.Clone()
whiteMat.SetUnifVec3("camPos", &cam.Pos) // See if we need overrides
containerMat.SetUnifVec3("camPos", &cam.Pos)
palleteMat.SetUnifVec3("camPos", &cam.Pos)
sunMat := palleteMat sunMat := palleteMat
chairMat := palleteMat chairMat := palleteMat
cubeMat := containerMat cubeMat := containerMat
if debugDrawDepthBuffer {
sunMat = debugDepthMat if overrideMat != nil {
chairMat = debugDepthMat sunMat = overrideMat
cubeMat = debugDepthMat chairMat = overrideMat
cubeMat = overrideMat
} }
// Draw dir light // 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 // Draw point lights
for i := 0; i < len(pointLights); i++ { for i := 0; i < len(pointLights); i++ {
pl := &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 // Chair
window.Rend.Draw(chairMesh, tempModelMatrix, chairMat) window.Rend.DrawMesh(chairMesh, tempModelMatrix, chairMat)
// Ground // 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 // Cubes
rowSize := 1 tempModelMatrix.Translate(gglm.NewVec3(-6, 0, 0))
for y := 0; y < rowSize; y++ { window.Rend.DrawMesh(cubeMesh, tempModelMatrix, cubeMat)
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))
}
if drawSkybox { tempModelMatrix.Translate(gglm.NewVec3(0, -1, -4))
g.DrawSkybox() window.Rend.DrawMesh(cubeMesh, tempModelMatrix, cubeMat)
}
// Rotating cubes
window.Rend.DrawMesh(cubeMesh, &rotatingCubeTrMat1, cubeMat)
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() { func (g *Game) DrawSkybox() {
gl.Disable(gl.CULL_FACE) gl.Disable(gl.CULL_FACE)
gl.DepthFunc(gl.LEQUAL) gl.DepthFunc(gl.LEQUAL)
skyboxMesh.Buf.Bind()
skyboxMat.Bind()
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_CUBE_MAP, skyboxCmap.TexID)
viewMat := cam.ViewMat.Clone() window.Rend.DrawCubemap(skyboxMesh, skyboxMat)
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)
}
gl.DepthFunc(gl.LESS) gl.DepthFunc(gl.LESS)
gl.Enable(gl.CULL_FACE) gl.Enable(gl.CULL_FACE)
@ -842,10 +1102,24 @@ func (g *Game) DeInit() {
g.Win.Destroy() g.Win.Destroy()
} }
func updateViewMat() { func updateAllProjViewMats(projMat, viewMat gglm.Mat4) {
cam.Update()
whiteMat.SetUnifMat4("viewMat", &cam.ViewMat) projViewMat := projMat.Clone().Mul(&viewMat)
containerMat.SetUnifMat4("viewMat", &cam.ViewMat)
palleteMat.SetUnifMat4("viewMat", &cam.ViewMat) unlitMat.SetUnifMat4("projViewMat", projViewMat)
debugDepthMat.SetUnifMat4("viewMat", &cam.ViewMat) 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))
} }

View File

@ -8,6 +8,18 @@ import (
"github.com/go-gl/gl/v4.1-core/gl" "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
TextureSlot_Cubemap_Array TextureSlot = 12
)
type Material struct { type Material struct {
Name string Name string
ShaderProg shaders.ShaderProgram ShaderProg shaders.ShaderProgram
@ -15,47 +27,66 @@ type Material struct {
UnifLocs map[string]int32 UnifLocs map[string]int32
AttribLocs map[string]int32 AttribLocs map[string]int32
// @TODO do this in a better way. Perhaps something like how we do fbo attachments
// Phong shading // Phong shading
DiffuseTex uint32 DiffuseTex uint32
SpecularTex uint32 SpecularTex uint32
NormalTex uint32 NormalTex uint32
EmissionTex uint32 EmissionTex uint32
// Shininess of specular highlights
Shininess float32 Shininess float32
// Cubemaps
CubemapTex uint32
CubemapArrayTex uint32
// Shadowmaps
ShadowMapTex1 uint32
} }
func (m *Material) Bind() { func (m *Material) Bind() {
gl.UseProgram(m.ShaderProg.ID) m.ShaderProg.Bind()
gl.ActiveTexture(gl.TEXTURE0) if m.DiffuseTex != 0 {
gl.BindTexture(gl.TEXTURE_2D, m.DiffuseTex) gl.ActiveTexture(uint32(gl.TEXTURE0 + TextureSlot_Diffuse))
gl.BindTexture(gl.TEXTURE_2D, m.DiffuseTex)
}
gl.ActiveTexture(gl.TEXTURE1) if m.SpecularTex != 0 {
gl.BindTexture(gl.TEXTURE_2D, m.SpecularTex) gl.ActiveTexture(uint32(gl.TEXTURE0 + TextureSlot_Specular))
gl.BindTexture(gl.TEXTURE_2D, m.SpecularTex)
}
gl.ActiveTexture(gl.TEXTURE2) if m.NormalTex != 0 {
gl.BindTexture(gl.TEXTURE_2D, m.NormalTex) gl.ActiveTexture(uint32(gl.TEXTURE0 + TextureSlot_Normal))
gl.BindTexture(gl.TEXTURE_2D, m.NormalTex)
}
gl.ActiveTexture(gl.TEXTURE3) if m.EmissionTex != 0 {
gl.BindTexture(gl.TEXTURE_2D, m.EmissionTex) 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.CubemapArrayTex != 0 {
gl.ActiveTexture(uint32(gl.TEXTURE0 + TextureSlot_Cubemap_Array))
gl.BindTexture(gl.TEXTURE_CUBE_MAP_ARRAY, m.CubemapArrayTex)
}
if m.ShadowMapTex1 != 0 {
gl.ActiveTexture(uint32(gl.TEXTURE0 + TextureSlot_ShadowMap))
gl.BindTexture(gl.TEXTURE_2D, m.ShadowMapTex1)
}
} }
func (m *Material) UnBind() { func (m *Material) UnBind() {
gl.UseProgram(0) 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 { func (m *Material) GetAttribLoc(attribName string) int32 {
@ -65,7 +96,7 @@ func (m *Material) GetAttribLoc(attribName string) int32 {
return loc return loc
} }
loc = gl.GetAttribLocation(m.ShaderProg.ID, gl.Str(attribName+"\x00")) loc = gl.GetAttribLocation(m.ShaderProg.Id, gl.Str(attribName+"\x00"))
assert.T(loc != -1, "Attribute '"+attribName+"' doesn't exist on material "+m.Name) assert.T(loc != -1, "Attribute '"+attribName+"' doesn't exist on material "+m.Name)
m.AttribLocs[attribName] = loc m.AttribLocs[attribName] = loc
return loc return loc
@ -78,7 +109,7 @@ func (m *Material) GetUnifLoc(uniformName string) int32 {
return loc return loc
} }
loc = gl.GetUniformLocation(m.ShaderProg.ID, gl.Str(uniformName+"\x00")) loc = gl.GetUniformLocation(m.ShaderProg.Id, gl.Str(uniformName+"\x00"))
assert.T(loc != -1, "Uniform '"+uniformName+"' doesn't exist on material "+m.Name) assert.T(loc != -1, "Uniform '"+uniformName+"' doesn't exist on material "+m.Name)
m.UnifLocs[uniformName] = loc m.UnifLocs[uniformName] = loc
return loc return loc
@ -93,46 +124,46 @@ func (m *Material) DisableAttribute(attribName string) {
} }
func (m *Material) SetUnifInt32(uniformName string, val int32) { func (m *Material) SetUnifInt32(uniformName string, val int32) {
gl.ProgramUniform1i(m.ShaderProg.ID, m.GetUnifLoc(uniformName), val) gl.ProgramUniform1i(m.ShaderProg.Id, m.GetUnifLoc(uniformName), val)
} }
func (m *Material) SetUnifFloat32(uniformName string, val float32) { func (m *Material) SetUnifFloat32(uniformName string, val float32) {
gl.ProgramUniform1f(m.ShaderProg.ID, m.GetUnifLoc(uniformName), val) gl.ProgramUniform1f(m.ShaderProg.Id, m.GetUnifLoc(uniformName), val)
} }
func (m *Material) SetUnifVec2(uniformName string, vec2 *gglm.Vec2) { func (m *Material) SetUnifVec2(uniformName string, vec2 *gglm.Vec2) {
gl.ProgramUniform2fv(m.ShaderProg.ID, m.GetUnifLoc(uniformName), 1, &vec2.Data[0]) gl.ProgramUniform2fv(m.ShaderProg.Id, m.GetUnifLoc(uniformName), 1, &vec2.Data[0])
} }
func (m *Material) SetUnifVec3(uniformName string, vec3 *gglm.Vec3) { func (m *Material) SetUnifVec3(uniformName string, vec3 *gglm.Vec3) {
gl.ProgramUniform3fv(m.ShaderProg.ID, m.GetUnifLoc(uniformName), 1, &vec3.Data[0]) gl.ProgramUniform3fv(m.ShaderProg.Id, m.GetUnifLoc(uniformName), 1, &vec3.Data[0])
} }
func (m *Material) SetUnifVec4(uniformName string, vec4 *gglm.Vec4) { func (m *Material) SetUnifVec4(uniformName string, vec4 *gglm.Vec4) {
gl.ProgramUniform4fv(m.ShaderProg.ID, m.GetUnifLoc(uniformName), 1, &vec4.Data[0]) gl.ProgramUniform4fv(m.ShaderProg.Id, m.GetUnifLoc(uniformName), 1, &vec4.Data[0])
} }
func (m *Material) SetUnifMat2(uniformName string, mat2 *gglm.Mat2) { func (m *Material) SetUnifMat2(uniformName string, mat2 *gglm.Mat2) {
gl.ProgramUniformMatrix2fv(m.ShaderProg.ID, m.GetUnifLoc(uniformName), 1, false, &mat2.Data[0][0]) gl.ProgramUniformMatrix2fv(m.ShaderProg.Id, m.GetUnifLoc(uniformName), 1, false, &mat2.Data[0][0])
} }
func (m *Material) SetUnifMat3(uniformName string, mat3 *gglm.Mat3) { func (m *Material) SetUnifMat3(uniformName string, mat3 *gglm.Mat3) {
gl.ProgramUniformMatrix3fv(m.ShaderProg.ID, m.GetUnifLoc(uniformName), 1, false, &mat3.Data[0][0]) gl.ProgramUniformMatrix3fv(m.ShaderProg.Id, m.GetUnifLoc(uniformName), 1, false, &mat3.Data[0][0])
} }
func (m *Material) SetUnifMat4(uniformName string, mat4 *gglm.Mat4) { func (m *Material) SetUnifMat4(uniformName string, mat4 *gglm.Mat4) {
gl.ProgramUniformMatrix4fv(m.ShaderProg.ID, m.GetUnifLoc(uniformName), 1, false, &mat4.Data[0][0]) gl.ProgramUniformMatrix4fv(m.ShaderProg.Id, m.GetUnifLoc(uniformName), 1, false, &mat4.Data[0][0])
} }
func (m *Material) Delete() { func (m *Material) Delete() {
gl.DeleteProgram(m.ShaderProg.ID) gl.DeleteProgram(m.ShaderProg.Id)
} }
func NewMaterial(matName, shaderPath string) *Material { func NewMaterial(matName, shaderPath string) *Material {
shdrProg, err := shaders.LoadAndCompileCombinedShader(shaderPath) shdrProg, err := shaders.LoadAndCompileCombinedShader(shaderPath)
if err != nil { 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)} return &Material{Name: matName, ShaderProg: shdrProg, UnifLocs: make(map[string]int32), AttribLocs: make(map[string]int32)}
@ -142,7 +173,7 @@ func NewMaterialSrc(matName string, shaderSrc []byte) *Material {
shdrProg, err := shaders.LoadAndCompileCombinedShaderSrc(shaderSrc) shdrProg, err := shaders.LoadAndCompileCombinedShaderSrc(shaderSrc)
if err != nil { 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)} return &Material{Name: matName, ShaderProg: shdrProg, UnifLocs: make(map[string]int32), AttribLocs: make(map[string]int32)}

View File

@ -2,7 +2,6 @@ package meshes
import ( import (
"errors" "errors"
"fmt"
"github.com/bloeys/assimp-go/asig" "github.com/bloeys/assimp-go/asig"
"github.com/bloeys/gglm/gglm" "github.com/bloeys/gglm/gglm"
@ -18,7 +17,7 @@ type SubMesh struct {
type Mesh struct { type Mesh struct {
Name string Name string
Buf buffers.Buffer Vao buffers.VertexArray
SubMeshes []SubMesh SubMeshes []SubMesh
} }
@ -36,21 +35,25 @@ func NewMesh(name, modelPath string, postProcessFlags asig.PostProcess) (*Mesh,
mesh := &Mesh{ mesh := &Mesh{
Name: name, Name: name,
Buf: buffers.NewBuffer(), Vao: buffers.NewVertexArray(),
SubMeshes: make([]SubMesh, 0, 1), SubMeshes: make([]SubMesh, 0, 1),
} }
vbo := buffers.NewVertexBuffer()
ibo := buffers.NewIndexBuffer()
// Initial sizes assuming one submesh that has vertex pos+normals+texCoords, and 3 indices per face // Initial sizes assuming one submesh that has vertex pos+normals+texCoords, and 3 indices per face
var vertexBufData []float32 = make([]float32, 0, len(scene.Meshes[0].Vertices)*3*3*2) var vertexBufData []float32 = make([]float32, 0, len(scene.Meshes[0].Vertices)*3*3*2)
var indexBufData []uint32 = make([]uint32, 0, len(scene.Meshes[0].Faces)*3) var indexBufData []uint32 = make([]uint32, 0, len(scene.Meshes[0].Faces)*3)
// fmt.Printf("\nMesh %s has %d meshe(s) with first mesh having %d vertices\n", name, len(scene.Meshes), len(scene.Meshes[0].Vertices))
for i := 0; i < len(scene.Meshes); i++ { for i := 0; i < len(scene.Meshes); i++ {
sceneMesh := scene.Meshes[i] sceneMesh := scene.Meshes[i]
if len(sceneMesh.TexCoords[0]) == 0 { if len(sceneMesh.TexCoords[0]) == 0 {
sceneMesh.TexCoords[0] = make([]gglm.Vec3, len(sceneMesh.Vertices)) sceneMesh.TexCoords[0] = make([]gglm.Vec3, len(sceneMesh.Vertices))
println("Zeroing tex coords for submesh", i)
} }
layoutToUse := []buffers.Element{{ElementType: buffers.DataTypeVec3}, {ElementType: buffers.DataTypeVec3}, {ElementType: buffers.DataTypeVec2}} layoutToUse := []buffers.Element{{ElementType: buffers.DataTypeVec3}, {ElementType: buffers.DataTypeVec3}, {ElementType: buffers.DataTypeVec2}}
@ -59,17 +62,20 @@ func NewMesh(name, modelPath string, postProcessFlags asig.PostProcess) (*Mesh,
} }
if i == 0 { if i == 0 {
mesh.Buf.SetLayout(layoutToUse...) vbo.SetLayout(layoutToUse...)
} else { } else {
// @NOTE: Require that all submeshes have the same vertex buffer layout // @TODO @NOTE: This requirement is because we are using one VAO+VBO for all
firstSubmeshLayout := mesh.Buf.GetLayout() // the meshes and so the buffer must have one format.
assert.T(len(firstSubmeshLayout) == len(layoutToUse), fmt.Sprintf("Vertex layout of submesh %d does not equal vertex layout of the first submesh. Original layout: %v; This layout: %v", i, firstSubmeshLayout, layoutToUse)) //
// If we want to allow different layouts then we can simply create one vbo per layout and put
// meshes of the same layout in the same vbo, and we store the index of the vbo the mesh
// uses in the submesh struct.
firstSubmeshLayout := vbo.GetLayout()
assert.T(len(firstSubmeshLayout) == len(layoutToUse), "Vertex layout of submesh '%d' of mesh '%s' at path '%s' does not equal vertex layout of the first submesh. Original layout: %v; This layout: %v", i, name, modelPath, firstSubmeshLayout, layoutToUse)
for i := 0; i < len(firstSubmeshLayout); i++ { for i := 0; i < len(firstSubmeshLayout); i++ {
if firstSubmeshLayout[i].ElementType != layoutToUse[i].ElementType { assert.T(firstSubmeshLayout[i].ElementType == layoutToUse[i].ElementType, "Vertex layout of submesh '%d' of mesh '%s' at path '%s' does not equal vertex layout of the first submesh. Original layout: %v; This layout: %v", i, name, modelPath, firstSubmeshLayout, layoutToUse)
panic(fmt.Sprintf("Vertex layout of submesh %d does not equal vertex layout of the first submesh. Original layout: %v; This layout: %v", i, firstSubmeshLayout, layoutToUse))
}
} }
} }
@ -82,7 +88,7 @@ func NewMesh(name, modelPath string, postProcessFlags asig.PostProcess) (*Mesh,
mesh.SubMeshes = append(mesh.SubMeshes, SubMesh{ mesh.SubMeshes = append(mesh.SubMeshes, SubMesh{
// Index of the vertex to start from (e.g. if index buffer says use vertex 5, and BaseVertex=3, the vertex used will be vertex 8) // Index of the vertex to start from (e.g. if index buffer says use vertex 5, and BaseVertex=3, the vertex used will be vertex 8)
BaseVertex: int32(len(vertexBufData)*4) / mesh.Buf.Stride, BaseVertex: int32(len(vertexBufData)*4) / vbo.Stride,
// Which index (in the index buffer) to start from // Which index (in the index buffer) to start from
BaseIndex: uint32(len(indexBufData)), BaseIndex: uint32(len(indexBufData)),
// How many indices in this submesh // How many indices in this submesh
@ -93,9 +99,16 @@ func NewMesh(name, modelPath string, postProcessFlags asig.PostProcess) (*Mesh,
indexBufData = append(indexBufData, indices...) indexBufData = append(indexBufData, indices...)
} }
// fmt.Printf("!!! Vertex count: %d; Submeshes: %+v\n", len(vertexBufData)*4/int(mesh.Buf.Stride), mesh.SubMeshes) vbo.SetData(vertexBufData, buffers.BufUsage_Static)
mesh.Buf.SetData(vertexBufData) ibo.SetData(indexBufData)
mesh.Buf.SetIndexBufData(indexBufData)
mesh.Vao.AddVertexBuffer(vbo)
mesh.Vao.SetIndexBuffer(ibo)
// This is needed so that if you load meshes one after the other the
// following mesh doesn't attach its vbo/ibo to this vao
mesh.Vao.UnBind()
return mesh, nil return mesh, nil
} }
@ -119,9 +132,9 @@ type arrToInterleave struct {
func (a *arrToInterleave) get(i int) []float32 { func (a *arrToInterleave) get(i int) []float32 {
assert.T(len(a.V2s) == 0 || len(a.V3s) == 0, "One array should be set in arrToInterleave, but both arrays are set") assert.T(len(a.V2s) == 0 || len(a.V3s) == 0, "One array should be set in arrToInterleave, but multiple arrays are set")
assert.T(len(a.V2s) == 0 || len(a.V4s) == 0, "One array should be set in arrToInterleave, but both arrays are set") assert.T(len(a.V2s) == 0 || len(a.V4s) == 0, "One array should be set in arrToInterleave, but multiple arrays are set")
assert.T(len(a.V3s) == 0 || len(a.V4s) == 0, "One array should be set in arrToInterleave, but both arrays are set") assert.T(len(a.V3s) == 0 || len(a.V4s) == 0, "One array should be set in arrToInterleave, but multiple arrays are set")
if len(a.V2s) > 0 { if len(a.V2s) > 0 {
return a.V2s[i].Data[:] return a.V2s[i].Data[:]
@ -173,7 +186,7 @@ func interleave(arrs ...arrToInterleave) []float32 {
func flattenFaces(faces []asig.Face) []uint32 { func flattenFaces(faces []asig.Face) []uint32 {
assert.T(len(faces[0].Indices) == 3, fmt.Sprintf("Face doesn't have 3 indices. Index count: %v\n", len(faces[0].Indices))) assert.T(len(faces[0].Indices) == 3, "Face doesn't have 3 indices. Index count: %v\n", len(faces[0].Indices))
uints := make([]uint32, len(faces)*3) uints := make([]uint32, len(faces)*3)
for i := 0; i < len(faces); i++ { for i := 0; i < len(faces); i++ {

View File

@ -2,6 +2,7 @@ package rend3dgl
import ( import (
"github.com/bloeys/gglm/gglm" "github.com/bloeys/gglm/gglm"
"github.com/bloeys/nmage/buffers"
"github.com/bloeys/nmage/materials" "github.com/bloeys/nmage/materials"
"github.com/bloeys/nmage/meshes" "github.com/bloeys/nmage/meshes"
"github.com/bloeys/nmage/renderer" "github.com/bloeys/nmage/renderer"
@ -11,23 +12,56 @@ import (
var _ renderer.Render = &Rend3DGL{} var _ renderer.Render = &Rend3DGL{}
type Rend3DGL struct { type Rend3DGL struct {
BoundVao *buffers.VertexArray
BoundMesh *meshes.Mesh BoundMesh *meshes.Mesh
BoundMat *materials.Material 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.Buf.Bind() mesh.Vao.Bind()
r3d.BoundMesh = mesh r.BoundMesh = mesh
} }
if mat != r3d.BoundMat { if mat != r.BoundMat {
mat.Bind() 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++ { 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) gl.DrawElementsBaseVertexWithOffset(gl.TRIANGLES, mesh.SubMeshes[i].IndexCount, gl.UNSIGNED_INT, uintptr(mesh.SubMeshes[i].BaseIndex), mesh.SubMeshes[i].BaseVertex)

View File

@ -2,11 +2,14 @@ package renderer
import ( import (
"github.com/bloeys/gglm/gglm" "github.com/bloeys/gglm/gglm"
"github.com/bloeys/nmage/buffers"
"github.com/bloeys/nmage/materials" "github.com/bloeys/nmage/materials"
"github.com/bloeys/nmage/meshes" "github.com/bloeys/nmage/meshes"
) )
type Render interface { 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() FrameEnd()
} }

Binary file not shown.

View File

@ -13,17 +13,18 @@ out vec3 fragPos;
//MVP = Model View Projection //MVP = Model View Projection
uniform mat4 modelMat; uniform mat4 modelMat;
uniform mat4 viewMat; uniform mat4 projViewMat;
uniform mat4 projMat;
void main() void main()
{ {
vertNormal = mat3(transpose(inverse(modelMat))) * vertNormalIn; vertNormal = mat3(transpose(inverse(modelMat))) * vertNormalIn;
vertUV0 = vertUV0In; vertUV0 = vertUV0In;
vertColor = vertColorIn; 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 //shader:fragment

View 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;
}

View File

@ -0,0 +1,65 @@
//shader:vertex
#version 410
layout(location=0) in vec3 vertPosIn;
uniform mat4 modelMat;
void main()
{
gl_Position = modelMat * vec4(vertPosIn, 1);
}
//shader:geometry
#version 410
layout (triangles) in;
// Cubemap means 6 faces, and the
// input 3 triangle vertices are drawn once per face, so 6*3=18
layout (triangle_strip, max_vertices=18) out;
uniform int cubemapIndex;
uniform mat4 cubemapProjViewMats[6];
out vec4 FragPos;
void main()
{
for(int face = 0; face < 6; ++face)
{
// Built in variable that specifies which cubemap face we are rendering to
// and only works when a cubemap is attached to the active fbo.
//
// We use an additional index here because our fbo has a cubemap array
gl_Layer = (cubemapIndex * 6) + face;
// Transform each triangle vertex
for(int i = 0; i < 3; ++i)
{
FragPos = gl_in[i].gl_Position;
gl_Position = cubemapProjViewMats[face] * FragPos;
EmitVertex();
}
EndPrimitive();
}
}
//shader:fragment
#version 410
in vec4 FragPos;
uniform vec3 lightPos;
uniform float farPlane;
void main()
{
// Get distance between fragment and light source
float lightDistance = length(FragPos.xyz - lightPos);
// Map to [0, 1] by dividing by far plane and use it as our depth
lightDistance = lightDistance / farPlane;
gl_FragDepth = lightDistance;
}

45
res/shaders/screen-quad.glsl Executable file
View 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
View 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);
}

View File

@ -10,25 +10,28 @@ out vec3 vertNormal;
out vec2 vertUV0; out vec2 vertUV0;
out vec3 vertColor; out vec3 vertColor;
out vec3 fragPos; out vec3 fragPos;
out vec4 fragPosDirLight;
//MVP = Model View Projection
uniform mat4 modelMat; uniform mat4 modelMat;
uniform mat4 viewMat; uniform mat4 projViewMat;
uniform mat4 projMat; uniform mat4 dirLightProjViewMat;
void main() void main()
{ {
// @TODO: Calculate this on the CPU and send it as a uniform // @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 // 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 // world space normal. Based on 'One last thing' section from: https://learnopengl.com/Lighting/Basic-Lighting
vertNormal = mat3(transpose(inverse(modelMat))) * vertNormalIn; vertNormal = mat3(transpose(inverse(modelMat))) * vertNormalIn;
vertUV0 = vertUV0In; vertUV0 = vertUV0In;
vertColor = vertColorIn; 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 //shader:fragment
@ -48,6 +51,7 @@ struct DirLight {
vec3 dir; vec3 dir;
vec3 diffuseColor; vec3 diffuseColor;
vec3 specularColor; vec3 specularColor;
sampler2D shadowMap;
}; };
uniform DirLight dirLight; uniform DirLight dirLight;
@ -59,10 +63,12 @@ struct PointLight {
float constant; float constant;
float linear; float linear;
float quadratic; float quadratic;
float farPlane;
}; };
#define NUM_POINT_LIGHTS 16 #define NUM_POINT_LIGHTS 8
uniform PointLight pointLights[NUM_POINT_LIGHTS]; uniform PointLight pointLights[NUM_POINT_LIGHTS];
uniform samplerCubeArray pointLightCubeShadowMaps;
struct SpotLight { struct SpotLight {
vec3 pos; vec3 pos;
@ -83,6 +89,7 @@ in vec3 vertColor;
in vec3 vertNormal; in vec3 vertNormal;
in vec2 vertUV0; in vec2 vertUV0;
in vec3 fragPos; in vec3 fragPos;
in vec4 fragPosDirLight;
out vec4 fragColor; out vec4 fragColor;
@ -93,6 +100,46 @@ vec4 emissionTexColor;
vec3 normalizedVertNorm; vec3 normalizedVertNorm;
vec3 viewDir; vec3 viewDir;
float CalcDirShadow(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'.
// 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 CalcDirLight()
{ {
vec3 lightDir = normalize(-dirLight.dir); vec3 lightDir = normalize(-dirLight.dir);
@ -102,14 +149,35 @@ vec3 CalcDirLight()
vec3 finalDiffuse = diffuseAmount * dirLight.diffuseColor * diffuseTexColor.rgb; vec3 finalDiffuse = diffuseAmount * dirLight.diffuseColor * diffuseTexColor.rgb;
// Specular // Specular
vec3 reflectDir = reflect(-lightDir, normalizedVertNorm); vec3 halfwayDir = normalize(lightDir + viewDir);
float specularAmount = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); float specularAmount = pow(max(dot(normalizedVertNorm, halfwayDir), 0.0), material.shininess);
vec3 finalSpecular = specularAmount * dirLight.specularColor * specularTexColor.rgb; vec3 finalSpecular = specularAmount * dirLight.specularColor * specularTexColor.rgb;
return finalDiffuse + finalSpecular; // Shadow
float shadow = CalcDirShadow(dirLight.shadowMap, lightDir);
return (finalDiffuse + finalSpecular) * (1 - shadow);
} }
vec3 CalcPointLight(PointLight pointLight) float CalcPointShadow(int lightIndex, vec3 lightPos, vec3 lightDir, float farPlane) {
vec3 lightToFrag = fragPos - lightPos;
float closestDepth = texture(pointLightCubeShadowMaps, vec4(lightToFrag, lightIndex)).r;
// We stored depth in the cubemap in the range [0, 1], so now we move back to [0, farPlane]
closestDepth *= farPlane;
// Get depth of current fragment
float currentDepth = length(lightToFrag);
float bias = max(0.05 * (1 - dot(normalizedVertNorm, lightDir)), 0.005);
float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
return shadow;
}
vec3 CalcPointLight(PointLight pointLight, int lightIndex)
{ {
// Ignore unset lights // Ignore unset lights
if (pointLight.constant == 0){ if (pointLight.constant == 0){
@ -123,15 +191,18 @@ vec3 CalcPointLight(PointLight pointLight)
vec3 finalDiffuse = diffuseAmount * pointLight.diffuseColor * diffuseTexColor.rgb; vec3 finalDiffuse = diffuseAmount * pointLight.diffuseColor * diffuseTexColor.rgb;
// Specular // Specular
vec3 reflectDir = reflect(-lightDir, normalizedVertNorm); vec3 halfwayDir = normalize(lightDir + viewDir);
float specularAmount = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); float specularAmount = pow(max(dot(normalizedVertNorm, halfwayDir), 0.0), material.shininess);
vec3 finalSpecular = specularAmount * pointLight.specularColor * specularTexColor.rgb; vec3 finalSpecular = specularAmount * pointLight.specularColor * specularTexColor.rgb;
// attenuation // Attenuation
float distToLight = length(pointLight.pos - fragPos); 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; // Shadow
float shadow = CalcPointShadow(lightIndex, pointLight.pos, lightDir, pointLight.farPlane);
return (finalDiffuse + finalSpecular) * attenuation * (1 - shadow);
} }
vec3 CalcSpotLight(SpotLight light) vec3 CalcSpotLight(SpotLight light)
@ -146,7 +217,7 @@ vec3 CalcSpotLight(SpotLight light)
// light after outer cutoff // light after outer cutoff
float theta = dot(fragToLightDir, normalize(-light.dir)); float theta = dot(fragToLightDir, normalize(-light.dir));
float epsilon = (light.innerCutoff - light.outerCutoff); 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) if (intensity == 0)
return vec3(0); return vec3(0);
@ -156,8 +227,8 @@ vec3 CalcSpotLight(SpotLight light)
vec3 finalDiffuse = diffuseAmount * light.diffuseColor * diffuseTexColor.rgb; vec3 finalDiffuse = diffuseAmount * light.diffuseColor * diffuseTexColor.rgb;
// Specular // Specular
vec3 reflectDir = reflect(-fragToLightDir, normalizedVertNorm); vec3 halfwayDir = normalize(fragToLightDir + viewDir);
float specularAmount = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); float specularAmount = pow(max(dot(normalizedVertNorm, halfwayDir), 0.0), material.shininess);
vec3 finalSpecular = specularAmount * light.specularColor * specularTexColor.rgb; vec3 finalSpecular = specularAmount * light.specularColor * specularTexColor.rgb;
return (finalDiffuse + finalSpecular) * intensity; return (finalDiffuse + finalSpecular) * intensity;
@ -178,7 +249,7 @@ void main()
for (int i = 0; i < NUM_POINT_LIGHTS; i++) for (int i = 0; i < NUM_POINT_LIGHTS; i++)
{ {
finalColor += CalcPointLight(pointLights[i]); finalColor += CalcPointLight(pointLights[i], i);
} }
for (int i = 0; i < NUM_SPOT_LIGHTS; i++) for (int i = 0; i < NUM_SPOT_LIGHTS; i++)

View File

@ -8,13 +8,12 @@ layout(location=3) in vec3 vertColorIn;
out vec3 vertUV0; out vec3 vertUV0;
uniform mat4 viewMat; uniform mat4 projViewMat;
uniform mat4 projMat;
void main() void main()
{ {
vertUV0 = vec3(vertPosIn.x, vertPosIn.y, -vertPosIn.z); 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; gl_Position = pos.xyww;
} }

View File

@ -6,32 +6,48 @@ import (
) )
type ShaderProgram struct { type ShaderProgram struct {
ID uint32 Id uint32
VertShaderID uint32 VertShaderId uint32
FragShaderID uint32 FragShaderId uint32
GeomShaderId uint32
} }
func (sp *ShaderProgram) AttachShader(shader Shader) { func (sp *ShaderProgram) AttachShader(shader Shader) {
gl.AttachShader(sp.ID, shader.ID) gl.AttachShader(sp.Id, shader.Id)
switch shader.ShaderType { switch shader.Type {
case VertexShaderType: case ShaderType_Vertex:
sp.VertShaderID = shader.ID sp.VertShaderId = shader.Id
case FragmentShaderType: case ShaderType_Fragment:
sp.FragShaderID = shader.ID sp.FragShaderId = shader.Id
case ShaderType_Geometry:
sp.GeomShaderId = shader.Id
default: default:
logging.ErrLog.Println("Unknown shader type ", shader.ShaderType, " for ID ", shader.ID) logging.ErrLog.Fatalf("Unknown shader type '%d' for shader id '%d'\n", shader.Type, shader.Id)
} }
} }
func (sp *ShaderProgram) Link() { func (sp *ShaderProgram) Link() {
gl.LinkProgram(sp.ID) gl.LinkProgram(sp.Id)
if sp.VertShaderID != 0 { if sp.VertShaderId != 0 {
gl.DeleteShader(sp.VertShaderID) gl.DeleteShader(sp.VertShaderId)
} }
if sp.FragShaderID != 0 {
gl.DeleteShader(sp.FragShaderID) if sp.FragShaderId != 0 {
gl.DeleteShader(sp.FragShaderId)
}
if sp.GeomShaderId != 0 {
gl.DeleteShader(sp.GeomShaderId)
} }
} }
func (s *ShaderProgram) Bind() {
gl.UseProgram(s.Id)
}
func (s *ShaderProgram) UnBind() {
gl.UseProgram(0)
}

View File

@ -1,10 +1,31 @@
package shaders package shaders
import "github.com/go-gl/gl/v4.1-core/gl" import (
"github.com/bloeys/nmage/logging"
"github.com/go-gl/gl/v4.1-core/gl"
)
type ShaderType int type ShaderType int32
func (s ShaderType) ToGl() uint32 {
switch s {
case ShaderType_Vertex:
return gl.VERTEX_SHADER
case ShaderType_Fragment:
return gl.FRAGMENT_SHADER
case ShaderType_Geometry:
return gl.GEOMETRY_SHADER
default:
logging.ErrLog.Fatalf("Unknown shader type '%d'\n", s)
return 0
}
}
const ( const (
VertexShaderType ShaderType = gl.VERTEX_SHADER ShaderType_Unknown ShaderType = iota
FragmentShaderType ShaderType = gl.FRAGMENT_SHADER ShaderType_Vertex
ShaderType_Fragment
ShaderType_Geometry
) )

View File

@ -3,6 +3,7 @@ package shaders
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt"
"os" "os"
"strings" "strings"
@ -11,12 +12,13 @@ import (
) )
type Shader struct { type Shader struct {
ID uint32 Id uint32
ShaderType ShaderType Type ShaderType
} }
func (s Shader) Delete() { func (s *Shader) Delete() {
gl.DeleteShader(s.ID) gl.DeleteShader(s.Id)
s.Id = 0
} }
func NewShaderProgram() (ShaderProgram, error) { func NewShaderProgram() (ShaderProgram, error) {
@ -26,7 +28,7 @@ func NewShaderProgram() (ShaderProgram, error) {
return ShaderProgram{}, errors.New("failed to create shader program") return ShaderProgram{}, errors.New("failed to create shader program")
} }
return ShaderProgram{ID: id}, nil return ShaderProgram{Id: id}, nil
} }
func LoadAndCompileCombinedShader(shaderPath string) (ShaderProgram, error) { func LoadAndCompileCombinedShader(shaderPath string) (ShaderProgram, error) {
@ -43,8 +45,8 @@ func LoadAndCompileCombinedShader(shaderPath string) (ShaderProgram, error) {
func LoadAndCompileCombinedShaderSrc(shaderSrc []byte) (ShaderProgram, error) { func LoadAndCompileCombinedShaderSrc(shaderSrc []byte) (ShaderProgram, error) {
shaderSources := bytes.Split(shaderSrc, []byte("//shader:")) shaderSources := bytes.Split(shaderSrc, []byte("//shader:"))
if len(shaderSources) == 1 { if len(shaderSources) < 2 {
return ShaderProgram{}, errors.New("failed to read combined shader. Did not find '//shader:vertex' or '//shader:fragment'") return ShaderProgram{}, errors.New("failed to read combined shader. The minimum shader types to have are '//shader:vertex' and '//shader:fragment'")
} }
shdrProg, err := NewShaderProgram() shdrProg, err := NewShaderProgram()
@ -65,12 +67,15 @@ func LoadAndCompileCombinedShaderSrc(shaderSrc []byte) (ShaderProgram, error) {
var shdrType ShaderType var shdrType ShaderType
if bytes.HasPrefix(src, []byte("vertex")) { if bytes.HasPrefix(src, []byte("vertex")) {
src = src[6:] src = src[6:]
shdrType = VertexShaderType shdrType = ShaderType_Vertex
} else if bytes.HasPrefix(src, []byte("fragment")) { } else if bytes.HasPrefix(src, []byte("fragment")) {
src = src[8:] src = src[8:]
shdrType = FragmentShaderType shdrType = ShaderType_Fragment
} else if bytes.HasPrefix(src, []byte("geometry")) {
src = src[8:]
shdrType = ShaderType_Geometry
} else { } else {
return ShaderProgram{}, errors.New("unknown shader type. Must be '//shader:vertex' or '//shader:fragment'") return ShaderProgram{}, errors.New("unknown shader type. Must be '//shader:vertex' or '//shader:fragment' or '//shader:geometry'")
} }
shdr, err := CompileShaderOfType(src, shdrType) shdr, err := CompileShaderOfType(src, shdrType)
@ -83,7 +88,15 @@ func LoadAndCompileCombinedShaderSrc(shaderSrc []byte) (ShaderProgram, error) {
} }
if loadedShdrCount == 0 { if loadedShdrCount == 0 {
return ShaderProgram{}, errors.New("no valid shaders found. Please put '//shader:vertex' or '//shader:fragment' before your shaders") return ShaderProgram{}, errors.New("no valid shaders found. Please put '//shader:vertex' or '//shader:fragment' or '//shader:geometry' before your shaders")
}
if shdrProg.VertShaderId == 0 {
return ShaderProgram{}, errors.New("no valid vertex shader found. Please put '//shader:vertex' before your vertex shader")
}
if shdrProg.FragShaderId == 0 {
return ShaderProgram{}, errors.New("no valid fragment shader found. Please put '//shader:fragment' before your vertex shader")
} }
shdrProg.Link() shdrProg.Link()
@ -92,41 +105,40 @@ func LoadAndCompileCombinedShaderSrc(shaderSrc []byte) (ShaderProgram, error) {
func CompileShaderOfType(shaderSource []byte, shaderType ShaderType) (Shader, error) { func CompileShaderOfType(shaderSource []byte, shaderType ShaderType) (Shader, error) {
shaderID := gl.CreateShader(uint32(shaderType)) shaderId := gl.CreateShader(shaderType.ToGl())
if shaderID == 0 { if shaderId == 0 {
logging.ErrLog.Println("Failed to create shader.") return Shader{}, fmt.Errorf("failed to create OpenGl shader. OpenGl Error=%d", gl.GetError())
return Shader{}, errors.New("failed to create shader")
} }
//Load shader source and compile //Load shader source and compile
shaderCStr, shaderFree := gl.Strs(string(shaderSource) + "\x00") shaderCStr, shaderFree := gl.Strs(string(shaderSource) + "\x00")
defer shaderFree() defer shaderFree()
gl.ShaderSource(shaderID, 1, shaderCStr, nil) gl.ShaderSource(shaderId, 1, shaderCStr, nil)
gl.CompileShader(shaderID) gl.CompileShader(shaderId)
if err := getShaderCompileErrors(shaderID); err != nil { if err := getShaderCompileErrors(shaderId); err != nil {
gl.DeleteShader(shaderID) gl.DeleteShader(shaderId)
return Shader{}, err return Shader{}, err
} }
return Shader{ID: shaderID, ShaderType: shaderType}, nil return Shader{Id: shaderId, Type: shaderType}, nil
} }
func getShaderCompileErrors(shaderID uint32) error { func getShaderCompileErrors(shaderId uint32) error {
var compiledSuccessfully int32 var compiledSuccessfully int32
gl.GetShaderiv(shaderID, gl.COMPILE_STATUS, &compiledSuccessfully) gl.GetShaderiv(shaderId, gl.COMPILE_STATUS, &compiledSuccessfully)
if compiledSuccessfully == gl.TRUE { if compiledSuccessfully == gl.TRUE {
return nil return nil
} }
var logLength int32 var logLength int32
gl.GetShaderiv(shaderID, gl.INFO_LOG_LENGTH, &logLength) gl.GetShaderiv(shaderId, gl.INFO_LOG_LENGTH, &logLength)
log := gl.Str(strings.Repeat("\x00", int(logLength))) log := gl.Str(strings.Repeat("\x00", int(logLength)))
gl.GetShaderInfoLog(shaderID, logLength, nil, log) gl.GetShaderInfoLog(shaderId, logLength, nil, log)
errMsg := gl.GoStr(log) errMsg := gl.GoStr(log)
logging.ErrLog.Println("Compilation of shader with id ", shaderID, " failed. Err: ", errMsg) logging.ErrLog.Println("Compilation of shader with id ", shaderId, " failed. Err: ", errMsg)
return errors.New(errMsg) return errors.New(errMsg)
} }