Compare commits

...

22 Commits

Author SHA1 Message Date
f5a5d72cc4 Working mac arm+mac x86+windows x86 builds 2024-09-15 18:48:20 +04:00
1bec97b128 test 2024-09-15 18:41:31 +04:00
dea2ac965f test 2024-09-15 18:39:49 +04:00
93b5f08352 test 2024-09-15 18:35:55 +04:00
34e19d9c66 Test 2024-09-15 18:27:03 +04:00
372d9ae6b7 Test 2024-09-15 18:25:18 +04:00
befc78c628 Correct windows action 2024-09-15 18:22:44 +04:00
98f8a96bb7 Build on commit 2024-09-15 18:13:55 +04:00
e767f32f2f Update windows gh action 2024-09-15 18:12:53 +04:00
6d94efbf97 Update actions 2024-09-15 17:47:58 +04:00
28f543a579 Update action 2024-09-15 17:35:40 +04:00
2a73a12885 Update action 2024-09-15 17:34:23 +04:00
e4199b8d30 Update imgui 2024-09-15 17:20:37 +04:00
38248822e2 Update to go 1.23 2024-09-15 16:33:22 +04:00
5c98903723 Add all gl BufUsage values+support bufusage in ubo 2024-09-15 16:29:54 +04:00
3cdd40f0a2 Remove test log 2024-09-15 16:18:59 +04:00
9dccb23613 Move ambient color to lightubo 2024-09-15 16:18:03 +04:00
5dfdea9a7b Move spotlights to ubo 2024-09-15 16:09:37 +04:00
bcb46d1699 Fix ubo alignment+Move point lights to ubo 2024-09-15 15:51:26 +04:00
91807a4093 Merge remote-tracking branch 'refs/remotes/origin/dev' into dev 2024-09-15 12:36:32 +04:00
09231c5ebd Update ubo.SetStruct to handle nested struct arrays 2024-09-15 12:33:34 +04:00
0e98dc85f5 Update ubo.SetStruct to handle nested structs 2024-09-15 12:31:26 +04:00
13 changed files with 309 additions and 513 deletions

View File

@ -1,24 +1,69 @@
name: build-nmage
on:
push:
branches:
- dev
create:
workflow_dispatch:
jobs:
build-nmage-macos:
runs-on: macos-12
steps:
build-nmage-windows:
runs-on: windows-latest
steps:
- name: Install golang
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: '>=1.22'
go-version: ">=1.23"
- name: Install assimp-go dll
run: |
Invoke-WebRequest -Uri "https://github.com/bloeys/assimp-go/releases/download/v0.4.2/libassimp-5.dll" -OutFile "C:\Windows\System32\libassimp-5.dll"
- name: Download and setup SDL2
run: |
Invoke-WebRequest -Uri "https://github.com/libsdl-org/SDL/releases/download/release-2.30.7/SDL2-devel-2.30.7-mingw.zip" -OutFile "SDL2.zip"
Expand-Archive -Path "SDL2.zip" -DestinationPath "SDL2"
Copy-Item -Path "SDL2\SDL2-2.30.7\x86_64-w64-mingw32" -Destination "C:\mingw64" -Recurse -Force
- name: Clone nmage
run: git clone https://github.com/bloeys/nmage
- name: Build nmage
working-directory: nmage
run: go build .
build-nmage-macos:
runs-on: ${{ matrix.os }}
strategy:
matrix:
# Based on: https://github.com/actions/runner-images?tab=readme-ov-file#available-images
os:
- macos-13 # x86
- macos-14 # Arm
steps:
- name: Install golang
uses: actions/setup-go@v5
with:
go-version: ">=1.23"
- name: Determine architecture
id: arch
run: |
if [ "$(uname -m)" = "arm64" ]; then
echo "arch=arm64" >> "$GITHUB_OUTPUT"
else
echo "arch=amd64" >> "$GITHUB_OUTPUT"
fi
- 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_${{ steps.arch.outputs.arch }}.dylib -O /usr/local/lib/libassimp.5.dylib
- name: Install SDL2
run: brew install sdl2{,_image,_mixer,_ttf,_gfx} pkg-config
run: brew install sdl2{,_image,_ttf,_gfx} pkg-config
- name: Clone nmage
run: git clone https://github.com/bloeys/nmage

View File

