Example #1
0
        internal Mesh(RendererCore rc, string name, int matidx, int[] indices, Vector3[] positions, Vector3[] normals,
            Vector3[] tangents, Vector3[] bitangents, List<Vector3>[] texcoords, List<Color4>[] colors, PrimitiveType type)
        {
            if (rc == null || indices == null || positions == null || name == null)
            {
                Logger.Log.AddLogEntry(LogLevel.Fatal, "Renderer.Mesh", FreezingArcher.Core.Status.BadArgument);
                throw new Exception();
            }
                
            m_MaterialIndex = matidx;
            m_PrimitiveType = type;

            //Init buffers

            long ticks = DateTime.Now.Ticks;

            m_VertexTexCoords = null;
            m_VertexColors = null;

            this.vertices = positions;
            this.indices = indices;

            m_Indices = rc.CreateIndexBuffer(indices, indices.Length * 4, RendererBufferUsage.StaticDraw, name + "_Indices_"+ticks);
            m_VertexPosition = rc.CreateVertexBuffer(positions, positions.Length * 3 * 4, RendererBufferUsage.StaticDraw, name + "_Positions_"+ticks);

            if (normals == null)
            {
                m_VertexNormal = new VertexBuffer();
  
                //TODO: Generate simple normals!
            }
            else
                m_VertexNormal = rc.CreateVertexBuffer(normals, normals.Length * 3 * 4, RendererBufferUsage.StreamDraw, name + "_Normals_"+ticks);

            if (tangents == null || tangents.Length == 0)
            {
                //TODO: generate tangents
                Vector3[] gen_tangents = new Vector3[positions.Length];

                for (int i = 0; i < gen_tangents.Length; i++)
                {
                    Vector3 c1 = Vector3.Cross(normals[i], Vector3.UnitZ);
                    Vector3 c2 = Vector3.Cross(normals[i], Vector3.UnitY);

                    if (c1.Length > c2.Length)
                        gen_tangents[i] = c1;
                    else
                        gen_tangents[i] = c2;

                    gen_tangents[i] = Vector3.Normalize(gen_tangents[i]);
                }

                tangents = gen_tangents;
            }

            m_VertexTangent = rc.CreateVertexBuffer(tangents, tangents.Length * 3 * 4, RendererBufferUsage.StaticDraw, name + "_Tangents_"+ticks);

            if (bitangents == null || bitangents.Length == 0)
            {
                //TODO: generate bitangents
                Vector3[] gen_bitangents = new Vector3[positions.Length];

                for (int i = 0; i < gen_bitangents.Length; i++)
                    gen_bitangents[i] = Vector3.Normalize(Vector3.Cross(normals[i], tangents[i]));

                bitangents = gen_bitangents;
            }

            m_VertexBiTangent = rc.CreateVertexBuffer(bitangents, bitangents.Length * 3 * 4, RendererBufferUsage.StaticDraw, name + "_BiNormals_"+ticks);

            if (texcoords != null && texcoords.Length > 0)
            {
                int count = 0;
                //check, which arrays are empty
                foreach (List<Vector3> texcs in texcoords)
                    if (texcs.Count > 0)
                        count++;

                m_VertexTexCoords = new VertexBuffer[count];

                for (int i = 0; i < count; i++)
                {
                    if (texcoords[i].Count > 0)
                    {
                        m_VertexTexCoords[i] = 
                        rc.CreateVertexBuffer(texcoords[i].ToArray(), texcoords[i].ToArray().Length * 3 * 4, RendererBufferUsage.StaticDraw, name + "_TexCoord" + i + "_"+ticks);
                    }
                }

            }

            if (colors != null && colors.Length > 0)
            {
                m_VertexColors = new VertexBuffer[colors.Length];

                for (int i = 0; i < colors.Length; i++)
                    m_VertexColors[i] = 
                        rc.CreateVertexBuffer(colors[i].ToArray(), colors[i].ToArray().Length * 4 * 4, RendererBufferUsage.StaticDraw, name + "_Colors" + i + "_"+ticks);
            }



            //Now, put everything into the VBA :'(
            VertexBufferLayoutKind[] vblk = new VertexBufferLayoutKind[1];
            vblk[0].AttributeID = 0;
            vblk[0].AttributeSize = 3;
            vblk[0].AttributeType = RendererVertexAttribType.Float;
            vblk[0].Normalized = false;
            vblk[0].Offset = 0;
            vblk[0].Stride = 3 * 4;

            m_VertexBufferArray = rc.CreateVertexBufferArray(m_VertexPosition, vblk, name + "_VertexBufferArray_"+ticks, m_Indices);

            m_VertexBufferArray.BeginPrepare();

            //Normals
            vblk[0].AttributeID = 1;
            vblk[0].AttributeSize = 3;
            vblk[0].AttributeType = RendererVertexAttribType.Float;
            vblk[0].Normalized = false;
            vblk[0].Offset = 0;
            vblk[0].Stride = 3 * 4;

            m_VertexBufferArray.AddVertexBuffer(vblk, m_VertexNormal);

            //Tangents
            vblk[0].AttributeID = 2;
            vblk[0].AttributeSize = 3;
            vblk[0].AttributeType = RendererVertexAttribType.Float;
            vblk[0].Normalized = false;
            vblk[0].Offset = 0;
            vblk[0].Stride = 3 * 4;

            m_VertexBufferArray.AddVertexBuffer(vblk, m_VertexTangent);

            //BiTangents
            vblk[0].AttributeID = 3;
            vblk[0].AttributeSize = 3;
            vblk[0].AttributeType = RendererVertexAttribType.Float;
            vblk[0].Normalized = false;
            vblk[0].Offset = 0;
            vblk[0].Stride = 3 * 4;

            m_VertexBufferArray.AddVertexBuffer(vblk, m_VertexTangent);


            //Textures
            uint foo_bla_counter = 0;

            if (m_VertexTexCoords != null)
            {
                foreach (VertexBuffer vb in m_VertexTexCoords)
                {
                    if (foo_bla_counter < 3)
                    {
                        vblk[0].AttributeID = (uint)4 + foo_bla_counter;
                        vblk[0].AttributeSize = 3;
                        vblk[0].AttributeType = RendererVertexAttribType.Float;
                        vblk[0].Normalized = false;
                        vblk[0].Offset = 0;
                        vblk[0].Stride = 3 * 4;

                        m_VertexBufferArray.AddVertexBuffer(vblk, vb);

                        foo_bla_counter++;
                    }
                }
            }

            /*
            //Colors
            foo_bla_counter = 0;

            if (m_VertexColors != null)
            {
                foreach (VertexBuffer vb in m_VertexColors)
                {
                    if (foo_bla_counter < 3)
                    {
                        vblk[0].AttributeID = (uint)7 + foo_bla_counter;
                        vblk[0].AttributeSize = 4;
                        vblk[0].AttributeType = RendererVertexAttribType.Float;
                        vblk[0].Normalized = false;
                        vblk[0].Offset = 0;
                        vblk[0].Stride = 4 * 4;

                        m_VertexBufferArray.AddVertexBuffer(vblk, vb);

                        foo_bla_counter++;
                    }
                }
            }*/

            m_VertexBufferArray.EndPrepare();
        }
