public void LoadBuffers(MyModelData modelData, string assetName = null)
        {
            System.Diagnostics.Debug.Assert(modelData.Sections.Count > 0, "Invalid object");
            if (modelData.Sections.Count == 0)
                return;

            // create index buffer
            {
                m_trianglesCount = modelData.Indices.Count / 3;
                m_Indices_16bit = new ushort[modelData.Indices.Count];

                for (int i = 0; i < modelData.Indices.Count; ++i)
                    m_Indices_16bit[i] = (ushort)modelData.Indices[i];

                m_indexBuffer = new IndexBuffer(MyRender.GraphicsDevice, m_Indices_16bit.Length * sizeof(short), Usage.WriteOnly, Pool.Default, true);
                m_indexBuffer.SetData(m_Indices_16bit);
                m_indexBuffer.Tag = this;
                m_indexBufferSize = m_Indices_16bit.Length * sizeof(short);

                SignResource(m_indexBuffer);
            }

            // create vertex buffer
            {
                m_verticesCount = modelData.Positions.Count;
                m_vertices      = new MyCompressedVertexNormal[m_verticesCount];
                var vertexArray = new MyVertexFormatPositionNormalTextureTangent[m_verticesCount];

                for (int i = 0; i < modelData.Positions.Count; ++i)
                {
                    vertexArray[i].Position = modelData.Positions[i];
                    vertexArray[i].Normal   = modelData.Normals[i];
                    vertexArray[i].Tangent  = modelData.Tangents[i];
                    vertexArray[i].TexCoord = modelData.TexCoords[i];

                    m_vertices[i] = new MyCompressedVertexNormal()
                    {
                        Position = vertexArray[i].PositionPacked,
                        Normal   = vertexArray[i].NormalPacked
                    };
                }

                m_vertexDeclaration = MyVertexFormatPositionNormalTextureTangent.VertexDeclaration;
                m_vertexStride      = MyVertexFormatPositionNormalTextureTangent.Stride;
                m_vertexBufferSize  = vertexArray.Length * m_vertexStride;
                m_vertexBuffer      = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                m_vertexBuffer.SetData(vertexArray);
                m_vertexBuffer.Tag = this;

                SignResource(m_vertexBuffer);
            }

            m_meshContainer.Clear();

            // apply materials here
            for (int s = 0; s < modelData.Sections.Count; ++s)
            {
                var mpi            = new MyMeshPartInfo();
                mpi.Technique      = m_drawTechnique;
                
                // Disabled, because it assert always when models are loaded before materials
                //System.Diagnostics.Debug.Assert(MyRenderModels.Materials.ContainsKey(modelData.Sections[s].MaterialName), "Mesh material not present!");
                
                if (MyRenderModels.Materials.ContainsKey(modelData.Sections[s].MaterialName))
                    mpi.m_MaterialDesc = MyRenderModels.Materials[modelData.Sections[s].MaterialName];

                var start = modelData.Sections[s].IndexStart;
                var end   = start + modelData.Sections[s].TriCount*3;

                for (int i = start; i < end; ++i)
                    mpi.m_indices.Add(modelData.Indices[i]);

                m_meshContainer.Add(new MyRenderMesh(mpi, null)
                {
                    IndexStart = modelData.Sections[s].IndexStart,
                    TriCount   = modelData.Sections[s].TriCount
                });
            }

            // store properties of this model
            {
                BoundingBox    = modelData.AABB;
                BoundingSphere = new BoundingSphere(modelData.AABB.Center, modelData.AABB.HalfExtents.Length());

                BoundingBoxSize     = BoundingBox.Size;
                BoundingBoxSizeHalf = BoundingBox.HalfExtents;

                ModelInfo = new MyModelInfo(m_trianglesCount, m_verticesCount, BoundingBoxSize);

                PreloadTextures(LoadingMode.Immediate);
                LoadState = Textures.LoadState.Loaded;

                m_loadedContent = true;
                m_loadedData    = true;
            }
        }
        void CreateVertexBuffer()
        {
            //  Create vertex buffer - vertex format type depends on draw technique

            switch (m_drawTechnique)
            {
                case MyMeshDrawTechnique.MESH:
                case MyMeshDrawTechnique.DECAL:
                case MyMeshDrawTechnique.HOLO:
                case MyMeshDrawTechnique.ALPHA_MASKED:
                case MyMeshDrawTechnique.SKINNED:
                    {
                        if (m_forLoadingTexCoords0 == null) throw new Exception("Model '" + m_assetName + "' doesn't have texture channel 0 specified, but this shader requires it");

                        if (m_forLoadingTexCoords0.Length == 0)
                        {
                            MyVertexFormatPosition[] vertexArray = new MyVertexFormatPosition[GetVerticesCount()];
                            for (int i = 0; i < GetVerticesCount(); i++)
                            {
                                vertexArray[i].Position = m_vertices[i].Position;
                            }

                            m_vertexDeclaration = MyVertexFormatPosition.VertexDeclaration;
                            m_vertexStride = MyVertexFormatPosition.Stride;
                            m_vertexBufferSize = vertexArray.Length * m_vertexStride;
                            m_vertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                            m_vertexBuffer.SetData(vertexArray);
                            m_vertexBuffer.Tag = this;
                        }
                        else
                            if (MyRenderConstants.RenderQualityProfile.UseNormals)
                            {
                                if (m_forLoadingTangents == null)
                                    throw new Exception("Model '" + m_assetName + "' doesn't have tangent vectors calculated, but this shader requires them");

                                if (BoneIndices.Length > 0)
                                {
                                    MyVertexFormatPositionNormalTextureTangentSkinned[] vertexArray = new MyVertexFormatPositionNormalTextureTangentSkinned[GetVerticesCount()];
                                    for (int i = 0; i < GetVerticesCount(); i++)
                                    {
                                        vertexArray[i].PositionPacked = m_vertices[i].Position;
                                        vertexArray[i].NormalPacked = m_vertices[i].Normal;
                                        vertexArray[i].TexCoordPacked = m_forLoadingTexCoords0[i];
                                        vertexArray[i].TangentPacked = m_forLoadingTangents[i];

                                        vertexArray[i].BoneIndices = new Byte4(BoneIndices[i].X, BoneIndices[i].Y, BoneIndices[i].Z, BoneIndices[i].W);
                                        vertexArray[i].BoneWeights = BoneWeights[i];

                                    }

                                    m_vertexDeclaration = MyVertexFormatPositionNormalTextureTangentSkinned.VertexDeclaration;
                                    m_vertexStride = MyVertexFormatPositionNormalTextureTangentSkinned.Stride;
                                    m_vertexBufferSize = vertexArray.Length * m_vertexStride;
                                    m_vertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                                    m_vertexBuffer.SetData(vertexArray);
                                    m_vertexBuffer.Tag = this;
                                }
                                else
                                {

                                    MyVertexFormatPositionNormalTextureTangent[] vertexArray = new MyVertexFormatPositionNormalTextureTangent[GetVerticesCount()];
                                    for (int i = 0; i < GetVerticesCount(); i++)
                                    {
                                        vertexArray[i].PositionPacked = m_vertices[i].Position;
                                        vertexArray[i].NormalPacked = m_vertices[i].Normal;
                                        vertexArray[i].TexCoordPacked = m_forLoadingTexCoords0[i];
                                        vertexArray[i].TangentPacked = m_forLoadingTangents[i];
                                    }

                                    m_vertexDeclaration = MyVertexFormatPositionNormalTextureTangent.VertexDeclaration;
                                    m_vertexStride = MyVertexFormatPositionNormalTextureTangent.Stride;
                                    m_vertexBufferSize = vertexArray.Length * m_vertexStride;
                                    m_vertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                                    m_vertexBuffer.SetData(vertexArray);
                                    m_vertexBuffer.Tag = this;
                                }

                            }
                            else
                            {

                                MyVertexFormatPositionNormalTexture[] vertexArray = new MyVertexFormatPositionNormalTexture[GetVerticesCount()];
                                for (int i = 0; i < GetVerticesCount(); i++)
                                {
                                    vertexArray[i].Position = GetVertexInt(i);
                                    vertexArray[i].Normal = GetVertexNormal(i);
                                    vertexArray[i].TexCoord = m_forLoadingTexCoords0[i].ToVector2();
                                }

                                m_vertexDeclaration = MyVertexFormatPositionNormalTexture.VertexDeclaration;
                                m_vertexStride = MyVertexFormatPositionNormalTexture.Stride;
                                m_vertexBufferSize = vertexArray.Length * m_vertexStride;
                                m_vertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                                m_vertexBuffer.SetData(vertexArray);
                                m_vertexBuffer.Tag = this;

                            }
                    }
                    break;

                case MyMeshDrawTechnique.VOXELS_DEBRIS:
                    {
                        MyVertexFormatPositionNormal[] vertexArray = new MyVertexFormatPositionNormal[GetVerticesCount()];
                        for (int i = 0; i < GetVerticesCount(); i++)
                        {
                            vertexArray[i].Position = GetVertexInt(i);
                            vertexArray[i].Normal = GetVertexNormal(i);
                        }

                        m_vertexDeclaration = MyVertexFormatPositionNormal.VertexDeclaration;
                        m_vertexStride = MyVertexFormatPositionNormal.Stride;
                        m_vertexBufferSize = vertexArray.Length * m_vertexStride;
                        m_vertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                        m_vertexBuffer.SetData(vertexArray);
                        m_vertexBuffer.Tag = this;
                    }
                    break;
                default:
                    {
                        throw new InvalidBranchException();
                    }
            }

            MyPerformanceCounter.PerAppLifetime.ModelVertexBuffersSize += m_vertexBufferSize;

            SignResource(m_vertexBuffer);
        }
        public override void LoadContent()
        {
            int vbSize;
            unsafe { vbSize = sizeof(MyVertexFormatPositionNormalTextureTangent) * m_vertices.Length; }
            int ibSize = sizeof(ushort) * m_indices.Length;

            m_vertexBuffer = new VertexBuffer(MyRender.Device, vbSize, Usage.Dynamic | Usage.WriteOnly, VertexFormat.None, Pool.Default);
            m_vertexBuffer.Tag = this;
            m_vertexBuffer.SetData(m_vertices, LockFlags.Discard);

            m_indexBuffer = new IndexBuffer(MyRender.Device, ibSize, Usage.Dynamic | Usage.WriteOnly, Pool.Default, true);
            m_indexBuffer.Tag = this;
            m_indexBuffer.SetData(m_indices, LockFlags.Discard);

            m_ropeMaterial.PreloadTexture();

            base.LoadContent();
        }
 public override void LoadContent()
 {
     if (m_cubeInstances != null)
     {
         // Copy whole array (even unused instances, it's not problem when called only on DeviceLost)
         m_instanceBuffer = new VertexBuffer(MyRender.GraphicsDevice, Stride * m_cubeInstances.Length, Usage.Dynamic | Usage.WriteOnly, VertexFormat.None, Pool.Default);
         m_instanceBuffer.SetData(m_cubeInstances, LockFlags.Discard);
     }
     else if(m_instances != null)
     {
         m_instanceBuffer = new VertexBuffer(MyRender.GraphicsDevice, Stride * m_instances.Length, Usage.Dynamic | Usage.WriteOnly, VertexFormat.None, Pool.Default);
         m_instanceBuffer.SetData(m_instances, LockFlags.Discard);
     }
 }
            public void LoadInDraw()
            {
                if (m_trianglesCount > 0)
                {
                    m_vertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, MyVertexFormatPositionTextureColor.Stride * m_vertices.Length, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                    m_vertexBuffer.SetData(m_vertices);
                    m_vertexBuffer.DebugName = "VoxelMapImpostors";
                    m_vertexBuffer.Tag = this;
                }

                //  Don't need anymore
                m_vertices = null;
            }
        /// <summary>
        /// Creates the VertexBuffer for the quad
        /// </summary>
        /// <param name="graphicsDevice">The GraphicsDevice to use</param>
        public void CreateFullScreenQuad(Device graphicsDevice)
        {
            // Create a vertex buffer for the quad, and fill it in
            m_vertexBuffer = new VertexBuffer(graphicsDevice, MyVertexFormatFullScreenQuad.Stride * 4, Usage.WriteOnly, VertexFormat.None, Pool.Default);
            m_vertexBuffer.DebugName = "FullScreenQuad";
            MyVertexFormatFullScreenQuad[] vbData = new MyVertexFormatFullScreenQuad[4];

            // Upper right
            vbData[0].Position = new Vector3(1, 1, 1);
            vbData[0].TexCoordAndCornerIndex = new Vector3(1, 0, 1);

            // Lower right
            vbData[1].Position = new Vector3(1, -1, 1);
            vbData[1].TexCoordAndCornerIndex = new Vector3(1, 1, 2);

            // Upper left
            vbData[2].Position = new Vector3(-1, 1, 1);
            vbData[2].TexCoordAndCornerIndex = new Vector3(0, 0, 0);

            // Lower left
            vbData[3].Position = new Vector3(-1, -1, 1);
            vbData[3].TexCoordAndCornerIndex = new Vector3(0, 1, 3);


            m_vertexBuffer.SetData(vbData);
        }
        //	Special method that loads data into GPU, and can be called only from Draw method, never from LoadContent or from background thread.
        //	Because that would lead to empty vertex/index buffers if they are filled/created while game is minimized (remember the issue - alt-tab during loading screen)
        static void LoadInDraw()
        {
            if (m_backgroundOrientationDirty)
                Static.UnloadContent();

            if (m_loaded) return;

            //  In fact it doesn't matter how large is cube, it will always look same as we are always in its middle
            //  I changed it from 1.0 to 100.0 only because will small length I had problems with near frustum plane and crazy aspect ratios.
            const float CUBE_LENGTH_HALF = 100;

            Vector3 shapeSize = Vector3.One * CUBE_LENGTH_HALF;
            Vector3 shapePosition = Vector3.Zero;

            MyVertexFormatPositionTexture3[] boxVertices = new MyVertexFormatPositionTexture3[36];

            Vector3 topLeftFront = shapePosition + new Vector3(-1.0f, 1.0f, -1.0f) * shapeSize;
            Vector3 bottomLeftFront = shapePosition + new Vector3(-1.0f, -1.0f, -1.0f) * shapeSize;
            Vector3 topRightFront = shapePosition + new Vector3(1.0f, 1.0f, -1.0f) * shapeSize;
            Vector3 bottomRightFront = shapePosition + new Vector3(1.0f, -1.0f, -1.0f) * shapeSize;
            Vector3 topLeftBack = shapePosition + new Vector3(-1.0f, 1.0f, 1.0f) * shapeSize;
            Vector3 topRightBack = shapePosition + new Vector3(1.0f, 1.0f, 1.0f) * shapeSize;
            Vector3 bottomLeftBack = shapePosition + new Vector3(-1.0f, -1.0f, 1.0f) * shapeSize;
            Vector3 bottomRightBack = shapePosition + new Vector3(1.0f, -1.0f, 1.0f) * shapeSize;

            Vector3 textureTopLeftFront = MyUtils.Normalize(topLeftFront);
            Vector3 textureBottomLeftFront = MyUtils.Normalize(bottomLeftFront);
            Vector3 textureTopRightFront = MyUtils.Normalize(topRightFront);
            Vector3 textureBottomRightFront = MyUtils.Normalize(bottomRightFront);
            Vector3 textureTopLeftBack = MyUtils.Normalize(topLeftBack);
            Vector3 textureTopRightBack = MyUtils.Normalize(topRightBack);
            Vector3 textureBottomLeftBack = MyUtils.Normalize(bottomLeftBack);
            Vector3 textureBottomRightBack = MyUtils.Normalize(bottomRightBack);
            textureTopLeftFront.Z *= -1;
            textureBottomLeftFront.Z *= -1;
            textureTopRightFront.Z *= -1;
            textureBottomRightFront.Z *= -1;
            textureTopLeftBack.Z *= -1;
            textureTopRightBack.Z *= -1;
            textureBottomLeftBack.Z *= -1;
            textureBottomRightBack.Z *= -1;

            // Front face.
            boxVertices[0] = new MyVertexFormatPositionTexture3(topLeftFront, textureTopLeftFront);
            boxVertices[1] = new MyVertexFormatPositionTexture3(bottomLeftFront, textureBottomLeftFront);
            boxVertices[2] = new MyVertexFormatPositionTexture3(topRightFront, textureTopRightFront);
            boxVertices[3] = new MyVertexFormatPositionTexture3(bottomLeftFront, textureBottomLeftFront);
            boxVertices[4] = new MyVertexFormatPositionTexture3(bottomRightFront, textureBottomRightFront);
            boxVertices[5] = new MyVertexFormatPositionTexture3(topRightFront, textureTopRightFront);

            // Back face.
            boxVertices[6] = new MyVertexFormatPositionTexture3(topLeftBack, textureTopLeftBack);
            boxVertices[7] = new MyVertexFormatPositionTexture3(topRightBack, textureTopRightBack);
            boxVertices[8] = new MyVertexFormatPositionTexture3(bottomLeftBack, textureBottomLeftBack);
            boxVertices[9] = new MyVertexFormatPositionTexture3(bottomLeftBack, textureBottomLeftBack);
            boxVertices[10] = new MyVertexFormatPositionTexture3(topRightBack, textureTopRightBack);
            boxVertices[11] = new MyVertexFormatPositionTexture3(bottomRightBack, textureBottomRightBack);

            // Top face.
            boxVertices[12] = new MyVertexFormatPositionTexture3(topLeftFront, textureTopLeftFront);
            boxVertices[13] = new MyVertexFormatPositionTexture3(topRightBack, textureTopRightBack);
            boxVertices[14] = new MyVertexFormatPositionTexture3(topLeftBack, textureTopLeftBack);
            boxVertices[15] = new MyVertexFormatPositionTexture3(topLeftFront, textureTopLeftFront);
            boxVertices[16] = new MyVertexFormatPositionTexture3(topRightFront, textureTopRightFront);
            boxVertices[17] = new MyVertexFormatPositionTexture3(topRightBack, textureTopRightBack);

            // Bottom face.
            boxVertices[18] = new MyVertexFormatPositionTexture3(bottomLeftFront, textureBottomLeftFront);
            boxVertices[19] = new MyVertexFormatPositionTexture3(bottomLeftBack, textureBottomLeftBack);
            boxVertices[20] = new MyVertexFormatPositionTexture3(bottomRightBack, textureBottomRightBack);
            boxVertices[21] = new MyVertexFormatPositionTexture3(bottomLeftFront, textureBottomLeftFront);
            boxVertices[22] = new MyVertexFormatPositionTexture3(bottomRightBack, textureBottomRightBack);
            boxVertices[23] = new MyVertexFormatPositionTexture3(bottomRightFront, textureBottomRightFront);

            // Left face.
            boxVertices[24] = new MyVertexFormatPositionTexture3(topLeftFront, textureTopLeftFront);
            boxVertices[25] = new MyVertexFormatPositionTexture3(bottomLeftBack, textureBottomLeftBack);
            boxVertices[26] = new MyVertexFormatPositionTexture3(bottomLeftFront, textureBottomLeftFront);
            boxVertices[27] = new MyVertexFormatPositionTexture3(topLeftBack, textureTopLeftBack);
            boxVertices[28] = new MyVertexFormatPositionTexture3(bottomLeftBack, textureBottomLeftBack);
            boxVertices[29] = new MyVertexFormatPositionTexture3(topLeftFront, textureTopLeftFront);

            // Right face.
            boxVertices[30] = new MyVertexFormatPositionTexture3(topRightFront, textureTopRightFront);
            boxVertices[31] = new MyVertexFormatPositionTexture3(bottomRightFront, textureBottomRightFront);
            boxVertices[32] = new MyVertexFormatPositionTexture3(bottomRightBack, textureBottomRightBack);
            boxVertices[33] = new MyVertexFormatPositionTexture3(topRightBack, textureTopRightBack);
            boxVertices[34] = new MyVertexFormatPositionTexture3(topRightFront, textureTopRightFront);
            boxVertices[35] = new MyVertexFormatPositionTexture3(bottomRightBack, textureBottomRightBack);
            
            // if we've loaded the cube from DDS, orient it towards the sun
            for (int i = 0; i < boxVertices.Length; i++)
            {
                boxVertices[i].Position = Vector3.Transform(boxVertices[i].Position, m_backgroundOrientation);
            }

            m_boxVertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, MyVertexFormatPositionTexture3.Stride * boxVertices.Length, Usage.WriteOnly, VertexFormat.None, Pool.Default);
            m_boxVertexBuffer.SetData(boxVertices);
            m_boxVertexBuffer.DebugName = "BackgroundCube";

            UpdateTexture();

            m_backgroundOrientationDirty = false;
            m_loaded = true;
        }