@ -9,25 +9,48 @@ import (
type BufUsage int
// Full docs for buffer usage can be found here: https://registry.khronos.org/OpenGL-Refpages/gl4/html/glBufferData.xhtml
const (
BufUsage_Unknown BufUsage = iota
//Buffer is set only once and used many times
BufUsage_Static
BufUsage_Static_Draw
//Buffer is changed a lot and used many times
BufUsage_Dynamic
BufUsage_Dynamic_Draw
//Buffer is set only once and used by the GPU at most a few times
BufUsage_Stream
BufUsage_Stream_Draw
BufUsage_Static_Read
BufUsage_Dynamic_Read
BufUsage_Stream_Read
BufUsage_Static_Copy
BufUsage_Dynamic_Copy
BufUsage_Stream_Copy
)
func (b BufUsage) ToGL() uint32 {
switch b {
case BufUsage_Static:
case BufUsage_Static_Draw:
return gl.STATIC_DRAW
case BufUsage_Dynamic:
case BufUsage_Dynamic_Draw:
return gl.DYNAMIC_DRAW
case BufUsage_Stream:
case BufUsage_Stream_Draw:
return gl.STREAM_DRAW
case BufUsage_Static_Read:
return gl.STATIC_READ
case BufUsage_Dynamic_Read:
return gl.DYNAMIC_READ
case BufUsage_Stream_Read:
return gl.STREAM_READ
case BufUsage_Static_Copy:
return gl.STATIC_COPY
case BufUsage_Dynamic_Copy:
return gl.DYNAMIC_COPY
case BufUsage_Stream_Copy:
return gl.STREAM_COPY
}
assert.T(false, fmt.Sprintf("Unexpected BufUsage value '%v'", b))

View File

@ -173,7 +173,7 @@ func (dt ElementType) Size() int32 {
}
}
func (dt ElementType) GlStd140BaseAlignment() uint8 {
func (dt ElementType) GlStd140SizeBytes() uint8 {
switch dt {
@ -187,22 +187,22 @@ func (dt ElementType) GlStd140BaseAlignment() uint8 {
case DataTypeVec2:
return 4 * 2
// Vec3 has the same alignment as vec4
case DataTypeVec3:
fallthrough
return 4 * 3
case DataTypeVec4:
return 4 * 4
// Matrices follow: (vec4Alignment) * numColumns
case DataTypeMat2:
return (4 * 4) * 2
return 2 * 2 * 4
case DataTypeMat3:
return (4 * 4) * 3
return 3 * 3 * 4
case DataTypeMat4:
return (4 * 4) * 4
return 4 * 4 * 4
case DataTypeStruct:
logging.ErrLog.Fatalf("ElementType.GlStd140BaseAlignment of DataTypeStruct is not supported")
logging.ErrLog.Fatalf("ElementType.GlStd140SizeBytes of DataTypeStruct is not supported")
return 0
default:

View File

@ -27,9 +27,9 @@ func (ib *IndexBuffer) SetData(values []uint32) {
ib.IndexBufCount = int32(len(values))
if sizeInBytes == 0 {
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, 0, gl.Ptr(nil), BufUsage_Static.ToGL())
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, 0, gl.Ptr(nil), BufUsage_Static_Draw.ToGL())
} else {
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, sizeInBytes, gl.Ptr(&values[0]), BufUsage_Static.ToGL())
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, sizeInBytes, gl.Ptr(&values[0]), BufUsage_Static_Draw.ToGL())
}
}

View File

@ -88,6 +88,8 @@ func addUniformBufferFieldsToArray(startAlignedOffset uint16, arrayToAddTo *[]Un
// offset + (boundary - alignErr) == 100 + (16 - 4) == 112; 112 % 16 == 0, meaning its a boundary
//
// Note that arrays of scalars/vectors are always aligned to 16 bytes, like a vec4
//
// Official spec and full details in subsection 'Standard Uniform Block Layout' at http://www.opengl.org/registry/specs/ARB/uniform_buffer_object.txt
var alignmentBoundary uint16 = 16
if f.Count == 1 {
alignmentBoundary = f.Type.GlStd140AlignmentBoundary()
@ -120,21 +122,36 @@ func addUniformBufferFieldsToArray(startAlignedOffset uint16, arrayToAddTo *[]Un
subfieldsAlignedOffset := uint16(addUniformBufferFieldsToArray(startAlignedOffset+alignedOffset, arrayToAddTo, f.Subfields))
// Pad structs to 16 byte boundary
subfieldsAlignmentError := subfieldsAlignedOffset % 16
if subfieldsAlignmentError != 0 {
subfieldsAlignedOffset += 16 - subfieldsAlignmentError
}
padTo16Boundary(&subfieldsAlignedOffset)
alignedOffset += subfieldsAlignedOffset * f.Count
} else {
alignedOffset = newField.AlignedOffset + alignmentBoundary*f.Count*multiplier - startAlignedOffset
// Elements advance the alignedOffset by their actual byte size.
// Aligned offset is padded if the place its at is not aligned to the boundary required by the next element.
//
// The exception is structs, because fields after a struct field are always aligned at a 16 byte boundary.
//
// For example, a vec3 starting at offset 80, taking 12 bytes, would put the aligned offset at 92.
// If the next element is a float32 (alignment boundary = 4) then no padding is required and
// the float will start at 92 and end at 96.
// However, if the element after the vec3 is a vec3 (alignment boundary = 16), then it would require
// a padding of 4 bytes so that it can start at 96, which is aligned to 16. In this case the second vec3
// would start at 96 and end at 96+12=108.
alignedOffset = newField.AlignedOffset + uint16(f.Type.GlStd140SizeBytes())*f.Count*multiplier - startAlignedOffset
}
}
return uint32(alignedOffset)
}
func padTo16Boundary[T uint16 | int | int32](val *T) {
alignmentError := *val % 16
if alignmentError != 0 {
*val += 16 - alignmentError
}
}
func (ub *UniformBuffer) getField(fieldId uint16, fieldType ElementType) UniformBufferField {
for i := 0; i < len(ub.Fields); i++ {
@ -203,10 +220,10 @@ func (ub *UniformBuffer) SetMat4(fieldId uint16, val *gglm.Mat4) {
}
func (ub *UniformBuffer) SetStruct(inputStruct any) {
setStruct(ub.Fields, make([]byte, ub.Size), inputStruct, 1000_000, false)
setStruct(ub.Fields, make([]byte, ub.Size), inputStruct, 1000_000, false, 0)
}
func setStruct(fields []UniformBufferField, buf []byte, inputStruct any, maxFieldsToConsume int, onlyBufWrite bool) (bytesWritten, fieldsConsumed int) {
func setStruct(fields []UniformBufferField, buf []byte, inputStruct any, maxFieldsToConsume int, onlyBufWrite bool, writeOffset int) (bytesWritten, fieldsConsumed int) {
if len(fields) == 0 {
return
@ -221,8 +238,8 @@ func setStruct(fields []UniformBufferField, buf []byte, inputStruct any, maxFiel
logging.ErrLog.Panicf("UniformBuffer.SetStruct called with a value that is not a struct. Val=%v\n", inputStruct)
}
// Needed because fieldIndex can move faster than struct fields in case of struct fields
structFieldIndex := 0
// structFieldCount := structVal.NumField()
for fieldIndex := 0; fieldIndex < len(fields) && fieldIndex < maxFieldsToConsume; fieldIndex++ {
ubField := &fields[fieldIndex]
@ -240,6 +257,7 @@ func setStruct(fields []UniformBufferField, buf []byte, inputStruct any, maxFiel
isArray := kind == reflect.Slice || kind == reflect.Array
if isArray {
elementType = valField.Type().Elem()
kind = elementType.Kind()
} else {
elementType = valField.Type()
}
@ -249,7 +267,7 @@ func setStruct(fields []UniformBufferField, buf []byte, inputStruct any, maxFiel
}
typeMatches := false
bytesWritten = int(ubField.AlignedOffset)
bytesWritten = int(ubField.AlignedOffset) + writeOffset
switch ubField.Type {
@ -383,15 +401,42 @@ func setStruct(fields []UniformBufferField, buf []byte, inputStruct any, maxFiel
}
case DataTypeStruct:
typeMatches = kind == reflect.Struct
if typeMatches {
setStructBytesWritten, setStructFieldsConsumed := setStruct(fields[fieldIndex+1:], buf, valField.Interface(), valField.NumField(), true)
if isArray {
bytesWritten += setStructBytesWritten
fieldIndex += setStructFieldsConsumed
fieldsConsumed += setStructFieldsConsumed
offset := 0
arrSize := valField.Len()
fieldsToUse := fields[fieldIndex+1:]
for i := 0; i < arrSize; i++ {
setStructBytesWritten, setStructFieldsConsumed := setStruct(fieldsToUse, buf, valField.Index(i).Interface(), elementType.NumField(), true, offset*i)
if offset == 0 {
offset = setStructBytesWritten
padTo16Boundary(&offset)
bytesWritten += offset * arrSize
// Tracking consumed fields is needed because if we have a struct inside another struct
// elementType.NumField() will only give us the fields consumed by the first struct,
// but we need to count all fields of all nested structs inside this one
fieldIndex += setStructFieldsConsumed
fieldsConsumed += setStructFieldsConsumed
}
}
} else {
setStructBytesWritten, setStructFieldsConsumed := setStruct(fields[fieldIndex+1:], buf, valField.Interface(), valField.NumField(), true, writeOffset)
bytesWritten += setStructBytesWritten
fieldIndex += setStructFieldsConsumed
fieldsConsumed += setStructFieldsConsumed
}
}
default:
@ -411,7 +456,7 @@ func setStruct(fields []UniformBufferField, buf []byte, inputStruct any, maxFiel
gl.BufferSubData(gl.UNIFORM_BUFFER, 0, bytesWritten, gl.Ptr(&buf[0]))
}
return bytesWritten - int(fields[0].AlignedOffset), fieldsConsumed
return bytesWritten - int(fields[0].AlignedOffset) - writeOffset, fieldsConsumed
}
func Write32BitIntegerToByteBuf[T uint32 | int32](buf []byte, startIndex *int, val T) {
@ -683,7 +728,7 @@ func ReflectValueMatchesUniformBufferField(v reflect.Value, ubField *UniformBuff
}
}
func NewUniformBuffer(fields []UniformBufferFieldInput) UniformBuffer {
func NewUniformBuffer(fields []UniformBufferFieldInput, usage BufUsage) UniformBuffer {
ub := UniformBuffer{}
@ -695,7 +740,7 @@ func NewUniformBuffer(fields []UniformBufferFieldInput) UniformBuffer {
}
ub.Bind()
gl.BufferData(gl.UNIFORM_BUFFER, int(ub.Size), gl.Ptr(nil), gl.STATIC_DRAW)
gl.BufferData(gl.UNIFORM_BUFFER, int(ub.Size), gl.Ptr(nil), usage.ToGL())
ub.UnBind()
return ub

View File

@ -107,9 +107,9 @@ func (w *Window) handleInputs() {
}
// If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
imIo.SetMouseButtonDown(imgui.MouseButtonLeft, isSdlButtonLeftDown)
imIo.SetMouseButtonDown(imgui.MouseButtonRight, isSdlButtonRightDown)
imIo.SetMouseButtonDown(imgui.MouseButtonMiddle, isSdlButtonMiddleDown)
imIo.SetMouseButtonDown(int(imgui.MouseButtonLeft), isSdlButtonLeftDown)
imIo.SetMouseButtonDown(int(imgui.MouseButtonRight), isSdlButtonRightDown)
imIo.SetMouseButtonDown(int(imgui.MouseButtonMiddle), isSdlButtonMiddleDown)
}
func (w *Window) handleWindowResize() {

4
go.mod
View File

@ -1,6 +1,6 @@
module github.com/bloeys/nmage
go 1.22
go 1.23
require github.com/veandco/go-sdl2 v0.4.35
@ -12,7 +12,7 @@ require (
)
require (
github.com/AllenDang/cimgui-go v0.0.0-20230720025235-f2ff398a66b2
github.com/AllenDang/cimgui-go v0.0.0-20240912193335-545751598105
github.com/mandykoh/prism v0.35.1
)

2
go.sum
View File

@ -1,5 +1,7 @@
github.com/AllenDang/cimgui-go v0.0.0-20230720025235-f2ff398a66b2 h1:3HA/5qD8Rimxz/y1sLyVaM7ws1dzjXzMt4hOBiwHggo=
github.com/AllenDang/cimgui-go v0.0.0-20230720025235-f2ff398a66b2/go.mod h1:iNfbIyOBN8k3XScMxULbrwYbPsXEAUD0Jb6UwrspQb8=
github.com/AllenDang/cimgui-go v0.0.0-20240912193335-545751598105 h1:bhXqv2EG5YCLdgkLSFCpqeVz4cCoNbi4RDFrHrwxQ1o=
github.com/AllenDang/cimgui-go v0.0.0-20240912193335-545751598105/go.mod h1:CYfBRenCaNtSvKVzChYh6gswUSo6c5IUcYeV6eCCRw0=
github.com/bloeys/assimp-go v0.4.4 h1:Yn5e/RpE0Oes0YMBy8O7KkwAO4R/RpgrZPJCt08dVIU=
github.com/bloeys/assimp-go v0.4.4/go.mod h1:my3yRxT7CfOztmvi+0svmwbaqw0KFrxaHxncoyaEIP0=
github.com/bloeys/gglm v0.50.0 h1:DlGLp9z8KMNx+hNR6PjnPmC0HjDRC19QwAKL1iwhOxs=

561
main.go
View File

@ -3,7 +3,6 @@ package main
import (
"fmt"
"os"
"reflect"
"runtime"
"runtime/pprof"
"strconv"
@ -40,9 +39,9 @@ import (
- Normals maps ✅
- HDR ✅
- Fix bad point light acne ✅
- UBO support
- UBO support
- Skeletal animations
- Cascaded shadow mapping
- (?) Cascaded shadow mapping
- In some cases we DO want input even when captured by UI. We need two systems within input package, one filtered and one not✅
- (?) Support OpenGL 4.1 and 4.6, and default to 4.6
- Proper model loading (i.e. load model by reading all its meshes, textures, and so on together)
@ -98,6 +97,10 @@ type PointLight struct {
Radius float32
Falloff float32
// MaxBias is the max shadow bias applied for this light.
// A usual value is 0.05
MaxBias float32
// NearPlane is the distance where if the pixel
// is closer to the light than this distance, no shadow will be casted.
//
@ -105,10 +108,6 @@ type PointLight struct {
// Same idea a camera near plane.
NearPlane float32
// MaxBias is the max shadow bias applied for this light.
// A usual value is 0.05
MaxBias float32
// Far plane is the max distance at which shadows from this
// light will show.
//
@ -217,13 +216,41 @@ type DirLightUboData struct {
Dir gglm.Vec3
DiffuseColor gglm.Vec3
SpecularColor gglm.Vec3
Shadowmap int32
}
type PointLightUboData struct {
Pos gglm.Vec3
DiffuseColor gglm.Vec3
SpecularColor gglm.Vec3
Radius float32
Falloff float32
MaxBias float32
NearPlane float32
FarPlane float32
}
type SpotLightUboData struct {
Pos gglm.Vec3
Dir gglm.Vec3
DiffuseColor gglm.Vec3
SpecularColor gglm.Vec3
InnerCutoff float32
OuterCutoff float32
}
type LightsUboData struct {
DirLight DirLightUboData
DirLight DirLightUboData
PointLights [POINT_LIGHT_COUNT]PointLightUboData
SpotLights [SPOT_LIGHT_COUNT]SpotLightUboData
AmbientColor gglm.Vec3
}
const (
// These must match the shader values
POINT_LIGHT_COUNT = 8
SPOT_LIGHT_COUNT = 4
UNSCALED_WINDOW_WIDTH = 1280
UNSCALED_WINDOW_HEIGHT = 720
@ -307,8 +334,6 @@ var (
dpiScaling float32
// Light settings
ambientColor = gglm.NewVec3(20.0/255, 20.0/255, 20.0/255)
dirLightDir = gglm.NewVec3(0, -0.5, -0.8)
// Lights
dirLight = DirLight{
@ -316,13 +341,13 @@ var (
DiffuseColor: gglm.NewVec3(63.0/255, 63.0/255, 63.0/255),
SpecularColor: gglm.NewVec3(1, 1, 1),
}
pointLights = [...]PointLight{
pointLights = [POINT_LIGHT_COUNT]PointLight{
{
Pos: gglm.NewVec3(0, 4, -3),
DiffuseColor: gglm.NewVec3(1, 0, 0),
SpecularColor: gglm.NewVec3(1, 1, 1),
Falloff: 1.0,
Radius: 10,
Falloff: 1.0,
MaxBias: 0.05,
NearPlane: 0.2,
FarPlane: 20 * pointLightRadiusToFarPlaneRatio,
@ -331,8 +356,8 @@ var (
Pos: gglm.NewVec3(5, 0, 0),
DiffuseColor: gglm.NewVec3(1, 1, 1),
SpecularColor: gglm.NewVec3(1, 1, 1),
Falloff: 1.0,
Radius: 10,
Falloff: 1.0,
MaxBias: 0.05,
NearPlane: 0.2,
FarPlane: 20 * pointLightRadiusToFarPlaneRatio,
@ -341,8 +366,8 @@ var (
Pos: gglm.NewVec3(-3, 4, 3),
DiffuseColor: gglm.NewVec3(1, 1, 1),
SpecularColor: gglm.NewVec3(1, 1, 1),
Falloff: 1.0,
Radius: 10,
Falloff: 1.0,
MaxBias: 0.05,
NearPlane: 0.2,
FarPlane: 20 * pointLightRadiusToFarPlaneRatio,
@ -350,7 +375,7 @@ var (
}
spotLightDir0 = gglm.NewVec3(1.5, -0.9, 0)
spotLights = [...]SpotLight{
spotLights = [SPOT_LIGHT_COUNT]SpotLight{
{
Pos: gglm.NewVec3(-4, 7, 5),
Dir: *spotLightDir0.Normalize(),
@ -588,7 +613,6 @@ func (g *Game) Init() {
whiteMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
whiteMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
whiteMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
whiteMat.SetUnifVec3("ambientColor", &ambientColor)
whiteMat.SetUnifFloat32("material.shininess", whiteMat.Shininess)
whiteMat.SetUnifInt32("dirLightShadowMap", int32(materials.TextureSlot_ShadowMap1))
whiteMat.SetUnifInt32("pointLightCubeShadowMaps", int32(materials.TextureSlot_Cubemap_Array))
@ -603,7 +627,6 @@ func (g *Game) Init() {
containerMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
containerMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
containerMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
containerMat.SetUnifVec3("ambientColor", &ambientColor)
containerMat.SetUnifFloat32("material.shininess", containerMat.Shininess)
containerMat.SetUnifInt32("dirLightShadowMap", int32(materials.TextureSlot_ShadowMap1))
containerMat.SetUnifInt32("pointLightCubeShadowMaps", int32(materials.TextureSlot_Cubemap_Array))
@ -618,7 +641,6 @@ func (g *Game) Init() {
groundMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
groundMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
groundMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
groundMat.SetUnifVec3("ambientColor", &ambientColor)
groundMat.SetUnifFloat32("material.shininess", groundMat.Shininess)
groundMat.SetUnifInt32("dirLightShadowMap", int32(materials.TextureSlot_ShadowMap1))
groundMat.SetUnifInt32("pointLightCubeShadowMaps", int32(materials.TextureSlot_Cubemap_Array))
@ -632,7 +654,6 @@ func (g *Game) Init() {
palleteMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
palleteMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
palleteMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
palleteMat.SetUnifVec3("ambientColor", &ambientColor)
palleteMat.SetUnifFloat32("material.shininess", palleteMat.Shininess)
palleteMat.SetUnifInt32("dirLightShadowMap", int32(materials.TextureSlot_ShadowMap1))
palleteMat.SetUnifInt32("pointLightCubeShadowMaps", int32(materials.TextureSlot_Cubemap_Array))
@ -667,21 +688,21 @@ func (g *Game) Init() {
// 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)
screenQuadVbo.SetData(make([]float32, 6), buffers.BufUsage_Static_Draw)
screenQuadVao = buffers.NewVertexArray()
screenQuadVao.AddVertexBuffer(screenQuadVbo)
// Fbos and lights
g.initFbos()
g.updateLights()
// Ubos
g.initUbos()
// testUbos()
// Initial camera update
cam.Update()
updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
lightsUboData.AmbientColor = gglm.NewVec3(20.0/255, 20.0/255, 20.0/255)
g.applyLightUpdates()
}
func (g *Game) initUbos() {
@ -691,6 +712,7 @@ func (g *Game) initUbos() {
{Id: 0, Type: buffers.DataTypeVec3},
{Id: 1, Type: buffers.DataTypeMat4},
},
buffers.BufUsage_Dynamic_Draw,
)
globalMatricesUbo.SetBindPoint(0)
@ -701,15 +723,49 @@ func (g *Game) initUbos() {
lightsUbo = buffers.NewUniformBuffer(
[]buffers.UniformBufferFieldInput{
{Id: 0, Type: buffers.DataTypeStruct, Subfields: []buffers.UniformBufferFieldInput{
{Id: 1, Type: buffers.DataTypeVec3},
{Id: 2, Type: buffers.DataTypeVec3},
{Id: 3, Type: buffers.DataTypeVec3},
{Id: 4, Type: buffers.DataTypeInt32},
}},
// Dir light
{Id: 0, Type: buffers.DataTypeStruct,
Subfields: []buffers.UniformBufferFieldInput{
{Id: 1, Type: buffers.DataTypeVec3}, // 12 00
{Id: 2, Type: buffers.DataTypeVec3}, // 12 16
{Id: 3, Type: buffers.DataTypeVec3}, // 12 32
},
},
// Point lights
{Id: 5, Type: buffers.DataTypeStruct,
Count: POINT_LIGHT_COUNT,
Subfields: []buffers.UniformBufferFieldInput{
{Id: 6, Type: buffers.DataTypeVec3}, // 12 48
{Id: 7, Type: buffers.DataTypeVec3}, // 12 64
{Id: 8, Type: buffers.DataTypeVec3}, // 12 80
{Id: 9, Type: buffers.DataTypeFloat32}, // 04 92
{Id: 10, Type: buffers.DataTypeFloat32}, // 04 96
{Id: 11, Type: buffers.DataTypeFloat32}, // 04 100
{Id: 12, Type: buffers.DataTypeFloat32}, // 04 104
{Id: 13, Type: buffers.DataTypeFloat32}, // 04 108
},
},
// Spot lights
{Id: 14, Type: buffers.DataTypeStruct,
Count: SPOT_LIGHT_COUNT,
Subfields: []buffers.UniformBufferFieldInput{
{Id: 15, Type: buffers.DataTypeVec3}, // 12 112
{Id: 16, Type: buffers.DataTypeVec3}, // 12 128
{Id: 17, Type: buffers.DataTypeVec3}, // 12 144
{Id: 18, Type: buffers.DataTypeVec3}, // 12 160
{Id: 19, Type: buffers.DataTypeFloat32}, // 04 172
{Id: 20, Type: buffers.DataTypeFloat32}, // 04 176
},
},
// Ambient
{Id: 21, Type: buffers.DataTypeVec3}, // 12 192
},
buffers.BufUsage_Dynamic_Draw,
)
// fmt.Printf("\n==Lights UBO (id=%d)==\nSize=%d\nFields: %+v\n\n", lightsUbo.Id, lightsUbo.Size, lightsUbo.Fields)
lightsUbo.SetBindPoint(1)
groundMat.SetUniformBlockBindingPoint("Lights", 1)
whiteMat.SetUniformBlockBindingPoint("Lights", 1)
@ -717,240 +773,6 @@ func (g *Game) initUbos() {
palleteMat.SetUniformBlockBindingPoint("Lights", 1)
}
func testUbos() {
xx := []int{1, 2, 3, 4}
xx2 := [4]int{1, 2, 3, 4}
fmt.Printf("XX: %v; Kind: %v; Elem Type: %v\n", reflect.ValueOf(xx), reflect.ValueOf(xx).Type().Kind(), reflect.ValueOf(xx).Type().Elem().Kind())
fmt.Printf("XX: %v; Kind: %v; Elem Type: %v\n", reflect.ValueOf(xx2), reflect.ValueOf(xx).Kind(), reflect.ValueOf(xx).Type().Elem().Kind())
ubo := buffers.NewUniformBuffer([]buffers.UniformBufferFieldInput{
{Id: 0, Type: buffers.DataTypeFloat32}, // 04 00
{Id: 1, Type: buffers.DataTypeVec3}, // 16 16
{Id: 2, Type: buffers.DataTypeFloat32}, // 04 32
{Id: 3, Type: buffers.DataTypeMat2}, // 32 48
}) // Total size: 48+32 = 80
ubo.Bind()
println("!!!!!!!!!!!!! Id:", ubo.Id, "; Size:", ubo.Size)
fmt.Printf("%+v\n", ubo.Fields)
ubo.SetFloat32(0, 99)
ubo.SetFloat32(2, 199)
ubo.SetVec3(1, &gglm.Vec3{Data: [3]float32{33, 33, 33}})
ubo.SetMat2(3, &gglm.Mat2{Data: [2][2]float32{{1, 3}, {2, 4}}})
var v gglm.Vec3
var m2 gglm.Mat2
var x, x2 float32
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 0, 4, gl.Ptr(&x))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32, 4, gl.Ptr(&x2))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 16, 12, gl.Ptr(&v.Data[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 48, 16, gl.Ptr(&m2.Data[0][0]))
fmt.Printf("x=%f; x2=%f; v3=%s; m2=%s\n", x, x2, v.String(), m2.String())
ubo.SetVec3(1, &gglm.Vec3{Data: [3]float32{-123, 33, 33}})
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 16, 12, gl.Ptr(&v.Data[0]))
type TestUBO struct {
FirstF32 float32
V3 gglm.Vec3
SecondF32 float32
M2 gglm.Mat2
}
s := TestUBO{
FirstF32: 1.5,
V3: gglm.Vec3{Data: [3]float32{11, 22, 33}},
SecondF32: 9.5,
M2: gglm.Mat2{Data: [2][2]float32{{6, 8}, {7, 9}}},
}
ubo.SetStruct(s)
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 0, 4, gl.Ptr(&x))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32, 4, gl.Ptr(&x2))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 16, 12, gl.Ptr(&v.Data[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 48, 16, gl.Ptr(&m2.Data[0][0]))
fmt.Printf("x=%f; x2=%f; v3=%s; m2=%s\n", x, x2, v.String(), m2.String())
//
// Ubo2
//
type TestUBO2 struct {
F32 float32
V3 gglm.Vec3
F32Slice []float32
I32 int32
I32Slice []int32
V3Slice []gglm.Vec3
V4Slice []gglm.Vec4
Mat2Slice []gglm.Mat2
Mat3Slice []gglm.Mat3
Mat4Slice []gglm.Mat4
}
s2 := TestUBO2{
F32: 1.5,
V3: gglm.Vec3{Data: [3]float32{11, 22, 33}},
F32Slice: []float32{-1, -2, -3, -4},
I32: 55,
I32Slice: []int32{41, 42, 43},
V3Slice: []gglm.Vec3{gglm.NewVec3(1.1, 1.2, 1.3), gglm.NewVec3(2.1, 2.2, 2.3)},
V4Slice: []gglm.Vec4{gglm.NewVec4(1.1, 1.2, 1.3, 1.4), gglm.NewVec4(2.1, 2.2, 2.3, 2.4)},
Mat2Slice: []gglm.Mat2{gglm.NewMat2Diag(1.1), gglm.NewMat2Diag(2.1)},
Mat3Slice: []gglm.Mat3{gglm.NewMat3Diag(3.1), gglm.NewMat3Diag(4.1)},
Mat4Slice: []gglm.Mat4{gglm.NewMat4Diag(5.1), gglm.NewMat4Diag(6.1)},
}
ubo2 := buffers.NewUniformBuffer([]buffers.UniformBufferFieldInput{
{Id: 0, Type: buffers.DataTypeFloat32},
{Id: 1, Type: buffers.DataTypeVec3},
{Id: 2, Type: buffers.DataTypeFloat32, Count: 4},
{Id: 3, Type: buffers.DataTypeInt32},
{Id: 4, Type: buffers.DataTypeInt32, Count: 3},
{Id: 5, Type: buffers.DataTypeVec3, Count: 2},
{Id: 6, Type: buffers.DataTypeVec4, Count: 2},
{Id: 7, Type: buffers.DataTypeMat2, Count: 2},
{Id: 8, Type: buffers.DataTypeMat3, Count: 2},
{Id: 9, Type: buffers.DataTypeMat4, Count: 2},
})
ubo2.Bind()
ubo2.SetStruct(s2)
var someInt32 int32
fArr := [4 * 4]float32{}
i32Arr := [3 * 4]int32{}
vec3Slice := [2 * 4]float32{}
vec4Slice := [2 * 4]float32{}
mat2Slice := [2 * 2 * 4]float32{}
mat3Slice := [2 * 3 * 4]float32{}
mat4Slice := [2 * 4 * 4]float32{}
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 0, 4, gl.Ptr(&x))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 16, 12, gl.Ptr(&v.Data[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32, 16*4, gl.Ptr(&fArr[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4, 4, gl.Ptr(&someInt32))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4+16, 16*3, gl.Ptr(&i32Arr[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4+16+16*3, 16*2, gl.Ptr(&vec3Slice[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4+16+16*3+16*2, 16*2, gl.Ptr(&vec4Slice[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4+16+16*3+16*2+16*2, 2*16*2, gl.Ptr(&mat2Slice[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4+16+16*3+16*2+16*2+2*16*2, 2*16*3, gl.Ptr(&mat3Slice[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4+16+16*3+16*2+16*2+2*16*2+2*16*3, 2*16*4, gl.Ptr(&mat4Slice[0]))
fmt.Printf("f32=%f; v3=%s; f32Slice=%v; i32=%d; i32Arr=%v; v3Slice=%v; v4Slice=%v; mat2Slice=%v; mat3Slice=%v; mat4Slice=%v\n", x, v.String(), fArr, someInt32, i32Arr, vec3Slice, vec4Slice, mat2Slice, mat3Slice, mat4Slice)
//
// Ubo3
//
type TestUBO3_Z struct {
V int32
}
type TestUBO3_Y struct {
V TestUBO3_Z
}
type TestUBO3_X struct {
V TestUBO3_Y
}
type TestUBO3_0 struct {
X int32
}
type TestUBO3_1 struct {
F32 float32
V3 gglm.Vec3
Zero TestUBO3_0
}
type TestUBO3_2 struct {
F32 float32
S TestUBO3_1
XX int32
Z2 TestUBO3_0
XX2 int32
Abcd TestUBO3_X
}
ubo3 := buffers.NewUniformBuffer([]buffers.UniformBufferFieldInput{
{Id: 0, Type: buffers.DataTypeFloat32}, // 04 00
{Id: 1, Type: buffers.DataTypeStruct, Subfields: []buffers.UniformBufferFieldInput{ // 00 16
{Id: 2, Type: buffers.DataTypeFloat32}, // 04 16
{Id: 3, Type: buffers.DataTypeVec3}, // 16 32
{Id: 4, Type: buffers.DataTypeStruct, Subfields: []buffers.UniformBufferFieldInput{ // 00 48
{Id: 5, Type: buffers.DataTypeInt32}, // 04 48
}},
}},
{Id: 6, Type: buffers.DataTypeInt32}, // 04 64
{Id: 7, Type: buffers.DataTypeStruct, Subfields: []buffers.UniformBufferFieldInput{ // 00 80
{Id: 8, Type: buffers.DataTypeInt32}, // 04 80
}},
{Id: 9, Type: buffers.DataTypeInt32}, // 04 96
{Id: 10, Type: buffers.DataTypeStruct, Subfields: []buffers.UniformBufferFieldInput{ // 00 112
{Id: 11, Type: buffers.DataTypeStruct, Subfields: []buffers.UniformBufferFieldInput{ // 00 112
{Id: 12, Type: buffers.DataTypeStruct, Subfields: []buffers.UniformBufferFieldInput{ // 00 112
{Id: 13, Type: buffers.DataTypeInt32}, // 04 112
}},
}},
}},
}) // 116
ubo3.Bind()
ubo3.SetBindPoint(2)
groundMat.SetUniformBlockBindingPoint("Test2", 2)
fmt.Printf("\n==UBO3 (id=%d)==\nSize=%d\nFields: %+v\n\n", ubo3.Id, ubo3.Size, ubo3.Fields)
s3 := TestUBO3_2{
F32: 76.1,
S: TestUBO3_1{
F32: 89.9,
V3: gglm.NewVec3(7.1, 7.2, 7.3),
Zero: TestUBO3_0{
X: 33,
},
},
XX: 41,
Z2: TestUBO3_0{
X: 8,
},
XX2: 321,
Abcd: TestUBO3_X{
V: TestUBO3_Y{
V: TestUBO3_Z{
V: 9911,
},
},
},
}
ubo3.SetStruct(s3)
ubo3F32 := float32(0.0)
ubo3SF32 := float32(0.0)
ubo3SV3 := gglm.Vec3{}
ubo3SZeroX := 0
ubo3Xx := 0
ubo3SZ2X := 0
ubo3Xx2 := 0
ubo3AbcdV := 0
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 0, 4, gl.Ptr(&ubo3F32))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 16, 4, gl.Ptr(&ubo3SF32))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32, 16, gl.Ptr(&ubo3SV3.Data[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 48, 4, gl.Ptr(&ubo3SZeroX))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 64, 4, gl.Ptr(&ubo3Xx))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 64, 4, gl.Ptr(&ubo3Xx))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 80, 4, gl.Ptr(&ubo3SZ2X))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 96, 4, gl.Ptr(&ubo3Xx2))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 112, 4, gl.Ptr(&ubo3AbcdV))
fmt.Printf("ubo3_f32=%f\nubo3_s_f32=%f\nubo3_s_v3=%s\nubo3_s_zero_x=%d\nubo3_xx=%d\nubo3_z2_x=%d\nubo3_xx2=%d\nubo3_abcd_v=%d\n", ubo3F32, ubo3SF32, ubo3SV3.String(), ubo3SZeroX, ubo3Xx, ubo3SZ2X, ubo3Xx2, ubo3AbcdV)
}
func (g *Game) initFbos() {
// @TODO: Resize window sized fbos on window resize
@ -1015,73 +837,22 @@ func (g *Game) initFbos() {
assert.T(hdrFbo.IsComplete(), "Hdr fbo is not complete after init")
}
func (g *Game) updateLights() {
// applyLightUpdates updates materials and light ubo using
// data from the game's light structs
func (g *Game) applyLightUpdates() {
// Directional light
lightsUboData.DirLight = DirLightUboData{
Dir: dirLight.Dir,
DiffuseColor: dirLight.DiffuseColor,
SpecularColor: dirLight.SpecularColor,
Shadowmap: int32(dirLightDepthMapFbo.Attachments[0].Id),
}
whiteMat.ShadowMapTex1 = uint32(lightsUboData.DirLight.Shadowmap)
containerMat.ShadowMapTex1 = uint32(lightsUboData.DirLight.Shadowmap)
groundMat.ShadowMapTex1 = uint32(lightsUboData.DirLight.Shadowmap)
palleteMat.ShadowMapTex1 = uint32(lightsUboData.DirLight.Shadowmap)
lightsUboData.DirLight = DirLightUboData(dirLight)
whiteMat.ShadowMapTex1 = dirLightDepthMapFbo.Attachments[0].Id
containerMat.ShadowMapTex1 = dirLightDepthMapFbo.Attachments[0].Id
groundMat.ShadowMapTex1 = dirLightDepthMapFbo.Attachments[0].Id
palleteMat.ShadowMapTex1 = dirLightDepthMapFbo.Attachments[0].Id
// Point lights
for i := 0; i < len(pointLights); i++ {
p := &pointLights[i]
indexString := "pointLights[" + strconv.Itoa(i) + "]"
posStr := indexString + ".pos"
whiteMat.SetUnifVec3(posStr, &p.Pos)
containerMat.SetUnifVec3(posStr, &p.Pos)
groundMat.SetUnifVec3(posStr, &p.Pos)
palleteMat.SetUnifVec3(posStr, &p.Pos)
diffuseStr := indexString + ".diffuseColor"
whiteMat.SetUnifVec3(diffuseStr, &p.DiffuseColor)
containerMat.SetUnifVec3(diffuseStr, &p.DiffuseColor)
groundMat.SetUnifVec3(diffuseStr, &p.DiffuseColor)
palleteMat.SetUnifVec3(diffuseStr, &p.DiffuseColor)
specularStr := indexString + ".specularColor"
whiteMat.SetUnifVec3(specularStr, &p.SpecularColor)
containerMat.SetUnifVec3(specularStr, &p.SpecularColor)
groundMat.SetUnifVec3(specularStr, &p.SpecularColor)
palleteMat.SetUnifVec3(specularStr, &p.SpecularColor)
falloffStr := indexString + ".falloff"
whiteMat.SetUnifFloat32(falloffStr, p.Falloff)
containerMat.SetUnifFloat32(falloffStr, p.Falloff)
groundMat.SetUnifFloat32(falloffStr, p.Falloff)
palleteMat.SetUnifFloat32(falloffStr, p.Falloff)
radiusStr := indexString + ".radius"
whiteMat.SetUnifFloat32(radiusStr, p.Radius)
containerMat.SetUnifFloat32(radiusStr, p.Radius)
groundMat.SetUnifFloat32(radiusStr, p.Radius)
palleteMat.SetUnifFloat32(radiusStr, p.Radius)
maxBiasStr := indexString + ".maxBias"
whiteMat.SetUnifFloat32(maxBiasStr, p.MaxBias)
containerMat.SetUnifFloat32(maxBiasStr, p.MaxBias)
groundMat.SetUnifFloat32(maxBiasStr, p.MaxBias)
palleteMat.SetUnifFloat32(maxBiasStr, p.MaxBias)
nearPlaneStr := indexString + ".nearPlane"
whiteMat.SetUnifFloat32(nearPlaneStr, p.NearPlane)
containerMat.SetUnifFloat32(nearPlaneStr, p.NearPlane)
groundMat.SetUnifFloat32(nearPlaneStr, p.NearPlane)
palleteMat.SetUnifFloat32(nearPlaneStr, p.NearPlane)
farPlaneStr := indexString + ".farPlane"
whiteMat.SetUnifFloat32(farPlaneStr, p.FarPlane)
containerMat.SetUnifFloat32(farPlaneStr, p.FarPlane)
groundMat.SetUnifFloat32(farPlaneStr, p.FarPlane)
palleteMat.SetUnifFloat32(farPlaneStr, p.FarPlane)
lightsUboData.PointLights[i] = PointLightUboData(*p)
}
whiteMat.CubemapArrayTex = pointLightDepthMapFbo.Attachments[0].Id
@ -1096,37 +867,14 @@ func (g *Game) updateLights() {
innerCutoffCos := l.InnerCutoffCos()
outerCutoffCos := l.OuterCutoffCos()
indexString := "spotLights[" + strconv.Itoa(i) + "]"
whiteMat.SetUnifVec3(indexString+".pos", &l.Pos)
containerMat.SetUnifVec3(indexString+".pos", &l.Pos)
groundMat.SetUnifVec3(indexString+".pos", &l.Pos)
palleteMat.SetUnifVec3(indexString+".pos", &l.Pos)
whiteMat.SetUnifVec3(indexString+".dir", &l.Dir)
containerMat.SetUnifVec3(indexString+".dir", &l.Dir)
groundMat.SetUnifVec3(indexString+".dir", &l.Dir)
palleteMat.SetUnifVec3(indexString+".dir", &l.Dir)
whiteMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
containerMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
groundMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
palleteMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
whiteMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
containerMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
groundMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
palleteMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
whiteMat.SetUnifFloat32(indexString+".innerCutoff", innerCutoffCos)
containerMat.SetUnifFloat32(indexString+".innerCutoff", innerCutoffCos)
groundMat.SetUnifFloat32(indexString+".innerCutoff", innerCutoffCos)
palleteMat.SetUnifFloat32(indexString+".innerCutoff", innerCutoffCos)
whiteMat.SetUnifFloat32(indexString+".outerCutoff", outerCutoffCos)
containerMat.SetUnifFloat32(indexString+".outerCutoff", outerCutoffCos)
groundMat.SetUnifFloat32(indexString+".outerCutoff", outerCutoffCos)
palleteMat.SetUnifFloat32(indexString+".outerCutoff", outerCutoffCos)
lightsUboData.SpotLights[i] = SpotLightUboData{
Pos: l.Pos,
Dir: l.Dir,
DiffuseColor: l.DiffuseColor,
SpecularColor: l.SpecularColor,
InnerCutoff: innerCutoffCos,
OuterCutoff: outerCutoffCos,
}
}
whiteMat.ShadowMapTexArray1 = spotLightDepthMapFbo.Attachments[0].Id
@ -1134,6 +882,7 @@ func (g *Game) updateLights() {
groundMat.ShadowMapTexArray1 = spotLightDepthMapFbo.Attachments[0].Id
palleteMat.ShadowMapTexArray1 = spotLightDepthMapFbo.Attachments[0].Id
// Apply changes
lightsUbo.Bind()
lightsUbo.SetStruct(lightsUboData)
}
@ -1159,7 +908,7 @@ func (g *Game) showDebugWindow() {
imgui.Begin("Debug controls")
imgui.PushStyleColorVec4(imgui.ColText, imgui.NewColor(1, 1, 0, 1).Value)
imgui.PushStyleColorVec4(imgui.ColText, imgui.NewColor(1, 1, 0, 1).FieldValue)
imgui.LabelText("FPS", fmt.Sprint(timing.GetAvgFPS()))
imgui.PopStyleColor()
@ -1207,11 +956,8 @@ func (g *Game) showDebugWindow() {
// Ambient light
imgui.Text("Ambient Light")
if imgui.ColorEdit3("Ambient Color", &ambientColor.Data) {
whiteMat.SetUnifVec3("ambientColor", &ambientColor)
containerMat.SetUnifVec3("ambientColor", &ambientColor)
groundMat.SetUnifVec3("ambientColor", &ambientColor)
palleteMat.SetUnifVec3("ambientColor", &ambientColor)
if imgui.ColorEdit3("Ambient Color", &lightsUboData.AmbientColor.Data) {
updateLights = true
}
imgui.Spacing()
@ -1273,73 +1019,29 @@ func (g *Game) showDebugWindow() {
continue
}
indexString := "pointLights[" + indexNumString + "]"
if imgui.DragFloat3("Pos", &pl.Pos.Data) {
posStr := indexString + ".pos"
whiteMat.SetUnifVec3(posStr, &pl.Pos)
containerMat.SetUnifVec3(posStr, &pl.Pos)
groundMat.SetUnifVec3(posStr, &pl.Pos)
palleteMat.SetUnifVec3(posStr, &pl.Pos)
updateLights = true
}
if imgui.ColorEdit3("Diffuse Color", &pl.DiffuseColor.Data) {
diffStr := indexString + ".diffuseColor"
whiteMat.SetUnifVec3(diffStr, &pl.DiffuseColor)
containerMat.SetUnifVec3(diffStr, &pl.DiffuseColor)
groundMat.SetUnifVec3(diffStr, &pl.DiffuseColor)
palleteMat.SetUnifVec3(diffStr, &pl.DiffuseColor)
updateLights = true
}
if imgui.ColorEdit3("Specular Color", &pl.SpecularColor.Data) {
specularStr := indexString + ".specularColor"
whiteMat.SetUnifVec3(specularStr, &pl.SpecularColor)
containerMat.SetUnifVec3(specularStr, &pl.SpecularColor)
groundMat.SetUnifVec3(specularStr, &pl.SpecularColor)
palleteMat.SetUnifVec3(specularStr, &pl.SpecularColor)
updateLights = true
}
if imgui.DragFloatV("Falloff", &pl.Falloff, 0.1, 0, 100, "%.3f", imgui.SliderFlagsNone) {
falloffStr := indexString + ".falloff"
whiteMat.SetUnifFloat32(falloffStr, pl.Falloff)
containerMat.SetUnifFloat32(falloffStr, pl.Falloff)
groundMat.SetUnifFloat32(falloffStr, pl.Falloff)
palleteMat.SetUnifFloat32(falloffStr, pl.Falloff)
updateLights = true
}
if imgui.DragFloatV("Radius", &pl.Radius, 0.2, 0, 500, "%.3f", imgui.SliderFlagsNone) {
radiusStr := indexString + ".radius"
whiteMat.SetUnifFloat32(radiusStr, pl.Radius)
containerMat.SetUnifFloat32(radiusStr, pl.Radius)
groundMat.SetUnifFloat32(radiusStr, pl.Radius)
palleteMat.SetUnifFloat32(radiusStr, pl.Radius)
farPlaneStr := indexString + ".farPlane"
updateLights = true
pl.FarPlane = pl.Radius * pointLightRadiusToFarPlaneRatio
whiteMat.SetUnifFloat32(farPlaneStr, pl.FarPlane)
containerMat.SetUnifFloat32(farPlaneStr, pl.FarPlane)
groundMat.SetUnifFloat32(farPlaneStr, pl.FarPlane)
palleteMat.SetUnifFloat32(farPlaneStr, pl.FarPlane)
}
if imgui.DragFloatV("Max Bias", &pl.MaxBias, 0.01, 0, 10, "%.3f", imgui.SliderFlagsNone) {
maxBiasStr := indexString + ".maxBias"
whiteMat.SetUnifFloat32(maxBiasStr, pl.MaxBias)
containerMat.SetUnifFloat32(maxBiasStr, pl.MaxBias)
groundMat.SetUnifFloat32(maxBiasStr, pl.MaxBias)
palleteMat.SetUnifFloat32(maxBiasStr, pl.MaxBias)
updateLights = true
}
imgui.TreePop()
@ -1362,34 +1064,20 @@ func (g *Game) showDebugWindow() {
continue
}
indexString := "spotLights[" + indexNumString + "]"
if imgui.DragFloat3("Pos", &l.Pos.Data) {
whiteMat.SetUnifVec3(indexString+".pos", &l.Pos)
containerMat.SetUnifVec3(indexString+".pos", &l.Pos)
groundMat.SetUnifVec3(indexString+".pos", &l.Pos)
palleteMat.SetUnifVec3(indexString+".pos", &l.Pos)
updateLights = true
}
if imgui.DragFloat3("Dir", &l.Dir.Data) {
whiteMat.SetUnifVec3(indexString+".dir", &l.Dir)
containerMat.SetUnifVec3(indexString+".dir", &l.Dir)
groundMat.SetUnifVec3(indexString+".dir", &l.Dir)
palleteMat.SetUnifVec3(indexString+".dir", &l.Dir)
updateLights = true
}
if imgui.ColorEdit3("Diffuse Color", &l.DiffuseColor.Data) {
whiteMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
containerMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
groundMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
palleteMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
updateLights = true
}
if imgui.ColorEdit3("Specular Color", &l.SpecularColor.Data) {
whiteMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
containerMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
groundMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
palleteMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
updateLights = true
}
if imgui.DragFloatRange2V(
@ -1403,18 +1091,7 @@ func (g *Game) showDebugWindow() {
"%.3f",
imgui.SliderFlagsNone,
) {
cos := l.InnerCutoffCos()
whiteMat.SetUnifFloat32(indexString+".innerCutoff", cos)
containerMat.SetUnifFloat32(indexString+".innerCutoff", cos)
groundMat.SetUnifFloat32(indexString+".innerCutoff", cos)
palleteMat.SetUnifFloat32(indexString+".innerCutoff", cos)
cos = l.OuterCutoffCos()
whiteMat.SetUnifFloat32(indexString+".outerCutoff", cos)
containerMat.SetUnifFloat32(indexString+".outerCutoff", cos)
groundMat.SetUnifFloat32(indexString+".outerCutoff", cos)
palleteMat.SetUnifFloat32(indexString+".outerCutoff", cos)
updateLights = true
}
imgui.DragFloat("Spot Near Plane", &l.NearPlane)
@ -1427,7 +1104,7 @@ func (g *Game) showDebugWindow() {
}
if updateLights {
g.updateLights()
g.applyLightUpdates()
}
// Demo fbo

View File

@ -146,7 +146,8 @@ func (m *Material) GetAttribLoc(attribName string) int32 {
return loc
}
loc = gl.GetAttribLocation(m.ShaderProg.Id, gl.Str(attribName+"\x00"))
name := gl.Str(attribName + "\x00")
loc = gl.GetAttribLocation(m.ShaderProg.Id, name)
assert.T(loc != -1, "Attribute '"+attribName+"' doesn't exist on material "+m.Name)
m.AttribLocs[attribName] = loc
return loc
@ -159,7 +160,8 @@ func (m *Material) GetUnifLoc(uniformName string) int32 {
return loc
}
loc = gl.GetUniformLocation(m.ShaderProg.Id, gl.Str(uniformName+"\x00"))
name := gl.Str(uniformName + "\x00")
loc = gl.GetUniformLocation(m.ShaderProg.Id, name)
assert.T(loc != -1, "Uniform '"+uniformName+"' doesn't exist on material "+m.Name)
m.UnifLocs[uniformName] = loc
return loc

View File

@ -151,7 +151,7 @@ func NewMesh(name, modelPath string, postProcessFlags asig.PostProcess) (Mesh, e
indexBufData = append(indexBufData, indices...)
}
vbo.SetData(vertexBufData, buffers.BufUsage_Static)
vbo.SetData(vertexBufData, buffers.BufUsage_Static_Draw)
ibo.SetData(indexBufData)
mesh.Vao.AddVertexBuffer(vbo)

View File

@ -27,8 +27,8 @@ struct PointLight {
vec3 pos;
vec3 diffuseColor;
vec3 specularColor;
float falloff;
float radius;
float falloff;
float maxBias;
float nearPlane;
float farPlane;
@ -50,13 +50,14 @@ layout (std140) uniform GlobalMatrices {
layout (std140) uniform Lights {
DirLight dirLight;
PointLight pointLights[NUM_POINT_LIGHTS];
SpotLight spotLights[NUM_SPOT_LIGHTS];
vec3 ambientColor;
};
//
// Uniforms
//
uniform PointLight pointLights[NUM_POINT_LIGHTS];
uniform SpotLight spotLights[NUM_SPOT_LIGHTS];
uniform mat4 modelMat;
uniform mat4 dirLightProjViewMat;
uniform mat4 spotLightProjViewMats[NUM_SPOT_LIGHTS];
@ -78,16 +79,6 @@ out vec3 tangentSpotLightPositions[NUM_SPOT_LIGHTS];
out vec3 tangentSpotLightDirections[NUM_SPOT_LIGHTS];
out vec3 tangentPointLightPositions[NUM_POINT_LIGHTS];
struct Test1 {
float ff;
vec3 v3;
};
layout (std140) uniform Test2 {
float f1;
Test1 s;
};
void main()
{
vertUV0 = vertUV0In;
@ -180,13 +171,12 @@ struct PointLight {
vec3 pos;
vec3 diffuseColor;
vec3 specularColor;
float falloff;
float radius;
float falloff;
float maxBias;
float nearPlane;
float farPlane;
};
uniform PointLight pointLights[NUM_POINT_LIGHTS];
uniform samplerCubeArray pointLightCubeShadowMaps;
struct SpotLight {
@ -197,7 +187,6 @@ struct SpotLight {
float innerCutoff;
float outerCutoff;
};
uniform SpotLight spotLights[NUM_SPOT_LIGHTS];
uniform sampler2DArray spotLightShadowMaps;
layout (std140) uniform GlobalMatrices {
@ -207,10 +196,11 @@ layout (std140) uniform GlobalMatrices {
layout (std140) uniform Lights {
DirLight dirLight;
PointLight pointLights[NUM_POINT_LIGHTS];
SpotLight spotLights[NUM_SPOT_LIGHTS];
vec3 ambientColor;
};
uniform vec3 ambientColor = vec3(0.2, 0.2, 0.2);
//
// Outputs
//

View File

@ -1,6 +1,17 @@
package nmageimgui
import (
"unsafe"
// The following is included just so we can get
// the c imports and cgo configs defined here: https://github.com/AllenDang/cimgui-go/blob/main/sdlbackend/sdl_backend.go
//
// This is needed because AllenDang/cimgui-go links sdl2 statically, which requires us
// to explicitly define all the libs it needs, which isn't normally needed if we were dynamically linking.
//
// Without this, we get compilation errors as sdl2 can't find libs it relies on.
_ "github.com/AllenDang/cimgui-go/sdlbackend"
imgui "github.com/AllenDang/cimgui-go"
"github.com/bloeys/gglm/gglm"
"github.com/bloeys/nmage/materials"
@ -129,7 +140,7 @@ func (i *ImguiInfo) AddFontTTF(fontPath string, fontSize float32, fontConfig *im
fontConfigToUse := imgui.NewFontConfig()
if fontConfig != nil {
fontConfigToUse = *fontConfig
fontConfigToUse = fontConfig
}
glyphRangesToUse := imgui.NewGlyphRange()
@ -141,13 +152,13 @@ func (i *ImguiInfo) AddFontTTF(fontPath string, fontSize float32, fontConfig *im
a := imIO.Fonts()
f := a.AddFontFromFileTTFV(fontPath, fontSize, fontConfigToUse, glyphRangesToUse.Data())
pixels, width, height, _ := a.GetTextureDataAsAlpha8()
pixels, width, height, _ := a.TextureDataAsAlpha8()
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, *i.TexID)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, int32(width), int32(height), 0, gl.RED, gl.UNSIGNED_BYTE, pixels)
return f
return *f
}
const DefaultImguiShader = `
@ -212,7 +223,7 @@ func NewImGui(shaderPath string) ImguiInfo {
}
imguiInfo := ImguiInfo{
ImCtx: imgui.CreateContext(),
ImCtx: *imgui.CreateContext(),
Mat: imguiMat,
TexID: new(uint32),
}
@ -233,11 +244,12 @@ func NewImGui(shaderPath string) ImguiInfo {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.PixelStorei(gl.UNPACK_ROW_LENGTH, 0)
pixels, width, height, _ := io.Fonts().GetTextureDataAsAlpha8()
pixels, width, height, _ := io.Fonts().TextureDataAsAlpha8()
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, int32(width), int32(height), 0, gl.RED, gl.UNSIGNED_BYTE, pixels)
// Store our identifier
io.Fonts().SetTexID(imgui.TextureID(imguiInfo.TexID))
io.Fonts().SetTexID(imgui.TextureID{Data: uintptr(unsafe.Pointer(imguiInfo.TexID))})
//Shader attributes
imguiInfo.Mat.Bind()