Esempio n. 1
0
        public void GenVBO()
        {
            vbo = new VBO();
            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 <OpenTK.Vector3>();
            vbo.Normals   = new List <OpenTK.Vector3>();
            vbo.TexCoords = new List <OpenTK.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>();
            System.Drawing.Color tcol = Voxalia.Shared.Colors.ForByte(Paint);
            for (int i = 0; i < vecs.Count; i++)
            {
                vbo.Vertices.Add(new OpenTK.Vector3((float)vecs[i].X, (float)vecs[i].Y, (float)vecs[i].Z));
                vbo.Normals.Add(new OpenTK.Vector3((float)norms[i].X, (float)norms[i].Y, (float)norms[i].Z));
                vbo.TexCoords.Add(new OpenTK.Vector3((float)tcoord[i].X, (float)tcoord[i].Y, (float)tcoord[i].Z));
                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;
                OpenTK.Vector3 v1      = vbo.Vertices[basis];
                OpenTK.Vector3 dv1     = vbo.Vertices[basis + 1] - v1;
                OpenTK.Vector3 dv2     = vbo.Vertices[basis + 2] - v1;
                OpenTK.Vector3 t1      = vbo.TexCoords[basis];
                OpenTK.Vector3 dt1     = vbo.TexCoords[basis + 1] - t1;
                OpenTK.Vector3 dt2     = vbo.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  = vbo.Normals[basis];
                tangent = (tangent - normal * OpenTK.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.GenerateVBO();
        }
 public override void Recalculate()
 {
     Position = ((Maxes - Mins) / 2) + Mins;
     PrimaryEditor.ContextView.Control.MakeCurrent();
     if (MyVBO != null)
     {
         MyVBO.Destroy();
     }
     MyVBO = new VBO();
     MyVBO.Prepare();
     MyVBO.Tex = PrimaryEditor.ContextView.Textures.GetTexture(Texture);
     MyVBO.AddSide(new Location(0, 0, 1), Coords[0]);
     MyVBO.AddSide(new Location(0, 0, -1), Coords[1]);
     MyVBO.AddSide(new Location(1, 0, 0), Coords[2]);
     MyVBO.AddSide(new Location(-1, 0, 0), Coords[3]);
     MyVBO.AddSide(new Location(0, 1, 0), Coords[4]);
     MyVBO.AddSide(new Location(0, -1, 0), Coords[5]);
     MyVBO.GenerateVBO();
 }
Esempio n. 3
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);
                            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 VBO();
            vbo.THVs = new List <OpenTK.Vector4>();
            vbo.THWs = new List <OpenTK.Vector4>();
            List <uint> Indices = new List <uint>(Vertices.Count);

            for (uint i = 0; i < Vertices.Count; i++)
            {
                Indices.Add(i);
                vbo.THVs.Add(new OpenTK.Vector4(0, 0, 0, 0));
                vbo.THWs.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();
        }