33using OpenTK . Graphics . OpenGL4 ;
44using System ;
55using System . Collections . Generic ;
6+ using System . Drawing ;
67using System . Linq ;
78using System . Text ;
89using System . Threading . Tasks ;
@@ -14,6 +15,8 @@ public class SubChunk
1415 List < Vertex > vertices = new List < Vertex > ( ) ;
1516 List < uint > triangles = new List < uint > ( ) ;
1617
18+ private ChunkOutline outline ;
19+
1720 public readonly uint [ ] blocks ;
1821 public readonly int [ ] renderers ;
1922 public Palette [ ] palette ;
@@ -33,27 +36,22 @@ public SubChunk(Vector3i _position, uint[] _blocks, int[] _renderers, Palette[]
3336 renderers = _renderers ;
3437 palette = _palette ;
3538 texId = _texId ;
39+ outline = new ChunkOutline ( pos , VoxelData . ChunkWidth , 4f , Color . Yellow ) ;
3640 }
3741
38- public void Init ( )
39- {
40- CreateMeshData ( ) ;
41- InitMesh ( ) ;
42- }
42+ public static Vector3i [ ] possitionLookUp ;
4343
44- public void CreateMeshData ( )
44+ static SubChunk ( )
4545 {
46- vertices . Clear ( ) ;
47- triangles . Clear ( ) ;
48-
46+ possitionLookUp = new Vector3i [ VoxelData . ChunkLayerLength * VoxelData . ChunkHeight ] ;
4947 int x = 0 ;
5048 int y = 0 ;
5149 int z = 0 ;
5250 int origx ;
5351 int origy ;
5452 int origz ;
5553
56- for ( int currentBlock = 0 ; currentBlock < blocks . Length ; currentBlock ++ ) { /*Maybe store lookup table. Maybe store lookup table*/
54+ for ( int currentBlock = 0 ; currentBlock < possitionLookUp . Length ; currentBlock ++ ) {
5755 z ++ ;
5856 if ( z == 16 ) { z = 0 ; y += 1 ; }
5957 if ( y == 16 ) { y = 0 ; x += 1 ; }
@@ -75,17 +73,38 @@ public void CreateMeshData()
7573 x -- ;
7674 }
7775
76+ possitionLookUp [ currentBlock ] = new Vector3i ( x , y , z ) ;
77+
78+ z = origz ;
79+ y = origy ;
80+ x = origx ;
81+ }
82+ }
83+
84+ public void Init ( )
85+ {
86+ CreateMeshData ( ) ;
87+ InitMesh ( ) ;
88+ }
89+
90+ public void CreateMeshData ( )
91+ {
92+ vertices . Clear ( ) ;
93+ triangles . Clear ( ) ;
94+
95+ for ( int currentBlock = 0 ; currentBlock < blocks . Length ; currentBlock ++ ) {
96+
7897 int renderer = renderers [ currentBlock ] ;
7998 if ( renderer > - 1 && blocks [ currentBlock ] < palette . Length ) { // don't render air
8099#if DEBUG
81100 uint blockId = blocks [ currentBlock ] ;
82101 Palette pal = palette [ blockId ] ;
83- World . blockRenderers [ renderer ] ( new Vector3 ( x , y , z ) , pos * 16 , pal . textures , pal . data ,
102+ World . blockRenderers [ renderer ] ( possitionLookUp [ currentBlock ] , pos * 16 , pal . textures , pal . data ,
84103 ref vertices , ref triangles ) ;
85104#else
86105 try {
87106 Palette pal = palette [ blocks [ currentBlock ] ] ;
88- World . blockRenderers [ renderer ] ( new Vector3 ( x , y , z ) , pos * 16 , pal . textures , pal . data ,
107+ World . blockRenderers [ renderer ] ( possitionLookUp [ currentBlock ] , pos * 16 , pal . textures , pal . data ,
89108 ref vertices , ref triangles ) ;
90109 } catch ( Exception ex ) {
91110 uint b = blocks [ currentBlock ] ;
@@ -100,10 +119,6 @@ public void CreateMeshData()
100119 }
101120#endif
102121 }
103-
104- z = origz ;
105- y = origy ;
106- x = origx ;
107122 }
108123 }
109124
@@ -141,7 +156,7 @@ public void Update()
141156 CreateMesh ( ) ;
142157 }
143158
144- public void Render ( Shader s )
159+ public void Render ( Shader s , Shader outlineShader )
145160 {
146161 Matrix4 transform = Matrix4 . CreateTranslation ( new Vector3 ( pos . X * VoxelData . ChunkWidth , pos . Y * VoxelData . ChunkHeight , pos . Z * VoxelData . ChunkWidth ) ) ;
147162
@@ -153,6 +168,9 @@ public void Render(Shader s)
153168
154169 GL . BindVertexArray ( vao ) ;
155170 GL . DrawElements ( BeginMode . Triangles , triangles . Count , DrawElementsType . UnsignedInt , 0 ) ;
171+
172+ if ( World . ShowChunkOutlines )
173+ outline . Render ( outlineShader ) ;
156174 }
157175
158176 public void SetBlock ( int X , int Y , int Z , uint id )
@@ -188,44 +206,13 @@ public void GetBlockIndex(int bx, int by, int bz, out int blockIndex)
188206 {
189207 Vector3i block = new Vector3i ( bx , by , bz ) ;
190208
191- int x = 0 ;
192- int y = 0 ;
193- int z = 0 ;
194- int origx ;
195- int origy ;
196- int origz ;
197209 Vector3i offset = new Vector3i ( pos . X * VoxelData . ChunkWidth , pos . Y * VoxelData . ChunkHeight , pos . Z * VoxelData . ChunkWidth ) ;
198210
199211 for ( int currentBlock = 0 ; currentBlock < blocks . Length ; currentBlock ++ ) {
200- z ++ ;
201- if ( z == 16 ) { z = 0 ; y += 1 ; }
202- if ( y == 16 ) { y = 0 ; x += 1 ; }
203-
204- origz = z ;
205- origy = y ;
206- origx = x ;
207-
208- if ( z == 0 ) {
209- z = 16 ;
210- y -= 1 ;
211- }
212-
213- if ( y == - 1 )
214- y = 16 ;
215-
216- if ( Math . Abs ( z ) % 16 == 0 && y == 16 ) {
217- y -- ;
218- x -- ;
219- }
220-
221- if ( new Vector3i ( x , y , z ) + offset == block ) {
212+ if ( possitionLookUp [ currentBlock ] + offset == block ) {
222213 blockIndex = currentBlock ;
223214 return ;
224215 }
225-
226- z = origz ;
227- y = origy ;
228- x = origx ;
229216 }
230217
231218 blockIndex = - 1 ;
@@ -254,48 +241,6 @@ public Palette GetPaletteByName(string name)
254241 return Palette . NULL ;
255242 }
256243
257- public Vector3i IndexToPos ( int i )
258- {
259- int x = 0 ;
260- int y = 0 ;
261- int z = 0 ;
262- int origx ;
263- int origy ;
264- int origz ;
265-
266- for ( int currentBlock = 0 ; currentBlock < blocks . Length ; currentBlock ++ ) {
267- z ++ ;
268- if ( z == 16 ) { z = 0 ; y += 1 ; }
269- if ( y == 16 ) { y = 0 ; x += 1 ; }
270-
271- origz = z ;
272- origy = y ;
273- origx = x ;
274-
275- if ( z == 0 ) {
276- z = 16 ;
277- y -= 1 ;
278- }
279-
280- if ( y == - 1 )
281- y = 16 ;
282-
283- if ( Math . Abs ( z ) % 16 == 0 && y == 16 ) {
284- y -- ;
285- x -- ;
286- }
287-
288- if ( currentBlock == i )
289- return new Vector3i ( x , y , z ) ;
290-
291- z = origz ;
292- y = origy ;
293- x = origx ;
294- }
295-
296- return - Vector3i . One ;
297- }
298-
299244 public void SetBlock ( int blockIndex , int palletteIndex , int renderer , int data , bool update )
300245 {
301246 if ( blockIndex < 0 || blockIndex >= blocks . Length )
@@ -304,7 +249,7 @@ public void SetBlock(int blockIndex, int palletteIndex, int renderer, int data,
304249 renderers [ blockIndex ] = renderer ;
305250 Update ( ) ;
306251
307- Vector3i p = IndexToPos ( blockIndex ) ;
252+ Vector3i p = possitionLookUp [ blockIndex ] ;
308253
309254 if ( p . X < 2 )
310255 World . UpdateChunk ( pos - Vector3i . UnitX ) ;
@@ -326,11 +271,12 @@ public int AddNewPalette(string name, int data)
326271 {
327272 GL . DeleteTexture ( texId ) ;
328273
329- for ( int i = 0 ; i < World . plate . sub_chunks . Count ; i ++ )
330- if ( World . plate . sub_chunks [ i ] . position == pos ) {
274+ for ( int i = 0 ; i < World . chunks . Length ; i ++ )
275+ if ( World . chunks [ i ] . pos == pos ) {
331276 string mcn = name . Contains ( ':' ) ? name : "minecraft:" + name ;
332- World . plate . sub_chunks [ i ] . block_palette . Add ( new BuildPlate . PaletteBlock ( ) { name = mcn , data = data } ) ;
333- World . UpdateChunkPalette ( i ) ;
277+ Array . Resize ( ref palette , palette . Length + 1 ) ;
278+ palette [ palette . Length - 1 ] = new Palette ( mcn , data , - 1 ) ;
279+ World . ReloadPaletteTextures ( i ) ;
334280 return palette . Length - 1 ;
335281 }
336282
@@ -346,5 +292,110 @@ public int NextPaletteIndex()
346292 else
347293 return - 1 ;
348294 }
295+
296+ public static bool WouldBeBlockInChunk ( Vector3i _chunkPos , Vector3i blockPos )
297+ {
298+ Vector3i chunkPos = _chunkPos * 16 ;
299+ for ( int i = 0 ; i < possitionLookUp . Length ; i ++ )
300+ if ( possitionLookUp [ i ] + chunkPos == blockPos )
301+ return true ;
302+ return false ;
303+ }
304+
305+ public static Vector3i GetToWhereCreateChunk ( Vector3i shouldBeThere , Vector3i blockPos )
306+ {
307+ if ( WouldBeBlockInChunk ( shouldBeThere , blockPos ) )
308+ return shouldBeThere ;
309+
310+ for ( int i = 0 ; i < VoxelData . faceChecks . Length ; i ++ ) {
311+ Vector3i offset = VoxelData . faceChecks [ i ] ;
312+ if ( WouldBeBlockInChunk ( shouldBeThere + offset , blockPos ) )
313+ return shouldBeThere + offset ;
314+ }
315+ return shouldBeThere ;
316+ }
317+
318+ class ChunkOutline
319+ {
320+ public Vector3 Position { get ; set ; }
321+
322+ public ColVertex [ ] vertices ;
323+ public uint [ ] triangles ;
324+ public bool Active { get ; set ; }
325+
326+
327+ protected int vao ;
328+ protected int vbo ;
329+ protected int ebo ;
330+
331+ public float lineWidth ;
332+
333+ public ChunkOutline ( Vector3i pos , float size , float _lineWidth , Color _c )
334+ {
335+ Vector4 c = new Vector4 (
336+ ( float ) _c . R / 255f ,
337+ ( float ) _c . G / 255f ,
338+ ( float ) _c . B / 255f ,
339+ ( float ) _c . A / 255f
340+ ) ;
341+ Vector3 half = Vector3 . One / 2f ;
342+
343+ vertices = new ColVertex [ VoxelData . voxelVerts . Length ] ;
344+ for ( int i = 0 ; i < vertices . Length ; i ++ )
345+ vertices [ i ] = new ColVertex ( VoxelData . voxelVerts [ i ] * size , c ) ;
346+
347+ triangles = VoxelData . voxelLines ;
348+
349+ lineWidth = _lineWidth ;
350+
351+ Position = pos * size - new Vector3 ( 0.5f , 0.5f , 0.5f ) ;
352+
353+ InitMesh ( ) ;
354+
355+ Active = true ;
356+ }
357+
358+ private void InitMesh ( )
359+ {
360+ GL . CreateVertexArrays ( 1 , out vao ) ;
361+ GL . BindVertexArray ( vao ) ;
362+ GL . CreateBuffers ( 1 , out ebo ) ;
363+ GL . CreateBuffers ( 1 , out vbo ) ;
364+ UploadMesh ( ) ;
365+ }
366+
367+ public void UploadMesh ( )
368+ {
369+ GL . NamedBufferData ( ebo , triangles . Length * sizeof ( uint ) , triangles , BufferUsageHint . StaticDraw ) ;
370+ GL . VertexArrayElementBuffer ( vao , ebo ) ;
371+
372+ int vertexBindingPoint = 0 ;
373+ GL . NamedBufferData ( vbo , vertices . Length * ColVertex . Size , vertices , BufferUsageHint . StaticDraw ) ;
374+ GL . VertexArrayVertexBuffer ( vao , vertexBindingPoint , vbo , IntPtr . Zero , ColVertex . Size ) ;
375+
376+ // pos
377+ GL . VertexArrayAttribFormat ( vao , 0 , 3 , VertexAttribType . Float , false , 0 ) ;
378+ GL . VertexArrayAttribBinding ( vao , 0 , vertexBindingPoint ) ;
379+ GL . EnableVertexArrayAttrib ( vao , 0 ) ;
380+ // color
381+ GL . VertexArrayAttribFormat ( vao , 1 , 4 , VertexAttribType . Float , false , 3 * sizeof ( float ) ) ;
382+ GL . VertexArrayAttribBinding ( vao , 1 , vertexBindingPoint ) ;
383+ GL . EnableVertexArrayAttrib ( vao , 1 ) ;
384+ }
385+
386+ public void Render ( Shader s )
387+ {
388+ if ( ! Active )
389+ return ;
390+
391+ s . Bind ( ) ;
392+ Matrix4 transform = Matrix4 . CreateTranslation ( Position ) ;
393+ s . UploadMat4 ( "uTransform" , ref transform ) ;
394+
395+ GL . BindVertexArray ( vao ) ;
396+ GL . LineWidth ( lineWidth ) ;
397+ GL . DrawElements ( BeginMode . Lines , triangles . Length , DrawElementsType . UnsignedInt , 0 ) ;
398+ }
399+ }
349400 }
350401}
0 commit comments