Exemplo n.º 1
0
        private void BuildItemVertexBuffer(int itemId, string name)
        {
            t.StartDrawingTiledQuads();
            Vector4 c1 = new Vector4(1, 1, 1, 1);

            t.ArrayIndex = indexMap[name];
            Vector3 normal = new Vector3(0, 0, 1);
            float   s      = 1f;

            t.AddVertexWithColor(new Vector4(0f, 0f, 0, 1.0f), c1, normal);
            t.AddVertexWithColor(new Vector4(0f, s, 0, 1.0f), c1, normal);
            t.AddVertexWithColor(new Vector4(s, s, 0, 1.0f), c1, normal);
            t.AddVertexWithColor(new Vector4(s, 0f, 0, 1.0f), c1, normal);
            itemVertexBuffers.Add(itemId, t.GetVertexBuffer());
        }
Exemplo n.º 2
0
        internal bool Render(bool forceCachedRendering)
        {
            // check if this is inside frustum
            RenewLease();
            bool rebuildOccured = false;


            if ((pass1VertexBuffer.Disposed || chunk.IsDirty) && !forceCachedRendering)
            {
                // pass1
                VertexBuffer.Dispose(ref pass1VertexBuffer);
                VertexBuffer.Dispose(ref pass2VertexBuffer);
                chunk.IsDirty = true;

                // rebuild vertices for cunk
                BlockRenderer blockRenderer = new BlockRenderer();
                PositionBlock startCorner;
                chunk.Position.GetMinCornerBlock(out startCorner);
                int                  minX        = startCorner.X;
                int                  minY        = startCorner.Y;
                int                  minZ        = startCorner.Z;
                int                  maxX        = startCorner.X + 16;
                int                  maxY        = startCorner.Y + 16;
                int                  maxZ        = startCorner.Z + 16;
                PositionBlock        blockPos    = new PositionBlock(0, 0, 0);
                List <PositionBlock> pass2Blocks = new List <PositionBlock>();
                t.StartDrawingTiledQuads();
                for (int x = 0; x < 16; x++)
                {
                    for (int y = 0; y < 16; y++)
                    {
                        for (int z = 0; z < 16; z++)
                        {
                            blockPos.X = x;
                            blockPos.Y = y;
                            blockPos.Z = z;
                            Block block = Block.FromId(chunk.SafeGetLocalBlock(x, y, z));
                            if (!block.IsTransparent)
                            {
                                blockRenderer.RenderBlock(blockPos, chunk);
                            }
                            else if (block.Id != BlockRepository.Air.Id)
                            {
                                pass2Blocks.Add(blockPos);
                            }
                        }
                    }
                }
                pass1VertexBuffer = t.GetVertexBuffer();

                // generate vertex buffer for pass2
                t.StartDrawingTiledQuadsPass2();
                foreach (PositionBlock pass2BlockPos in pass2Blocks)
                {
                    blockRenderer.RenderBlock(pass2BlockPos, chunk);
                }
                pass2VertexBuffer = t.GetVertexBuffer();

                chunk.IsDirty  = false;
                rebuildOccured = true;
            }

            // draw chunk if drawbuffer has been calculated
            t.ResetTransformation();
            if (pass1VertexBuffer.Vertices != null)
            {
                t.StartDrawingTiledQuads();
                t.Draw(pass1VertexBuffer);
            }
            // draw entities in chunk
            foreach (EntityStack stack in chunk.StackEntities)
            {
                int entitiesToDraw = stack.Count > 2 ? 2 : stack.Count;
                if (stack.AsBlock != null)
                {
                    t.Translate = stack.Position;
                    t.Scale     = new Vector3(0.5f, 0.5f, 0.5f);
                    t.Rotate    = new Vector3(stack.Pitch, stack.Yaw, 0);
                    for (int i = 0; i < entitiesToDraw; i++)
                    {
                        t.StartDrawingTiledQuads();
                        t.Draw(TileTextures.Instance.GetBlockVertexBuffer(stack.Id));
                        t.Translate += new Vector3(0.05f, 0.05f, 0.05f);
                    }
                }
                else if (stack.AsItem != null)
                {
                    t.Translate = stack.Position;
                    t.Scale     = new Vector3(0.5f, 0.5f, 0.5f);
                    for (int i = 0; i < entitiesToDraw; i++)
                    {
                        Player p = World.Instance.Player;
                        t.Rotate = new Vector3(-p.Pitch, p.Yaw + (float)Math.PI, 0);
                        t.StartDrawingTiledQuadsPass2();
                        t.Draw(TileTextures.Instance.GetItemVertexBuffer(stack.Id));
                        t.Translate += new Vector3(0.2f, 0.2f, 0.2f);
                    }
                }
            }
            return(rebuildOccured);
        }