Exemplo n.º 1
0
 public bool Equals(XZPair other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     return other.X == X && other.Z == Z;
 }
Exemplo n.º 2
0
        public TerrainChunk GenerateChunk(XZPair id)
        {
            int offsetX = id.X*_blockWidth*(_chunkWidthInBlocks);
            int offsetZ = id.Z*_blockWidth*(_chunkWidthInBlocks);

            var rawGeometry = new float[_chunkWidthInVerts*_chunkWidthInVerts*4];
            var rawNormals = new ushort[_chunkWidthInVerts * _chunkWidthInVerts * 4];
            var rawBinormals = new byte[_chunkWidthInVerts*_chunkWidthInVerts*4];
            var rawTangents = new byte[_chunkWidthInVerts*_chunkWidthInVerts*4];
            var rawUVCoords = new float[_chunkWidthInVerts*_chunkWidthInVerts*2];
            var indicies = new int[(_chunkWidthInBlocks)*(_chunkWidthInBlocks)*8];
            var activeVerts = new byte[_chunkWidthInVerts*_chunkWidthInVerts];

            _terrainGenKernel.SetValueArgument(1, offsetX);
            _terrainGenKernel.SetValueArgument(2, offsetZ);
            _normalGenKernel.SetValueArgument(1, offsetX);
            _normalGenKernel.SetValueArgument(2, offsetZ);

            _cmdQueue.WriteToBuffer(_emptyVerts, _activeVerts, true, null);
            _cmdQueue.WriteToBuffer(_emptyIndices, _indicies, true, null);
            _cmdQueue.Execute(_terrainGenKernel, null, new long[]{_chunkWidthInVerts, _chunkWidthInVerts}, null, null);
            _cmdQueue.Execute(_normalGenKernel, null, new long[]{_chunkWidthInVerts, _chunkWidthInVerts}, null, null);

            for (int depth = 0; depth < 5; depth++){
                _qTreeKernel.SetValueArgument(0, depth);
                _crossCullKernel.SetValueArgument(0, depth);
                int cellWidth = (int) Math.Pow(2, depth)*2;
                int qTreeWidth = _chunkWidthInBlocks/(cellWidth);
                _cmdQueue.Execute(_qTreeKernel, null, new long[]{(qTreeWidth) - 1, (qTreeWidth*2)}, null, null);
                _cmdQueue.Execute(_crossCullKernel, null, new long[]{_chunkWidthInBlocks/cellWidth, _chunkWidthInBlocks/cellWidth}, null, null);
            }
            _cmdQueue.Execute(_winderKernel, null, new long[]{(_chunkWidthInBlocks), (_chunkWidthInBlocks*2)}, null, null);

            _cmdQueue.ReadFromBuffer(_geometry, ref rawGeometry, true, null);
            _cmdQueue.ReadFromBuffer(_normals, ref rawNormals, true, null);
            _cmdQueue.ReadFromBuffer(_binormals, ref rawBinormals, true, null);
            _cmdQueue.ReadFromBuffer(_tangents, ref rawTangents, true, null);
            _cmdQueue.ReadFromBuffer(_uvCoords, ref rawUVCoords, true, null);
            _cmdQueue.ReadFromBuffer(_indicies, ref indicies, true, null);
            _cmdQueue.ReadFromBuffer(_activeVerts, ref activeVerts, true, null);

            _cmdQueue.Finish();

            for (int v = 3; v < rawNormals.Length; v += 4){
                rawNormals[v] = 1;
            }
            for (int v = 3; v < rawBinormals.Length; v += 4){
                rawBinormals[v] = 1;
            }
            for (int v = 3; v < rawTangents.Length; v += 4){
                rawTangents[v] = 1;
            }

            var texNormal = new Texture2D(Gbl.Device, _chunkWidthInVerts, _chunkWidthInVerts, false, SurfaceFormat.Rgba64);
            var texBinormal = new Texture2D(Gbl.Device, _chunkWidthInVerts, _chunkWidthInVerts, false, SurfaceFormat.Color);
            var texTangent = new Texture2D(Gbl.Device, _chunkWidthInVerts, _chunkWidthInVerts, false, SurfaceFormat.Color);

            texNormal.SetData(rawNormals);
            texBinormal.SetData(rawBinormals);
            texTangent.SetData(rawTangents);

            //var maxHeight = verts.Aggregate((agg, next) => next.Y > agg.Y ? next : agg).Y;
            //var minHeight = verts.Aggregate((agg, next) => next.Y < agg.Y ? next : agg).Y;

            //these functions take the raw buffer data from opencl and remove stride and empty fields
            var parsedUV = ParseUV(rawUVCoords);
            var parsedGeometry = ParseGeometry(rawGeometry);
            var parsedIndicies = ParseIndicies(indicies);

            var sw = new Stopwatch();
            sw.Start();

            int[] culledIndexes;
            VertexPositionTexture[] culledVertexes;

            CullVertexes(activeVerts, parsedIndicies.ToList(), parsedGeometry, parsedUV, out culledIndexes, out culledVertexes);
            sw.Stop();
            double elapsed = sw.ElapsedMilliseconds;
            var chunkData = new TerrainChunk(id, culledVertexes, culledIndexes, texNormal, texBinormal, texTangent);

            return chunkData;
        }
Exemplo n.º 3
0
 public XZPair(XZPair xzpair)
 {
     X = xzpair.X;
     Z = xzpair.Z;
 }