Ejemplo n.º 1
0
        public void GenVBO()
        {
            vbo = new ChunkVBO();
            List <BEPUutilities.Vector3> vecs   = BlockShapeRegistry.BSD[Dat].GetVertices(new BEPUutilities.Vector3(0, 0, 0), false, false, false, false, false, false);
            List <BEPUutilities.Vector3> norms  = BlockShapeRegistry.BSD[Dat].GetNormals(new BEPUutilities.Vector3(0, 0, 0), false, false, false, false, false, false);
            List <BEPUutilities.Vector3> tcoord = BlockShapeRegistry.BSD[Dat].GetTCoords(new BEPUutilities.Vector3(0, 0, 0), Mat, false, false, false, false, false, false);

            vbo.Vertices  = new List <Vector3>();
            vbo.Normals   = new List <Vector3>();
            vbo.TexCoords = new List <Vector3>();
            vbo.Indices   = new List <uint>();
            vbo.Colors    = new List <Vector4>();
            vbo.TCOLs     = new List <Vector4>();
            vbo.Tangents  = new List <Vector3>();
            vbo.THVs      = new List <Vector4>();
            vbo.THWs      = new List <Vector4>();
            vbo.THVs2     = new List <Vector4>();
            vbo.THWs2     = new List <Vector4>();
            Color4F tcol = Colors.ForByte(Paint);

            for (int i = 0; i < vecs.Count; i++)
            {
                vbo.Vertices.Add(new Vector3((float)vecs[i].X, (float)vecs[i].Y, (float)vecs[i].Z));
                vbo.Normals.Add(new Vector3((float)norms[i].X, (float)norms[i].Y, (float)norms[i].Z));
                vbo.TexCoords.Add(new Vector3((float)tcoord[i].X, (float)tcoord[i].Y, (float)TheClient.TBlock.TexList[(int)tcoord[i].Z].ResultantID));
                vbo.Indices.Add((uint)i);
                vbo.Colors.Add(new Vector4(1, 1, 1, 1));
                vbo.TCOLs.Add(TheClient.Rendering.AdaptColor(vbo.Vertices[i], tcol));
            }
            for (int i = 0; i < vecs.Count; i += 3)
            {
                int     basis   = i;
                Vector3 v1      = vbo.Vertices[basis];
                Vector3 dv1     = vbo.Vertices[basis + 1] - v1;
                Vector3 dv2     = vbo.Vertices[basis + 2] - v1;
                Vector3 t1      = vbo.TexCoords[basis];
                Vector3 dt1     = vbo.TexCoords[basis + 1] - t1;
                Vector3 dt2     = vbo.TexCoords[basis + 2] - t1;
                Vector3 tangent = (dv1 * dt2.Y - dv2 * dt1.Y) * 1f / (dt1.X * dt2.Y - dt1.Y * dt2.X);
                Vector3 normal  = vbo.Normals[basis];
                tangent = (tangent - normal * Vector3.Dot(normal, tangent)).Normalized();
                for (int x = 0; x < 3; x++)
                {
                    vbo.Tangents.Add(tangent);
                    vbo.THVs.Add(new Vector4(0, 0, 0, 0));
                    vbo.THWs.Add(new Vector4(0, 0, 0, 0));
                    vbo.THVs2.Add(new Vector4(0, 0, 0, 0));
                    vbo.THWs2.Add(new Vector4(0, 0, 0, 0));
                }
            }
            vbo.GenerateVBO();
        }
        private void AddTileToVBO(ChunkVBO vbo, ushort tile, int x, int y)
        {
            bool flipX = ((tile >> 10) & 1) == 1;
            bool flipY = ((tile >> 11) & 1) == 1;

            if (!vbo.VerticesUpdated)
            {
                vbo.Vertices.Add(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
            }
            if (!vbo.TexCoordsUpdated)
            {
                vbo.TexCoords.Add(0, (tile & 0x3ff) * TILE_SIZE, TILE_SIZE, TILE_SIZE, flipX, flipY);
            }
        }
 private void AddSelectIndices(ChunkVBO vbo)
 {
     // Make rectangle around the selected block, using the vertices
     // Line 1
     vbo.SelectIndices.Add(vbo.Vertices.Count);
     vbo.SelectIndices.Add(vbo.Vertices.Count + 1);
     // Line 2
     vbo.SelectIndices.Add(vbo.Vertices.Count + 1);
     vbo.SelectIndices.Add(vbo.Vertices.Count + 2);
     // Line 3
     vbo.SelectIndices.Add(vbo.Vertices.Count + 2);
     vbo.SelectIndices.Add(vbo.Vertices.Count + 3);
     // Line 4
     vbo.SelectIndices.Add(vbo.Vertices.Count + 3);
     vbo.SelectIndices.Add(vbo.Vertices.Count);
 }
        private void UpdateSelectedOOBVBO()
        {
            ChunkVBO vbo = selectedOOB;

            if (vbo.TexCoordsUpdated && vbo.VerticesUpdated)
            {
                return;
            }

            if (!vbo.VerticesUpdated)
            {
                vbo.Vertices.SetBuffer(new float[SelectedTiles.GetOOB().Count * 8]);
                vbo.SelectIndices.SetBuffer(new uint[SelectedTiles.GetOOB().Count * 8]);
            }

            if (!vbo.TexCoordsUpdated)
            {
                vbo.TexCoords.SetBuffer(new float[SelectedTiles.GetOOB().Count * 8]);
            }

            foreach (Point p in SelectedTiles.GetOOB())
            {
                if (!vbo.VerticesUpdated)
                {
                    AddSelectIndices(vbo);
                }
                AddTileToVBO(vbo, SelectedTilesValue[p], p.X, p.Y);
            }

            if (!vbo.VerticesUpdated)
            {
                vbo.Vertices.SetData();
                vbo.SelectIndices.SetData();
                vbo.VerticesUpdated = true;
            }
            if (!vbo.TexCoordsUpdated)
            {
                vbo.TexCoords.SetData();
                vbo.TexCoordsUpdated = true;
            }
        }
        public EditorLayer(SceneLayer layer)
        {
            this.Layer = layer;

            chunks = new ChunkVBO[DivideRoundUp(this.Layer.Height, TILES_CHUNK_SIZE)][];
            for (int i = 0; i < chunks.Length; ++i)
            {
                chunks[i] = new ChunkVBO[DivideRoundUp(this.Layer.Width, TILES_CHUNK_SIZE)];
                for (int j = 0; j < chunks[i].Length; ++j)
                {
                    // We can use the same buffers because we fill each chunk at a time
                    chunks[i][j]           = new ChunkVBO();
                    chunks[i][j].Vertices  = new Vertices(verticesBuffer);
                    chunks[i][j].TexCoords = new TexCoords(textCoordsBuffer, TILE_SIZE, TILE_SIZE * 0x400);
                }
            }

            selectedChunks = new ChunkVBO[DivideRoundUp(this.Layer.Height, TILES_CHUNK_SIZE)][];
            for (int i = 0; i < selectedChunks.Length; ++i)
            {
                selectedChunks[i] = new ChunkVBO[DivideRoundUp(this.Layer.Width, TILES_CHUNK_SIZE)];
                for (int j = 0; j < selectedChunks[i].Length; ++j)
                {
                    selectedChunks[i][j]               = new ChunkVBO();
                    selectedChunks[i][j].Vertices      = new Vertices(verticesBuffer);
                    selectedChunks[i][j].TexCoords     = new TexCoords(textCoordsBuffer, TILE_SIZE, TILE_SIZE * 0x400);
                    selectedChunks[i][j].SelectIndices = new Indices(indicesBuffer);
                }
            }

            selectedOOB               = new ChunkVBO();
            selectedOOB.TexCoords     = new TexCoords(null, TILE_SIZE, TILE_SIZE * 0x400);
            selectedOOB.Vertices      = new Vertices(null);
            selectedOOB.SelectIndices = new Indices(null);

            SelectedTiles      = new PointsMap(this.Layer.Width, this.Layer.Height);
            TempSelectionTiles = new PointsMap(this.Layer.Width, this.Layer.Height);
        }
        private void UpdateSelectedChunkVBO(int x, int y)
        {
            ChunkVBO vbo = selectedChunks[y][x];

            if (vbo.TexCoordsUpdated && vbo.VerticesUpdated)
            {
                return;
            }

            if (!vbo.VerticesUpdated)
            {
                vbo.Vertices.Reset();
                vbo.SelectIndices.Reset();
            }
            if (!vbo.TexCoordsUpdated)
            {
                vbo.TexCoords.Reset();
            }

            Rectangle rect = GetTilesChunkArea(x, y);

            foreach (Point p in SelectedTiles.GetChunkPoint(x, y))
            {
                if (!TempSelectionDeselect || !TempSelectionTiles.Contains(p))
                {
                    if (!vbo.VerticesUpdated)
                    {
                        AddSelectIndices(vbo);
                    }

                    if (SelectedTilesValue.ContainsKey(p))
                    {
                        AddTileToVBO(vbo, SelectedTilesValue[p], p.X, p.Y);
                    }
                    else // It is still in the original place
                    {
                        AddTileToVBO(vbo, Layer.Tiles[p.Y][p.X], p.X, p.Y);
                    }
                }
            }

            foreach (Point p in TempSelectionTiles.GetChunkPoint(x, y))
            {
                if (SelectedTiles.Contains(p))
                {
                    continue;
                }
                if (!vbo.VerticesUpdated)
                {
                    AddSelectIndices(vbo);
                }
                AddTileToVBO(vbo, Layer.Tiles[p.Y][p.X], p.X, p.Y);
            }

            if (!vbo.VerticesUpdated)
            {
                vbo.Vertices.UpdateData();
                vbo.SelectIndices.UpdateData();
                vbo.VerticesUpdated = true;
            }
            if (!vbo.TexCoordsUpdated)
            {
                vbo.TexCoords.UpdateData();
                vbo.TexCoordsUpdated = true;
            }
        }
        private void UpdateChunkVBO(int x, int y)
        {
            ChunkVBO vbo = chunks[y][x];

            if (vbo.TexCoordsUpdated && vbo.VerticesUpdated)
            {
                return;
            }

            if (!vbo.VerticesUpdated)
            {
                vbo.Vertices.Reset();
            }
            if (!vbo.TexCoordsUpdated)
            {
                vbo.TexCoords.Reset();
            }

            bool checkSelected = SelectedTiles.IsChunkUsed(x, y) || TempSelectionTiles.IsChunkUsed(x, y);

            Rectangle rect = GetTilesChunkArea(x, y);

            for (int ty = rect.Y; ty < rect.Y + rect.Height; ++ty)
            {
                for (int tx = rect.X; tx < rect.X + rect.Width; ++tx)
                {
                    if (checkSelected && TempSelectionDeselect && SelectedTiles.Contains(new Point(tx, ty)) && TempSelectionTiles.Contains(new Point(tx, ty)))
                    {
                        Point p = new Point(tx, ty);
                        if (SelectedTilesValue.ContainsKey(p))
                        {
                            AddTileToVBO(vbo, SelectedTilesValue[p], p.X, p.Y);
                        }
                        else // It is still in the original place
                        {
                            AddTileToVBO(vbo, Layer.Tiles[p.Y][p.X], p.X, p.Y);
                        }
                    }
                    else if (Layer.Tiles[ty][tx] != 0xffff)
                    {
                        if (!checkSelected || dragging)
                        {
                            AddTileToVBO(vbo, Layer.Tiles[ty][tx], tx, ty);
                        }
                        else
                        {
                            Point p = new Point(tx, ty);
                            if (SelectedTiles.Contains(p) || TempSelectionTiles.Contains(p))
                            {
                                if (SelectedTiles.Contains(p) && TempSelectionTiles.Contains(p) && TempSelectionDeselect)
                                {
                                    AddTileToVBO(vbo, Layer.Tiles[ty][tx], tx, ty);
                                }
                            }
                            else
                            {
                                AddTileToVBO(vbo, Layer.Tiles[ty][tx], tx, ty);
                            }
                        }
                    }
                }
            }

            if (!vbo.VerticesUpdated)
            {
                vbo.Vertices.UpdateData();
                vbo.VerticesUpdated = true;
            }
            if (!vbo.TexCoordsUpdated)
            {
                vbo.TexCoords.UpdateData();
                vbo.TexCoordsUpdated = true;
            }
        }
Ejemplo n.º 8
0
        // TODO: Make asyncable!
        public void SetupVBO()
        {
            List <OpenTK.Vector3> Vertices  = new List <OpenTK.Vector3>(XWidth * YWidth * ZWidth);
            List <OpenTK.Vector3> Normals   = new List <OpenTK.Vector3>(XWidth * YWidth * ZWidth);
            List <OpenTK.Vector3> TexCoords = new List <OpenTK.Vector3>(XWidth * YWidth * ZWidth);
            List <OpenTK.Vector4> Colrs     = new List <OpenTK.Vector4>(XWidth * YWidth * ZWidth);
            List <OpenTK.Vector4> TCOLs     = new List <OpenTK.Vector4>(XWidth * YWidth * ZWidth);
            List <OpenTK.Vector3> Tangs     = new List <OpenTK.Vector3>(XWidth * YWidth * ZWidth);

            for (int x = 0; x < XWidth; x++)
            {
                for (int y = 0; y < YWidth; y++)
                {
                    for (int z = 0; z < ZWidth; z++)
                    {
                        BlockInternal c = GetBlockAt(x, y, z);
                        if (((Material)c.BlockMaterial).RendersAtAll())
                        {
                            BlockInternal def = new BlockInternal(0, 0, 0, 0);
                            BlockInternal zp  = z + 1 < ZWidth?GetBlockAt(x, y, z + 1) : def;

                            BlockInternal zm = z > 0 ? GetBlockAt(x, y, z - 1) : def;
                            BlockInternal yp = y + 1 < YWidth?GetBlockAt(x, y + 1, z) : def;

                            BlockInternal ym = y > 0 ? GetBlockAt(x, y - 1, z) : def;
                            BlockInternal xp = x + 1 < XWidth?GetBlockAt(x + 1, y, z) : def;

                            BlockInternal xm  = x > 0 ? GetBlockAt(x - 1, y, z) : def;
                            bool          rAS = !((Material)c.BlockMaterial).GetCanRenderAgainstSelf();
                            bool          zps = (zp.IsOpaque() || (rAS && (zp.BlockMaterial == c.BlockMaterial))) && BlockShapeRegistry.BSD[zp.BlockData].OccupiesBOTTOM();
                            bool          zms = (zm.IsOpaque() || (rAS && (zm.BlockMaterial == c.BlockMaterial))) && BlockShapeRegistry.BSD[zm.BlockData].OccupiesTOP();
                            bool          xps = (xp.IsOpaque() || (rAS && (xp.BlockMaterial == c.BlockMaterial))) && BlockShapeRegistry.BSD[xp.BlockData].OccupiesXM();
                            bool          xms = (xm.IsOpaque() || (rAS && (xm.BlockMaterial == c.BlockMaterial))) && BlockShapeRegistry.BSD[xm.BlockData].OccupiesXP();
                            bool          yps = (yp.IsOpaque() || (rAS && (yp.BlockMaterial == c.BlockMaterial))) && BlockShapeRegistry.BSD[yp.BlockData].OccupiesYM();
                            bool          yms = (ym.IsOpaque() || (rAS && (ym.BlockMaterial == c.BlockMaterial))) && BlockShapeRegistry.BSD[ym.BlockData].OccupiesYP();
                            Vector3       pos = new Vector3(x, y, z);
                            List <BEPUutilities.Vector3> vecsi  = BlockShapeRegistry.BSD[c.BlockData].GetVertices(pos, xps, xms, yps, yms, zps, zms);
                            List <BEPUutilities.Vector3> normsi = BlockShapeRegistry.BSD[c.BlockData].GetNormals(pos, xps, xms, yps, yms, zps, zms);
                            List <BEPUutilities.Vector3> tci    = BlockShapeRegistry.BSD[c.BlockData].GetTCoords(pos, (Material)c.BlockMaterial, xps, xms, yps, yms, zps, zms);
                            for (int i = 0; i < tci.Count; i++)
                            {
                                tci[i] = new Vector3(tci[i].X, tci[i].Y, TheClient.TBlock.TexList[(int)tci[i].Z].ResultantID);
                            }
                            int vertcount = Vertices.Count;
                            for (int i = 0; i < vecsi.Count; i++)
                            {
                                // TODO: is PosMultiplier used correctly here?
                                OpenTK.Vector3 vt = new OpenTK.Vector3((float)vecsi[i].X, (float)vecsi[i].Y, (float)vecsi[i].Z);
                                Vertices.Add(vt);
                                OpenTK.Vector3 nt = new OpenTK.Vector3((float)normsi[i].X, (float)normsi[i].Y, (float)normsi[i].Z);
                                Normals.Add(nt);
                                TexCoords.Add(new OpenTK.Vector3((float)tci[i].X, (float)tci[i].Y, (float)tci[i].Z));
                                Colrs.Add(new OpenTK.Vector4(1, 1, 1, 1));
                                TCOLs.Add(TheClient.Rendering.AdaptColor(vt, Colors.ForByte(c.BlockPaint)));
                            }
                            for (int i = 0; i < vecsi.Count; i += 3)
                            {
                                int            basis   = vertcount + i;
                                OpenTK.Vector3 v1      = Vertices[basis];
                                OpenTK.Vector3 dv1     = Vertices[basis + 1] - v1;
                                OpenTK.Vector3 dv2     = Vertices[basis + 2] - v1;
                                OpenTK.Vector3 t1      = TexCoords[basis];
                                OpenTK.Vector3 dt1     = TexCoords[basis + 1] - t1;
                                OpenTK.Vector3 dt2     = TexCoords[basis + 2] - t1;
                                OpenTK.Vector3 tangent = (dv1 * dt2.Y - dv2 * dt1.Y) * 1f / (dt1.X * dt2.Y - dt1.Y * dt2.X);
                                OpenTK.Vector3 normal  = Normals[basis];
                                tangent = (tangent - normal * OpenTK.Vector3.Dot(normal, tangent)).Normalized();
                                Tangs.Add(tangent);
                                Tangs.Add(tangent);
                                Tangs.Add(tangent);
                            }
                            if (!c.IsOpaque() && BlockShapeRegistry.BSD[c.BlockData].BackTextureAllowed)
                            {
                                int tf = Colrs.Count - vecsi.Count;
                                for (int i = vecsi.Count - 1; i >= 0; i--)
                                {
                                    Vertices.Add(new OpenTK.Vector3((float)vecsi[i].X, (float)vecsi[i].Y, (float)vecsi[i].Z));
                                    int tx = tf + i;
                                    Colrs.Add(Colrs[tx]);
                                    TCOLs.Add(TCOLs[tx]);
                                    Normals.Add(new OpenTK.Vector3(-(float)normsi[i].X, -(float)normsi[i].Y, -(float)normsi[i].Z));
                                    TexCoords.Add(new OpenTK.Vector3((float)tci[i].X, (float)tci[i].Y, (float)tci[i].Z));
                                }
                            }
                        }
                    }
                }
            }
            if (vbo != null)
            {
                vbo.Destroy();
                vbo = null;
            }
            if (Vertices.Count == 0)
            {
                return;
            }
            vbo = new ChunkVBO()
            {
                THVs  = new List <OpenTK.Vector4>(),
                THWs  = new List <OpenTK.Vector4>(),
                THVs2 = new List <OpenTK.Vector4>(),
                THWs2 = new List <OpenTK.Vector4>()
            };
            List <uint> Indices = new List <uint>(Vertices.Count);

            for (uint i = 0; i < Vertices.Count; i++)
            {
                Indices.Add(i);
                // TODO: Actual values?
                vbo.THVs.Add(new OpenTK.Vector4(0, 0, 0, 0));
                vbo.THWs.Add(new OpenTK.Vector4(0, 0, 0, 0));
                vbo.THVs2.Add(new OpenTK.Vector4(0, 0, 0, 0));
                vbo.THWs2.Add(new OpenTK.Vector4(0, 0, 0, 0));
            }
            vbo.Vertices  = Vertices;
            vbo.Normals   = Normals;
            vbo.TexCoords = TexCoords;
            vbo.Colors    = Colrs;
            vbo.TCOLs     = TCOLs;
            vbo.Tangents  = Tangs;
            vbo.Indices   = Indices;
            vbo.GenerateVBO();
        }