Example #2
0
        public unsafe bool Init(RendererCore rc)
        {
            m_VertexBufferLayoutKind[0].AttributeID = rc.BasicEffect.LocationInPosition;
            m_VertexBufferLayoutKind[0].AttributeSize = 3;
            m_VertexBufferLayoutKind[0].AttributeType = RendererVertexAttribType.Float;
            m_VertexBufferLayoutKind[0].Normalized = false;
            m_VertexBufferLayoutKind[0].Offset = 0;
            m_VertexBufferLayoutKind[0].Stride = CubeVertex.SIZE;

            m_VertexBufferLayoutKind[1].AttributeID = rc.BasicEffect.LocationInNormal;
            m_VertexBufferLayoutKind[1].AttributeSize = 3;
            m_VertexBufferLayoutKind[1].AttributeType = RendererVertexAttribType.Float;
            m_VertexBufferLayoutKind[1].Normalized = false;
            m_VertexBufferLayoutKind[1].Offset = sizeof(Vector3);
            m_VertexBufferLayoutKind[1].Stride = CubeVertex.SIZE;

            m_VertexBufferLayoutKind[2].AttributeID = rc.BasicEffect.LocationInTexCoord1;
            m_VertexBufferLayoutKind[2].AttributeSize = 3;
            m_VertexBufferLayoutKind[2].AttributeType = RendererVertexAttribType.Float;
            m_VertexBufferLayoutKind[2].Normalized = false;
            m_VertexBufferLayoutKind[2].Offset = sizeof(Vector3) + sizeof(Vector3);
            m_VertexBufferLayoutKind[2].Stride = CubeVertex.SIZE;

            m_VertexBufferLayoutKind[3].AttributeID = rc.BasicEffect.LocationInColor;
            m_VertexBufferLayoutKind[3].AttributeSize = 4;
            m_VertexBufferLayoutKind[3].AttributeType = RendererVertexAttribType.Float;
            m_VertexBufferLayoutKind[3].Normalized = false;
            m_VertexBufferLayoutKind[3].Offset = sizeof(Vector3) + sizeof(Vector3) + sizeof(Vector3);
            m_VertexBufferLayoutKind[3].Stride = CubeVertex.SIZE;

            Int64 next = System.DateTime.Now.Ticks;

            m_VertexBuffer = rc.CreateVertexBuffer<CubeVertex>(m_Vertices, m_Vertices.Length * CubeVertex.SIZE, RendererBufferUsage.StaticDraw, "CubeVertexVertexBuffer_" + next);
            m_VertexBufferArray = rc.CreateVertexBufferArray(m_VertexBuffer, m_VertexBufferLayoutKind, "CubeVertexVertexBufferArray_" + next);

            return true;
        }