Ejemplo n.º 1
0
 public override void DestroyBody()
 {
     if (vbo != null)
     {
         vbo.Destroy();
     }
     base.DestroyBody();
 }
Ejemplo n.º 2
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();
        }