From 340b45f8e07059981d3261ecc2f123c2b93eda71 Mon Sep 17 00:00:00 2001 From: a pirate Date: Thu, 26 Oct 2023 08:44:31 -0600 Subject: [PATCH 01/15] Add v2 TransEmiss to gpu_mapped_element_buffer --- src/engine/debug_draw.cpp | 22 +++----- src/engine/editor.cpp | 2 +- src/engine/entity.cpp | 8 +-- src/engine/gpu_mapped_buffer.cpp | 44 ++++++++++------ src/engine/loaders/vox.cpp | 4 +- src/engine/mesh.cpp | 33 ++++++------ src/engine/mesh.h | 88 ++++++++++++++++++-------------- src/engine/model.h | 2 +- src/engine/render.cpp | 4 +- src/engine/render.h | 3 +- src/engine/triangle.cpp | 20 +++----- src/engine/world_chunk.cpp | 70 +++++++++++++------------ src/tools/asset_packer.cpp | 2 +- 13 files changed, 159 insertions(+), 143 deletions(-) diff --git a/src/engine/debug_draw.cpp b/src/engine/debug_draw.cpp index 670c4f377..d4a8dfdf0 100644 --- a/src/engine/debug_draw.cpp +++ b/src/engine/debug_draw.cpp @@ -19,9 +19,11 @@ DEBUG_DrawLine( untextured_3d_geometry_buffer *Mesh, v3 P1, v3 P2, u32 ColorInde /* P1.x = P1.x - (Thickness/2.0f); */ /* P2.x = P2.x - (Thickness/2.0f); */ - v4 FaceColors[VERTS_PER_FACE]; + v3 FaceColors[VERTS_PER_FACE]; FillColorArray(ColorIndex, FaceColors, DefaultPalette, VERTS_PER_FACE); + v2 TransEmiss[VERTS_PER_FACE] = {}; + r32 HalfThickness = Thickness/2.f; { v3 localVertexData[] = @@ -36,11 +38,7 @@ DEBUG_DrawLine( untextured_3d_geometry_buffer *Mesh, v3 P1, v3 P2, u32 ColorInde }; - BufferVertsChecked(Mesh, - 6, - localVertexData, - localNormalData, - FaceColors); + BufferVertsChecked(Mesh, 6, localVertexData, localNormalData, FaceColors, TransEmiss); } { @@ -56,11 +54,7 @@ DEBUG_DrawLine( untextured_3d_geometry_buffer *Mesh, v3 P1, v3 P2, u32 ColorInde }; - BufferVertsChecked(Mesh, - 6, - localVertexData, - localNormalData, - FaceColors); + BufferVertsChecked(Mesh, 6, localVertexData, localNormalData, FaceColors, TransEmiss); } { @@ -76,11 +70,7 @@ DEBUG_DrawLine( untextured_3d_geometry_buffer *Mesh, v3 P1, v3 P2, u32 ColorInde }; - BufferVertsChecked(Mesh, - 6, - localVertexData, - localNormalData, - FaceColors); + BufferVertsChecked(Mesh, 6, localVertexData, localNormalData, FaceColors, TransEmiss); } u32 VertsBuffered = Mesh->At - MeshStartingAt; diff --git a/src/engine/editor.cpp b/src/engine/editor.cpp index 795f7c1b1..2e65b16b3 100644 --- a/src/engine/editor.cpp +++ b/src/engine/editor.cpp @@ -98,7 +98,7 @@ DoLevelEditor(engine_resources *Engine) ui_element_reference ColorTable = PushTableStart(Ui); RangeIterator(ColorIndex, s32(u8_MAX)+1 ) { - v3 Color = GetColorData(DefaultPalette, u32(ColorIndex)).rgb; + v3 Color = GetColorData(DefaultPalette, u32(ColorIndex)); ui_style Style = FlatUiStyle(Color); v2 QuadDim = V2(22); diff --git a/src/engine/entity.cpp b/src/engine/entity.cpp index 14e82b5b6..490070f68 100644 --- a/src/engine/entity.cpp +++ b/src/engine/entity.cpp @@ -1199,14 +1199,14 @@ SimulateParticleSystem(work_queue_entry_sim_particle_system *Job) r32 Transparency = Lerp(t, System->ParticleEndingTransparency, System->ParticleStartingTransparency); /* r32 Transparency = System->ParticleStartingTransparency; */ - if (System->ParticleStartingTransparency > 0.f) { DrawVoxel( &TranspDest, RenderSpaceP + Particle->Offset, System->Colors[ColorIndex], Diameter, Transparency ); } - /* if (System->ParticleStartingTransparency > 0.f) { DrawVoxel( &TranspDest, RenderSpaceP + Particle->Offset, System->Colors[ColorIndex], Diameter, Transparency * Max(System->ParticleLightEmission, 1.f) ); } */ - if (System->ParticleStartingTransparency <= 0.f && System->ParticleLightEmission <= 0.f) { DrawVoxel( &SolidDest, RenderSpaceP + Particle->Offset, System->Colors[ColorIndex], Diameter, 0.f); } + + auto Dest = System->ParticleStartingTransparency > 0.f ? &TranspDest : &SolidDest; + DrawVoxel( Dest, RenderSpaceP + Particle->Offset, System->Colors[ColorIndex], Diameter, V2(Transparency, System->ParticleLightEmission) ); #if 1 if (Particle->IsLight) { - v3 EmissionColor = GetColorData(DefaultPalette, System->Colors[ColorIndex]).rgb; + v3 EmissionColor = GetColorData(DefaultPalette, System->Colors[ColorIndex]); engine_resources *Engine = GetEngineResources(); DoLight(&Engine->Graphics->Lighting.Lights, RenderSpaceP + Particle->Offset, EmissionColor); } diff --git a/src/engine/gpu_mapped_buffer.cpp b/src/engine/gpu_mapped_buffer.cpp index a026c106f..be5668d3a 100644 --- a/src/engine/gpu_mapped_buffer.cpp +++ b/src/engine/gpu_mapped_buffer.cpp @@ -22,9 +22,17 @@ FlushBuffersToCard(gpu_mapped_element_buffer* GpuMap) GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->ColorHandle); BufferUnmapped &= GL.UnmapBuffer(GL_ARRAY_BUFFER); GpuMap->Buffer.Colors = 0; - GL.VertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (void*)0); + GL.VertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); AssertNoGlErrors; + GL.EnableVertexAttribArray(3); + GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->TransEmissHandle); + BufferUnmapped &= GL.UnmapBuffer(GL_ARRAY_BUFFER); + GpuMap->Buffer.TransEmiss = 0; + GL.VertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); + AssertNoGlErrors; + + if (BufferUnmapped == False) { Error("glUnmapBuffer Failed"); } /* GL.BindBuffer(GL_ARRAY_BUFFER, 0); */ @@ -35,10 +43,10 @@ FlushBuffersToCard(gpu_mapped_element_buffer* GpuMap) void AllocateGpuElementBuffer(gpu_mapped_element_buffer *GpuMap, u32 ElementCount) { + u32 v2Size = sizeof(v2)*ElementCount; u32 v3Size = sizeof(v3)*ElementCount; - u32 v4Size = sizeof(v4)*ElementCount; - GL.GenBuffers(3, &GpuMap->VertexHandle); + GL.GenBuffers(4, &GpuMap->VertexHandle); GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->VertexHandle); GL.BufferData(GL_ARRAY_BUFFER, v3Size, 0, GL_STATIC_DRAW); @@ -47,7 +55,10 @@ AllocateGpuElementBuffer(gpu_mapped_element_buffer *GpuMap, u32 ElementCount) GL.BufferData(GL_ARRAY_BUFFER, v3Size, 0, GL_STATIC_DRAW); GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->ColorHandle); - GL.BufferData(GL_ARRAY_BUFFER, v4Size, 0, GL_STATIC_DRAW); + GL.BufferData(GL_ARRAY_BUFFER, v3Size, 0, GL_STATIC_DRAW); + + GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->TransEmissHandle); + GL.BufferData(GL_ARRAY_BUFFER, v2Size, 0, GL_STATIC_DRAW); GL.BindBuffer(GL_ARRAY_BUFFER, 0); @@ -57,12 +68,9 @@ AllocateGpuElementBuffer(gpu_mapped_element_buffer *GpuMap, u32 ElementCount) link_internal void DeallocateGpuElementBuffer(gpu_mapped_element_buffer *Buf) { - GL.DeleteBuffers(3, &Buf->VertexHandle); + GL.DeleteBuffers(4, &Buf->VertexHandle); } -// NOTE(Jesse): Chosen at random. Is there some sensible tradeoff to be made here? -#define GPU_MAP_BUFFER_ATOM_SIZE (Kilobytes(64)) - link_internal untextured_3d_geometry_buffer * MapGpuElementBuffer(gpu_mapped_element_buffer *GpuMap) { @@ -74,15 +82,14 @@ MapGpuElementBuffer(gpu_mapped_element_buffer *GpuMap) GpuMap->Buffer.End += GpuMap->Buffer.BufferNeedsToGrow; DeallocateGpuElementBuffer(GpuMap); AllocateGpuElementBuffer(GpuMap, GpuMap->Buffer.End); - GpuMap->Buffer.BufferNeedsToGrow = 0; } u32 ElementCount = GpuMap->Buffer.End; + u32 v2Size = sizeof(v2)*ElementCount; u32 v3Size = sizeof(v3)*ElementCount; - u32 v4Size = sizeof(v4)*ElementCount; GL.EnableVertexAttribArray(0); GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->VertexHandle); @@ -98,13 +105,20 @@ MapGpuElementBuffer(gpu_mapped_element_buffer *GpuMap) GL.EnableVertexAttribArray(2); GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->ColorHandle); - GpuMap->Buffer.Colors = (v4*)GL.MapBufferRange(GL_ARRAY_BUFFER, 0, v4Size, GL_MAP_WRITE_BIT); - GL.VertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (void*)0); + GpuMap->Buffer.Colors = (v3*)GL.MapBufferRange(GL_ARRAY_BUFFER, 0, v3Size, GL_MAP_WRITE_BIT); + GL.VertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); + AssertNoGlErrors; + + GL.EnableVertexAttribArray(3); + GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->TransEmissHandle); + GpuMap->Buffer.TransEmiss = (v2*)GL.MapBufferRange(GL_ARRAY_BUFFER, 0, v2Size, GL_MAP_WRITE_BIT); + GL.VertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); AssertNoGlErrors; - if (!GpuMap->Buffer.Verts) { Error("Allocating gpu_mapped_element_buffer::Verts"); } - if (!GpuMap->Buffer.Normals) { Error("Allocating gpu_mapped_element_buffer::Normals"); } - if (!GpuMap->Buffer.Colors) { Error("Allocating gpu_mapped_element_buffer::Colors"); } + if (!GpuMap->Buffer.Verts) { Error("Allocating gpu_mapped_element_buffer::Verts"); } + if (!GpuMap->Buffer.Normals) { Error("Allocating gpu_mapped_element_buffer::Normals"); } + if (!GpuMap->Buffer.Colors) { Error("Allocating gpu_mapped_element_buffer::Colors"); } + if (!GpuMap->Buffer.TransEmiss) { Error("Allocating gpu_mapped_element_buffer::TransEmiss"); } GL.BindBuffer(GL_ARRAY_BUFFER, 0); diff --git a/src/engine/loaders/vox.cpp b/src/engine/loaders/vox.cpp index 255f52f87..679c62aa3 100644 --- a/src/engine/loaders/vox.cpp +++ b/src/engine/loaders/vox.cpp @@ -217,7 +217,7 @@ LoadVoxData(memory_arena *WorldStorage, heap_allocator *Heap, char const *filepa Assert(ChildChunkCount == 0); /* Result.Palette = (v4*)HeapAllocate(Heap, 256); */ - Result.Palette = Allocate(v4, WorldStorage, 256); + Result.Palette = Allocate(v3, WorldStorage, 256); for (u32 PaletteIndex = 0; PaletteIndex < 256; ++PaletteIndex) { @@ -228,7 +228,7 @@ LoadVoxData(memory_arena *WorldStorage, heap_allocator *Heap, char const *filepa Result.Palette[PaletteIndex].r = R; Result.Palette[PaletteIndex].g = G; Result.Palette[PaletteIndex].b = B; - Result.Palette[PaletteIndex].a = A; + /* Result.Palette[PaletteIndex].a = A; */ } } break; diff --git a/src/engine/mesh.cpp b/src/engine/mesh.cpp index 0eeb2c8d8..8d546f659 100644 --- a/src/engine/mesh.cpp +++ b/src/engine/mesh.cpp @@ -1,6 +1,6 @@ // TODO(Jesse): Delete this func in favor of BuildWorldChunkMesh -void -BuildEntityMesh(chunk_data *Chunk, untextured_3d_geometry_buffer* Mesh, v4 *ColorPalette, chunk_dimension Dim) +link_internal void +BuildEntityMesh(chunk_data *Chunk, untextured_3d_geometry_buffer* Mesh, v3 *ColorPalette, chunk_dimension Dim) { for ( int z = 0; z < Dim.z ; ++z ) { @@ -21,9 +21,10 @@ BuildEntityMesh(chunk_data *Chunk, untextured_3d_geometry_buffer* Mesh, v4 *Colo v3 Diameter = V3(1.0f); v3 VertexData[6]; - v4 FaceColors[VERTS_PER_FACE]; + v3 FaceColors[VERTS_PER_FACE]; FillColorArray(Voxel->Color, FaceColors, ColorPalette, VERTS_PER_FACE); + v2 TransEmissArray[VERTS_PER_FACE] = {}; voxel_position rightVoxel = LocalVoxelP + Voxel_Position(1, 0, 0); voxel_position leftVoxel = LocalVoxelP - Voxel_Position(1, 0, 0); @@ -40,32 +41,32 @@ BuildEntityMesh(chunk_data *Chunk, untextured_3d_geometry_buffer* Mesh, v4 *Colo if ( (!IsInsideDim(Dim, rightVoxel)) || NotFilled( Chunk->Voxels, rightVoxel, Dim)) { RightFaceVertexData( VP, Diameter, VertexData); - BufferVertsDirect(Mesh, 6, VertexData, RightFaceNormalData, FaceColors); + BufferVertsDirect(Mesh, 6, VertexData, RightFaceNormalData, FaceColors, TransEmissArray); } if ( (!IsInsideDim( Dim, leftVoxel )) || NotFilled( Chunk->Voxels, leftVoxel, Dim)) { LeftFaceVertexData( VP, Diameter, VertexData); - BufferVertsDirect(Mesh, 6, VertexData, LeftFaceNormalData, FaceColors); + BufferVertsDirect(Mesh, 6, VertexData, LeftFaceNormalData, FaceColors, TransEmissArray); } if ( (!IsInsideDim( Dim, botVoxel )) || NotFilled( Chunk->Voxels, botVoxel, Dim)) { BottomFaceVertexData( VP, Diameter, VertexData); - BufferVertsDirect(Mesh, 6, VertexData, BottomFaceNormalData, FaceColors); + BufferVertsDirect(Mesh, 6, VertexData, BottomFaceNormalData, FaceColors, TransEmissArray); } if ( (!IsInsideDim( Dim, topVoxel )) || NotFilled( Chunk->Voxels, topVoxel, Dim)) { TopFaceVertexData( VP, Diameter, VertexData); - BufferVertsDirect(Mesh, 6, VertexData, TopFaceNormalData, FaceColors); + BufferVertsDirect(Mesh, 6, VertexData, TopFaceNormalData, FaceColors, TransEmissArray); } if ( (!IsInsideDim( Dim, frontVoxel )) || NotFilled( Chunk->Voxels, frontVoxel, Dim)) { FrontFaceVertexData( VP, Diameter, VertexData); - BufferVertsDirect(Mesh, 6, VertexData, FrontFaceNormalData, FaceColors); + BufferVertsDirect(Mesh, 6, VertexData, FrontFaceNormalData, FaceColors, TransEmissArray); } if ( (!IsInsideDim( Dim, backVoxel )) || NotFilled( Chunk->Voxels, backVoxel, Dim)) { BackFaceVertexData( VP, Diameter, VertexData); - BufferVertsDirect(Mesh, 6, VertexData, BackFaceNormalData, FaceColors); + BufferVertsDirect(Mesh, 6, VertexData, BackFaceNormalData, FaceColors, TransEmissArray); } } @@ -76,9 +77,10 @@ BuildEntityMesh(chunk_data *Chunk, untextured_3d_geometry_buffer* Mesh, v4 *Colo void AllocateMesh(untextured_3d_geometry_buffer *Mesh, u32 NumVerts, memory_arena *Memory) { - Mesh->Verts = AllocateAlignedProtection(v3, Memory, NumVerts, CACHE_LINE_SIZE, False); - Mesh->Normals = AllocateAlignedProtection(v3, Memory, NumVerts, CACHE_LINE_SIZE, False); - Mesh->Colors = AllocateAlignedProtection(v4, Memory, NumVerts, CACHE_LINE_SIZE, False); + Mesh->Verts = AllocateAlignedProtection(v3, Memory, NumVerts, CACHE_LINE_SIZE, False); + Mesh->Normals = AllocateAlignedProtection(v3, Memory, NumVerts, CACHE_LINE_SIZE, False); + Mesh->Colors = AllocateAlignedProtection(v3, Memory, NumVerts, CACHE_LINE_SIZE, False); + Mesh->TransEmiss = AllocateAlignedProtection(v2, Memory, NumVerts, CACHE_LINE_SIZE, False); Mesh->End = NumVerts; Mesh->At = 0; @@ -90,9 +92,10 @@ AllocateMesh(untextured_3d_geometry_buffer *Mesh, u32 NumVerts, memory_arena *Me void AllocateMesh(untextured_3d_geometry_buffer *Mesh, u32 NumVerts, heap_allocator *Heap) { - Mesh->Verts = (v3*)HeapAllocate(Heap, sizeof(v3)*NumVerts); - Mesh->Normals = (v3*)HeapAllocate(Heap, sizeof(v3)*NumVerts); - Mesh->Colors = (v4*)HeapAllocate(Heap, sizeof(v4)*NumVerts); + Mesh->Verts = (v3*)HeapAllocate(Heap, sizeof(v3)*NumVerts); + Mesh->Normals = (v3*)HeapAllocate(Heap, sizeof(v3)*NumVerts); + Mesh->Colors = (v3*)HeapAllocate(Heap, sizeof(v4)*NumVerts); + Mesh->TransEmiss = (v2*)HeapAllocate(Heap, sizeof(v2)*NumVerts); Mesh->End = NumVerts; Mesh->At = 0; diff --git a/src/engine/mesh.h b/src/engine/mesh.h index 47d2d790e..23d863155 100644 --- a/src/engine/mesh.h +++ b/src/engine/mesh.h @@ -89,15 +89,16 @@ inline void BufferVertsDirect( untextured_3d_geometry_buffer *Dest, u32 NumVerts, - v3 *Positions, v3 *Normals, v4 *Colors + v3 *Positions, v3 *Normals, v3 *Colors, v2 *TransEmiss ) { TIMED_FUNCTION(); if (BufferHasRoomFor(Dest, NumVerts)) { - MemCopy((u8*)Positions, (u8*)&Dest->Verts[Dest->At] , sizeof(*Positions)*NumVerts ); - MemCopy((u8*)Normals, (u8*)&Dest->Normals[Dest->At], sizeof(*Normals)*NumVerts ); - MemCopy((u8*)Colors, (u8*)&Dest->Colors[Dest->At] , sizeof(*Colors)*NumVerts ); + MemCopy((u8*)Positions, (u8*)&Dest->Verts[Dest->At], sizeof(*Positions)*NumVerts ); + MemCopy((u8*)Normals, (u8*)&Dest->Normals[Dest->At], sizeof(*Normals)*NumVerts ); + MemCopy((u8*)Colors, (u8*)&Dest->Colors[Dest->At], sizeof(*Colors)*NumVerts ); + MemCopy((u8*)TransEmiss, (u8*)&Dest->TransEmiss[Dest->At], sizeof(*TransEmiss)*NumVerts ); Dest->At += NumVerts; } @@ -110,9 +111,9 @@ BufferVertsDirect( inline void BufferVertsDirect( - v3 *DestVerts, v3 *DestNormals, v4 *DestColors, + v3 *DestVerts, v3 *DestNormals, v3 *DestColors, v2 *DestTransEmiss, u32 NumVerts, - v3 *SrcVerts, v3 *SrcNormals, v4 *SrcVertColors, + v3 *SrcVerts, v3 *SrcNormals, v3 *SrcVertColors, v2 *SrcTransEmiss, v3 Offset, v3 Scale, Quaternion Rot @@ -125,8 +126,9 @@ BufferVertsDirect( Assert(NumVerts % 3 == 0); - MemCopy((u8*)SrcNormals, (u8*)DestNormals, sizeof(*SrcNormals)*NumVerts ); - MemCopy((u8*)SrcVertColors, (u8*)DestColors, sizeof(*SrcVertColors)*NumVerts ); + MemCopy((u8*)SrcNormals, (u8*)DestNormals, sizeof(*SrcNormals)*NumVerts ); + MemCopy((u8*)SrcVertColors, (u8*)DestColors, sizeof(*SrcVertColors)*NumVerts ); + MemCopy((u8*)SrcTransEmiss, (u8*)DestTransEmiss, sizeof(*SrcTransEmiss)*NumVerts ); /* v3 HalfOffset = Offset*0.5f; */ for ( u32 VertIndex = 0; @@ -168,9 +170,9 @@ BufferVertsDirect( } inline void BufferVertsDirect( - v3 *DestVerts, v3 *DestNormals, v4 *DestColors, + v3 *DestVerts, v3 *DestNormals, v3 *DestColors, v2 *DestTransEmiss, u32 NumVerts, - v3 *SrcVerts, v3 *SrcNormals, v4 *SrcVertColors, + v3 *SrcVerts, v3 *SrcNormals, v3 *SrcVertColors, v2 *SrcTransEmiss, v3 Offset, v3 Scale ) @@ -227,7 +229,7 @@ inline void BufferVertsChecked( untextured_3d_geometry_buffer *Dest, u32 NumVerts, - v3 *VertsPositions, v3 *Normals, v4 *VertColors, + v3 *VertsPositions, v3 *Normals, v3 *VertColors, v2 *TransEmiss, v3 Offset, v3 Scale, Quaternion Rot @@ -239,8 +241,9 @@ BufferVertsChecked( BufferVertsDirect(Dest->Verts + Dest->At, Dest->Normals + Dest->At, Dest->Colors + Dest->At, + Dest->TransEmiss + Dest->At, NumVerts, - VertsPositions, Normals, VertColors, + VertsPositions, Normals, VertColors, TransEmiss, Offset, Scale, Rot); Dest->At += NumVerts; @@ -256,7 +259,7 @@ inline void BufferVertsDirect( untextured_3d_geometry_buffer *Dest, u32 NumVerts, - v3 *VertsPositions, v3 *Normals, v4 *VertColors, + v3 *VertsPositions, v3 *Normals, v3 *VertColors, v2 *TransEmiss, v3 Offset, v3 Scale ) @@ -267,8 +270,9 @@ BufferVertsDirect( BufferVertsDirect(Dest->Verts + Dest->At, Dest->Normals + Dest->At, Dest->Colors + Dest->At, + Dest->TransEmiss + Dest->At, NumVerts, - VertsPositions, Normals, VertColors, + VertsPositions, Normals, VertColors, TransEmiss, Offset, Scale); Dest->At += NumVerts; @@ -297,8 +301,9 @@ BufferVertsCounted( BufferVertsDirect(Dest->Verts + Dest->At, Dest->Normals + Dest->At, Dest->Colors + Dest->At, + Dest->TransEmiss + Dest->At, Src->At, - Src->Verts, Src->Normals, Src->Colors, + Src->Verts, Src->Normals, Src->Colors, Src->TransEmiss, Offset, Scale); Dest->At += Src->At; @@ -328,8 +333,9 @@ BufferVertsChecked( BufferVertsDirect(Dest->Verts + Dest->At, Dest->Normals + Dest->At, Dest->Colors + Dest->At, + Dest->TransEmiss + Dest->At, Src->At, - Src->Verts, Src->Normals, Src->Colors, + Src->Verts, Src->Normals, Src->Colors, Src->TransEmiss, Offset, Scale); Dest->At += Src->At; @@ -345,7 +351,7 @@ inline void BufferVertsChecked( untextured_3d_geometry_buffer *Target, u32 NumVerts, - v3* Positions, v3* Normals, v4* Colors, + v3 *Positions, v3 *Normals, v3 *Colors, v2 *TransEmiss, v3 Offset = V3(0), v3 Scale = V3(1) ) @@ -354,7 +360,7 @@ BufferVertsChecked( if (BufferHasRoomFor(Target, NumVerts)) { - BufferVertsDirect( Target, NumVerts, Positions, Normals, Colors, Offset, Scale); + BufferVertsDirect( Target, NumVerts, Positions, Normals, Colors,TransEmiss, Offset, Scale); } else { @@ -367,69 +373,75 @@ BufferVertsChecked( inline void -DrawVoxel( untextured_3d_geometry_buffer *Mesh, v3 RenderP_VoxelCenter, v4 Color, v3 Diameter) +DrawVoxel( untextured_3d_geometry_buffer *Mesh, v3 RenderP_VoxelCenter, v3 Color, v3 Diameter, v2 TransEmiss) { /* TIMED_FUNCTION(); */ v3 VertexData[6]; - v4 FaceColors[VERTS_PER_FACE]; + v3 FaceColors[VERTS_PER_FACE]; FillColorArray(Color, FaceColors, VERTS_PER_FACE); + v2 TransEmissArray[VERTS_PER_FACE]; + FillArray(TransEmiss, TransEmissArray, VERTS_PER_FACE); + v3 MinP = RenderP_VoxelCenter - (Diameter*0.5); RightFaceVertexData( MinP, Diameter, VertexData); - BufferVertsChecked(Mesh, 6, VertexData, RightFaceNormalData, FaceColors); + BufferVertsChecked(Mesh, 6, VertexData, RightFaceNormalData, FaceColors, TransEmissArray); LeftFaceVertexData( MinP, Diameter, VertexData); - BufferVertsChecked(Mesh, 6, VertexData, LeftFaceNormalData, FaceColors); + BufferVertsChecked(Mesh, 6, VertexData, LeftFaceNormalData, FaceColors, TransEmissArray); BottomFaceVertexData( MinP, Diameter, VertexData); - BufferVertsChecked(Mesh, 6, VertexData, BottomFaceNormalData, FaceColors); + BufferVertsChecked(Mesh, 6, VertexData, BottomFaceNormalData, FaceColors, TransEmissArray); TopFaceVertexData( MinP, Diameter, VertexData); - BufferVertsChecked(Mesh, 6, VertexData, TopFaceNormalData, FaceColors); + BufferVertsChecked(Mesh, 6, VertexData, TopFaceNormalData, FaceColors, TransEmissArray); FrontFaceVertexData( MinP, Diameter, VertexData); - BufferVertsChecked(Mesh, 6, VertexData, FrontFaceNormalData, FaceColors); + BufferVertsChecked(Mesh, 6, VertexData, FrontFaceNormalData, FaceColors, TransEmissArray); BackFaceVertexData( MinP, Diameter, VertexData); - BufferVertsChecked(Mesh, 6, VertexData, BackFaceNormalData, FaceColors); + BufferVertsChecked(Mesh, 6, VertexData, BackFaceNormalData, FaceColors, TransEmissArray); } inline void -DrawVoxel_CenterDim( untextured_3d_geometry_buffer *Mesh, v3 RenderP_VoxelCenter, v4 Color, v3 Diameter) +DrawVoxel_CenterDim( untextured_3d_geometry_buffer *Mesh, + v3 RenderP_VoxelCenter, v3 Color, v3 Diameter, v2 TransEmiss = V2(0.f, 0.f)) { - DrawVoxel(Mesh, RenderP_VoxelCenter, Color, Diameter); + DrawVoxel(Mesh, RenderP_VoxelCenter, Color, Diameter, TransEmiss); } inline void -DrawVoxel_CenterDim( untextured_3d_geometry_buffer *Mesh, v3i RenderP_VoxelCenter, v4 Color, v3 Diameter) +DrawVoxel_CenterDim( untextured_3d_geometry_buffer *Mesh, + v3i RenderP_VoxelCenter, v3 Color, v3 Diameter, v2 TransEmiss = V2(0.f, 0.f)) { - DrawVoxel(Mesh, V3(RenderP_VoxelCenter), Color, Diameter); + DrawVoxel(Mesh, V3(RenderP_VoxelCenter), Color, Diameter, TransEmiss); } inline void DrawVoxel( untextured_3d_geometry_buffer *Mesh, - v3 RenderP_VoxelCenter, u32 ColorIndex, v3 Diameter, - r32 ExtraChannel = 0.0f) + v3 RenderP_VoxelCenter, u32 ColorIndex, v3 Diameter, v2 TransEmiss = V2(0.f, 0.f)) { - v4 Color = GetColorData(DefaultPalette, ColorIndex, ExtraChannel); - DrawVoxel(Mesh, RenderP_VoxelCenter, Color, Diameter); + v3 Color = GetColorData(DefaultPalette, ColorIndex); + DrawVoxel(Mesh, RenderP_VoxelCenter, Color, Diameter, TransEmiss); } inline void -DrawVoxel_MinDim( untextured_3d_geometry_buffer *Mesh, v3 RenderP_VoxelMin, v4 Color, v3 Diameter, r32 ExtraChannel = 0.0f) +DrawVoxel_MinDim( untextured_3d_geometry_buffer *Mesh, + v3 RenderP_VoxelMin, v3 Color, v3 Diameter, v2 TransEmiss = V2(0.f, 0.f)) { v3 HalfDim = Diameter/2.f; v3 VoxelCenter = RenderP_VoxelMin + HalfDim; - DrawVoxel(Mesh, VoxelCenter, Color, Diameter); + DrawVoxel(Mesh, VoxelCenter, Color, Diameter, TransEmiss); } inline void -DrawVoxel_MinDim( untextured_3d_geometry_buffer *Mesh, v3 RenderP_VoxelMin, u32 ColorIndex, v3 Diameter, r32 ExtraChannel = 0.0f) +DrawVoxel_MinDim( untextured_3d_geometry_buffer *Mesh, + v3 RenderP_VoxelMin, u32 ColorIndex, v3 Diameter, v2 TransEmiss = V2(0.f, 0.f)) { - v4 Color = GetColorData(DefaultPalette, ColorIndex, ExtraChannel); + v3 Color = GetColorData(DefaultPalette, ColorIndex); DrawVoxel_MinDim(Mesh, RenderP_VoxelMin, Color, Diameter); } diff --git a/src/engine/model.h b/src/engine/model.h index af3d41585..ac1003c6a 100644 --- a/src/engine/model.h +++ b/src/engine/model.h @@ -60,7 +60,7 @@ poof(string_and_value_tables(model_index)) struct vox_data { chunk_data *ChunkData; - v4 *Palette; + v3 *Palette; }; struct model diff --git a/src/engine/render.cpp b/src/engine/render.cpp index abd052014..459f267df 100644 --- a/src/engine/render.cpp +++ b/src/engine/render.cpp @@ -690,8 +690,8 @@ TraverseSurfaceToBoundary( world *World, world_chunk *Chunk, voxel_position Star void DrawParticle(untextured_3d_geometry_buffer *Source, untextured_3d_geometry_buffer *Dest, u8 ColorIndex) { - v4 FaceColors[VERTS_PER_FACE]; - FillColorArray(ColorIndex, FaceColors, DefaultPalette, VERTS_PER_FACE, 2.f); + v3 FaceColors[VERTS_PER_FACE]; + FillColorArray(ColorIndex, FaceColors, DefaultPalette, VERTS_PER_FACE); BufferVertsChecked( Source, Dest ); return; } diff --git a/src/engine/render.h b/src/engine/render.h index 806c9088c..7eb3753c5 100644 --- a/src/engine/render.h +++ b/src/engine/render.h @@ -40,12 +40,13 @@ struct g_buffer_render_group }; untextured_3d_geometry_buffer -Untextured3dGeometryBuffer(v3* Verts, v4* Colors, v3* Normals, u32 Count) +Untextured3dGeometryBuffer(v3* Verts, v3* Colors, v3* Normals, v2 *TransEmiss, u32 Count) { untextured_3d_geometry_buffer Result = {}; Result.Verts = Verts; Result.Colors = Colors; Result.Normals = Normals; + Result.TransEmiss = TransEmiss; Result.At = Count; return Result; diff --git a/src/engine/triangle.cpp b/src/engine/triangle.cpp index 479e1470f..7b02396a6 100644 --- a/src/engine/triangle.cpp +++ b/src/engine/triangle.cpp @@ -80,16 +80,12 @@ BufferTriangle(untextured_3d_geometry_buffer* Dest, triangle* Triangle, v3 Norma VertBuffer[1] = V3(Triangle->Points[1]); VertBuffer[2] = V3(Triangle->Points[2]); - v4 FaceColors[VERTS_PER_FACE]; + v3 FaceColors[VERTS_PER_FACE]; FillColorArray(ColorIndex, FaceColors, DefaultPalette, VERTS_PER_FACE); - BufferVertsChecked( - Dest, - 3, - VertBuffer, - NormalBuffer, - FaceColors); + v2 TransEmiss[VERTS_PER_FACE] = {}; + BufferVertsChecked( Dest, 3, VertBuffer, NormalBuffer, FaceColors, TransEmiss); } inline void @@ -101,16 +97,12 @@ BufferTriangle(untextured_3d_geometry_buffer *Mesh, v3 *Verts, v3 Normal, u32 Co // TODO(Jesse, id: 139, tags: robustness, speed): Is this necessary to avoid some pointer aliasing bug? MemCopy((u8*)Verts, (u8*)VertBuffer, 9 * sizeof(r32) ); - v4 FaceColors[VERTS_PER_FACE]; + v3 FaceColors[VERTS_PER_FACE]; FillColorArray(ColorIndex, FaceColors, DefaultPalette, VERTS_PER_FACE); - BufferVertsChecked( - Mesh, - 3, - VertBuffer, - NormalBuffer, - FaceColors); + v2 TransEmiss[VERTS_PER_FACE] = {}; + BufferVertsChecked( Mesh, 3, VertBuffer, NormalBuffer, FaceColors, TransEmiss); } #if 0 diff --git a/src/engine/world_chunk.cpp b/src/engine/world_chunk.cpp index 9a51abb99..158e89fc7 100644 --- a/src/engine/world_chunk.cpp +++ b/src/engine/world_chunk.cpp @@ -960,7 +960,8 @@ DrawDebugVoxels( voxel *Voxels, v3 Diameter = V3(1.0f); v3 VertexData[VERTS_PER_FACE]; - v4 FaceColors[VERTS_PER_FACE]; + v3 FaceColors[VERTS_PER_FACE]; + v2 TransEmiss[VERTS_PER_FACE]; auto MinDim = SrcChunkMin; auto MaxDim = Min(SrcChunkDim, SrcChunkMax); // SrcChunkMin+DestChunkDim+1 @@ -990,32 +991,32 @@ DrawDebugVoxels( voxel *Voxels, if (Voxel->Flags & Voxel_RightFace) { RightFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_LeftFace) { LeftFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_BottomFace) { BottomFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_TopFace) { TopFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_FrontFace) { FrontFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_BackFace) { BackFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, FaceColors, TransEmiss); } } } @@ -1267,7 +1268,7 @@ BuildWorldChunkMeshFromMarkedVoxels_Greedy( voxel *Voxels, untextured_3d_geometry_buffer *DestGeometry, untextured_3d_geometry_buffer *DestTransparentGeometry, memory_arena *TempMemory, - v4* ColorPallette = DefaultPalette ) + v3 *ColorPallette = DefaultPalette ) { TIMED_FUNCTION(); @@ -1290,7 +1291,8 @@ BuildWorldChunkMeshFromMarkedVoxels_Greedy( voxel *Voxels, v3 VertexData[VERTS_PER_FACE]; - v4 FaceColors[VERTS_PER_FACE]; + v3 FaceColors[VERTS_PER_FACE]; + v2 TransEmiss[VERTS_PER_FACE]; auto SrcMinP = SrcChunkMin; auto MaxDim = Min(SrcChunkDim, SrcChunkMax); // SrcChunkMin+DestChunkDim+1 @@ -1339,38 +1341,38 @@ BuildWorldChunkMeshFromMarkedVoxels_Greedy( voxel *Voxels, { v3 Dim = DoXStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_RightFace, Voxel->Color); RightFaceVertexData( V3(TmpVoxP), Dim, VertexData); - BufferVertsDirect(Dest, 6, VertexData, RightFaceNormalData, FaceColors); + BufferVertsDirect(Dest, 6, VertexData, RightFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_LeftFace) { v3 Dim = DoXStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_LeftFace, Voxel->Color); LeftFaceVertexData( V3(TmpVoxP), Dim, VertexData); - BufferVertsDirect(Dest, 6, VertexData, LeftFaceNormalData, FaceColors); + BufferVertsDirect(Dest, 6, VertexData, LeftFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_BottomFace) { v3 Dim = DoZStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_BottomFace, Voxel->Color); BottomFaceVertexData( V3(TmpVoxP), Dim, VertexData); - BufferVertsDirect(Dest, 6, VertexData, BottomFaceNormalData, FaceColors); + BufferVertsDirect(Dest, 6, VertexData, BottomFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_TopFace) { v3 Dim = DoZStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_TopFace, Voxel->Color); TopFaceVertexData( V3(TmpVoxP), Dim, VertexData); - BufferVertsDirect(Dest, 6, VertexData, TopFaceNormalData, FaceColors); + BufferVertsDirect(Dest, 6, VertexData, TopFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_FrontFace) { v3 Dim = DoYStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_FrontFace, Voxel->Color); FrontFaceVertexData( V3(TmpVoxP), Dim, VertexData); - BufferVertsDirect(Dest, 6, VertexData, FrontFaceNormalData, FaceColors); + BufferVertsDirect(Dest, 6, VertexData, FrontFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_BackFace) { v3 Dim = DoYStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_BackFace, Voxel->Color); BackFaceVertexData( V3(TmpVoxP), Dim, VertexData); - BufferVertsDirect(Dest, 6, VertexData, BackFaceNormalData, FaceColors); + BufferVertsDirect(Dest, 6, VertexData, BackFaceNormalData, FaceColors, TransEmiss); } } } @@ -1398,7 +1400,7 @@ BuildMipMesh( voxel *Voxels, untextured_3d_geometry_buffer *DestGeometry, memory_arena *TempMemory, - v4* ColorPallette = DefaultPalette ) + v3 *ColorPallette = DefaultPalette ) { TIMED_FUNCTION(); @@ -1423,7 +1425,8 @@ BuildMipMesh( voxel *Voxels, v3 VertexData[VERTS_PER_FACE]; - v4 FaceColors[VERTS_PER_FACE]; + v3 FaceColors[VERTS_PER_FACE]; + v2 TransEmiss[VERTS_PER_FACE]; Assert(VoxDim >= InnerMax); @@ -1560,38 +1563,38 @@ BuildMipMesh( voxel *Voxels, { v3 Dim = DoXStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_RightFace, Voxel->Color); RightFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_LeftFace) { v3 Dim = DoXStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_LeftFace, Voxel->Color); LeftFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_BottomFace) { v3 Dim = DoZStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_BottomFace, Voxel->Color); BottomFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_TopFace) { v3 Dim = DoZStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_TopFace, Voxel->Color); TopFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_FrontFace) { v3 Dim = DoYStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_FrontFace, Voxel->Color); FrontFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_BackFace) { v3 Dim = DoYStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_BackFace, Voxel->Color); BackFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, FaceColors, TransEmiss); } } } @@ -1608,7 +1611,7 @@ BuildWorldChunkMeshFromMarkedVoxels_Naieve( voxel *Voxels, chunk_dimension SrcChunkMax, untextured_3d_geometry_buffer *DestGeometry, - v4* ColorPallette = DefaultPalette ) + v3 *ColorPallette = DefaultPalette ) { TIMED_FUNCTION(); @@ -1637,7 +1640,8 @@ BuildWorldChunkMeshFromMarkedVoxels_Naieve( voxel *Voxels, v3 Diameter = V3(1.0f); v3 VertexData[VERTS_PER_FACE]; - v4 FaceColors[VERTS_PER_FACE]; + v3 FaceColors[VERTS_PER_FACE]; + v2 TransEmiss[VERTS_PER_FACE] = {}; auto MinDim = SrcChunkMin; auto MaxDim = Min(VoxDim, SrcChunkMax); // SrcChunkMin+DestChunkDim+1 @@ -1657,32 +1661,32 @@ BuildWorldChunkMeshFromMarkedVoxels_Naieve( voxel *Voxels, if (Voxel->Flags & Voxel_RightFace) { RightFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_LeftFace) { LeftFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_BottomFace) { BottomFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_TopFace) { TopFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_FrontFace) { FrontFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, FaceColors, TransEmiss); } if (Voxel->Flags & Voxel_BackFace) { BackFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, FaceColors, TransEmiss); } } } @@ -4550,14 +4554,14 @@ BufferChunkMesh( graphics *Graphics, if (Length(Rot.xyz) == 0.f) { BufferVertsChecked(&CopyBuffer, Src->At, - Src->Verts, Src->Normals, Src->Colors, + Src->Verts, Src->Normals, Src->Colors, Src->TransEmiss, ModelBasisP, V3(Scale)); } else { BufferVertsChecked(&CopyBuffer, Src->At, - Src->Verts, Src->Normals, Src->Colors, + Src->Verts, Src->Normals, Src->Colors, Src->TransEmiss, ModelBasisP, V3(Scale), Rot); } } diff --git a/src/tools/asset_packer.cpp b/src/tools/asset_packer.cpp index cddfa61c1..fe2584f5a 100644 --- a/src/tools/asset_packer.cpp +++ b/src/tools/asset_packer.cpp @@ -62,7 +62,7 @@ s32 main(s32 ArgCount, const char **Args) world_chunk *Chunk = Chunks + ChunkIndex; Chunk->Flags = Chunk_VoxelsInitialized; - v4 *Palette = Vox.Palette ? Vox.Palette : DefaultPalette; + v3 *Palette = Vox.Palette ? Vox.Palette : DefaultPalette; if (Vox.Palette == 0) { Warn("No Palette found, using default"); } chunk_dimension SrcChunkOffset = Global_ChunkApronMinDim + (Chunk->WorldP * WorldChunkDim); From 22643612f38f7858a3ff78ec5c82f897ea023c55 Mon Sep 17 00:00:00 2001 From: a pirate Date: Thu, 26 Oct 2023 19:14:24 -0600 Subject: [PATCH 02/15] Emissive particles nominally working --- shaders/3DTransparency.fragmentshader | 32 ++++++++++++++++++-------- shaders/Lighting.fragmentshader | 33 +++++++++++++++++---------- shaders/gBuffer.vertexshader | 9 ++++++-- src/engine/game_effects.cpp | 6 ++--- src/engine/mesh.h | 4 ++++ src/engine/render.cpp | 10 +++++--- src/engine/render/render_init.cpp | 2 +- src/engine/work_queue.cpp | 2 +- 8 files changed, 66 insertions(+), 32 deletions(-) diff --git a/shaders/3DTransparency.fragmentshader b/shaders/3DTransparency.fragmentshader index 374a849b2..5a6d1085b 100644 --- a/shaders/3DTransparency.fragmentshader +++ b/shaders/3DTransparency.fragmentshader @@ -1,7 +1,8 @@ -in vec3 vertexP_worldspace; -in vec3 vertexN_worldspace; -in vec4 MaterialColor; +in vec3 WorldP; +in vec3 Normal; +in vec3 MaterialColor; +in vec2 TransEmiss; uniform sampler2D gBufferDepthTexture; @@ -9,7 +10,7 @@ uniform bool BravoilMyersOIT; uniform bool BravoilMcGuireOIT; layout (location = 0) out vec4 ColorAccumulation; -layout (location = 1) out vec4 Count; +layout (location = 1) out vec2 Count; float Far = 5000.f; @@ -68,18 +69,25 @@ void main() float gBufferDepth = texture(gBufferDepthTexture, UV).r; float FragDepth = gl_FragCoord.z; + float Alpha = TransEmiss.x; + float Emission = TransEmiss.y; + /* ColorAccumulation = v4(Alpha, Emission, 0.f, 1.f); */ + /* ColorAccumulation = v4(WorldP, 1.f); */ + /* ColorAccumulation = v4(Normal, 1.f); */ + /* ColorAccumulation = v4(MaterialColor, 1.f); */ + /* Count = V4(Alpha); */ + /* return; */ + if (FragDepth < gBufferDepth) { if (BravoilMyersOIT) { - ColorAccumulation = MaterialColor * MaterialColor.a; - Count = V4(1); + ColorAccumulation = v4(MaterialColor * Alpha, Alpha); + Count = V2(1.f); } if (BravoilMcGuireOIT) { - r32 Alpha = MaterialColor.a; - /* ColorAccumulation = MaterialColor * Eq7(Linearize(FragDepth, 500.f, 0.1f), Alpha); */ /* ColorAccumulation = MaterialColor * Eq7(FragDepth, Alpha); */ /* ColorAccumulation.rgb = MaterialColor.rgb * Linearize(FragDepth, 800.f, 0.1f); */ @@ -90,7 +98,7 @@ void main() float AttenuatedAlpha = Alpha * DepthAtten; - ColorAccumulation.rgb = MaterialColor.rgb * Alpha; + ColorAccumulation.rgb = MaterialColor * Alpha; /* ColorAccumulation.rgb = MaterialColor.rgb * AttenuatedAlpha; */ ColorAccumulation.a = AttenuatedAlpha; @@ -98,7 +106,11 @@ void main() /* ColorAccumulation = MaterialColor * Eq10(FragDepth, Alpha); */ /* Count = -v4(1.f-Alpha); */ - Count = V4(Alpha); + /* Count = V4(0.f, 0.f, 0.f, Alpha); */ + /* Count = V4(0.f, 0.f, 0.f, Alpha); */ + Count.r = Alpha; + Count.g = Emission; + /* Count = V4(Alpha, Emission, 0.f 0.f); */ /* Count = -V4(ColorAccumulation.a); */ /* Count = -V4(1.f-(Alpha*DepthAtten)); */ } diff --git a/shaders/Lighting.fragmentshader b/shaders/Lighting.fragmentshader index 7a18f581c..6ee8e718e 100644 --- a/shaders/Lighting.fragmentshader +++ b/shaders/Lighting.fragmentshader @@ -86,8 +86,9 @@ void main() ivec2 texelCoord = ivec2(gBufferUV*gBufferTextureDim); vec4 FragPosition = texelFetch(gPosition, texelCoord, 0); - vec4 AccumTex = texture(TransparencyAccum, gBufferUV); - float AccumCountTex = texture(TransparencyCount, gBufferUV).r; + vec4 TransAccum = texture(TransparencyAccum, gBufferUV); + float TransAccumCount = texture(TransparencyCount, gBufferUV).r; + float TransAccumEmission = texture(TransparencyCount, gBufferUV).g; vec3 AmbientLightContrib = SunColor*0.06f; vec3 KeyLightContrib = V3(0.f); @@ -99,7 +100,7 @@ void main() vec4 gColorTexel = texelFetch(gColor, texelCoord, 0); vec3 Diffuse = gColorTexel.rgb; - float Emission = gColorTexel.a; + /* float Emission = gColorTexel.a; */ /* Diffuse *= Emission; */ @@ -304,14 +305,14 @@ void main() if (BravoilMyersOIT) { - /* TransparencyContrib = BravoilMyersWeightedAverage(AccumTex, AccumCountTex); */ + /* TransparencyContrib = BravoilMyersWeightedAverage(TransAccum, TransAccumCount); */ - float Count = max(1.f, AccumCountTex); + float Count = max(1.f, TransAccumCount); // Have to clamp because this is > 1.f for emissive surfaces, which breaks the following equation - float Alpha = clamp(AccumTex.a, 0.f, 1.f); + float Alpha = clamp(TransAccum.a, 0.f, 1.f); - v3 ColorResult = AccumTex.rgb/max(AccumTex.a, 0.00001f); + v3 ColorResult = TransAccum.rgb/max(TransAccum.a, 0.00001f); float AlphaResult = pow(max(0.0, 1.0-(Alpha/Count)), Count); TransparencyContrib = v3(ColorResult * AlphaResult); @@ -321,13 +322,20 @@ void main() if (BravoilMcGuireOIT) { - float Revealage = clamp(AccumCountTex, 0.f, 1.f); + float Revealage = clamp(TransAccumCount, 0.f, 1.f); float Coverage = 1.f-Revealage; - TransparencyContrib = (AccumTex.rgb / clamp(AccumTex.a, 1e-4, 5e4)) * Coverage; + TransparencyContrib = (TransAccum.rgb / clamp(TransAccum.a, 1e-4, 5e4)); // * Coverage; float LightTransmission = length(KeyLightContrib + BackLightContrib + PointLightsContrib + AmbientLightContrib); + float LightEmission = TransAccumEmission; - TransparencyContrib *= LightTransmission; + /* out_LightColor = V4(TransAccumEmission, 0.f, 0.f, 1.f); */ + /* out_LightColor = V4(LightTransmission, 0.f, 0.f, 1.f); */ + /* out_LightColor = V4(LightEmission, 0.f, 0.f, 1.f); */ + /* out_LightColor = V4(max(LightTransmission, LightEmission), 0.f, 0.f, 1.f); */ + /* return; */ + + TransparencyContrib *= max(LightTransmission, LightEmission); /* out_LightColor = vec4( TotalLight + TransparencyContrib*LightTransmission, 1.f); */ /* out_LightColor = vec4(Revealage, 0.f, 0.f, 1.f); */ @@ -336,10 +344,11 @@ void main() } out_LightColor = V4(TotalLight + TransparencyContrib, 1.f); + /* out_LightColor = V4(Emission, 0.f, 0.f, 1.f); */ + /* return; */ - - if (UseLightingBloom) { BloomColor = TotalLight + (Emission*Diffuse) + TransparencyContrib.rgb; } + if (UseLightingBloom) { BloomColor = TotalLight + TransparencyContrib.rgb; } /* else { BloomColor = vec3(0.0f, 0.0f, 0.0f); } */ diff --git a/shaders/gBuffer.vertexshader b/shaders/gBuffer.vertexshader index d4c967567..0a8be4524 100644 --- a/shaders/gBuffer.vertexshader +++ b/shaders/gBuffer.vertexshader @@ -1,10 +1,12 @@ layout(location = 0) in vec3 vertexPosition_modelspace; layout(location = 1) in vec3 vertexNormal_modelspace; -layout(location = 2) in vec4 vertexColor; +layout(location = 2) in vec3 vertexColor; +layout(location = 3) in vec2 in_TransEmiss; out vec3 vertexP_worldspace; out vec3 vertexN_worldspace; -out vec4 MaterialColor; +out vec3 MaterialColor; +out vec2 TransEmiss; uniform mat4 ViewProjection; uniform mat4 Model; @@ -12,6 +14,9 @@ uniform mat4 Model; void main() { MaterialColor = vertexColor; + /* MaterialColor.xy = in_TransEmiss.xy; */ + /* MaterialColor.z = 0.f; */ + TransEmiss = in_TransEmiss; vertexP_worldspace = vec4(Model * vec4(vertexPosition_modelspace, 1)).xyz; vertexN_worldspace = vec4(Model * vec4(vertexNormal_modelspace, 1)).xyz; diff --git a/src/engine/game_effects.cpp b/src/engine/game_effects.cpp index 59ded84bf..36dff7097 100644 --- a/src/engine/game_effects.cpp +++ b/src/engine/game_effects.cpp @@ -13,10 +13,10 @@ SpawnFire(entity *Entity, random_series *Entropy, v3 Offset, r32 Dim, b32 Colorf System->ParticleLightEmissionChance = 0.40f; /* System->ParticleLightEmission = 1.f + Dim; */ - System->ParticleLightEmission = 3.0f; + System->ParticleLightEmission = 0.05f; System->ParticleStartingTransparency = 0.4f; - System->ParticleEndingTransparency = 0.55f; + System->ParticleEndingTransparency = 0.3f; if (Colorful) @@ -101,7 +101,7 @@ SpawnExplosion(entity *Entity, random_series *Entropy, v3 Offset, r32 Radius, un System->ParticleLightEmission = 1.2f; System->ParticleLightEmissionChance = 0.80f; - /* System->ParticleLightEmission = 3.0f; */ + System->ParticleLightEmission = 0.1f; System->SpawnRegion = aabb(Offset, V3(Radius*0.20f) ); diff --git a/src/engine/mesh.h b/src/engine/mesh.h index 23d863155..2bd0fb7c6 100644 --- a/src/engine/mesh.h +++ b/src/engine/mesh.h @@ -63,6 +63,7 @@ MarkBufferForGrowth(untextured_3d_geometry_buffer *Dest, umm Grow) ToMark->BufferNeedsToGrow += Grow; } +#if 0 inline void BufferVertsDirect( untextured_2d_geometry_buffer *Dest, @@ -76,6 +77,7 @@ BufferVertsDirect( { MemCopy((u8*)&Dest->Verts[Dest->At], (u8*)Positions, sizeof(*Positions)*NumVerts ); MemCopy((u8*)Colors, (u8*)&Dest->Colors[Dest->At], sizeof(*Colors)*NumVerts ); + MemCopy((u8*)Colors, (u8*)&Dest->Colors[Dest->At], sizeof(*Colors)*NumVerts ); Dest->At += NumVerts; } else @@ -84,6 +86,7 @@ BufferVertsDirect( MarkBufferForGrowth(Dest, NumVerts); } } +#endif inline void BufferVertsDirect( @@ -186,6 +189,7 @@ BufferVertsDirect( MemCopy((u8*)SrcNormals, (u8*)DestNormals, sizeof(*SrcNormals)*NumVerts ); MemCopy((u8*)SrcVertColors, (u8*)DestColors, sizeof(*SrcVertColors)*NumVerts ); + MemCopy((u8*)SrcTransEmiss, (u8*)DestTransEmiss, sizeof(*SrcTransEmiss)*NumVerts ); for ( u32 VertIndex = 0; VertIndex < NumVerts; diff --git a/src/engine/render.cpp b/src/engine/render.cpp index 459f267df..7768fcbaa 100644 --- a/src/engine/render.cpp +++ b/src/engine/render.cpp @@ -353,6 +353,9 @@ ClearFramebuffers(graphics *Graphics, render_entity_to_texture_group *RTTGroup) { GL.BindFramebuffer(GL_FRAMEBUFFER, Graphics->Transparency.FBO.ID); +#if 1 + GL.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +#else { u32 Attachments = GL_COLOR_ATTACHMENT0; GL.DrawBuffers(1, &Attachments); @@ -362,7 +365,7 @@ ClearFramebuffers(graphics *Graphics, render_entity_to_texture_group *RTTGroup) { u32 Attachments = GL_COLOR_ATTACHMENT0 + 1; GL.DrawBuffers(1, &Attachments); - GL.ClearColor(1.f, 0.f, 0.f, 1.f); + GL.ClearColor(1.f, 1.f, 1.f, 1.f); GL.Clear(GL_COLOR_BUFFER_BIT); } @@ -370,6 +373,7 @@ ClearFramebuffers(graphics *Graphics, render_entity_to_texture_group *RTTGroup) // Reset draw buffers SetDrawBuffers(&Graphics->Transparency.FBO); } +#endif } else { @@ -783,8 +787,8 @@ RenderTransparencyBuffers(render_settings *Settings, transparency_render_group * /* GL.BlendFuncSeparate(GL_ONE, GL_ONE, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA); */ GL.BlendFunci(0, GL_ONE, GL_ONE); - /* GL.BlendFunci(1, GL_ONE, GL_ONE); */ - GL.BlendFunci(1, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA); + GL.BlendFunci(1, GL_ONE, GL_ONE); + /* GL.BlendFunci(1, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA); */ /* GL.BlendFunci(1, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); */ Draw(Group->GpuBuffer.Buffer.At); diff --git a/src/engine/render/render_init.cpp b/src/engine/render/render_init.cpp index fc5784ccc..8b0dd7de3 100644 --- a/src/engine/render/render_init.cpp +++ b/src/engine/render/render_init.cpp @@ -580,7 +580,7 @@ InitTransparencyRenderGroup(render_settings *Settings, transparency_render_group GL.TexImage2D( GL_TEXTURE_2D, 0, GL_RGBA32F, TextureSize.x, TextureSize.y, 0, GL_RGBA, GL_FLOAT, Image); Group->RevealTex = GenTexture(TextureSize, Memory); - GL.TexImage2D( GL_TEXTURE_2D, 0, GL_R32F, TextureSize.x, TextureSize.y, 0, GL_RED, GL_FLOAT, Image); + GL.TexImage2D( GL_TEXTURE_2D, 0, GL_RG32F, TextureSize.x, TextureSize.y, 0, GL_RG, GL_FLOAT, Image); /* Group->Depth = MakeDepthTexture(TextureSize, Memory); */ diff --git a/src/engine/work_queue.cpp b/src/engine/work_queue.cpp index 9f058b74d..9f67af050 100644 --- a/src/engine/work_queue.cpp +++ b/src/engine/work_queue.cpp @@ -73,7 +73,7 @@ DoCopyJob(work_queue_entry_copy_buffer_ref *Job, tiered_mesh_freelist* MeshFreel untextured_3d_geometry_buffer CopyDest = ReserveBufferSpace(Job->Dest, Src->At); v3 Basis = Job->Basis; BufferVertsChecked( Src, &CopyDest, Basis, V3(1.0f)); - } + ReleaseOwnership(Job->Buf, Job->MeshBit, Src); } From 2ad1f02477a6c886633bb83302e56575eb9b94a5 Mon Sep 17 00:00:00 2001 From: a pirate Date: Thu, 26 Oct 2023 19:27:29 -0600 Subject: [PATCH 03/15] Particle tuning --- jesse.make.sh | 2 +- shaders/3DTransparency.fragmentshader | 2 +- shaders/Lighting.fragmentshader | 1 + src/engine/constants.h | 4 +--- src/engine/game_effects.cpp | 6 +++--- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/jesse.make.sh b/jesse.make.sh index 40226b083..56d90d383 100644 --- a/jesse.make.sh +++ b/jesse.make.sh @@ -28,7 +28,7 @@ # BuildSingleExample examples/terrain_gen \ # BuildDebugSystem -# OPT="-O2" +OPT="-O2" ./make.sh $OPT \ BuildSingleExample examples/transparency \ BuildSingleExample examples/turn_based \ diff --git a/shaders/3DTransparency.fragmentshader b/shaders/3DTransparency.fragmentshader index 5a6d1085b..ef3fbf48b 100644 --- a/shaders/3DTransparency.fragmentshader +++ b/shaders/3DTransparency.fragmentshader @@ -109,7 +109,7 @@ void main() /* Count = V4(0.f, 0.f, 0.f, Alpha); */ /* Count = V4(0.f, 0.f, 0.f, Alpha); */ Count.r = Alpha; - Count.g = Emission; + Count.g = Emission * Alpha; /* Count = V4(Alpha, Emission, 0.f 0.f); */ /* Count = -V4(ColorAccumulation.a); */ /* Count = -V4(1.f-(Alpha*DepthAtten)); */ diff --git a/shaders/Lighting.fragmentshader b/shaders/Lighting.fragmentshader index 6ee8e718e..d7f0e369d 100644 --- a/shaders/Lighting.fragmentshader +++ b/shaders/Lighting.fragmentshader @@ -336,6 +336,7 @@ void main() /* return; */ TransparencyContrib *= max(LightTransmission, LightEmission); + /* TransparencyContrib *= LightEmission; */ /* out_LightColor = vec4( TotalLight + TransparencyContrib*LightTransmission, 1.f); */ /* out_LightColor = vec4(Revealage, 0.f, 0.f, 1.f); */ diff --git a/src/engine/constants.h b/src/engine/constants.h index 739892807..efe9f897a 100644 --- a/src/engine/constants.h +++ b/src/engine/constants.h @@ -42,6 +42,4 @@ #define TOTAL_ENTITY_COUNT (1024*2) -// THIS MUST MATCH THE DEFINE IN header.glsl -// Also must be a power of two -#define MAX_LIGHTS 1024 +#define MAX_LIGHTS (1024*2) diff --git a/src/engine/game_effects.cpp b/src/engine/game_effects.cpp index 36dff7097..e97902043 100644 --- a/src/engine/game_effects.cpp +++ b/src/engine/game_effects.cpp @@ -99,8 +99,8 @@ SpawnExplosion(entity *Entity, random_series *Entropy, v3 Offset, r32 Radius, un System->ParticleStartingTransparency = 0.4f; System->ParticleEndingTransparency = 0.4f; - System->ParticleLightEmission = 1.2f; - System->ParticleLightEmissionChance = 0.80f; + /* System->ParticleLightEmission = 1.2f; */ + System->ParticleLightEmissionChance = 0.05f; System->ParticleLightEmission = 0.1f; System->SpawnRegion = aabb(Offset, V3(Radius*0.20f) ); @@ -112,7 +112,7 @@ SpawnExplosion(entity *Entity, random_series *Entropy, v3 Offset, r32 Radius, un // Fire particles are emissive - System->ParticleLightEmission = 0.0f; + /* System->ParticleLightEmission = 0.0f; */ /* System->Physics.Speed = 2; */ /* System->Physics.Drag = V3(2.2f); */ From 4474b27a785f8b3df21d1c7b2e738b47545e5b63 Mon Sep 17 00:00:00 2001 From: a pirate Date: Thu, 26 Oct 2023 20:29:29 -0600 Subject: [PATCH 04/15] Tweak naming; fix init bug in SSAO noise --- shaders/Lighting.fragmentshader | 7 ++++--- src/engine/render/render_init.cpp | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/shaders/Lighting.fragmentshader b/shaders/Lighting.fragmentshader index d7f0e369d..99bb85310 100644 --- a/shaders/Lighting.fragmentshader +++ b/shaders/Lighting.fragmentshader @@ -322,9 +322,10 @@ void main() if (BravoilMcGuireOIT) { - float Revealage = clamp(TransAccumCount, 0.f, 1.f); - float Coverage = 1.f-Revealage; - TransparencyContrib = (TransAccum.rgb / clamp(TransAccum.a, 1e-4, 5e4)); // * Coverage; + float Coverage = clamp(TransAccumCount, 0.f, 1.f); + /* float Coverage = Revealage; */ + TransparencyContrib = (TransAccum.rgb / clamp(TransAccum.a, 1e-4, 5e4)); + /* TransparencyContrib = (TransAccum.rgb / clamp(TransAccum.a, 1e-4, 5e4)) * Coverage; */ float LightTransmission = length(KeyLightContrib + BackLightContrib + PointLightsContrib + AmbientLightContrib); float LightEmission = TransAccumEmission; diff --git a/src/engine/render/render_init.cpp b/src/engine/render/render_init.cpp index 8b0dd7de3..f9c9e6134 100644 --- a/src/engine/render/render_init.cpp +++ b/src/engine/render/render_init.cpp @@ -29,7 +29,7 @@ texture * AllocateAndInitSsaoNoise(ao_render_group *AoGroup, memory_arena *GraphicsMemory) { v2i SsaoNoiseDim = V2i(4,4); - random_series SsaoEntropy; + random_series SsaoEntropy = {453265436}; AoGroup->NoiseTile = V3(SCR_WIDTH/SsaoNoiseDim.x, SCR_HEIGHT/SsaoNoiseDim.y, 1); From 4a2f3213ed135119c8bb1f4392ef74708080e6f7 Mon Sep 17 00:00:00 2001 From: a pirate Date: Thu, 26 Oct 2023 20:55:26 -0600 Subject: [PATCH 05/15] Update deps, tune constants --- external/bonsai_stdlib | 2 +- jesse.make.sh | 2 +- src/engine/game_effects.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/external/bonsai_stdlib b/external/bonsai_stdlib index c5fb8e3f4..6dba74d04 160000 --- a/external/bonsai_stdlib +++ b/external/bonsai_stdlib @@ -1 +1 @@ -Subproject commit c5fb8e3f43ec1ffc7b0f5d1c7de5ea4f8f1ced87 +Subproject commit 6dba74d04629ebcc10490ccb708345203f3521f9 diff --git a/jesse.make.sh b/jesse.make.sh index 56d90d383..40226b083 100644 --- a/jesse.make.sh +++ b/jesse.make.sh @@ -28,7 +28,7 @@ # BuildSingleExample examples/terrain_gen \ # BuildDebugSystem -OPT="-O2" +# OPT="-O2" ./make.sh $OPT \ BuildSingleExample examples/transparency \ BuildSingleExample examples/turn_based \ diff --git a/src/engine/game_effects.cpp b/src/engine/game_effects.cpp index e97902043..218e66161 100644 --- a/src/engine/game_effects.cpp +++ b/src/engine/game_effects.cpp @@ -105,10 +105,10 @@ SpawnExplosion(entity *Entity, random_series *Entropy, v3 Offset, r32 Radius, un System->SpawnRegion = aabb(Offset, V3(Radius*0.20f) ); - System->EmissionLifespan = 0.20f; + System->EmissionLifespan = 0.12f; System->LifespanMod = 0.5f; System->ParticleLifespan = 0.15f; - System->ParticlesPerSecond = 600.0f*Radius; + System->ParticlesPerSecond = 500.0f*Radius; // Fire particles are emissive From 5bfe42339c64639d5f4e5a56f898098022a0eded Mon Sep 17 00:00:00 2001 From: a pirate Date: Fri, 27 Oct 2023 07:11:50 -0600 Subject: [PATCH 06/15] Fix transparency buffers not getting filled out --- examples/tools/voxel_synthesis_rule_baker/game.cpp | 1 + jesse.make.sh | 2 +- src/engine/loaders/vox.cpp | 2 +- src/engine/world_chunk.cpp | 13 ++++++++++++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/examples/tools/voxel_synthesis_rule_baker/game.cpp b/examples/tools/voxel_synthesis_rule_baker/game.cpp index de8d061f7..cc35bcd51 100644 --- a/examples/tools/voxel_synthesis_rule_baker/game.cpp +++ b/examples/tools/voxel_synthesis_rule_baker/game.cpp @@ -818,6 +818,7 @@ BONSAI_API_MAIN_THREAD_INIT_CALLBACK() ChunkData->Dim, VoxOffset, VoxOffset+Global_TileDim, &TileEntity->Model.Mesh, + &TileEntity->Model.TransparentMesh, GetTranArena(), VoxData->Palette ); diff --git a/jesse.make.sh b/jesse.make.sh index 40226b083..24fc7bc6b 100644 --- a/jesse.make.sh +++ b/jesse.make.sh @@ -32,10 +32,10 @@ ./make.sh $OPT \ BuildSingleExample examples/transparency \ BuildSingleExample examples/turn_based \ + BuildSingleExample examples/the_wanderer \ BuildExecutables \ BuildDebugSystem \ # BuildSingleExample examples/blank_project \ - # BuildSingleExample examples/the_wanderer \ # # BuildTests \ # # BuildSingleExample examples/turn_based2 diff --git a/src/engine/loaders/vox.cpp b/src/engine/loaders/vox.cpp index 679c62aa3..cb2a1e6e0 100644 --- a/src/engine/loaders/vox.cpp +++ b/src/engine/loaders/vox.cpp @@ -333,7 +333,7 @@ LoadVoxData(memory_arena *WorldStorage, heap_allocator *Heap, char const *filepa Voxel->Offset = Voxel->Offset - Min + HalfApronMin; s32 Index = GetIndex(Voxel->Offset, ModelDim); Result.ChunkData->Voxels[Index] = Voxel->V; - Result.ChunkData->Voxels[Index].Transparency = 0xff; + Result.ChunkData->Voxels[Index].Transparency = 1; Result.ChunkData->VoxelLighting[Index] = VoxelLighting(0xff); } diff --git a/src/engine/world_chunk.cpp b/src/engine/world_chunk.cpp index 158e89fc7..27081dbef 100644 --- a/src/engine/world_chunk.cpp +++ b/src/engine/world_chunk.cpp @@ -1336,7 +1336,18 @@ BuildWorldChunkMeshFromMarkedVoxels_Greedy( voxel *Voxels, // TODO(Jesse): This copy could be avoided in multiple ways, and should be. FillColorArray(C, FaceColors, ColorPallette, VERTS_PER_FACE); - auto Dest = Voxel->Transparency ? DestTransparentGeometry : DestGeometry; + untextured_3d_geometry_buffer *Dest = {}; + if (Voxel->Transparency) + { + Dest = DestTransparentGeometry; + FillArray(V2(1.f, 0.f), TransEmiss, VERTS_PER_FACE); + } + else + { + Dest = DestGeometry; + FillArray({}, TransEmiss, VERTS_PER_FACE); + } + if (Voxel->Flags & Voxel_RightFace) { v3 Dim = DoXStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_RightFace, Voxel->Color); From ed5bdce317d0a78f45ddf826433e0fdbf0be8e8b Mon Sep 17 00:00:00 2001 From: a pirate Date: Fri, 27 Oct 2023 07:58:40 -0600 Subject: [PATCH 07/15] Add water to wishlist --- readme.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 6b3e30c53..bf55e3405 100644 --- a/readme.md +++ b/readme.md @@ -4,14 +4,13 @@ Bonsai is a 3D voxel-based engine built with the intention of writing both fast-paced arcade games and tile/turn-based RPGs Bonsai, and nearly all it's dependencies, are written completely from scratch. -The one external dependency is the C runtime library for startup, and a small +One external dependency is the C runtime library for startup, and a small handful of trig functions (sin, cos, atan2). I have a back-burner task to remove the CRT entirely, though it's unclear when/if I'll get around to it. The only requirements to build and run Bonsai are an OpenGL 3.3+ driver, C++ compiler, and a few appropriate system headers. - ## Renderer Features * Deferred Shading @@ -41,6 +40,12 @@ compiler, and a few appropriate system headers. * Context Switches (windows only) * Physical Core (windows only) +# Getting Started + +## Building + +See the docs on the [build process](docs/01_build_process.md). + # Wishlist ------------------------------------------------------------------------------- @@ -56,6 +61,8 @@ compiler, and a few appropriate system headers. [ ] FXAA : http://blog.simonrodriguez.fr/articles/2016/07/implementing_fxaa.html +[ ] Water : https://www.youtube.com/watch?v=5yhDb9dzJ58 + ------------------------------------------------------------------------------- ## Terrain Generation @@ -81,7 +88,3 @@ compiler, and a few appropriate system headers. [ ] Octree ? https://graphics.tudelft.nl/Publications-new/2020/CBE20/ModifyingCompressedVoxels-main.pdf [ ] Better floating-point rng : https://www.corsix.org/content/higher-quality-random-floats - -# Building - -See the docs on the [build process](docs/01_build_process.md). From 0ab6308311200a4becb146e9a1d344e794aacbc0 Mon Sep 17 00:00:00 2001 From: a pirate Date: Fri, 27 Oct 2023 12:09:10 -0600 Subject: [PATCH 08/15] Add vertex_material --- shaders/gBuffer.fragmentshader | 9 +- shaders/gBuffer.vertexshader | 2 - src/engine/asset.cpp | 112 +++++++++++++++++++--- src/engine/asset.h | 28 +++++- src/engine/debug.cpp | 4 +- src/engine/debug_draw.cpp | 13 ++- src/engine/gpu_mapped_buffer.cpp | 52 ++++++---- src/engine/loaders/vox.cpp | 6 +- src/engine/mesh.cpp | 16 ++-- src/engine/mesh.h | 79 ++++++++-------- src/engine/render.h | 6 +- src/engine/triangle.cpp | 18 ++-- src/engine/world_chunk.cpp | 158 ++++++++++++++++--------------- 13 files changed, 313 insertions(+), 190 deletions(-) diff --git a/shaders/gBuffer.fragmentshader b/shaders/gBuffer.fragmentshader index 5522b09ed..7c9cc250b 100644 --- a/shaders/gBuffer.fragmentshader +++ b/shaders/gBuffer.fragmentshader @@ -1,7 +1,8 @@ -in vec3 vertexP_worldspace; -in vec3 vertexN_worldspace; -in vec4 MaterialColor; +in v3 vertexP_worldspace; +in v3 vertexN_worldspace; +in v3 MaterialColor; +in v2 TransEmiss; layout (location = 0) out vec4 gColor; layout (location = 1) out vec3 gNormal; @@ -71,7 +72,7 @@ void main() gPosition.w = Linearize(gl_FragCoord.z); // Depth gNormal = vertexN_worldspace; - vec4 FinalColor = MaterialColor; + vec4 FinalColor = vec4(MaterialColor, 1.f); #if 0 { diff --git a/shaders/gBuffer.vertexshader b/shaders/gBuffer.vertexshader index 0a8be4524..786054838 100644 --- a/shaders/gBuffer.vertexshader +++ b/shaders/gBuffer.vertexshader @@ -14,8 +14,6 @@ uniform mat4 Model; void main() { MaterialColor = vertexColor; - /* MaterialColor.xy = in_TransEmiss.xy; */ - /* MaterialColor.z = 0.f; */ TransEmiss = in_TransEmiss; vertexP_worldspace = vec4(Model * vec4(vertexPosition_modelspace, 1)).xyz; diff --git a/src/engine/asset.cpp b/src/engine/asset.cpp index 6ae791f2a..3972781ef 100644 --- a/src/engine/asset.cpp +++ b/src/engine/asset.cpp @@ -25,6 +25,32 @@ MakeWorldChunkFileHeader_v2(world_chunk *Chunk) return Result; } + +link_internal world_chunk_file_header_v3 +MakeWorldChunkFileHeader_v3(world_chunk *Chunk) +{ + world_chunk_file_header_v3 Result = {}; + + Result.WHNK = WorldChunkFileTag_WHNK; + Result.Version = 3; + Result.Checksum = 0xdeadbeef; + + Result.VoxelElementCount = Volume(Chunk); + Result.StandingSpotElementCount = (u32)AtElements(&Chunk->StandingSpots); + + if (HasMesh(&Chunk->Meshes, MeshBit_Main)) + { + Result.MeshElementCount = Chunk->Meshes.E[MeshIndex_Main]->At; + } + + Result.VertexElementSize = (u32)sizeof(v3); + Result.NormalElementSize = (u32)sizeof(v3); + Result.MaterialElementSize = (u32)sizeof(vertex_material); + Result.StandingSpotElementSize = (u32)sizeof(v3i); + Result.VoxelElementSize = (u32)sizeof(voxel); + + return Result; +} link_internal world_chunk_file_header_v1 MakeWorldChunkFileHeader_v1(world_chunk *Chunk) { @@ -73,7 +99,7 @@ GetSizeRequirements(untextured_3d_geometry_buffer *Mesh) #endif link_internal b32 -SerializeMesh(native_file *File, untextured_3d_geometry_buffer *Mesh, world_chunk_file_header_v2 *FileHeader) +SerializeMesh(native_file *File, untextured_3d_geometry_buffer *Mesh, world_chunk_file_header_v3 *FileHeader) { b32 Result = True; @@ -84,8 +110,8 @@ SerializeMesh(native_file *File, untextured_3d_geometry_buffer *Mesh, world_chun u32 VertElementSize = FileHeader->VertexElementSize; u64 VertByteCount = VertElementSize * TotalElements; - u32 ColorElementSize = FileHeader->ColorElementSize; - u64 ColorByteCount = ColorElementSize * TotalElements; + u32 ColorElementSize = FileHeader->MaterialElementSize; + u64 MatlByteCount = ColorElementSize * TotalElements; u32 NormalElementSize = FileHeader->NormalElementSize; u64 NormalByteCount = NormalElementSize * TotalElements; @@ -94,9 +120,9 @@ SerializeMesh(native_file *File, untextured_3d_geometry_buffer *Mesh, world_chun Result &= WriteToFile(File, Tag); Result &= WriteToFile(File, (u8*)Mesh->Verts, VertByteCount); - Tag = WorldChunkFileTag_COLO; + Tag = WorldChunkFileTag_MATL; Result &= WriteToFile(File, Tag); - Result &= WriteToFile(File, (u8*)Mesh->Colors, ColorByteCount); + Result &= WriteToFile(File, (u8*)Mesh->Mat, MatlByteCount); Tag = WorldChunkFileTag_NORM; Result &= WriteToFile(File, Tag); @@ -106,9 +132,11 @@ SerializeMesh(native_file *File, untextured_3d_geometry_buffer *Mesh, world_chun return Result; } +#if 0 link_internal b32 SerializeMesh(native_file *File, untextured_3d_geometry_buffer *Mesh, world_chunk_file_header_v1 *FileHeader) { + NotImplemented; b32 Result = True; u64 TotalElements = FileHeader->MeshElementCount; @@ -130,7 +158,7 @@ SerializeMesh(native_file *File, untextured_3d_geometry_buffer *Mesh, world_chun Tag = WorldChunkFileTag_COLO; Result &= WriteToFile(File, Tag); - Result &= WriteToFile(File, (u8*)Mesh->Colors, ColorByteCount); + Result &= WriteToFile(File, (u8*)Mesh->Mat, MatlByteCount); Tag = WorldChunkFileTag_NORM; Result &= WriteToFile(File, Tag); @@ -139,6 +167,7 @@ SerializeMesh(native_file *File, untextured_3d_geometry_buffer *Mesh, world_chun return Result; } +#endif #if 0 link_internal void* @@ -167,7 +196,7 @@ Read_u32(native_file *File) } link_internal untextured_3d_geometry_buffer* -DeserializeMesh(native_file *File, world_chunk_file_header *Header, untextured_3d_geometry_buffer *Result) +DeserializeMesh(native_file *File, world_chunk_file_header_v2 *Header, untextured_3d_geometry_buffer *Result) { u64 TotalElements = Header->MeshElementCount; @@ -189,10 +218,56 @@ DeserializeMesh(native_file *File, world_chunk_file_header *Header, untextured_3 // Color data Tag = Read_u32(File); Assert(Tag == WorldChunkFileTag_COLO); + + + // TODO(Jesse): Copy into temp buffer and scatter to material props + NotImplemented; u32 ColorElementSize = Header->ColorElementSize; Assert(ColorElementSize == (u32)sizeof(v4)); - ReadBytesIntoBuffer(File, ColorElementSize*TotalElements, (u8*)Result->Colors); + /* ReadBytesIntoBuffer(File, ColorElementSize*TotalElements, (u8*)Result->Colors); */ + + // + // Normal data + Tag = Read_u32(File); + Assert(Tag == WorldChunkFileTag_NORM); + + u32 NormalElementSize = Header->NormalElementSize; + Assert(NormalElementSize == (u32)sizeof(v3)); + ReadBytesIntoBuffer(File, NormalElementSize*TotalElements, (u8*)Result->Normals); + + Result->Timestamp = __rdtsc(); + + return Result; +} + +link_internal untextured_3d_geometry_buffer* +DeserializeMesh(native_file *File, world_chunk_file_header *Header, untextured_3d_geometry_buffer *Result) +{ + u64 TotalElements = Header->MeshElementCount; + + Assert(Result->At == 0); + Assert(TotalElements < Result->End); + Result->At = (u32)TotalElements; + + // + // Vertex data + // + u32 Tag = Read_u32(File); + Assert(Tag == WorldChunkFileTag_VERT); + + u32 VertElementSize = Header->VertexElementSize; + Assert(VertElementSize == (u32)sizeof(v3)); + ReadBytesIntoBuffer(File, VertElementSize*TotalElements, (u8*)Result->Verts); + + // + // Color data + Tag = Read_u32(File); + Assert(Tag == WorldChunkFileTag_MATL); + + u32 MaterialElementSize = Header->MaterialElementSize; + Assert(MaterialElementSize == (u32)sizeof(vertex_material)); + ReadBytesIntoBuffer(File, MaterialElementSize*TotalElements, (u8*)Result->Mat); // // Normal data @@ -288,9 +363,12 @@ DeserializeChunk(native_file *AssetFile, world_chunk *Result, tiered_mesh_freeli Header.MeshElementCount = v1Header.MeshElementCount; - Header.VertexElementSize = SafeTruncateU8(v1Header.VertexElementSize); - Header.ColorElementSize = SafeTruncateU8(v1Header.ColorElementSize); - Header.NormalElementSize = SafeTruncateU8(v1Header.NormalElementSize); + Header.VertexElementSize = SafeTruncateU8(v1Header.VertexElementSize); + + NotImplemented; // TODO(Jesse): What do we do when we remove a member?? + /* Header.MaterialElementSize = SafeTruncateU8(v1Header.ColorElementSize); */ + + Header.NormalElementSize = SafeTruncateU8(v1Header.NormalElementSize); } break; case 2: @@ -307,13 +385,21 @@ DeserializeChunk(native_file *AssetFile, world_chunk *Result, tiered_mesh_freeli Header.MeshElementCount = v2Header.MeshElementCount; Header.VertexElementSize = v2Header.VertexElementSize; - Header.ColorElementSize = v2Header.ColorElementSize; + + NotImplemented; // TODO(Jesse): What do we do when we remove a member?? + /* Header.ColorElementSize = v2Header.ColorElementSize; */ + Header.NormalElementSize = v2Header.NormalElementSize; Header.StandingSpotElementSize = v2Header.StandingSpotElementSize; Header.VoxelElementSize = v2Header.VoxelElementSize; } break; + case 3: + { + NotImplemented; + } break; + default: { SoftError("Invalid chunk version encountered (%u)", Header.Version); @@ -368,7 +454,7 @@ SerializeChunk(world_chunk *Chunk, counted_string AssetPath) native_file File = OpenFile(Filename, "w+b"); - world_chunk_file_header_v2 FileHeader = MakeWorldChunkFileHeader_v2(Chunk); + world_chunk_file_header_v3 FileHeader = MakeWorldChunkFileHeader_v3(Chunk); Result &= WriteToFile(&File, (u8*)&FileHeader, sizeof(FileHeader)); diff --git a/src/engine/asset.h b/src/engine/asset.h index bb40bfa91..a2ff523a1 100644 --- a/src/engine/asset.h +++ b/src/engine/asset.h @@ -50,11 +50,15 @@ enum world_chunk_file_tag WorldChunkFileTag_VOXD = 'DXOV', // Voxel data WorldChunkFileTag_VERT = 'TREV', // Vertex data - WorldChunkFileTag_COLO = 'OLOC', // Color data WorldChunkFileTag_NORM = 'MRON', // Normal data + WorldChunkFileTag_COLO = 'OLOC', // Color data @@ DEPRECATED @@ + // v2 WorldChunkFileTag_SPOT = 'TOPS', // Standing spots + + // v3 + WorldChunkFileTag_MATL = 'LTAM', // Material data }; struct world_chunk_file_header_v1 @@ -93,9 +97,29 @@ struct world_chunk_file_header_v2 u8 VoxelElementSize; u8 pad[3]; }; + +struct world_chunk_file_header_v3 +{ + u32 WHNK; // WorldChunkFileTag_WHNK + u32 Version = 3; + u64 Checksum; + + u32 VoxelElementCount; + u32 StandingSpotElementCount; + + u64 MeshElementCount; + + u8 VertexElementSize; + u8 NormalElementSize; + u8 MaterialElementSize; + u8 StandingSpotElementSize; + + u8 VoxelElementSize; + u8 pad[3]; +}; #pragma pack(pop) -typedef world_chunk_file_header_v2 world_chunk_file_header; +typedef world_chunk_file_header_v3 world_chunk_file_header; enum asset_load_state { diff --git a/src/engine/debug.cpp b/src/engine/debug.cpp index 58f610987..214180874 100644 --- a/src/engine/debug.cpp +++ b/src/engine/debug.cpp @@ -131,8 +131,8 @@ DebugUi(engine_resources *Engine, cs Name, untextured_3d_geometry_buffer *Value) PushColumn(Ui, CS((u64)Value->Verts)); PushNewRow(Ui); - PushColumn(Ui, CS("Colors : ")); - PushColumn(Ui, CS((u64)Value->Colors)); + PushColumn(Ui, CS("Mats : ")); + PushColumn(Ui, CS((u64)Value->Mat)); PushNewRow(Ui); PushColumn(Ui, CS("Normals : ")); diff --git a/src/engine/debug_draw.cpp b/src/engine/debug_draw.cpp index d4a8dfdf0..9ff38cb92 100644 --- a/src/engine/debug_draw.cpp +++ b/src/engine/debug_draw.cpp @@ -19,8 +19,11 @@ DEBUG_DrawLine( untextured_3d_geometry_buffer *Mesh, v3 P1, v3 P2, u32 ColorInde /* P1.x = P1.x - (Thickness/2.0f); */ /* P2.x = P2.x - (Thickness/2.0f); */ - v3 FaceColors[VERTS_PER_FACE]; - FillColorArray(ColorIndex, FaceColors, DefaultPalette, VERTS_PER_FACE); + v3 Color = GetColorData(DefaultPalette, ColorIndex); + + vertex_material Materials[VERTS_PER_FACE]; + FillArray(VertexMaterial(Color, 0.f, 0.f), Materials, VERTS_PER_FACE); + /* FillColorArray(ColorIndex, Materials, DefaultPalette, VERTS_PER_FACE); */ v2 TransEmiss[VERTS_PER_FACE] = {}; @@ -38,7 +41,7 @@ DEBUG_DrawLine( untextured_3d_geometry_buffer *Mesh, v3 P1, v3 P2, u32 ColorInde }; - BufferVertsChecked(Mesh, 6, localVertexData, localNormalData, FaceColors, TransEmiss); + BufferVertsChecked(Mesh, 6, localVertexData, localNormalData, Materials); } { @@ -54,7 +57,7 @@ DEBUG_DrawLine( untextured_3d_geometry_buffer *Mesh, v3 P1, v3 P2, u32 ColorInde }; - BufferVertsChecked(Mesh, 6, localVertexData, localNormalData, FaceColors, TransEmiss); + BufferVertsChecked(Mesh, 6, localVertexData, localNormalData, Materials); } { @@ -70,7 +73,7 @@ DEBUG_DrawLine( untextured_3d_geometry_buffer *Mesh, v3 P1, v3 P2, u32 ColorInde }; - BufferVertsChecked(Mesh, 6, localVertexData, localNormalData, FaceColors, TransEmiss); + BufferVertsChecked(Mesh, 6, localVertexData, localNormalData, Materials); } u32 VertsBuffered = Mesh->At - MeshStartingAt; diff --git a/src/engine/gpu_mapped_buffer.cpp b/src/engine/gpu_mapped_buffer.cpp index be5668d3a..968f0c06b 100644 --- a/src/engine/gpu_mapped_buffer.cpp +++ b/src/engine/gpu_mapped_buffer.cpp @@ -18,20 +18,26 @@ FlushBuffersToCard(gpu_mapped_element_buffer* GpuMap) GL.VertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); AssertNoGlErrors; - GL.EnableVertexAttribArray(2); - GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->ColorHandle); - BufferUnmapped &= GL.UnmapBuffer(GL_ARRAY_BUFFER); - GpuMap->Buffer.Colors = 0; - GL.VertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); - AssertNoGlErrors; + + const u32 MtlFloatElements = sizeof(matl)/sizeof(f32); + CAssert(MtlFloatElements == 5); + + GL.EnableVertexAttribArray(2); GL.EnableVertexAttribArray(3); - GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->TransEmissHandle); + GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->MatHandle); BufferUnmapped &= GL.UnmapBuffer(GL_ARRAY_BUFFER); - GpuMap->Buffer.TransEmiss = 0; - GL.VertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); + GpuMap->Buffer.Mat = 0; + GL.VertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(matl), (void*)0); + GL.VertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(matl), (void*)12); AssertNoGlErrors; + /* GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->TransEmissHandle); */ + /* BufferUnmapped &= GL.UnmapBuffer(GL_ARRAY_BUFFER); */ + /* GpuMap->Buffer.TransEmiss = 0; */ + /* GL.VertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); */ + /* AssertNoGlErrors; */ + if (BufferUnmapped == False) { Error("glUnmapBuffer Failed"); } @@ -45,6 +51,7 @@ AllocateGpuElementBuffer(gpu_mapped_element_buffer *GpuMap, u32 ElementCount) { u32 v2Size = sizeof(v2)*ElementCount; u32 v3Size = sizeof(v3)*ElementCount; + u32 matlSize = sizeof(matl)*ElementCount; GL.GenBuffers(4, &GpuMap->VertexHandle); @@ -54,11 +61,11 @@ AllocateGpuElementBuffer(gpu_mapped_element_buffer *GpuMap, u32 ElementCount) GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->NormalHandle); GL.BufferData(GL_ARRAY_BUFFER, v3Size, 0, GL_STATIC_DRAW); - GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->ColorHandle); - GL.BufferData(GL_ARRAY_BUFFER, v3Size, 0, GL_STATIC_DRAW); + GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->MatHandle); + GL.BufferData(GL_ARRAY_BUFFER, matlSize, 0, GL_STATIC_DRAW); - GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->TransEmissHandle); - GL.BufferData(GL_ARRAY_BUFFER, v2Size, 0, GL_STATIC_DRAW); + /* GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->TransEmissHandle); */ + /* GL.BufferData(GL_ARRAY_BUFFER, v2Size, 0, GL_STATIC_DRAW); */ GL.BindBuffer(GL_ARRAY_BUFFER, 0); @@ -90,6 +97,7 @@ MapGpuElementBuffer(gpu_mapped_element_buffer *GpuMap) u32 v2Size = sizeof(v2)*ElementCount; u32 v3Size = sizeof(v3)*ElementCount; + u32 matlSize = sizeof(matl)*ElementCount; GL.EnableVertexAttribArray(0); GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->VertexHandle); @@ -103,22 +111,24 @@ MapGpuElementBuffer(gpu_mapped_element_buffer *GpuMap) GL.VertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); AssertNoGlErrors; + + // Color data GL.EnableVertexAttribArray(2); - GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->ColorHandle); - GpuMap->Buffer.Colors = (v3*)GL.MapBufferRange(GL_ARRAY_BUFFER, 0, v3Size, GL_MAP_WRITE_BIT); - GL.VertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); + GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->MatHandle); + GpuMap->Buffer.Mat = (matl*)GL.MapBufferRange(GL_ARRAY_BUFFER, 0, matlSize, GL_MAP_WRITE_BIT); + GL.VertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(matl), (void*)0); AssertNoGlErrors; + // transparency & emission data GL.EnableVertexAttribArray(3); - GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->TransEmissHandle); - GpuMap->Buffer.TransEmiss = (v2*)GL.MapBufferRange(GL_ARRAY_BUFFER, 0, v2Size, GL_MAP_WRITE_BIT); - GL.VertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); + /* GL.BindBuffer(GL_ARRAY_BUFFER, GpuMap->MatHandle); */ + /* GpuMap->Buffer.Mat = (matl*)GL.MapBufferRange(GL_ARRAY_BUFFER, 0, matlSize, GL_MAP_WRITE_BIT); */ + GL.VertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(matl), (void*)12); AssertNoGlErrors; if (!GpuMap->Buffer.Verts) { Error("Allocating gpu_mapped_element_buffer::Verts"); } if (!GpuMap->Buffer.Normals) { Error("Allocating gpu_mapped_element_buffer::Normals"); } - if (!GpuMap->Buffer.Colors) { Error("Allocating gpu_mapped_element_buffer::Colors"); } - if (!GpuMap->Buffer.TransEmiss) { Error("Allocating gpu_mapped_element_buffer::TransEmiss"); } + if (!GpuMap->Buffer.Mat) { Error("Allocating gpu_mapped_element_buffer::Mat"); } GL.BindBuffer(GL_ARRAY_BUFFER, 0); diff --git a/src/engine/loaders/vox.cpp b/src/engine/loaders/vox.cpp index cb2a1e6e0..8424dd71d 100644 --- a/src/engine/loaders/vox.cpp +++ b/src/engine/loaders/vox.cpp @@ -325,6 +325,8 @@ LoadVoxData(memory_arena *WorldStorage, heap_allocator *Heap, char const *filepa Result.ChunkData = AllocateChunkData(WorldStorage, ModelDim); Result.ChunkData->Dim = ModelDim; + global_variable random_series TMP = {54235432543}; + for( s32 VoxelCacheIndex = 0; VoxelCacheIndex < ActualVoxelCount; ++VoxelCacheIndex) @@ -333,7 +335,9 @@ LoadVoxData(memory_arena *WorldStorage, heap_allocator *Heap, char const *filepa Voxel->Offset = Voxel->Offset - Min + HalfApronMin; s32 Index = GetIndex(Voxel->Offset, ModelDim); Result.ChunkData->Voxels[Index] = Voxel->V; - Result.ChunkData->Voxels[Index].Transparency = 1; + + /* if (RandomUnilateral(&TMP) > 0.5f) { Result.ChunkData->Voxels[Index].Transparency = 128; } */ + Result.ChunkData->Voxels[Index].Transparency = 128; Result.ChunkData->VoxelLighting[Index] = VoxelLighting(0xff); } diff --git a/src/engine/mesh.cpp b/src/engine/mesh.cpp index 8d546f659..d727c37df 100644 --- a/src/engine/mesh.cpp +++ b/src/engine/mesh.cpp @@ -1,3 +1,4 @@ +#if 0 // TODO(Jesse): Delete this func in favor of BuildWorldChunkMesh link_internal void BuildEntityMesh(chunk_data *Chunk, untextured_3d_geometry_buffer* Mesh, v3 *ColorPalette, chunk_dimension Dim) @@ -21,10 +22,12 @@ BuildEntityMesh(chunk_data *Chunk, untextured_3d_geometry_buffer* Mesh, v3 *Colo v3 Diameter = V3(1.0f); v3 VertexData[6]; - v3 FaceColors[VERTS_PER_FACE]; - FillColorArray(Voxel->Color, FaceColors, ColorPalette, VERTS_PER_FACE); + /* v3 FaceColors[VERTS_PER_FACE]; */ + /* FillColorArray(Voxel->Color, FaceColors, ColorPalette, VERTS_PER_FACE); */ - v2 TransEmissArray[VERTS_PER_FACE] = {}; + v3 Color = GetColorData(ColorPalette, Voxel->Color); + vertex_material Materials[VERTS_PER_FACE]; + FillArray(VertexMaterial(Color, 0.f, 0.f), Materials, VERTS_PER_FACE); voxel_position rightVoxel = LocalVoxelP + Voxel_Position(1, 0, 0); voxel_position leftVoxel = LocalVoxelP - Voxel_Position(1, 0, 0); @@ -73,14 +76,14 @@ BuildEntityMesh(chunk_data *Chunk, untextured_3d_geometry_buffer* Mesh, v3 *Colo } } } +#endif void AllocateMesh(untextured_3d_geometry_buffer *Mesh, u32 NumVerts, memory_arena *Memory) { Mesh->Verts = AllocateAlignedProtection(v3, Memory, NumVerts, CACHE_LINE_SIZE, False); Mesh->Normals = AllocateAlignedProtection(v3, Memory, NumVerts, CACHE_LINE_SIZE, False); - Mesh->Colors = AllocateAlignedProtection(v3, Memory, NumVerts, CACHE_LINE_SIZE, False); - Mesh->TransEmiss = AllocateAlignedProtection(v2, Memory, NumVerts, CACHE_LINE_SIZE, False); + Mesh->Mat = AllocateAlignedProtection(matl, Memory, NumVerts, CACHE_LINE_SIZE, False); Mesh->End = NumVerts; Mesh->At = 0; @@ -94,8 +97,7 @@ AllocateMesh(untextured_3d_geometry_buffer *Mesh, u32 NumVerts, heap_allocator * { Mesh->Verts = (v3*)HeapAllocate(Heap, sizeof(v3)*NumVerts); Mesh->Normals = (v3*)HeapAllocate(Heap, sizeof(v3)*NumVerts); - Mesh->Colors = (v3*)HeapAllocate(Heap, sizeof(v4)*NumVerts); - Mesh->TransEmiss = (v2*)HeapAllocate(Heap, sizeof(v2)*NumVerts); + Mesh->Mat = (matl*)HeapAllocate(Heap, sizeof(matl)*NumVerts); Mesh->End = NumVerts; Mesh->At = 0; diff --git a/src/engine/mesh.h b/src/engine/mesh.h index 2bd0fb7c6..4e30b6fdd 100644 --- a/src/engine/mesh.h +++ b/src/engine/mesh.h @@ -92,7 +92,7 @@ inline void BufferVertsDirect( untextured_3d_geometry_buffer *Dest, u32 NumVerts, - v3 *Positions, v3 *Normals, v3 *Colors, v2 *TransEmiss + v3 *Positions, v3 *Normals, vertex_material *Mats ) { TIMED_FUNCTION(); @@ -100,8 +100,8 @@ BufferVertsDirect( { MemCopy((u8*)Positions, (u8*)&Dest->Verts[Dest->At], sizeof(*Positions)*NumVerts ); MemCopy((u8*)Normals, (u8*)&Dest->Normals[Dest->At], sizeof(*Normals)*NumVerts ); - MemCopy((u8*)Colors, (u8*)&Dest->Colors[Dest->At], sizeof(*Colors)*NumVerts ); - MemCopy((u8*)TransEmiss, (u8*)&Dest->TransEmiss[Dest->At], sizeof(*TransEmiss)*NumVerts ); + MemCopy((u8*)Mats, (u8*)&Dest->Mat[Dest->At], sizeof(*Mats)*NumVerts ); + /* MemCopy((u8*)TransEmiss, (u8*)&Dest->TransEmiss[Dest->At], sizeof(*TransEmiss)*NumVerts ); */ Dest->At += NumVerts; } @@ -114,9 +114,9 @@ BufferVertsDirect( inline void BufferVertsDirect( - v3 *DestVerts, v3 *DestNormals, v3 *DestColors, v2 *DestTransEmiss, + v3 *DestVerts, v3 *DestNormals, vertex_material *DestMats, u32 NumVerts, - v3 *SrcVerts, v3 *SrcNormals, v3 *SrcVertColors, v2 *SrcTransEmiss, + v3 *SrcVerts, v3 *SrcNormals, vertex_material *SrcMats, v3 Offset, v3 Scale, Quaternion Rot @@ -129,9 +129,8 @@ BufferVertsDirect( Assert(NumVerts % 3 == 0); - MemCopy((u8*)SrcNormals, (u8*)DestNormals, sizeof(*SrcNormals)*NumVerts ); - MemCopy((u8*)SrcVertColors, (u8*)DestColors, sizeof(*SrcVertColors)*NumVerts ); - MemCopy((u8*)SrcTransEmiss, (u8*)DestTransEmiss, sizeof(*SrcTransEmiss)*NumVerts ); + MemCopy((u8*)SrcNormals, (u8*)DestNormals, sizeof(*SrcNormals)*NumVerts ); + MemCopy((u8*)SrcMats, (u8*)DestMats, sizeof(*SrcMats)*NumVerts ); /* v3 HalfOffset = Offset*0.5f; */ for ( u32 VertIndex = 0; @@ -163,8 +162,8 @@ BufferVertsDirect( DestVerts[2] = Result2; DestVerts += 3; - DestNormals += 3; - DestColors += 3; + /* DestNormals += 3; */ + /* DestMats += 3; */ } @@ -173,9 +172,9 @@ BufferVertsDirect( } inline void BufferVertsDirect( - v3 *DestVerts, v3 *DestNormals, v3 *DestColors, v2 *DestTransEmiss, + v3 *DestVerts, v3 *DestNormals, vertex_material *DestMats, u32 NumVerts, - v3 *SrcVerts, v3 *SrcNormals, v3 *SrcVertColors, v2 *SrcTransEmiss, + v3 *SrcVerts, v3 *SrcNormals, vertex_material *SrcMats, v3 Offset, v3 Scale ) @@ -188,8 +187,7 @@ BufferVertsDirect( Assert(NumVerts % 3 == 0); MemCopy((u8*)SrcNormals, (u8*)DestNormals, sizeof(*SrcNormals)*NumVerts ); - MemCopy((u8*)SrcVertColors, (u8*)DestColors, sizeof(*SrcVertColors)*NumVerts ); - MemCopy((u8*)SrcTransEmiss, (u8*)DestTransEmiss, sizeof(*SrcTransEmiss)*NumVerts ); + MemCopy((u8*)SrcMats, (u8*)DestMats, sizeof(*SrcMats)*NumVerts ); for ( u32 VertIndex = 0; VertIndex < NumVerts; @@ -220,8 +218,9 @@ BufferVertsDirect( DestVerts[2] = Result2; DestVerts += 3; - DestNormals += 3; - DestColors += 3; + /* DestNormals += 3; */ + /* DestMats += 3; */ + /* DestColors += 3; */ } @@ -233,7 +232,7 @@ inline void BufferVertsChecked( untextured_3d_geometry_buffer *Dest, u32 NumVerts, - v3 *VertsPositions, v3 *Normals, v3 *VertColors, v2 *TransEmiss, + v3 *VertsPositions, v3 *Normals, vertex_material *Mats, v3 Offset, v3 Scale, Quaternion Rot @@ -244,10 +243,9 @@ BufferVertsChecked( { BufferVertsDirect(Dest->Verts + Dest->At, Dest->Normals + Dest->At, - Dest->Colors + Dest->At, - Dest->TransEmiss + Dest->At, + Dest->Mat + Dest->At, NumVerts, - VertsPositions, Normals, VertColors, TransEmiss, + VertsPositions, Normals, Mats, Offset, Scale, Rot); Dest->At += NumVerts; @@ -263,7 +261,7 @@ inline void BufferVertsDirect( untextured_3d_geometry_buffer *Dest, u32 NumVerts, - v3 *VertsPositions, v3 *Normals, v3 *VertColors, v2 *TransEmiss, + v3 *VertsPositions, v3 *Normals, vertex_material *Mats, v3 Offset, v3 Scale ) @@ -273,10 +271,9 @@ BufferVertsDirect( { BufferVertsDirect(Dest->Verts + Dest->At, Dest->Normals + Dest->At, - Dest->Colors + Dest->At, - Dest->TransEmiss + Dest->At, + Dest->Mat + Dest->At, NumVerts, - VertsPositions, Normals, VertColors, TransEmiss, + VertsPositions, Normals, Mats, Offset, Scale); Dest->At += NumVerts; @@ -304,10 +301,9 @@ BufferVertsCounted( BufferVertsDirect(Dest->Verts + Dest->At, Dest->Normals + Dest->At, - Dest->Colors + Dest->At, - Dest->TransEmiss + Dest->At, + Dest->Mat + Dest->At, Src->At, - Src->Verts, Src->Normals, Src->Colors, Src->TransEmiss, + Src->Verts, Src->Normals, Src->Mat, Offset, Scale); Dest->At += Src->At; @@ -336,10 +332,9 @@ BufferVertsChecked( BufferVertsDirect(Dest->Verts + Dest->At, Dest->Normals + Dest->At, - Dest->Colors + Dest->At, - Dest->TransEmiss + Dest->At, + Dest->Mat + Dest->At, Src->At, - Src->Verts, Src->Normals, Src->Colors, Src->TransEmiss, + Src->Verts, Src->Normals, Src->Mat, Offset, Scale); Dest->At += Src->At; @@ -355,7 +350,7 @@ inline void BufferVertsChecked( untextured_3d_geometry_buffer *Target, u32 NumVerts, - v3 *Positions, v3 *Normals, v3 *Colors, v2 *TransEmiss, + v3 *Positions, v3 *Normals, vertex_material *Mats, v3 Offset = V3(0), v3 Scale = V3(1) ) @@ -364,7 +359,7 @@ BufferVertsChecked( if (BufferHasRoomFor(Target, NumVerts)) { - BufferVertsDirect( Target, NumVerts, Positions, Normals, Colors,TransEmiss, Offset, Scale); + BufferVertsDirect( Target, NumVerts, Positions, Normals, Mats, Offset, Scale); } else { @@ -383,31 +378,31 @@ DrawVoxel( untextured_3d_geometry_buffer *Mesh, v3 RenderP_VoxelCenter, v3 Color v3 VertexData[6]; - v3 FaceColors[VERTS_PER_FACE]; - FillColorArray(Color, FaceColors, VERTS_PER_FACE); + /* v3 FaceColors[VERTS_PER_FACE]; */ + /* FillColorArray(Color, FaceColors, VERTS_PER_FACE); */ - v2 TransEmissArray[VERTS_PER_FACE]; - FillArray(TransEmiss, TransEmissArray, VERTS_PER_FACE); + vertex_material Materials[VERTS_PER_FACE]; + FillArray(VertexMaterial(Color, TransEmiss.E[0], TransEmiss.E[1]), Materials, VERTS_PER_FACE); v3 MinP = RenderP_VoxelCenter - (Diameter*0.5); RightFaceVertexData( MinP, Diameter, VertexData); - BufferVertsChecked(Mesh, 6, VertexData, RightFaceNormalData, FaceColors, TransEmissArray); + BufferVertsChecked(Mesh, 6, VertexData, RightFaceNormalData, Materials); LeftFaceVertexData( MinP, Diameter, VertexData); - BufferVertsChecked(Mesh, 6, VertexData, LeftFaceNormalData, FaceColors, TransEmissArray); + BufferVertsChecked(Mesh, 6, VertexData, LeftFaceNormalData, Materials); BottomFaceVertexData( MinP, Diameter, VertexData); - BufferVertsChecked(Mesh, 6, VertexData, BottomFaceNormalData, FaceColors, TransEmissArray); + BufferVertsChecked(Mesh, 6, VertexData, BottomFaceNormalData, Materials); TopFaceVertexData( MinP, Diameter, VertexData); - BufferVertsChecked(Mesh, 6, VertexData, TopFaceNormalData, FaceColors, TransEmissArray); + BufferVertsChecked(Mesh, 6, VertexData, TopFaceNormalData, Materials); FrontFaceVertexData( MinP, Diameter, VertexData); - BufferVertsChecked(Mesh, 6, VertexData, FrontFaceNormalData, FaceColors, TransEmissArray); + BufferVertsChecked(Mesh, 6, VertexData, FrontFaceNormalData, Materials); BackFaceVertexData( MinP, Diameter, VertexData); - BufferVertsChecked(Mesh, 6, VertexData, BackFaceNormalData, FaceColors, TransEmissArray); + BufferVertsChecked(Mesh, 6, VertexData, BackFaceNormalData, Materials); } inline void diff --git a/src/engine/render.h b/src/engine/render.h index 7eb3753c5..de56d5fcc 100644 --- a/src/engine/render.h +++ b/src/engine/render.h @@ -40,13 +40,13 @@ struct g_buffer_render_group }; untextured_3d_geometry_buffer -Untextured3dGeometryBuffer(v3* Verts, v3* Colors, v3* Normals, v2 *TransEmiss, u32 Count) +Untextured3dGeometryBuffer(v3* Verts, v3* Normals, vertex_material *Mat, u32 Count) { untextured_3d_geometry_buffer Result = {}; Result.Verts = Verts; - Result.Colors = Colors; Result.Normals = Normals; - Result.TransEmiss = TransEmiss; + Result.Mat = Mat; + /* Result.TransEmiss = TransEmiss; */ Result.At = Count; return Result; diff --git a/src/engine/triangle.cpp b/src/engine/triangle.cpp index 7b02396a6..e4ed8c619 100644 --- a/src/engine/triangle.cpp +++ b/src/engine/triangle.cpp @@ -80,12 +80,11 @@ BufferTriangle(untextured_3d_geometry_buffer* Dest, triangle* Triangle, v3 Norma VertBuffer[1] = V3(Triangle->Points[1]); VertBuffer[2] = V3(Triangle->Points[2]); - v3 FaceColors[VERTS_PER_FACE]; - FillColorArray(ColorIndex, FaceColors, DefaultPalette, VERTS_PER_FACE); + v3 Color = GetColorData(DefaultPalette, ColorIndex); + vertex_material Materials[VERTS_PER_FACE]; + FillArray(VertexMaterial(Color, 0.f, 0.f), Materials, VERTS_PER_FACE); - v2 TransEmiss[VERTS_PER_FACE] = {}; - - BufferVertsChecked( Dest, 3, VertBuffer, NormalBuffer, FaceColors, TransEmiss); + BufferVertsChecked( Dest, 3, VertBuffer, NormalBuffer, Materials); } inline void @@ -97,12 +96,11 @@ BufferTriangle(untextured_3d_geometry_buffer *Mesh, v3 *Verts, v3 Normal, u32 Co // TODO(Jesse, id: 139, tags: robustness, speed): Is this necessary to avoid some pointer aliasing bug? MemCopy((u8*)Verts, (u8*)VertBuffer, 9 * sizeof(r32) ); - v3 FaceColors[VERTS_PER_FACE]; - FillColorArray(ColorIndex, FaceColors, DefaultPalette, VERTS_PER_FACE); - - v2 TransEmiss[VERTS_PER_FACE] = {}; + v3 Color = GetColorData(DefaultPalette, ColorIndex); + vertex_material Materials[VERTS_PER_FACE]; + FillArray(VertexMaterial(Color, 0.f, 0.f), Materials, VERTS_PER_FACE); - BufferVertsChecked( Mesh, 3, VertBuffer, NormalBuffer, FaceColors, TransEmiss); + BufferVertsChecked( Mesh, 3, VertBuffer, NormalBuffer, Materials); } #if 0 diff --git a/src/engine/world_chunk.cpp b/src/engine/world_chunk.cpp index 27081dbef..9c7de9bd5 100644 --- a/src/engine/world_chunk.cpp +++ b/src/engine/world_chunk.cpp @@ -743,6 +743,9 @@ IsDifferentTransparency(voxel *Voxels, s32 SrcIndex, v3i DestP, v3i SrcChunkDim) voxel *SrcVox = Voxels+SrcIndex; voxel *DstVox = Voxels+DestIndex; +#if 0 + b32 Result = True; +#else b32 Result = False; if (SrcVox->Transparency) { @@ -752,7 +755,7 @@ IsDifferentTransparency(voxel *Voxels, s32 SrcIndex, v3i DestP, v3i SrcChunkDim) } else { - Result = True; + Result = False; } } else @@ -766,6 +769,7 @@ IsDifferentTransparency(voxel *Voxels, s32 SrcIndex, v3i DestP, v3i SrcChunkDim) Result = False; } } +#endif return Result; } @@ -958,10 +962,9 @@ DrawDebugVoxels( voxel *Voxels, /* random_series ColorEntropy = {33453}; */ - v3 Diameter = V3(1.0f); - v3 VertexData[VERTS_PER_FACE]; - v3 FaceColors[VERTS_PER_FACE]; - v2 TransEmiss[VERTS_PER_FACE]; + v3 Diameter = V3(1.0f); + v3 VertexData[VERTS_PER_FACE]; + matl Materials[VERTS_PER_FACE]; auto MinDim = SrcChunkMin; auto MaxDim = Min(SrcChunkDim, SrcChunkMax); // SrcChunkMin+DestChunkDim+1 @@ -975,48 +978,39 @@ DrawDebugVoxels( voxel *Voxels, s32 Index = GetIndex(DestP, SrcChunkDim); voxel *Voxel = Voxels + Index; -#if 0 - FillColorArray(RED, FaceColors, DefaultPalette, VERTS_PER_FACE); - if (Voxel->Flags & Voxel_Filled) - { - TopFaceVertexData( V3(DestP-SrcChunkMin), V3(0.25f), VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, FaceColors); - /* DrawVoxel( DestGeometry, V3(x,y,z), V4(1,0,0,1), V3(.1f)); */ - } -#endif - // TODO(Jesse): This copy could be avoided in multiple ways, and should be. - FillColorArray(Voxel->Color, FaceColors, DefaultPalette, VERTS_PER_FACE); + v3 Color = GetColorData(DefaultPalette, Voxel->Color); + FillArray(VertexMaterial(Color, 0.f, 0.f), Materials, VERTS_PER_FACE); if (Voxel->Flags & Voxel_RightFace) { RightFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, Materials); } if (Voxel->Flags & Voxel_LeftFace) { LeftFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BottomFace) { BottomFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, Materials); } if (Voxel->Flags & Voxel_TopFace) { TopFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, Materials); } if (Voxel->Flags & Voxel_FrontFace) { FrontFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BackFace) { BackFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, Materials); } } } @@ -1196,7 +1190,8 @@ BuildWorldChunkMesh_DebugVoxels( voxel *Voxels, s32 backVoxelReadIndex; v3 VertexData[VERTS_PER_FACE]; - v4 FaceColors[VERTS_PER_FACE]; + /* v4 FaceColors[VERTS_PER_FACE]; */ + matl Materials[VERTS_PER_FACE]; auto SrcMinP = SrcChunkMin; auto MaxDim = Min(SrcChunkDim, SrcChunkMax); // SrcChunkMin+DestChunkDim+1 @@ -1224,27 +1219,27 @@ BuildWorldChunkMesh_DebugVoxels( voxel *Voxels, { RightFaceVertexData( DestP, Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, Materials); } { LeftFaceVertexData( DestP, Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, Materials); } { BottomFaceVertexData( DestP, Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, Materials); } { TopFaceVertexData( DestP, Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, Materials); } { FrontFaceVertexData( DestP, Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, Materials); } { BackFaceVertexData( DestP, Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, Materials); } } TmpIndex++; @@ -1291,8 +1286,9 @@ BuildWorldChunkMeshFromMarkedVoxels_Greedy( voxel *Voxels, v3 VertexData[VERTS_PER_FACE]; - v3 FaceColors[VERTS_PER_FACE]; - v2 TransEmiss[VERTS_PER_FACE]; + /* v3 FaceColors[VERTS_PER_FACE]; */ + /* v2 TransEmiss[VERTS_PER_FACE]; */ + matl Materials[VERTS_PER_FACE]; auto SrcMinP = SrcChunkMin; auto MaxDim = Min(SrcChunkDim, SrcChunkMax); // SrcChunkMin+DestChunkDim+1 @@ -1331,59 +1327,55 @@ BuildWorldChunkMeshFromMarkedVoxels_Greedy( voxel *Voxels, voxel *Voxel = TempVoxels + Index; /* u8 C = ((Voxel->Color + RandomU32(&ColorEntropy)) & 0xFF); */ - u8 C = Voxel->Color; + /* u8 C = Voxel->Color; */ // TODO(Jesse): This copy could be avoided in multiple ways, and should be. - FillColorArray(C, FaceColors, ColorPallette, VERTS_PER_FACE); + /* FillColorArray(C, FaceColors, ColorPallette, VERTS_PER_FACE); */ + + + v3 Color = GetColorData(ColorPallette, Voxel->Color); + f32 Trans = (f32)Voxel->Transparency / 255.f; + FillArray(VertexMaterial(Color, Trans, 0.f), Materials, VERTS_PER_FACE); untextured_3d_geometry_buffer *Dest = {}; - if (Voxel->Transparency) - { - Dest = DestTransparentGeometry; - FillArray(V2(1.f, 0.f), TransEmiss, VERTS_PER_FACE); - } - else - { - Dest = DestGeometry; - FillArray({}, TransEmiss, VERTS_PER_FACE); - } + if (Voxel->Transparency) { Dest = DestTransparentGeometry; } else { Dest = DestGeometry; } if (Voxel->Flags & Voxel_RightFace) { v3 Dim = DoXStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_RightFace, Voxel->Color); RightFaceVertexData( V3(TmpVoxP), Dim, VertexData); - BufferVertsDirect(Dest, 6, VertexData, RightFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(Dest, 6, VertexData, RightFaceNormalData, Materials); } if (Voxel->Flags & Voxel_LeftFace) { v3 Dim = DoXStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_LeftFace, Voxel->Color); LeftFaceVertexData( V3(TmpVoxP), Dim, VertexData); - BufferVertsDirect(Dest, 6, VertexData, LeftFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(Dest, 6, VertexData, LeftFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BottomFace) { v3 Dim = DoZStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_BottomFace, Voxel->Color); BottomFaceVertexData( V3(TmpVoxP), Dim, VertexData); - BufferVertsDirect(Dest, 6, VertexData, BottomFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(Dest, 6, VertexData, BottomFaceNormalData, Materials); } if (Voxel->Flags & Voxel_TopFace) { v3 Dim = DoZStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_TopFace, Voxel->Color); TopFaceVertexData( V3(TmpVoxP), Dim, VertexData); - BufferVertsDirect(Dest, 6, VertexData, TopFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(Dest, 6, VertexData, TopFaceNormalData, Materials); } if (Voxel->Flags & Voxel_FrontFace) { v3 Dim = DoYStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_FrontFace, Voxel->Color); FrontFaceVertexData( V3(TmpVoxP), Dim, VertexData); - BufferVertsDirect(Dest, 6, VertexData, FrontFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(Dest, 6, VertexData, FrontFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BackFace) { v3 Dim = DoYStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_BackFace, Voxel->Color); BackFaceVertexData( V3(TmpVoxP), Dim, VertexData); - BufferVertsDirect(Dest, 6, VertexData, BackFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(Dest, 6, VertexData, BackFaceNormalData, Materials); } } } @@ -1436,8 +1428,9 @@ BuildMipMesh( voxel *Voxels, v3 VertexData[VERTS_PER_FACE]; - v3 FaceColors[VERTS_PER_FACE]; - v2 TransEmiss[VERTS_PER_FACE]; + /* v3 FaceColors[VERTS_PER_FACE]; */ + /* v2 TransEmiss[VERTS_PER_FACE]; */ + matl Materials[VERTS_PER_FACE]; Assert(VoxDim >= InnerMax); @@ -1565,47 +1558,51 @@ BuildMipMesh( voxel *Voxels, voxel_position ActualP = TmpVoxP-1; /* u8 C = ((Voxel->Color + RandomU32(&ColorEntropy)) & 0xFF); */ - u8 C = Voxel->Color; + /* u8 C = Voxel->Color; */ // TODO(Jesse): This copy could be avoided in multiple ways, and should be. - FillColorArray(C, FaceColors, ColorPallette, VERTS_PER_FACE); + /* FillColorArray(C, FaceColors, ColorPallette, VERTS_PER_FACE); */ + + v3 Color = GetColorData(ColorPallette, Voxel->Color); + f32 Trans = (f32)Voxel->Transparency / 255.f; + FillArray(VertexMaterial(Color, Trans, 0.f), Materials, VERTS_PER_FACE); if (Voxel->Flags & Voxel_RightFace) { v3 Dim = DoXStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_RightFace, Voxel->Color); RightFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, Materials); } if (Voxel->Flags & Voxel_LeftFace) { v3 Dim = DoXStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_LeftFace, Voxel->Color); LeftFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BottomFace) { v3 Dim = DoZStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_BottomFace, Voxel->Color); BottomFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, Materials); } if (Voxel->Flags & Voxel_TopFace) { v3 Dim = DoZStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_TopFace, Voxel->Color); TopFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, Materials); } if (Voxel->Flags & Voxel_FrontFace) { v3 Dim = DoYStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_FrontFace, Voxel->Color); FrontFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BackFace) { v3 Dim = DoYStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_BackFace, Voxel->Color); BackFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, Materials); } } } @@ -1651,8 +1648,9 @@ BuildWorldChunkMeshFromMarkedVoxels_Naieve( voxel *Voxels, v3 Diameter = V3(1.0f); v3 VertexData[VERTS_PER_FACE]; - v3 FaceColors[VERTS_PER_FACE]; - v2 TransEmiss[VERTS_PER_FACE] = {}; + /* v3 FaceColors[VERTS_PER_FACE]; */ + /* v2 TransEmiss[VERTS_PER_FACE] = {}; */ + matl Materials[VERTS_PER_FACE]; auto MinDim = SrcChunkMin; auto MaxDim = Min(VoxDim, SrcChunkMax); // SrcChunkMin+DestChunkDim+1 @@ -1667,37 +1665,41 @@ BuildWorldChunkMeshFromMarkedVoxels_Naieve( voxel *Voxels, voxel *Voxel = Voxels + Index; // TODO(Jesse): This copy could be avoided in multiple ways, and should be. - FillColorArray(Voxel->Color, FaceColors, ColorPallette, VERTS_PER_FACE); + /* FillColorArray(Voxel->Color, FaceColors, ColorPallette, VERTS_PER_FACE); */ + + v3 Color = GetColorData(ColorPallette, Voxel->Color); + f32 Trans = (f32)Voxel->Transparency / 255.f; + FillArray(VertexMaterial(Color, Trans, 0.f), Materials, VERTS_PER_FACE); if (Voxel->Flags & Voxel_RightFace) { RightFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, Materials); } if (Voxel->Flags & Voxel_LeftFace) { LeftFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BottomFace) { BottomFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, Materials); } if (Voxel->Flags & Voxel_TopFace) { TopFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, Materials); } if (Voxel->Flags & Voxel_FrontFace) { FrontFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BackFace) { BackFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, FaceColors, TransEmiss); + BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, Materials); } } } @@ -1788,32 +1790,32 @@ BuildWorldChunkMesh_Direct( voxel *Voxels, if ( !IsInsideDim( VoxDim, rightVoxel) || NotFilled( Voxels, rightVoxel, VoxDim) ) { RightFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, Materials); } if ( !IsInsideDim( VoxDim, leftVoxel) || NotFilled( Voxels, leftVoxel, VoxDim) ) { LeftFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, Materials); } if ( !IsInsideDim( VoxDim, botVoxel) || NotFilled( Voxels, botVoxel, VoxDim) ) { BottomFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, Materials); } if ( !IsInsideDim( VoxDim, topVoxel) || NotFilled( Voxels, topVoxel, VoxDim) ) { TopFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, Materials); } if ( !IsInsideDim( VoxDim, frontVoxel) || NotFilled( Voxels, frontVoxel, VoxDim) ) { FrontFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, Materials); } if ( !IsInsideDim( VoxDim, backVoxel) || NotFilled( Voxels, backVoxel, VoxDim) ) { BackFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, FaceColors); + BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, Materials); } } } @@ -3986,10 +3988,10 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ random_series Entropy = {43246}; // NOTE(Jesse): We can actually do the entire dim here, but it's probably // better (faster) to just do what we actually need to - MarkBoundaryVoxels_NoExteriorFaces( CopiedVoxels, QueryDim, {{1,1,1}}, QueryDim-1, &Entropy, GREY_5, GREY_7); + /* MarkBoundaryVoxels_NoExteriorFaces( CopiedVoxels, QueryDim, {{1,1,1}}, QueryDim-1, &Entropy, GREY_5, GREY_7); */ /* MarkBoundaryVoxels_NoExteriorFaces( CopiedVoxels, QueryDim, {}, QueryDim, &Entropy, GREY_5, GREY_7); */ /* MarkBoundaryVoxels_MakeExteriorFaces( CopiedVoxels, QueryDim, {{1,1,1}}, QueryDim-1); */ - /* MarkBoundaryVoxels_MakeExteriorFaces( CopiedVoxels, QueryDim, {}, QueryDim); */ + MarkBoundaryVoxels_MakeExteriorFaces( CopiedVoxels, QueryDim, {}, QueryDim); for (u32 ChunkIndex = 0; ChunkIndex < ChunkCount; ++ChunkIndex) @@ -4565,14 +4567,14 @@ BufferChunkMesh( graphics *Graphics, if (Length(Rot.xyz) == 0.f) { BufferVertsChecked(&CopyBuffer, Src->At, - Src->Verts, Src->Normals, Src->Colors, Src->TransEmiss, + Src->Verts, Src->Normals, Src->Mat, ModelBasisP, V3(Scale)); } else { BufferVertsChecked(&CopyBuffer, Src->At, - Src->Verts, Src->Normals, Src->Colors, Src->TransEmiss, + Src->Verts, Src->Normals, Src->Mat, ModelBasisP, V3(Scale), Rot); } } From a69f2271821fa6741ddfb142fcbe4c5ca3a15a47 Mon Sep 17 00:00:00 2001 From: a pirate Date: Fri, 27 Oct 2023 16:58:48 -0600 Subject: [PATCH 09/15] Fix issue when a transparent face was the last in the update kernel --- src/engine/world_chunk.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/engine/world_chunk.cpp b/src/engine/world_chunk.cpp index 9c7de9bd5..ca050fd37 100644 --- a/src/engine/world_chunk.cpp +++ b/src/engine/world_chunk.cpp @@ -3667,6 +3667,17 @@ QueueWorldUpdateForRegion(engine_resources *Engine, world_update_op_mode Mode, w cp MinPCoarse = {}; cp MaxPCoarse = {}; + + // NOTE(Jesse): We have to expand by at least two in either direction because + // we need at least one additional voxel to tell if we have a solid face, and + // another one after that in case the last one on the exterior edge is + // transparent and the next one is solid + // + // I can't remember why the MaxPStroke has to be one more, and I actually + // think that it might not . + f32 MinPStroke = 2.f; + f32 MaxPStroke = 3.f; + switch (Shape->Type) { InvalidCase(type_world_update_op_shape_params_noop); @@ -3676,9 +3687,8 @@ QueueWorldUpdateForRegion(engine_resources *Engine, world_update_op_mode Mode, w auto *ShapeSphere = SafeCast(world_update_op_shape_params_sphere, Shape); cp P = ShapeSphere->Location; - MinPCoarse = Canonicalize(World, P-V3(ShapeSphere->Radius+1.f) - V3(Global_ChunkApronMinDim)); - // TODO(Jesse): I think because we're eventually comparing MaxP with <= the +2 here can be a +1 ..? - MaxPCoarse = Canonicalize(World, P+V3(ShapeSphere->Radius+2.f) + V3(Global_ChunkApronMaxDim)); + MinPCoarse = Canonicalize(World, P-V3(ShapeSphere->Radius+MinPStroke) - V3(Global_ChunkApronMinDim)); + MaxPCoarse = Canonicalize(World, P+V3(ShapeSphere->Radius+MaxPStroke) + V3(Global_ChunkApronMaxDim)); } break; case type_world_update_op_shape_params_rect: @@ -3688,8 +3698,8 @@ QueueWorldUpdateForRegion(engine_resources *Engine, world_update_op_mode Mode, w v3 MinP0 = Min(ShapeRect->P0, ShapeRect->P1); v3 MaxP0 = Max(ShapeRect->P0, ShapeRect->P1); - MinPCoarse = SimSpaceToCanonical(World, MinP0 - V3(1) - V3(Global_ChunkApronMinDim)); - MaxPCoarse = SimSpaceToCanonical(World, MaxP0 + V3(2) + V3(Global_ChunkApronMaxDim)); + MinPCoarse = SimSpaceToCanonical(World, MinP0 - V3(MinPStroke) - V3(Global_ChunkApronMinDim)); + MaxPCoarse = SimSpaceToCanonical(World, MaxP0 + V3(MaxPStroke) + V3(Global_ChunkApronMaxDim)); } break; case type_world_update_op_shape_params_asset: @@ -3699,8 +3709,8 @@ QueueWorldUpdateForRegion(engine_resources *Engine, world_update_op_mode Mode, w v3 MinSimP = GetSimSpaceP(World, ShapeAsset->Origin); v3 MaxSimP = MinSimP + ShapeAsset->Asset->Model.Dim; - MinPCoarse = SimSpaceToCanonical(World, MinSimP-1); - MaxPCoarse = SimSpaceToCanonical(World, MaxSimP+2); + MinPCoarse = SimSpaceToCanonical(World, MinSimP-MinPStroke); + MaxPCoarse = SimSpaceToCanonical(World, MaxSimP+MaxPStroke); } break; } @@ -3988,10 +3998,10 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ random_series Entropy = {43246}; // NOTE(Jesse): We can actually do the entire dim here, but it's probably // better (faster) to just do what we actually need to - /* MarkBoundaryVoxels_NoExteriorFaces( CopiedVoxels, QueryDim, {{1,1,1}}, QueryDim-1, &Entropy, GREY_5, GREY_7); */ + MarkBoundaryVoxels_NoExteriorFaces( CopiedVoxels, QueryDim, {{1,1,1}}, QueryDim-1, &Entropy, GREY_5, GREY_7); /* MarkBoundaryVoxels_NoExteriorFaces( CopiedVoxels, QueryDim, {}, QueryDim, &Entropy, GREY_5, GREY_7); */ /* MarkBoundaryVoxels_MakeExteriorFaces( CopiedVoxels, QueryDim, {{1,1,1}}, QueryDim-1); */ - MarkBoundaryVoxels_MakeExteriorFaces( CopiedVoxels, QueryDim, {}, QueryDim); + /* MarkBoundaryVoxels_MakeExteriorFaces( CopiedVoxels, QueryDim, {}, QueryDim); */ for (u32 ChunkIndex = 0; ChunkIndex < ChunkCount; ++ChunkIndex) From 6446f19afde494b884136bbf9b4fd4452bb0dfa1 Mon Sep 17 00:00:00 2001 From: a pirate Date: Fri, 27 Oct 2023 17:00:11 -0600 Subject: [PATCH 10/15] Update stdlib --- external/bonsai_stdlib | 2 +- generated/gen_constructor_vertex_material.h | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 generated/gen_constructor_vertex_material.h diff --git a/external/bonsai_stdlib b/external/bonsai_stdlib index 6dba74d04..36216eeac 160000 --- a/external/bonsai_stdlib +++ b/external/bonsai_stdlib @@ -1 +1 @@ -Subproject commit 6dba74d04629ebcc10490ccb708345203f3521f9 +Subproject commit 36216eeacf03a12eb5185887e67924a265cc23de diff --git a/generated/gen_constructor_vertex_material.h b/generated/gen_constructor_vertex_material.h new file mode 100644 index 000000000..09c16ce46 --- /dev/null +++ b/generated/gen_constructor_vertex_material.h @@ -0,0 +1,11 @@ +link_internal vertex_material +VertexMaterial( v3 Color , f32 Transparency , f32 Emission ) +{ + vertex_material Reuslt = { + .Color = Color, + .Transparency = Transparency, + /* .Emission = Emission */ + }; + return Reuslt; +} + From 54eebbf01f2db11ecedeb0590fb8cc027f65e146 Mon Sep 17 00:00:00 2001 From: a pirate Date: Sat, 28 Oct 2023 07:49:26 -0600 Subject: [PATCH 11/15] Improve world chunk debug UI slightly --- src/engine/debug.cpp | 54 ++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/src/engine/debug.cpp b/src/engine/debug.cpp index 214180874..770d050fa 100644 --- a/src/engine/debug.cpp +++ b/src/engine/debug.cpp @@ -122,40 +122,50 @@ DebugUi(engine_resources *Engine, cs Name, untextured_3d_geometry_buffer *Value) { UNPACK_ENGINE_RESOURCES(Engine); + /* Text(Ui, Name); */ PushNewRow(Ui); - Text(Ui, Name); - PushNewRow(Ui); + if (Value) { - PushColumn(Ui, CS("Verts : ")); - PushColumn(Ui, CS((u64)Value->Verts)); - PushNewRow(Ui); + cs ButtonName = FSz("%S : 0x%x", Name, Value); + if (ToggleButton(Ui, Name, ButtonName, umm(Name.Start) ^ umm(Value))) + { + /* PushForceUpdateBasis(Ui, V2(16, 0)); */ + PushNewRow(Ui); - PushColumn(Ui, CS("Mats : ")); - PushColumn(Ui, CS((u64)Value->Mat)); - PushNewRow(Ui); + PushTableStart(Ui, {}, {}, {}, &DefaultStyle, V4(32, 0, 0, 0)); + PushColumn(Ui, CSz("Verts : ")); + PushColumn(Ui, FSz("0x%x",(u64)Value->Verts)); + PushNewRow(Ui); - PushColumn(Ui, CS("Normals : ")); - PushColumn(Ui, CS((u64)Value->Normals)); - PushNewRow(Ui); + PushColumn(Ui, CSz("Normals : ")); + PushColumn(Ui, FSz("0x%x", (u64)Value->Normals)); + PushNewRow(Ui); - PushColumn(Ui, CS("End : ")); - PushColumn(Ui, CS(Value->End)); - PushNewRow(Ui); + PushColumn(Ui, CSz("Mats : ")); + PushColumn(Ui, FSz("0x%x", (u64)Value->Mat)); + PushNewRow(Ui); - PushColumn(Ui, CS("At : ")); - PushColumn(Ui, CS(Value->At)); - PushNewRow(Ui); + PushColumn(Ui, CSz("At : ")); + PushColumn(Ui, FSz("%u", Value->At)); + PushNewRow(Ui); - PushColumn(Ui, CS("Timestamp : ")); - PushColumn(Ui, CS(Value->Timestamp)); - PushNewRow(Ui); + PushColumn(Ui, CSz("End : ")); + PushColumn(Ui, FSz("%u", Value->End)); + PushNewRow(Ui); + + PushColumn(Ui, CSz("Timestamp : ")); + PushColumn(Ui, FSz("%u", Value->Timestamp)); + PushTableEnd(Ui); + /* PushForceUpdateBasis(Ui, V2(-16, 0)); */ + } } else { - Text(Ui, CSz(" 0")); - PushNewRow(Ui); + PushColumn(Ui, Name); + PushColumn(Ui, CSz(" : null")); } + PushNewRow(Ui); } From 53cda37cdee2501b2b723e0797c4b51ee0452eb2 Mon Sep 17 00:00:00 2001 From: a pirate Date: Sat, 28 Oct 2023 09:01:08 -0600 Subject: [PATCH 12/15] Implement updated windowing API --- src/engine/debug.cpp | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/engine/debug.cpp b/src/engine/debug.cpp index 770d050fa..ddf22d2ba 100644 --- a/src/engine/debug.cpp +++ b/src/engine/debug.cpp @@ -130,7 +130,6 @@ DebugUi(engine_resources *Engine, cs Name, untextured_3d_geometry_buffer *Value) cs ButtonName = FSz("%S : 0x%x", Name, Value); if (ToggleButton(Ui, Name, ButtonName, umm(Name.Start) ^ umm(Value))) { - /* PushForceUpdateBasis(Ui, V2(16, 0)); */ PushNewRow(Ui); PushTableStart(Ui, {}, {}, {}, &DefaultStyle, V4(32, 0, 0, 0)); @@ -367,8 +366,7 @@ DoEngineDebug(engine_resources *Engine) if (ToggledOn(&ButtonGroup, CSz("WorldChunks"))) { - v2 WindowDim = {{650.f, 950.f}}; - local_persist window_layout WorldChunkWindow = WindowLayout("World Chunks", AlignRightWindowBasis(*Ui->ScreenDim, WindowDim), WindowDim); + local_persist window_layout WorldChunkWindow = WindowLayout("World Chunks"); PushWindowStart(Ui, &WorldChunkWindow); if ( Clicked(&ButtonGroup, CSz("WorldChunks")) || @@ -403,7 +401,10 @@ DoEngineDebug(engine_resources *Engine) #if 1 if (ToggledOn(&ButtonGroup, CSz("Textures"))) { - local_persist window_layout TexturesWindow = WindowLayout("Textures", DefaultWindowBasis(*Ui->ScreenDim, V2(1700, 450))); + v2 TexDim = V2(400); + + auto Flags = window_layout_flags(WindowLayoutFlag_StartupAlign_Bottom|WindowLayoutFlag_StartupSize_InferWidth); + local_persist window_layout TexturesWindow = WindowLayout("Textures", {}, V2(0.f, TexDim.y + 30.f), Flags); PushWindowStart(Ui, &TexturesWindow); s32 xAdvance = 15; @@ -418,27 +419,27 @@ DoEngineDebug(engine_resources *Engine) PushNewRow(Ui); StartColumn(Ui); - PushTexturedQuad(Ui, Graphics->Transparency.AccumTex, V2(400), zDepth_Text); + PushTexturedQuad(Ui, Graphics->Transparency.AccumTex, TexDim, zDepth_Text); PushForceAdvance(Ui, V2(xAdvance, 0)); EndColumn(Ui); StartColumn(Ui); - PushTexturedQuad(Ui, Graphics->Transparency.RevealTex, V2(400), zDepth_Text); + PushTexturedQuad(Ui, Graphics->Transparency.RevealTex, TexDim, zDepth_Text); PushForceAdvance(Ui, V2(xAdvance, 0)); EndColumn(Ui); StartColumn(Ui); - PushTexturedQuad(Ui, Graphics->Transparency.Depth, V2(400), zDepth_Text, True); + PushTexturedQuad(Ui, Graphics->Transparency.Depth, TexDim, zDepth_Text, True); PushForceAdvance(Ui, V2(xAdvance, 0)); EndColumn(Ui); StartColumn(Ui); - PushTexturedQuad(Ui, Graphics->Lighting.LightingTex, V2(400), zDepth_Text); + PushTexturedQuad(Ui, Graphics->Lighting.LightingTex, TexDim, zDepth_Text); PushForceAdvance(Ui, V2(xAdvance, 0)); EndColumn(Ui); StartColumn(Ui); - PushTexturedQuad(Ui, Graphics->Lighting.BloomTex, V2(400), zDepth_Text); + PushTexturedQuad(Ui, Graphics->Lighting.BloomTex, TexDim, zDepth_Text); PushForceAdvance(Ui, V2(xAdvance, 0)); EndColumn(Ui); PushNewRow(Ui); @@ -450,22 +451,22 @@ DoEngineDebug(engine_resources *Engine) PushNewRow(Ui); StartColumn(Ui); - PushTexturedQuad(Ui, Graphics->gBuffer->Textures->Color, V2(400), zDepth_Text); + PushTexturedQuad(Ui, Graphics->gBuffer->Textures->Color, TexDim, zDepth_Text); PushForceAdvance(Ui, V2(xAdvance, 0)); EndColumn(Ui); StartColumn(Ui); - PushTexturedQuad(Ui, Graphics->gBuffer->Textures->Normal, V2(400), zDepth_Text); + PushTexturedQuad(Ui, Graphics->gBuffer->Textures->Normal, TexDim, zDepth_Text); PushForceAdvance(Ui, V2(xAdvance, 0)); EndColumn(Ui); StartColumn(Ui); - PushTexturedQuad(Ui, Graphics->gBuffer->Textures->Position, V2(400), zDepth_Text); + PushTexturedQuad(Ui, Graphics->gBuffer->Textures->Position, TexDim, zDepth_Text); PushForceAdvance(Ui, V2(xAdvance, 0)); EndColumn(Ui); StartColumn(Ui); - PushTexturedQuad(Ui, Graphics->gBuffer->Textures->Depth, V2(400), zDepth_Text, True); + PushTexturedQuad(Ui, Graphics->gBuffer->Textures->Depth, TexDim, zDepth_Text, True); PushForceAdvance(Ui, V2(xAdvance, 0)); EndColumn(Ui); PushNewRow(Ui); @@ -487,7 +488,7 @@ DoEngineDebug(engine_resources *Engine) if (ToggledOn(&ButtonGroup, CSz("RenderSettings"))) { v2 WindowDim = {{1200.f, 250.f}}; - local_persist window_layout RenderSettingsWindow = WindowLayout("Render Settings", DefaultWindowBasis(*Ui->ScreenDim, WindowDim), WindowDim); + local_persist window_layout RenderSettingsWindow = WindowLayout("Render Settings", WindowLayoutFlag_StartupAlign_Right); render_settings *Settings = &Graphics->Settings; PushWindowStart(Ui, &RenderSettingsWindow); @@ -517,7 +518,7 @@ DoEngineDebug(engine_resources *Engine) if (ToggledOn(&ButtonGroup, CSz("EngineDebug"))) { v2 WindowDim = {{1200.f, 250.f}}; - local_persist window_layout Window = WindowLayout("Engine Debug", DefaultWindowBasis(*Ui->ScreenDim, WindowDim), WindowDim); + local_persist window_layout Window = WindowLayout("Engine Debug", WindowLayoutFlag_StartupAlign_Right); render_settings *Settings = &Graphics->Settings; PushWindowStart(Ui, &Window); From 8a0627efedc4b85ed4430ee3f7d819682b52591c Mon Sep 17 00:00:00 2001 From: a pirate Date: Sat, 28 Oct 2023 09:08:40 -0600 Subject: [PATCH 13/15] Update stdlib --- external/bonsai_stdlib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/external/bonsai_stdlib b/external/bonsai_stdlib index 36216eeac..5d1532817 160000 --- a/external/bonsai_stdlib +++ b/external/bonsai_stdlib @@ -1 +1 @@ -Subproject commit 36216eeacf03a12eb5185887e67924a265cc23de +Subproject commit 5d1532817eb0d9d935943dce7c52f27f3a3dfa1e From 1dc06272c9375a9d1682b07a98f6bc46a3a38ed6 Mon Sep 17 00:00:00 2001 From: a pirate Date: Sat, 28 Oct 2023 13:44:27 -0600 Subject: [PATCH 14/15] Position to fix bug wrt. subtractive flood-fill removing solid voxels surrounded by transparent voxels --- examples/turn_based/game.cpp | 6 +- src/engine/debug.cpp | 5 +- src/engine/game_effects.cpp | 2 +- src/engine/loaders/vox.cpp | 25 +++- src/engine/render.cpp | 3 + src/engine/work_queue.h | 14 +- src/engine/world_chunk.cpp | 274 ++++++++++++++++++++++------------- src/engine/world_chunk.h | 1 + 8 files changed, 219 insertions(+), 111 deletions(-) diff --git a/examples/turn_based/game.cpp b/examples/turn_based/game.cpp index 59aaebe94..afce938bd 100644 --- a/examples/turn_based/game.cpp +++ b/examples/turn_based/game.cpp @@ -254,19 +254,19 @@ BONSAI_API_MAIN_THREAD_CALLBACK() if (Input->R.Clicked) { - DoSplotion(Resources, PickCP, 4.f, &Global_GameEntropy, GetTranArena()); + DoSplotion(Resources, PickCP, 5.f, &Global_GameEntropy, GetTranArena()); /* DoIceBlock(Resources, &Pick, PickCP, 4.f, GetTranArena()); */ } if (Input->T.Clicked) { - DoSplotion(Resources, PickCP, 6.f, &Global_GameEntropy, GetTranArena()); + DoSplotion(Resources, PickCP, 7.f, &Global_GameEntropy, GetTranArena()); /* DoIceBlock(Resources, &Pick, PickCP, 6.f, GetTranArena()); */ } if (Input->Y.Clicked) { - DoSplotion(Resources, PickCP, 8.f, &Global_GameEntropy,GetTranArena()); + DoSplotion(Resources, PickCP, 9.f, &Global_GameEntropy,GetTranArena()); /* DoIceBlock(Resources, &Pick, PickCP, 8.f, GetTranArena()); */ } diff --git a/src/engine/debug.cpp b/src/engine/debug.cpp index ddf22d2ba..cfb9e5f37 100644 --- a/src/engine/debug.cpp +++ b/src/engine/debug.cpp @@ -377,7 +377,10 @@ DoEngineDebug(engine_resources *Engine) if (Button(Ui, CSz("RebuildMesh"), (umm)"RebuildMesh"^(umm)"WorldChunks")) { - QueueChunkForMeshRebuild(&Plat->LowPriority, EngineDebug->PickedChunk); + world_chunk *PickedChunk = EngineDebug->PickedChunk; + MarkBoundaryVoxels_Debug(PickedChunk->Voxels, PickedChunk->Dim); + /* MarkBoundaryVoxels_NoExteriorFaces(PickedChunk->Voxels, PickedChunk->Dim, {}, PickedChunk->Dim); */ + QueueChunkForMeshRebuild(&Plat->LowPriority, PickedChunk); } PushNewRow(Ui); diff --git a/src/engine/game_effects.cpp b/src/engine/game_effects.cpp index 218e66161..39743901f 100644 --- a/src/engine/game_effects.cpp +++ b/src/engine/game_effects.cpp @@ -304,7 +304,7 @@ DoSplotion( engine_resources *Resources, canonical_position PickCP, f32 Radius, .world_update_op_shape_params_sphere.Radius = Radius, .world_update_op_shape_params_sphere.Location = PickCP, }; - QueueWorldUpdateForRegion(Resources, WorldUpdateOperationMode_Subtractive, &Shape, DARK_GREY, Resources->Memory); + QueueWorldUpdateForRegion(Resources, WorldUpdateOperationMode_Subtractive, WorldUpdateOperationModeModifier_Flood, &Shape, DARK_GREY, Resources->Memory); /* QueueWorldUpdateForRegion(Resources, Pick, MinPCoarse, MaxPCoarse, WorldUpdateOperation_Additive, WorldUpdateOperationShape_Sphere, DARK_GREY, Radius, Resources->Memory); */ v3 SplosionSimP = GetSimSpaceP(World, PickCP); diff --git a/src/engine/loaders/vox.cpp b/src/engine/loaders/vox.cpp index 8424dd71d..9230a31ca 100644 --- a/src/engine/loaders/vox.cpp +++ b/src/engine/loaders/vox.cpp @@ -183,6 +183,9 @@ enum vox_loader_clip_behavior VoxLoaderClipBehavior_NoClipping, }; + +global_variable random_series TMP = {54235432543}; + link_internal vox_data LoadVoxData(memory_arena *WorldStorage, heap_allocator *Heap, char const *filepath, vox_loader_clip_behavior ClipBehavior, v3i HalfApronMin = {}, v3i HalfApronMax = {}, v3i ModDim = {}) { @@ -325,8 +328,6 @@ LoadVoxData(memory_arena *WorldStorage, heap_allocator *Heap, char const *filepa Result.ChunkData = AllocateChunkData(WorldStorage, ModelDim); Result.ChunkData->Dim = ModelDim; - global_variable random_series TMP = {54235432543}; - for( s32 VoxelCacheIndex = 0; VoxelCacheIndex < ActualVoxelCount; ++VoxelCacheIndex) @@ -334,12 +335,23 @@ LoadVoxData(memory_arena *WorldStorage, heap_allocator *Heap, char const *filepa boundary_voxel *Voxel = &LocalVoxelCache[VoxelCacheIndex]; Voxel->Offset = Voxel->Offset - Min + HalfApronMin; s32 Index = GetIndex(Voxel->Offset, ModelDim); - Result.ChunkData->Voxels[Index] = Voxel->V; - /* if (RandomUnilateral(&TMP) > 0.5f) { Result.ChunkData->Voxels[Index].Transparency = 128; } */ - Result.ChunkData->Voxels[Index].Transparency = 128; + if (Voxel->V.Flags & Voxel_Filled) + /* if (Voxel->V.Flags & Voxel_Filled && VoxelCacheIndex == 16) */ + { + if (RandomUnilateral(&TMP) > 0.25f) { Voxel->V.Transparency = 255; } + /* Result.ChunkData->Voxels[Index].Transparency = 128; */ + /* Voxel->V.Transparency = 255; */ + } + else + { + /* Voxel->V.Transparency = 0; */ + Assert(Voxel->V.Transparency == 0); + } + /* Voxel->V.Transparency = 0; */ - Result.ChunkData->VoxelLighting[Index] = VoxelLighting(0xff); + Result.ChunkData->Voxels[Index] = Voxel->V; + /* Result.ChunkData->VoxelLighting[Index] = VoxelLighting(0xff); */ } FullBarrier; @@ -395,6 +407,7 @@ LoadVoxData(memory_arena *WorldStorage, heap_allocator *Heap, char const *filepa if (Error == False) { Assert(bytesRemaining == 0); + /* MarkBoundaryVoxels_NoExteriorFaces( Result.ChunkData->Voxels, Result.ChunkData->Dim, {}, Result.ChunkData->Dim); */ MarkBoundaryVoxels_MakeExteriorFaces( Result.ChunkData->Voxels, Result.ChunkData->Dim, {}, Result.ChunkData->Dim); } else diff --git a/src/engine/render.cpp b/src/engine/render.cpp index 7768fcbaa..eb13d6c9a 100644 --- a/src/engine/render.cpp +++ b/src/engine/render.cpp @@ -123,7 +123,10 @@ RenderGBuffer(gpu_mapped_element_buffer *GpuMap, graphics *Graphics) BindShaderUniforms(&GBufferRenderGroup->gBufferShader); FlushBuffersToCard(GpuMap); + + GL.Disable(GL_CULL_FACE); Draw(GpuMap->Buffer.At); + GL.Enable(GL_CULL_FACE); AssertNoGlErrors; diff --git a/src/engine/work_queue.h b/src/engine/work_queue.h index 4911765ef..c377f63ca 100644 --- a/src/engine/work_queue.h +++ b/src/engine/work_queue.h @@ -103,8 +103,8 @@ struct world_update_op_shape world_update_op_shape_type Type; union { world_update_op_shape_params_sphere world_update_op_shape_params_sphere; - world_update_op_shape_params_rect world_update_op_shape_params_rect; - world_update_op_shape_params_asset world_update_op_shape_params_asset; + world_update_op_shape_params_rect world_update_op_shape_params_rect; + world_update_op_shape_params_asset world_update_op_shape_params_asset; }; }; @@ -115,10 +115,18 @@ enum world_update_op_mode WorldUpdateOperationMode_Subtractive, }; +enum world_update_op_mode_modifier +{ + WorldUpdateOperationModeModifier_None, + WorldUpdateOperationModeModifier_Flood, +}; + struct work_queue_entry_update_world_region { - world_update_op_mode Mode; + world_update_op_mode Mode; + world_update_op_mode_modifier Modifier; + world_update_op_shape Shape; u8 ColorIndex; diff --git a/src/engine/world_chunk.cpp b/src/engine/world_chunk.cpp index ca050fd37..005a8cb41 100644 --- a/src/engine/world_chunk.cpp +++ b/src/engine/world_chunk.cpp @@ -737,43 +737,67 @@ typedef u32 (*chunk_init_callback)( perlin_noise *Noise, world_chunk *Chunk, chu link_internal b32 -IsDifferentTransparency(voxel *Voxels, s32 SrcIndex, v3i DestP, v3i SrcChunkDim) +TransparencyIncreases(voxel *Voxels, s32 SrcIndex, v3i DestP, v3i SrcChunkDim) { s32 DestIndex = GetIndex(DestP, SrcChunkDim); voxel *SrcVox = Voxels+SrcIndex; voxel *DstVox = Voxels+DestIndex; -#if 0 - b32 Result = True; -#else + Assert(SrcVox->Flags & Voxel_Filled); + b32 Result = False; if (SrcVox->Transparency) { - if (DstVox->Transparency) - { - Result = False; - } - else + // Transparent source voxels can only increase in transparency if the dest is unfilled + if ( (DstVox->Flags&Voxel_Filled) == False) { - Result = False; + Assert(DstVox->Transparency == 0); + Result = True; } } else { - if (DstVox->Transparency) + // Opaque source voxels can increase in transparency if the dest is unfilled or filled and transparent + if ( (DstVox->Flags&Voxel_Filled) == False) { + Assert(DstVox->Transparency == 0); Result = True; } - else + + if ( (DstVox->Flags&Voxel_Filled) && DstVox->Transparency) { - Result = False; + Result = True; } + } -#endif return Result; } +link_internal void +MarkBoundaryVoxels_Debug( voxel *Voxels, chunk_dimension SrcChunkDim) +{ + RangeIterator(z, SrcChunkDim.z) + RangeIterator(y, SrcChunkDim.y) + RangeIterator(x, SrcChunkDim.x) + { + voxel_position DestP = Voxel_Position(x,y,z); + s32 SrcIndex = GetIndex(DestP, SrcChunkDim); + + voxel *Voxel = Voxels + SrcIndex; + + if (IsFilled(Voxel)) + { + Voxel->Flags |= Voxel_RightFace; + Voxel->Flags |= Voxel_LeftFace; + Voxel->Flags |= Voxel_BottomFace; + Voxel->Flags |= Voxel_TopFace; + Voxel->Flags |= Voxel_FrontFace; + Voxel->Flags |= Voxel_BackFace; + } + } +} + link_internal void MarkBoundaryVoxels_MakeExteriorFaces( voxel *Voxels, chunk_dimension SrcChunkDim, @@ -782,12 +806,8 @@ MarkBoundaryVoxels_MakeExteriorFaces( voxel *Voxels, { TIMED_FUNCTION(); - v3 Diameter = V3(1.0f); - v3 VertexData[VERTS_PER_FACE]; - v4 FaceColors[VERTS_PER_FACE]; - auto MinDim = SrcChunkMin; - auto MaxDim = Min(SrcChunkDim, SrcChunkMax); // SrcChunkMin+DestChunkDim+1 + auto MaxDim = Min(SrcChunkDim, SrcChunkMax); for ( s32 z = MinDim.z; z < MaxDim.z ; ++z ) { for ( s32 y = MinDim.y; y < MaxDim.y ; ++y ) @@ -801,7 +821,7 @@ MarkBoundaryVoxels_MakeExteriorFaces( voxel *Voxels, if (IsFilled(Voxel)) { - Voxel->Flags = Voxel_Filled; + /* Voxel->Flags = Voxel_Filled; */ voxel_position rightVoxel = DestP + Voxel_Position(1, 0, 0); voxel_position leftVoxel = DestP - Voxel_Position(1, 0, 0); @@ -811,27 +831,27 @@ MarkBoundaryVoxels_MakeExteriorFaces( voxel *Voxels, voxel_position backVoxel = DestP - Voxel_Position(0, 1, 0); - if ( !IsInsideDim( SrcChunkDim, rightVoxel) || NotFilled( Voxels, rightVoxel, SrcChunkDim) || IsDifferentTransparency( Voxels, SrcIndex, rightVoxel, SrcChunkDim) ) + if ( !IsInsideDim( SrcChunkDim, rightVoxel) || NotFilled( Voxels, rightVoxel, SrcChunkDim) || TransparencyIncreases( Voxels, SrcIndex, rightVoxel, SrcChunkDim) ) { Voxel->Flags |= Voxel_RightFace; } - if ( !IsInsideDim( SrcChunkDim, leftVoxel) || NotFilled( Voxels, leftVoxel, SrcChunkDim) || IsDifferentTransparency( Voxels, SrcIndex, leftVoxel, SrcChunkDim) ) + if ( !IsInsideDim( SrcChunkDim, leftVoxel) || NotFilled( Voxels, leftVoxel, SrcChunkDim) || TransparencyIncreases( Voxels, SrcIndex, leftVoxel, SrcChunkDim) ) { Voxel->Flags |= Voxel_LeftFace; } - if ( !IsInsideDim( SrcChunkDim, botVoxel) || NotFilled( Voxels, botVoxel, SrcChunkDim) || IsDifferentTransparency( Voxels, SrcIndex, botVoxel, SrcChunkDim) ) + if ( !IsInsideDim( SrcChunkDim, botVoxel) || NotFilled( Voxels, botVoxel, SrcChunkDim) || TransparencyIncreases( Voxels, SrcIndex, botVoxel, SrcChunkDim) ) { Voxel->Flags |= Voxel_BottomFace; } - if ( !IsInsideDim( SrcChunkDim, topVoxel) || NotFilled( Voxels, topVoxel, SrcChunkDim) || IsDifferentTransparency( Voxels, SrcIndex, topVoxel, SrcChunkDim) ) + if ( !IsInsideDim( SrcChunkDim, topVoxel) || NotFilled( Voxels, topVoxel, SrcChunkDim) || TransparencyIncreases( Voxels, SrcIndex, topVoxel, SrcChunkDim) ) { Voxel->Flags |= Voxel_TopFace; } - if ( !IsInsideDim( SrcChunkDim, frontVoxel) || NotFilled( Voxels, frontVoxel, SrcChunkDim) || IsDifferentTransparency( Voxels, SrcIndex, frontVoxel, SrcChunkDim) ) + if ( !IsInsideDim( SrcChunkDim, frontVoxel) || NotFilled( Voxels, frontVoxel, SrcChunkDim) || TransparencyIncreases( Voxels, SrcIndex, frontVoxel, SrcChunkDim) ) { Voxel->Flags |= Voxel_FrontFace; } - if ( !IsInsideDim( SrcChunkDim, backVoxel) || NotFilled( Voxels, backVoxel, SrcChunkDim) || IsDifferentTransparency( Voxels, SrcIndex, backVoxel, SrcChunkDim) ) + if ( !IsInsideDim( SrcChunkDim, backVoxel) || NotFilled( Voxels, backVoxel, SrcChunkDim) || TransparencyIncreases( Voxels, SrcIndex, backVoxel, SrcChunkDim) ) { Voxel->Flags |= Voxel_BackFace; } @@ -852,10 +872,6 @@ MarkBoundaryVoxels_NoExteriorFaces( voxel *Voxels, { TIMED_FUNCTION(); - v3 Diameter = V3(1.0f); - v3 VertexData[VERTS_PER_FACE]; - v4 FaceColors[VERTS_PER_FACE]; - auto MinDim = SrcChunkMin; auto MaxDim = Min(SrcChunkDim, SrcChunkMax); // SrcChunkMin+DestChunkDim+1 for ( s32 z = MinDim.z; z < MaxDim.z ; ++z ) @@ -864,8 +880,8 @@ MarkBoundaryVoxels_NoExteriorFaces( voxel *Voxels, { for ( s32 x = MinDim.x; x < MaxDim.x ; ++x ) { - voxel_position DestP = Voxel_Position(x,y,z); - s32 SrcIndex = GetIndex(DestP, SrcChunkDim); + voxel_position SrcP = Voxel_Position(x,y,z); + s32 SrcIndex = GetIndex(SrcP, SrcChunkDim); voxel *Voxel = Voxels + SrcIndex; @@ -875,35 +891,35 @@ MarkBoundaryVoxels_NoExteriorFaces( voxel *Voxels, { Voxel->Flags = Voxel_Filled; - voxel_position rightVoxel = DestP + Voxel_Position(1, 0, 0); - voxel_position leftVoxel = DestP - Voxel_Position(1, 0, 0); - voxel_position topVoxel = DestP + Voxel_Position(0, 0, 1); - voxel_position botVoxel = DestP - Voxel_Position(0, 0, 1); - voxel_position frontVoxel = DestP + Voxel_Position(0, 1, 0); - voxel_position backVoxel = DestP - Voxel_Position(0, 1, 0); + voxel_position rightVoxel = SrcP + Voxel_Position(1, 0, 0); + voxel_position leftVoxel = SrcP - Voxel_Position(1, 0, 0); + voxel_position topVoxel = SrcP + Voxel_Position(0, 0, 1); + voxel_position botVoxel = SrcP - Voxel_Position(0, 0, 1); + voxel_position frontVoxel = SrcP + Voxel_Position(0, 1, 0); + voxel_position backVoxel = SrcP - Voxel_Position(0, 1, 0); - if ( IsInsideDim( SrcChunkDim, rightVoxel) && (NotFilled( Voxels, rightVoxel, SrcChunkDim) || IsDifferentTransparency( Voxels, SrcIndex, rightVoxel, SrcChunkDim)) ) + if ( IsInsideDim( SrcChunkDim, rightVoxel) && (NotFilled( Voxels, rightVoxel, SrcChunkDim) || TransparencyIncreases( Voxels, SrcIndex, rightVoxel, SrcChunkDim)) ) { Voxel->Flags |= Voxel_RightFace; } - if ( IsInsideDim( SrcChunkDim, leftVoxel) && (NotFilled( Voxels, leftVoxel, SrcChunkDim) || IsDifferentTransparency( Voxels, SrcIndex, leftVoxel, SrcChunkDim)) ) + if ( IsInsideDim( SrcChunkDim, leftVoxel) && (NotFilled( Voxels, leftVoxel, SrcChunkDim) || TransparencyIncreases( Voxels, SrcIndex, leftVoxel, SrcChunkDim)) ) { Voxel->Flags |= Voxel_LeftFace; } - if ( IsInsideDim( SrcChunkDim, botVoxel) && (NotFilled( Voxels, botVoxel, SrcChunkDim) || IsDifferentTransparency( Voxels, SrcIndex, botVoxel, SrcChunkDim)) ) + if ( IsInsideDim( SrcChunkDim, botVoxel) && (NotFilled( Voxels, botVoxel, SrcChunkDim) || TransparencyIncreases( Voxels, SrcIndex, botVoxel, SrcChunkDim)) ) { Voxel->Flags |= Voxel_BottomFace; } - if ( IsInsideDim( SrcChunkDim, topVoxel) && (NotFilled( Voxels, topVoxel, SrcChunkDim) || IsDifferentTransparency( Voxels, SrcIndex, topVoxel, SrcChunkDim)) ) + if ( IsInsideDim( SrcChunkDim, topVoxel) && (NotFilled( Voxels, topVoxel, SrcChunkDim) || TransparencyIncreases( Voxels, SrcIndex, topVoxel, SrcChunkDim)) ) { Voxel->Flags |= Voxel_TopFace; } - if ( IsInsideDim( SrcChunkDim, frontVoxel) && (NotFilled( Voxels, frontVoxel, SrcChunkDim) || IsDifferentTransparency( Voxels, SrcIndex, frontVoxel, SrcChunkDim)) ) + if ( IsInsideDim( SrcChunkDim, frontVoxel) && (NotFilled( Voxels, frontVoxel, SrcChunkDim) || TransparencyIncreases( Voxels, SrcIndex, frontVoxel, SrcChunkDim)) ) { Voxel->Flags |= Voxel_FrontFace; } - if ( IsInsideDim( SrcChunkDim, backVoxel) && (NotFilled( Voxels, backVoxel, SrcChunkDim) || IsDifferentTransparency( Voxels, SrcIndex, backVoxel, SrcChunkDim)) ) + if ( IsInsideDim( SrcChunkDim, backVoxel) && (NotFilled( Voxels, backVoxel, SrcChunkDim) || TransparencyIncreases( Voxels, SrcIndex, backVoxel, SrcChunkDim)) ) { Voxel->Flags |= Voxel_BackFace; } @@ -959,8 +975,6 @@ DrawDebugVoxels( voxel *Voxels, s32 frontVoxelReadIndex; s32 backVoxelReadIndex; - /* random_series ColorEntropy = {33453}; */ - v3 Diameter = V3(1.0f); v3 VertexData[VERTS_PER_FACE]; @@ -974,8 +988,8 @@ DrawDebugVoxels( voxel *Voxels, { for ( s32 x = MinDim.x; x < MaxDim.x ; ++x ) { - voxel_position DestP = Voxel_Position(x,y,z); - s32 Index = GetIndex(DestP, SrcChunkDim); + voxel_position SrcP = Voxel_Position(x,y,z); + s32 Index = GetIndex(SrcP, SrcChunkDim); voxel *Voxel = Voxels + Index; // TODO(Jesse): This copy could be avoided in multiple ways, and should be. @@ -984,32 +998,32 @@ DrawDebugVoxels( voxel *Voxels, if (Voxel->Flags & Voxel_RightFace) { - RightFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); + RightFaceVertexData( V3(SrcP-SrcChunkMin), Diameter, VertexData); BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, Materials); } if (Voxel->Flags & Voxel_LeftFace) { - LeftFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); + LeftFaceVertexData( V3(SrcP-SrcChunkMin), Diameter, VertexData); BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BottomFace) { - BottomFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); + BottomFaceVertexData( V3(SrcP-SrcChunkMin), Diameter, VertexData); BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, Materials); } if (Voxel->Flags & Voxel_TopFace) { - TopFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); + TopFaceVertexData( V3(SrcP-SrcChunkMin), Diameter, VertexData); BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, Materials); } if (Voxel->Flags & Voxel_FrontFace) { - FrontFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); + FrontFaceVertexData( V3(SrcP-SrcChunkMin), Diameter, VertexData); BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BackFace) { - BackFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); + BackFaceVertexData( V3(SrcP-SrcChunkMin), Diameter, VertexData); BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, Materials); } } @@ -1020,7 +1034,36 @@ DrawDebugVoxels( voxel *Voxels, } link_internal b32 -Step(voxel *Voxels, v3i SrcDim, v3i StepDir, v3i StepShape, v3i *AtP, voxel_flag FaceFlag, u8 ColorIndex ) +TransparencyIsSimilar(u8 T0, u8 T1) +{ + b32 Result = False; + if (T0) + { + if (T1) + { + Result = True; + } + else + { + Result = False; + } + } + else + { + if (T1) + { + Result = False; + } + else + { + Result = True; + } + } + return Result; +} + +link_internal b32 +Step(voxel *Voxels, v3i SrcDim, v3i StepDir, v3i StepShape, v3i *AtP, voxel_flag FaceFlag, u8 ColorIndex, u8 Transparency) { b32 Result = True; for ( s32 z = 0; z <= StepShape.z; ++z ) @@ -1034,7 +1077,7 @@ Step(voxel *Voxels, v3i SrcDim, v3i StepDir, v3i StepShape, v3i *AtP, voxel_flag { s32 VoxI = GetIndex(Next, SrcDim); voxel *V = Voxels + VoxI; - if ( (V->Flags&FaceFlag) && V->Color == ColorIndex) + if ( (V->Flags&FaceFlag) && V->Color == ColorIndex && TransparencyIsSimilar(V->Transparency, Transparency)) { /* UnSetFlag((voxel_flag*)&V->Flags, FaceFlag); */ } @@ -1063,18 +1106,18 @@ Step(voxel *Voxels, v3i SrcDim, v3i StepDir, v3i StepShape, v3i *AtP, voxel_flag global_variable random_series ColorEntropy = {33453}; link_internal v3 -DoXStepping(voxel *Voxels, v3i SrcChunkDim, v3i SrcP, voxel_flag Face, u8 Color) +DoXStepping(voxel *Voxels, v3i SrcChunkDim, v3i SrcP, voxel_flag Face, u8 Color, u8 Transparency) { v3i AtY = SrcP; s32 DidStepY = 0; - while (Step(Voxels, SrcChunkDim, {{0, 1, 0}}, {{0, 1, 0}}, &AtY, Face, Color )) + while (Step(Voxels, SrcChunkDim, {{0, 1, 0}}, {{0, 1, 0}}, &AtY, Face, Color, Transparency )) { DidStepY++; } s32 DidStepZ = 0; v3i AtZ = SrcP; - while (Step(Voxels, SrcChunkDim, {{0, 0, 1}}, {{0, DidStepY, 1}}, &AtZ, Face, Color )) + while (Step(Voxels, SrcChunkDim, {{0, 0, 1}}, {{0, DidStepY, 1}}, &AtZ, Face, Color, Transparency )) { DidStepZ++; } @@ -1095,18 +1138,18 @@ DoXStepping(voxel *Voxels, v3i SrcChunkDim, v3i SrcP, voxel_flag Face, u8 Color) } link_internal v3 -DoYStepping(voxel *Voxels, v3i SrcChunkDim, v3i SrcP, voxel_flag Face, u8 Color) +DoYStepping(voxel *Voxels, v3i SrcChunkDim, v3i SrcP, voxel_flag Face, u8 Color, u8 Transparency) { v3i AtX = SrcP; s32 DidStepX = 0; - while (Step(Voxels, SrcChunkDim, {{1, 0, 0}}, {{1, 0, 0}}, &AtX, Face, Color )) + while (Step(Voxels, SrcChunkDim, {{1, 0, 0}}, {{1, 0, 0}}, &AtX, Face, Color, Transparency)) { DidStepX++; } s32 DidStepZ = 0; v3i AtZ = SrcP; - while (Step(Voxels, SrcChunkDim, {{0, 0, 1}}, {{DidStepX, 0, 1}}, &AtZ, Face, Color )) + while (Step(Voxels, SrcChunkDim, {{0, 0, 1}}, {{DidStepX, 0, 1}}, &AtZ, Face, Color, Transparency )) { DidStepZ++; } @@ -1127,18 +1170,18 @@ DoYStepping(voxel *Voxels, v3i SrcChunkDim, v3i SrcP, voxel_flag Face, u8 Color) } link_internal v3 -DoZStepping(voxel *Voxels, v3i SrcChunkDim, v3i SrcP, voxel_flag Face, u8 Color) +DoZStepping(voxel *Voxels, v3i SrcChunkDim, v3i SrcP, voxel_flag Face, u8 Color, u8 Transparency) { v3i AtX = SrcP; s32 DidStepX = 0; - while (Step(Voxels, SrcChunkDim, {{1, 0, 0}}, {{1, 0, 0}}, &AtX, Face, Color )) + while (Step(Voxels, SrcChunkDim, {{1, 0, 0}}, {{1, 0, 0}}, &AtX, Face, Color, Transparency )) { DidStepX++; } s32 DidStepY = 0; v3i AtY = SrcP; - while (Step(Voxels, SrcChunkDim, {{0, 1, 0}}, {{DidStepX, 1, 0}}, &AtY, Face, Color )) + while (Step(Voxels, SrcChunkDim, {{0, 1, 0}}, {{DidStepX, 1, 0}}, &AtY, Face, Color, Transparency )) { DidStepY++; } @@ -1342,38 +1385,38 @@ BuildWorldChunkMeshFromMarkedVoxels_Greedy( voxel *Voxels, if (Voxel->Flags & Voxel_RightFace) { - v3 Dim = DoXStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_RightFace, Voxel->Color); + v3 Dim = DoXStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_RightFace, Voxel->Color, Voxel->Transparency); RightFaceVertexData( V3(TmpVoxP), Dim, VertexData); BufferVertsDirect(Dest, 6, VertexData, RightFaceNormalData, Materials); } if (Voxel->Flags & Voxel_LeftFace) { - v3 Dim = DoXStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_LeftFace, Voxel->Color); + v3 Dim = DoXStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_LeftFace, Voxel->Color, Voxel->Transparency); LeftFaceVertexData( V3(TmpVoxP), Dim, VertexData); BufferVertsDirect(Dest, 6, VertexData, LeftFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BottomFace) { - v3 Dim = DoZStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_BottomFace, Voxel->Color); + v3 Dim = DoZStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_BottomFace, Voxel->Color, Voxel->Transparency); BottomFaceVertexData( V3(TmpVoxP), Dim, VertexData); BufferVertsDirect(Dest, 6, VertexData, BottomFaceNormalData, Materials); } if (Voxel->Flags & Voxel_TopFace) { - v3 Dim = DoZStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_TopFace, Voxel->Color); + v3 Dim = DoZStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_TopFace, Voxel->Color, Voxel->Transparency); TopFaceVertexData( V3(TmpVoxP), Dim, VertexData); BufferVertsDirect(Dest, 6, VertexData, TopFaceNormalData, Materials); } if (Voxel->Flags & Voxel_FrontFace) { - v3 Dim = DoYStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_FrontFace, Voxel->Color); + v3 Dim = DoYStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_FrontFace, Voxel->Color, Voxel->Transparency); FrontFaceVertexData( V3(TmpVoxP), Dim, VertexData); BufferVertsDirect(Dest, 6, VertexData, FrontFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BackFace) { - v3 Dim = DoYStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_BackFace, Voxel->Color); + v3 Dim = DoYStepping(TempVoxels, TmpDim, TmpVoxP, Voxel_BackFace, Voxel->Color, Voxel->Transparency); BackFaceVertexData( V3(TmpVoxP), Dim, VertexData); BufferVertsDirect(Dest, 6, VertexData, BackFaceNormalData, Materials); } @@ -1569,38 +1612,38 @@ BuildMipMesh( voxel *Voxels, if (Voxel->Flags & Voxel_RightFace) { - v3 Dim = DoXStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_RightFace, Voxel->Color); + v3 Dim = DoXStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_RightFace, Voxel->Color, Voxel->Transparency); RightFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, Materials); } if (Voxel->Flags & Voxel_LeftFace) { - v3 Dim = DoXStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_LeftFace, Voxel->Color); + v3 Dim = DoXStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_LeftFace, Voxel->Color, Voxel->Transparency); LeftFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BottomFace) { - v3 Dim = DoZStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_BottomFace, Voxel->Color); + v3 Dim = DoZStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_BottomFace, Voxel->Color, Voxel->Transparency); BottomFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, Materials); } if (Voxel->Flags & Voxel_TopFace) { - v3 Dim = DoZStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_TopFace, Voxel->Color); + v3 Dim = DoZStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_TopFace, Voxel->Color, Voxel->Transparency); TopFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, Materials); } if (Voxel->Flags & Voxel_FrontFace) { - v3 Dim = DoYStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_FrontFace, Voxel->Color); + v3 Dim = DoYStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_FrontFace, Voxel->Color, Voxel->Transparency); FrontFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BackFace) { - v3 Dim = DoYStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_BackFace, Voxel->Color); + v3 Dim = DoYStepping(FilterVoxels, FilterDim, TmpVoxP, Voxel_BackFace, Voxel->Color, Voxel->Transparency); BackFaceVertexData( V3(ActualP)*MipLevel, Dim*MipLevel, VertexData); BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, Materials); } @@ -1619,6 +1662,7 @@ BuildWorldChunkMeshFromMarkedVoxels_Naieve( voxel *Voxels, chunk_dimension SrcChunkMax, untextured_3d_geometry_buffer *DestGeometry, + untextured_3d_geometry_buffer *DestTransparentGeometry, v3 *ColorPallette = DefaultPalette ) { TIMED_FUNCTION(); @@ -1671,35 +1715,38 @@ BuildWorldChunkMeshFromMarkedVoxels_Naieve( voxel *Voxels, f32 Trans = (f32)Voxel->Transparency / 255.f; FillArray(VertexMaterial(Color, Trans, 0.f), Materials, VERTS_PER_FACE); + untextured_3d_geometry_buffer *Dest = {}; + if (Voxel->Transparency) { Dest = DestTransparentGeometry; } else { Dest = DestGeometry; } + if (Voxel->Flags & Voxel_RightFace) { RightFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, RightFaceNormalData, Materials); + BufferVertsDirect(Dest, 6, VertexData, RightFaceNormalData, Materials); } if (Voxel->Flags & Voxel_LeftFace) { LeftFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, LeftFaceNormalData, Materials); + BufferVertsDirect(Dest, 6, VertexData, LeftFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BottomFace) { BottomFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BottomFaceNormalData, Materials); + BufferVertsDirect(Dest, 6, VertexData, BottomFaceNormalData, Materials); } if (Voxel->Flags & Voxel_TopFace) { TopFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, TopFaceNormalData, Materials); + BufferVertsDirect(Dest, 6, VertexData, TopFaceNormalData, Materials); } if (Voxel->Flags & Voxel_FrontFace) { FrontFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, FrontFaceNormalData, Materials); + BufferVertsDirect(Dest, 6, VertexData, FrontFaceNormalData, Materials); } if (Voxel->Flags & Voxel_BackFace) { BackFaceVertexData( V3(DestP-SrcChunkMin), Diameter, VertexData); - BufferVertsDirect(DestGeometry, 6, VertexData, BackFaceNormalData, Materials); + BufferVertsDirect(Dest, 6, VertexData, BackFaceNormalData, Materials); } } } @@ -3314,6 +3361,7 @@ RebuildWorldChunkMesh(thread_local_state *Thread, world_chunk *Chunk) untextured_3d_geometry_buffer *TempMesh = AllocateTempWorldChunkMesh(Thread->TempMemory); untextured_3d_geometry_buffer *TempTransparentMesh = AllocateTempWorldChunkMesh(Thread->TempMemory); BuildWorldChunkMeshFromMarkedVoxels_Greedy( Chunk->Voxels, Chunk->Dim, {}, Chunk->Dim, TempMesh, TempTransparentMesh, GetTranArena() ); + /* BuildWorldChunkMeshFromMarkedVoxels_Naieve( Chunk->Voxels, Chunk->Dim, {}, Chunk->Dim, TempMesh, TempTransparentMesh); */ if (TempMesh->At) { @@ -3353,11 +3401,12 @@ WorkQueueEntryRebuildMesh(world_chunk *Chunk) } link_internal work_queue_entry_update_world_region -WorkQueueEntryUpdateWorldRegion(world_update_op_mode Mode, world_update_op_shape *Shape, u8 ColorIndex, canonical_position MinP, canonical_position MaxP, world_chunk** ChunkBuffer, u32 ChunkCount) +WorkQueueEntryUpdateWorldRegion(world_update_op_mode Mode, world_update_op_mode_modifier Modifier, world_update_op_shape *Shape, u8 ColorIndex, canonical_position MinP, canonical_position MaxP, world_chunk** ChunkBuffer, u32 ChunkCount) { work_queue_entry_update_world_region Result = { .Mode = Mode, + .Modifier = Modifier, .Shape = *Shape, .ColorIndex = ColorIndex, .ChunkBuffer = ChunkBuffer, @@ -3659,7 +3708,7 @@ BlitAssetIntoWorld(engine_resources *Engine, asset *Asset, cp Origin) } link_internal void -QueueWorldUpdateForRegion(engine_resources *Engine, world_update_op_mode Mode, world_update_op_shape *Shape, u8 ColorIndex, memory_arena *Memory) +QueueWorldUpdateForRegion(engine_resources *Engine, world_update_op_mode Mode, world_update_op_mode_modifier Modifier, world_update_op_shape *Shape, u8 ColorIndex, memory_arena *Memory) { TIMED_FUNCTION(); @@ -3769,12 +3818,18 @@ QueueWorldUpdateForRegion(engine_resources *Engine, world_update_op_mode Mode, w { work_queue_entry Entry = { .Type = type_work_queue_entry_update_world_region, - .work_queue_entry_update_world_region = WorkQueueEntryUpdateWorldRegion(Mode, Shape, ColorIndex, MinP, MaxP, Buffer, ChunkIndex), + .work_queue_entry_update_world_region = WorkQueueEntryUpdateWorldRegion(Mode, Modifier, Shape, ColorIndex, MinP, MaxP, Buffer, ChunkIndex), }; PushWorkQueueEntry(&Plat->LowPriority, &Entry); } } +link_internal void +QueueWorldUpdateForRegion(engine_resources *Engine, world_update_op_mode Mode, world_update_op_shape *Shape, u8 ColorIndex, memory_arena *Memory) +{ + QueueWorldUpdateForRegion(Engine, Mode, WorldUpdateOperationModeModifier_None, Shape, ColorIndex, Memory); +} + link_internal u32 MapIntoQueryBox(v3i SimSpaceVoxP, v3i SimSpaceQueryMinP, voxel_position SimSpaceQueryDim) { @@ -3796,7 +3851,9 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ { TIMED_FUNCTION(); - world_update_op_mode Mode = Job->Mode; + world_update_op_mode Mode = Job->Mode; + world_update_op_mode_modifier Modifier = Job->Modifier; + world_update_op_shape Shape = Job->Shape; u8 NewColor = Job->ColorIndex; canonical_position MaxP = Job->MaxP; @@ -3871,23 +3928,35 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ canonical_position P = Sphere->Location; auto LocationSimSpace = GetSimSpaceP(World, P); - // TODO(Jesse): This routine maybe should be split into multiple - // routines that have this logic welded into them ..? if (LengthSq(SimSpaceVoxPExact - LocationSimSpace) < Square(Sphere->Radius)) { - /* CopyValue.Flags &= Voxel_MarkBit; */ switch(Mode) { InvalidCase(WorldUpdateOperationMode_None); case WorldUpdateOperationMode_Subtractive: { - if (CopyValue.Flags&VoxelFaceMask) + switch(Modifier) { - --Chunk->FilledCount; - CopyValue.Flags = Voxel_Empty; + case WorldUpdateOperationModeModifier_None: + { + if (CopyValue.Flags&Voxel_Filled) { --Chunk->FilledCount; CopyValue.Flags = Voxel_Empty; } + } break; + + case WorldUpdateOperationModeModifier_Flood: + { + if (LengthSq(SimSpaceVoxPExact - LocationSimSpace) < Square(Sphere->Radius-1.f)) + { + if (CopyValue.Flags&VoxelFaceMask) { --Chunk->FilledCount; CopyValue.Flags = Voxel_Empty; } + } + else + { + if (CopyValue.Flags&VoxelFaceMask) { CopyValue.Color = NewColor; } + } + CopyValue.Flags |= Voxel_MarkBit; + } break; } - CopyValue.Flags |= Voxel_MarkBit; + } break; case WorldUpdateOperationMode_Additive: @@ -3899,7 +3968,8 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ } } - else if (LengthSq(SimSpaceVoxPExact - LocationSimSpace) < Square(Sphere->Radius+1.f)) +#if 0 + /* else if (LengthSq(SimSpaceVoxPExact - LocationSimSpace) < Square(Sphere->Radius+1.f)) */ { switch(Mode) { @@ -3922,10 +3992,14 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ } break; } } +#endif } break; case type_world_update_op_shape_params_rect: { + // Not implemented + Assert(Modifier == WorldUpdateOperationModeModifier_None); + world_update_op_shape_params_rect *Rect = SafeCast(world_update_op_shape_params_rect, &Shape); // NOTE(Jesse): These _should_ already be min/maxed, so we can change these to asserts. @@ -3963,6 +4037,9 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ case type_world_update_op_shape_params_asset: { + // Not implemented + Assert(Modifier == WorldUpdateOperationModeModifier_None); + world_update_op_shape_params_asset *AssetJob = SafeCast(world_update_op_shape_params_asset, &Shape); asset *Asset = AssetJob->Asset; /* BlitAssetIntoWorld(Engine, Asset, Origin, Memory); */ @@ -4046,6 +4123,8 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ // DEBUG CODE + untextured_3d_geometry_buffer *DebugMesh = {}; + #define DEBUG_VIEW_WORLD_UPDATE 0 #if DEBUG_VIEW_WORLD_UPDATE // TODO(Jesse): Need to copy the voxels because the Greedy thing blows away @@ -4055,6 +4134,7 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ auto DebugMesh = AllocateMesh(Thread->PermMemory, (u32)Kilobytes(64*32)); // GetMeshForChunk(&EngineResources->MeshFreelist, Thread->PermMemory); BuildWorldChunkMeshFromMarkedVoxels_Greedy( CopiedVoxels, QueryDim, {}, QueryDim, DebugMesh ); + /* BuildWorldChunkMeshFromMarkedVoxels_Naieve( CopiedVoxels, QueryDim, {}, QueryDim, DebugMesh ); */ /* aabb QueryAABB = AABBMinMax( {}, V3i(7.f + Radius*2.f) ); */ @@ -4074,16 +4154,16 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ v3 QueryRelLocation = LocationSimSpace - SimSpaceQueryMinP; DrawVoxel_MinDim(DebugMesh, QueryRelLocation, V4(1,0,0,1), V3(1.f)); -#else - untextured_3d_geometry_buffer *DebugMesh = {}; #endif voxel_position_cursor StandingSpots = V3iCursor(ChunkCount*WORLD_CHUNK_STANDING_SPOT_COUNT, Thread->TempMemory); +#if 0 ComputeStandingSpots( QueryDim, CopiedVoxels, {}, {}, Global_StandingSpotDim, QueryDim, DebugMesh, &StandingSpots, Thread->TempMemory ); +#endif FullBarrier; for (u32 ChunkIndex = 0; ChunkIndex < ChunkCount; ++ChunkIndex) diff --git a/src/engine/world_chunk.h b/src/engine/world_chunk.h index 4f3840a57..a4a901d10 100644 --- a/src/engine/world_chunk.h +++ b/src/engine/world_chunk.h @@ -110,6 +110,7 @@ struct boundary_voxel this->V.Color = w; this->V.Flags = Voxel_Empty; + this->V.Transparency = 0; } boundary_voxel(voxel *V_in, voxel_position Offset_in) From e645658b6d7b013d2085d84b3da7e7b41c4009f7 Mon Sep 17 00:00:00 2001 From: a pirate Date: Sat, 28 Oct 2023 16:45:49 -0600 Subject: [PATCH 15/15] Rewrite implementations in DoWorldUpdate such that we can support Flood fill --- src/engine/bonsai.h | 8 + src/engine/game_effects.cpp | 1 + src/engine/world_chunk.cpp | 315 ++++++++++++++++++------------------ 3 files changed, 165 insertions(+), 159 deletions(-) diff --git a/src/engine/bonsai.h b/src/engine/bonsai.h index 1a25a28b7..09e7cbbb4 100644 --- a/src/engine/bonsai.h +++ b/src/engine/bonsai.h @@ -547,6 +547,14 @@ GetVoxel( world_chunk* Chunk, voxel_position VoxelP) return Result; } +inline voxel +CopyVoxel( world_chunk* Chunk, voxel_position VoxelP) +{ + s32 VoxelIndex = GetIndex(VoxelP, Chunk->Dim); + voxel Result = Chunk->Voxels[VoxelIndex]; + return Result; +} + inline world_position GetAbsoluteP( world_position P, chunk_dimension WorldChunkDim) { diff --git a/src/engine/game_effects.cpp b/src/engine/game_effects.cpp index 39743901f..1f6f1cc64 100644 --- a/src/engine/game_effects.cpp +++ b/src/engine/game_effects.cpp @@ -304,6 +304,7 @@ DoSplotion( engine_resources *Resources, canonical_position PickCP, f32 Radius, .world_update_op_shape_params_sphere.Radius = Radius, .world_update_op_shape_params_sphere.Location = PickCP, }; + /* QueueWorldUpdateForRegion(Resources, WorldUpdateOperationMode_Subtractive, &Shape, DARK_GREY, Resources->Memory); */ QueueWorldUpdateForRegion(Resources, WorldUpdateOperationMode_Subtractive, WorldUpdateOperationModeModifier_Flood, &Shape, DARK_GREY, Resources->Memory); /* QueueWorldUpdateForRegion(Resources, Pick, MinPCoarse, MaxPCoarse, WorldUpdateOperation_Additive, WorldUpdateOperationShape_Sphere, DARK_GREY, Radius, Resources->Memory); */ diff --git a/src/engine/world_chunk.cpp b/src/engine/world_chunk.cpp index 005a8cb41..ad22dad80 100644 --- a/src/engine/world_chunk.cpp +++ b/src/engine/world_chunk.cpp @@ -3830,12 +3830,12 @@ QueueWorldUpdateForRegion(engine_resources *Engine, world_update_op_mode Mode, w QueueWorldUpdateForRegion(Engine, Mode, WorldUpdateOperationModeModifier_None, Shape, ColorIndex, Memory); } -link_internal u32 +link_internal s32 MapIntoQueryBox(v3i SimSpaceVoxP, v3i SimSpaceQueryMinP, voxel_position SimSpaceQueryDim) { auto Rel = SimSpaceVoxP - SimSpaceQueryMinP; - auto Result = GetIndex(Rel, SimSpaceQueryDim); - return (u32)Result; + s32 Result = GetIndex(Rel, SimSpaceQueryDim); + return Result; } link_internal u32 @@ -3846,6 +3846,20 @@ MapIntoQueryBox(v3 SimSpaceVoxP, v3 SimSpaceQueryMinP, voxel_position SimSpaceQu return (u32)Result; } +global_variable voxel g_UnsetVoxel = { 0xff, 0xff, 0xff }; + +link_internal void +StoreToContiguousBuffer(v3i SimSpaceChunkMin, v3i SimSpaceQueryMinP, v3i RelVoxP, v3i QueryDim, voxel *CopiedVoxels, voxel CopyValue) +{ + v3i SimVoxP = SimSpaceChunkMin + RelVoxP; + + Assert(SimSpaceQueryMinP <= SimVoxP); + s32 Index = MapIntoQueryBox(SimVoxP, SimSpaceQueryMinP, QueryDim); + Assert(Index < Volume(QueryDim)); + Assert(CopiedVoxels[Index] == g_UnsetVoxel); + CopiedVoxels[Index] = CopyValue; +} + link_internal void DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_queue_entry_update_world_region *Job) { @@ -3863,25 +3877,22 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ auto P0 = GetSimSpaceP(World, MinP); auto P1 = GetSimSpaceP(World, MaxP); - /* auto SimSpaceMin = Min(P0, P1); */ + /* auto SimSpaceMin = Min(P0, P1); */ /* auto SimSpaceMax = Max(P0, P1); */ rect3i SimSpaceQueryAABB = Rect3iMinMax( V3i(P0), V3i(P1)); - voxel_position QueryDim = GetDim(SimSpaceQueryAABB); - - Assert(QueryDim.x % Global_StandingSpotDim.x == 0); - Assert(QueryDim.y % Global_StandingSpotDim.y == 0); + v3i CopiedVoxelsDim = GetDim(SimSpaceQueryAABB); - s32 TotalVoxels_signed = Volume(SimSpaceQueryAABB); - Assert(TotalVoxels_signed > 0); + Assert(CopiedVoxelsDim.x % Global_StandingSpotDim.x == 0); + Assert(CopiedVoxelsDim.y % Global_StandingSpotDim.y == 0); - u32 TotalVoxels = (u32)TotalVoxels_signed; + s32 TotalVoxels = Volume(SimSpaceQueryAABB); + Assert(TotalVoxels > 0); voxel *CopiedVoxels = Allocate(voxel, Thread->PermMemory, TotalVoxels); - voxel UnsetVoxel = { 0xff, 0xff, 0xff }; - for (u32 VoxelIndex = 0; VoxelIndex < TotalVoxels; ++VoxelIndex) { CopiedVoxels[VoxelIndex] = UnsetVoxel; } + for (s32 VoxelIndex = 0; VoxelIndex < TotalVoxels; ++VoxelIndex) { CopiedVoxels[VoxelIndex] = g_UnsetVoxel; } v3i SimSpaceQueryMinP = SimSpaceQueryAABB.Min; @@ -3903,182 +3914,169 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ random_series Entropy = {54392}; - for (s32 zVoxel = s32(ChunkRelRectMin.z); zVoxel < s32(ChunkRelRectMax.z); zVoxel += 1) + switch (Shape.Type) { - for (s32 yVoxel = s32(ChunkRelRectMin.y); yVoxel < s32(ChunkRelRectMax.y); yVoxel += 1) + InvalidCase(type_world_update_op_shape_params_noop); + + case type_world_update_op_shape_params_sphere: { - for (s32 xVoxel = s32(ChunkRelRectMin.x); xVoxel < s32(ChunkRelRectMax.x); xVoxel += 1) - { - voxel_position RelVoxP = Voxel_Position(s32(xVoxel), s32(yVoxel), s32(zVoxel)); - voxel *V = GetVoxel(Chunk, RelVoxP); - Assert( (V->Flags & Voxel_MarkBit) == 0); + world_update_op_shape_params_sphere *Sphere = SafeCast(world_update_op_shape_params_sphere, &Shape); - v3i SimSpaceVoxPExact = V3i(xVoxel, yVoxel, zVoxel) + SimSpaceChunkMin; + canonical_position P = Sphere->Location; + auto LocationSimSpace = GetSimSpaceP(World, P); - voxel CopyValue = *V; + switch(Mode) + { + InvalidCase(WorldUpdateOperationMode_None); - switch (Shape.Type) + case WorldUpdateOperationMode_Subtractive: { - InvalidCase(type_world_update_op_shape_params_noop); - - case type_world_update_op_shape_params_sphere: + switch(Modifier) { - world_update_op_shape_params_sphere *Sphere = SafeCast(world_update_op_shape_params_sphere, &Shape); - - canonical_position P = Sphere->Location; - auto LocationSimSpace = GetSimSpaceP(World, P); - - if (LengthSq(SimSpaceVoxPExact - LocationSimSpace) < Square(Sphere->Radius)) + case WorldUpdateOperationModeModifier_None: { - switch(Mode) + MinMaxIterator(xVoxel, yVoxel, zVoxel, ChunkRelRectMin, ChunkRelRectMax) { - InvalidCase(WorldUpdateOperationMode_None); - - case WorldUpdateOperationMode_Subtractive: + v3i RelVoxP = V3i(xVoxel, yVoxel, zVoxel); + v3i SimVoxP = (SimSpaceChunkMin+RelVoxP); + voxel V = CopyVoxel(Chunk, RelVoxP); + if (LengthSq( SimVoxP - LocationSimSpace) < Square(Sphere->Radius-1.f)) { - switch(Modifier) - { - case WorldUpdateOperationModeModifier_None: - { - if (CopyValue.Flags&Voxel_Filled) { --Chunk->FilledCount; CopyValue.Flags = Voxel_Empty; } - } break; - - case WorldUpdateOperationModeModifier_Flood: - { - if (LengthSq(SimSpaceVoxPExact - LocationSimSpace) < Square(Sphere->Radius-1.f)) - { - if (CopyValue.Flags&VoxelFaceMask) { --Chunk->FilledCount; CopyValue.Flags = Voxel_Empty; } - } - else - { - if (CopyValue.Flags&VoxelFaceMask) { CopyValue.Color = NewColor; } - } - CopyValue.Flags |= Voxel_MarkBit; - } break; - } - - } break; - - case WorldUpdateOperationMode_Additive: - { - if ( (CopyValue.Flags&Voxel_Filled) == 0 ) { ++Chunk->FilledCount; } - CopyValue.Flags = Voxel_Filled; - CopyValue.Color = NewColor; - } break; + if (V.Flags&Voxel_Filled) { --Chunk->FilledCount; V.Flags = Voxel_Empty; } + } + StoreToContiguousBuffer(SimSpaceChunkMin, SimSpaceQueryMinP, RelVoxP, CopiedVoxelsDim, CopiedVoxels, V); } + } break; - } -#if 0 - /* else if (LengthSq(SimSpaceVoxPExact - LocationSimSpace) < Square(Sphere->Radius+1.f)) */ + case WorldUpdateOperationModeModifier_Flood: { - switch(Mode) + MinMaxIterator(xVoxel, yVoxel, zVoxel, ChunkRelRectMin, ChunkRelRectMax) { - InvalidCase(WorldUpdateOperationMode_None); - - case WorldUpdateOperationMode_Subtractive: + v3i RelVoxP = V3i(xVoxel, yVoxel, zVoxel); + v3i SimVoxP = (SimSpaceChunkMin+RelVoxP); + voxel CopyValue = CopyVoxel(Chunk, RelVoxP); + if (LengthSq(SimVoxP - LocationSimSpace) < Square(Sphere->Radius-1.f)) { - if (CopyValue.Flags&VoxelFaceMask) - { - CopyValue.Color = GREY_8; - } - else - { - CopyValue.Flags |= Voxel_MarkBit; - } - } break; - - case WorldUpdateOperationMode_Additive: + if (CopyValue.Flags&VoxelFaceMask) { --Chunk->FilledCount; CopyValue.Flags = Voxel_Empty; } + CopyValue.Flags |= Voxel_MarkBit; + } + else if (LengthSq(SimVoxP - LocationSimSpace) < Square(Sphere->Radius)) { - } break; + if (CopyValue.Flags&VoxelFaceMask) { CopyValue.Color = NewColor; } + CopyValue.Flags |= Voxel_MarkBit; + } + StoreToContiguousBuffer(SimSpaceChunkMin, SimSpaceQueryMinP, RelVoxP, CopiedVoxelsDim, CopiedVoxels, CopyValue); } - } -#endif - } break; + } break; + } - case type_world_update_op_shape_params_rect: - { - // Not implemented - Assert(Modifier == WorldUpdateOperationModeModifier_None); + } break; - world_update_op_shape_params_rect *Rect = SafeCast(world_update_op_shape_params_rect, &Shape); + case WorldUpdateOperationMode_Additive: + { + NotImplemented; + /* if ( (CopyValue.Flags&Voxel_Filled) == 0 ) { ++Chunk->FilledCount; } */ + /* CopyValue.Flags = Voxel_Filled; */ + /* CopyValue.Color = NewColor; */ + } break; + } + } break; - // NOTE(Jesse): These _should_ already be min/maxed, so we can change these to asserts. - v3i P0SS = V3i(Rect->P0); - v3i P1SS = V3i(Rect->P1); + case type_world_update_op_shape_params_rect: + { + // Not implemented + Assert(Modifier == WorldUpdateOperationModeModifier_None); - v3i MinSS = Min(P0SS, P1SS); - v3i MaxSS = Max(P0SS, P1SS); + world_update_op_shape_params_rect *Rect = SafeCast(world_update_op_shape_params_rect, &Shape); - /* MinSS += ClampNegative(GetSign(MinSS)); */ - /* MaxSS += 1; */ + // NOTE(Jesse): These _should_ already be min/maxed, so we can change these to asserts. + v3i P0SS = V3i(Rect->P0); + v3i P1SS = V3i(Rect->P1); - rect3i SSRect = {MinSS, MaxSS}; - if (Contains(SSRect, SimSpaceVoxPExact)) - { - switch(Mode) - { - InvalidCase(WorldUpdateOperationMode_None); + v3i MinSS = Min(P0SS, P1SS); + v3i MaxSS = Max(P0SS, P1SS); - case WorldUpdateOperationMode_Subtractive: - { - if (CopyValue.Flags & Voxel_Filled) { --Chunk->FilledCount; } - CopyValue.Flags = Voxel_Empty; - } break; + /* MinSS += ClampNegative(GetSign(MinSS)); */ + /* MaxSS += 1; */ - case WorldUpdateOperationMode_Additive: - { - if ( (CopyValue.Flags & Voxel_Filled) == 0 ) { ++Chunk->FilledCount; } - CopyValue.Flags = Voxel_Filled; - CopyValue.Color = NewColor; - } break; + rect3i SSRect = {MinSS, MaxSS}; + /* if (Contains(SSRect, SimVoxP)) */ + { + switch(Mode) + { + InvalidCase(WorldUpdateOperationMode_None); + + case WorldUpdateOperationMode_Subtractive: + { + MinMaxIterator(xVoxel, yVoxel, zVoxel, ChunkRelRectMin, ChunkRelRectMax) + { + v3i RelVoxP = V3i(xVoxel, yVoxel, zVoxel); + v3i SimVoxP = (SimSpaceChunkMin+RelVoxP); + voxel V = CopyVoxel(Chunk, RelVoxP); + if (Contains(SSRect, SimVoxP)) + { + if (V.Flags&Voxel_Filled) { --Chunk->FilledCount; } + V.Flags = Voxel_Empty; } + StoreToContiguousBuffer(SimSpaceChunkMin, SimSpaceQueryMinP, RelVoxP, CopiedVoxelsDim, CopiedVoxels, V); } } break; - case type_world_update_op_shape_params_asset: + case WorldUpdateOperationMode_Additive: { - // Not implemented - Assert(Modifier == WorldUpdateOperationModeModifier_None); - - world_update_op_shape_params_asset *AssetJob = SafeCast(world_update_op_shape_params_asset, &Shape); - asset *Asset = AssetJob->Asset; - /* BlitAssetIntoWorld(Engine, Asset, Origin, Memory); */ - - v3 AssetOriginP = GetSimSpaceP(World, AssetJob->Origin); - - v3i OriginToCurrentVoxP = SimSpaceVoxPExact - AssetOriginP; - - /* s32 AssetVoxelIndex = GetIndex(OriginToCurrentVoxP, Asset->Model.Dim); */ - /* if (AssetVoxelIndex != -1) */ + MinMaxIterator(xVoxel, yVoxel, zVoxel, ChunkRelRectMin, ChunkRelRectMax) { - voxel *AssetV = TryGetVoxel(Asset->Model.Vox.ChunkData, OriginToCurrentVoxP); - if (AssetV && (AssetV->Flags&Voxel_Filled)) { CopyValue = *AssetV; } + v3i RelVoxP = V3i(xVoxel, yVoxel, zVoxel); + v3i SimVoxP = (SimSpaceChunkMin+RelVoxP); + voxel V = CopyVoxel(Chunk, RelVoxP); + if (Contains(SSRect, SimVoxP)) + { + if ( (V.Flags & Voxel_Filled) == 0 ) { ++Chunk->FilledCount; } + V.Flags = Voxel_Filled; + V.Color = NewColor; + } + StoreToContiguousBuffer(SimSpaceChunkMin, SimSpaceQueryMinP, RelVoxP, CopiedVoxelsDim, CopiedVoxels, V); } - } break; - } + } + } break; + case type_world_update_op_shape_params_asset: + { + // Not implemented + Assert(Modifier == WorldUpdateOperationModeModifier_None); - Assert(SimSpaceQueryMinP <= SimSpaceVoxPExact); - u32 Index = MapIntoQueryBox(SimSpaceVoxPExact, SimSpaceQueryMinP, QueryDim); - Assert(Index < TotalVoxels); - Assert(CopiedVoxels[Index] == UnsetVoxel); - CopiedVoxels[Index] = CopyValue; - /* Assert(IsValid(CopiedVoxels+Index)); */ + world_update_op_shape_params_asset *AssetJob = SafeCast(world_update_op_shape_params_asset, &Shape); + asset *Asset = AssetJob->Asset; + + v3 AssetOriginP = GetSimSpaceP(World, AssetJob->Origin); + + MinMaxIterator(xVoxel, yVoxel, zVoxel, ChunkRelRectMin, ChunkRelRectMax) + { + v3i RelVoxP = V3i(xVoxel, yVoxel, zVoxel); + v3i SimVoxP = (SimSpaceChunkMin+RelVoxP); + v3i OriginToCurrentVoxP = SimVoxP - AssetOriginP; + voxel V = CopyVoxel(Chunk, RelVoxP); + voxel *AssetV = TryGetVoxel(Asset->Model.Vox.ChunkData, OriginToCurrentVoxP); + if (AssetV && (AssetV->Flags & Voxel_Filled)) + { + V = *AssetV; + } + StoreToContiguousBuffer(SimSpaceChunkMin, SimSpaceQueryMinP, RelVoxP, CopiedVoxelsDim, CopiedVoxels, V); } - } + + } break; + } } random_series Entropy = {43246}; - // NOTE(Jesse): We can actually do the entire dim here, but it's probably - // better (faster) to just do what we actually need to - MarkBoundaryVoxels_NoExteriorFaces( CopiedVoxels, QueryDim, {{1,1,1}}, QueryDim-1, &Entropy, GREY_5, GREY_7); - /* MarkBoundaryVoxels_NoExteriorFaces( CopiedVoxels, QueryDim, {}, QueryDim, &Entropy, GREY_5, GREY_7); */ - /* MarkBoundaryVoxels_MakeExteriorFaces( CopiedVoxels, QueryDim, {{1,1,1}}, QueryDim-1); */ - /* MarkBoundaryVoxels_MakeExteriorFaces( CopiedVoxels, QueryDim, {}, QueryDim); */ + MarkBoundaryVoxels_NoExteriorFaces( CopiedVoxels, CopiedVoxelsDim, {{1,1,1}}, CopiedVoxelsDim-1, &Entropy, GREY_5, GREY_7); + /* MarkBoundaryVoxels_NoExteriorFaces( CopiedVoxels, CopiedVoxelsDim, {}, CopiedVoxelsDim, &Entropy, GREY_5, GREY_7); */ + /* MarkBoundaryVoxels_MakeExteriorFaces( CopiedVoxels, CopiedVoxelsDim, {{1,1,1}}, CopiedVoxelsDim-1); */ + /* MarkBoundaryVoxels_MakeExteriorFaces( CopiedVoxels, CopiedVoxelsDim, {}, CopiedVoxelsDim); */ for (u32 ChunkIndex = 0; ChunkIndex < ChunkCount; ++ChunkIndex) @@ -4105,12 +4103,12 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ voxel_position RelVoxP = Voxel_Position(s32(xVoxel), s32(yVoxel), s32(zVoxel)); voxel *V = GetVoxel(Chunk, RelVoxP); - v3i SimSpaceVoxPExact = V3i(xVoxel, yVoxel, zVoxel) + SimSpaceChunkMin; + v3i SimVoxP = V3i(xVoxel, yVoxel, zVoxel) + SimSpaceChunkMin; - Assert(SimSpaceQueryMinP <= SimSpaceVoxPExact); - u32 Index = MapIntoQueryBox(SimSpaceVoxPExact, SimSpaceQueryMinP, QueryDim); + Assert(SimSpaceQueryMinP <= SimVoxP); + s32 Index = MapIntoQueryBox(SimVoxP, SimSpaceQueryMinP, CopiedVoxelsDim); Assert(Index < TotalVoxels); - Assert(CopiedVoxels[Index] != UnsetVoxel); + Assert(CopiedVoxels[Index] != g_UnsetVoxel); /* Assert(IsValid(CopiedVoxels+Index)); */ Assert( (V->Flags & Voxel_MarkBit) == 0); *V = CopiedVoxels[Index]; @@ -4127,22 +4125,21 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ #define DEBUG_VIEW_WORLD_UPDATE 0 #if DEBUG_VIEW_WORLD_UPDATE - // TODO(Jesse): Need to copy the voxels because the Greedy thing blows away - // the face flags as it does the traversal. + NotImplemented; auto DebugMesh = AllocateMesh(Thread->PermMemory, (u32)Kilobytes(64*32)); // GetMeshForChunk(&EngineResources->MeshFreelist, Thread->PermMemory); - BuildWorldChunkMeshFromMarkedVoxels_Greedy( CopiedVoxels, QueryDim, {}, QueryDim, DebugMesh ); - /* BuildWorldChunkMeshFromMarkedVoxels_Naieve( CopiedVoxels, QueryDim, {}, QueryDim, DebugMesh ); */ + BuildWorldChunkMeshFromMarkedVoxels_Greedy( CopiedVoxels, CopiedVoxelsDim, {}, CopiedVoxelsDim, DebugMesh ); + /* BuildWorldChunkMeshFromMarkedVoxels_Naieve( CopiedVoxels, CopiedVoxelsDim, {}, CopiedVoxelsDim, DebugMesh ); */ /* aabb QueryAABB = AABBMinMax( {}, V3i(7.f + Radius*2.f) ); */ - DEBUG_DrawAABB(DebugMesh, AABBMinDim({}, V3(QueryDim)), BLUE); + DEBUG_DrawAABB(DebugMesh, AABBMinDim({}, V3(CopiedVoxelsDim)), BLUE); DEBUG_DrawAABB(DebugMesh, AABBMinDim(V3(-1), V3(2)), PINK); - world_chunk *TempChunk = AllocateWorldChunk(Thread->PermMemory, MinP.WorldP, QueryDim); + world_chunk *TempChunk = AllocateWorldChunk(Thread->PermMemory, MinP.WorldP, CopiedVoxelsDim); picked_world_chunk *PickedChunk = Allocate(picked_world_chunk, Thread->PermMemory, 1); PickedChunk->Chunk = TempChunk; @@ -4159,9 +4156,9 @@ DoWorldUpdate(work_queue *Queue, world *World, thread_local_state *Thread, work_ voxel_position_cursor StandingSpots = V3iCursor(ChunkCount*WORLD_CHUNK_STANDING_SPOT_COUNT, Thread->TempMemory); #if 0 - ComputeStandingSpots( QueryDim, CopiedVoxels, {}, + ComputeStandingSpots( CopiedVoxelsDim, CopiedVoxels, {}, {}, Global_StandingSpotDim, - QueryDim, + CopiedVoxelsDim, DebugMesh, &StandingSpots, Thread->TempMemory ); #endif