Ejemplo n.º 1
0
        public void AddVertexBuffer(VertexBufferLayoutKind[] vblk, VertexBuffer vb)
        {
            if (!m_PrepareMode)
            {
                GL.BindVertexArray(ID);
            }

            if (vb.Created)
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, vb.ID);

                for (int i = 0; i < vblk.Length; i++)
                {
                    GL.EnableVertexAttribArray(vblk[i].AttributeID);
                    GL.VertexAttribPointer(vblk[i].AttributeID, vblk[i].AttributeSize, (VertexAttribPointerType)vblk[i].AttributeType,
                        vblk[i].Normalized, vblk[i].Stride, (IntPtr)vblk[i].Offset);
                }

                VertexBufferLayoutKind[] layout = new VertexBufferLayoutKind[vblk.Length];
                Array.Copy(vblk, layout, layout.Length);

                _LayoutElements.Add(layout);

                _InternalBuffers.Add(vb);
                vb.SetUseCount(vb.InternalUseCount + 1);
            }

            if(!m_PrepareMode)
            {
                GL.BindVertexArray(0);
            }
        }
Ejemplo n.º 2
0
        public List<VertexBufferLayoutKind[]> GetLayoutElements()
        {
            List<VertexBufferLayoutKind[]> list = new List<VertexBufferLayoutKind[]>();

            foreach(VertexBufferLayoutKind[] lk in _LayoutElements)
            {
                VertexBufferLayoutKind[] temp = new VertexBufferLayoutKind[lk.Length];
                Array.Copy(lk, temp, temp.Length);

                list.Add(temp);
            }

            return list;
        }
Ejemplo n.º 3
0
        internal VertexBufferArray(string name, int id, VertexBufferLayoutKind[] vblk, VertexBuffer vb, IndexBuffer ib = null) : base(name, id, GraphicsResourceType.VertexBufferArray)
        {
            _LayoutElements = new List<VertexBufferLayoutKind[]>();
            _InternalBuffers = new List<VertexBuffer>();

            VertexBufferLayoutKind[] layout = new VertexBufferLayoutKind[vblk.Length];
            Array.Copy(vblk, layout, layout.Length);

            _LayoutElements.Add(layout);

            _InternalBuffers.Add(vb);
            vb.SetUseCount(vb.InternalUseCount + 1);

            _InternalOptionalIndexBuffer = ib;
            if(ib != null)
                _InternalOptionalIndexBuffer.SetUseCount(_InternalOptionalIndexBuffer.InternalUseCount + 1);
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
0
 public virtual void UnPrepareInstanced(RendererContext rc, VertexBufferLayoutKind[] vblks){}
Ejemplo n.º 6
0
        public override bool Init(RendererContext rc)
        {
            CoreScene.RCActionInitSceneObject iso = new CoreScene.RCActionInitSceneObject(this, rc);

            if (rc.Application.ManagedThreadId == System.Threading.Thread.CurrentThread.ManagedThreadId)
            {
                ParticleVBO = rc.CreateVertexBuffer(IntPtr.Zero, 40 * (int)ParticleCount, RendererBufferUsage.StreamDraw, "ParticleVBO_" + DateTime.Now.Ticks);
                if (ParticleVBO == null)
                    return false;

                VertexBufferLayoutKind[] vblk = new VertexBufferLayoutKind[4];
                vblk[0] = new VertexBufferLayoutKind();
                vblk[0].AttributeID = 0;
                vblk[0].AttributeSize = 3;
                vblk[0].AttributeType = RendererVertexAttribType.Float;
                vblk[0].Normalized = false;
                vblk[0].Offset = 0;
                vblk[0].Stride = sizeof(float) * 10;

                vblk[1] = new VertexBufferLayoutKind();
                vblk[1].AttributeID = 1;
                vblk[1].AttributeSize = 4;
                vblk[1].AttributeType = RendererVertexAttribType.Float;
                vblk[1].Normalized = false;
                vblk[1].Offset = 12;
                vblk[1].Stride = sizeof(float) * 10;

                vblk[2] = new VertexBufferLayoutKind();
                vblk[2].AttributeID = 2;
                vblk[2].AttributeSize = 2;
                vblk[2].AttributeType = RendererVertexAttribType.Float;
                vblk[2].Normalized = false;
                vblk[2].Offset = 28;
                vblk[2].Stride = sizeof(float) * 10;

                vblk[3] = new VertexBufferLayoutKind();
                vblk[3].AttributeID = 3;
                vblk[3].AttributeSize = 1;
                vblk[3].AttributeType = RendererVertexAttribType.Float;
                vblk[3].Normalized = false;
                vblk[3].Offset = 36;
                vblk[3].Stride = sizeof(float) * 10;

                ParticleVAO = rc.CreateVertexBufferArray(ParticleVBO, vblk, "ParticleVAO_" + DateTime.Now.Ticks);
                if (ParticleVAO == null)
                {
                    rc.DeleteGraphicsResourceAsync(ParticleVBO);
                    return false;
                }

                //Load BasicEffect
                long ticks = DateTime.Now.Ticks;

                ParticleEffect = rc.CreateEffect("ParticleEffect_" + ticks);
                ParticleEffect.VertexProgram = rc.CreateShaderProgramFromFile("ParticleEffect_VertexProgram_" + ticks, ShaderType.VertexShader, "lib/Renderer/Effects/BasicParticleEffect/vertex_shader.vs");
                ParticleEffect.PixelProgram = rc.CreateShaderProgramFromFile("ParticleEffect_PixelProgram_" + ticks, ShaderType.PixelShader, "lib/Renderer/Effects/BasicParticleEffect/pixel_shader.ps");
                ParticleEffect.GeometryProgram = rc.CreateShaderProgramFromFile("ParticleEffect_GeometryProgram" + ticks, ShaderType.GeometryShader, "lib/Renderer/Effects/BasicParticleEffect/geometry_shader.gs");

                BillboardTexture = rc.CreateTexture2D("BillboardTexture_" + ticks, true, "Content/Particles/particle_03.png");
                BillboardTexture.Sampler.MagnificationFilter = MagnificationFilter.InterpolateLinear;
                BillboardTexture.Sampler.MinificationFilter = MinificationFilter.InterpolateLinearAndInterpolateMipmapLinear;
            }
            else
                rc.AddRCActionJob(iso);

            IsInitialized = true;

            return true;
        }