Beispiel #1
0
        public void Draw()
        {
            if (state == DrawStates.None)
            {
                return;
            }

            if ((state & DrawStates.Vertex) == DrawStates.Vertex)
            {
                VertexBuffer.Bind(BufferTarget.ArrayBuffer);
                VertexBuffer.UseVertexPointer(vertexSize, VertexStride, VertexOffset);
            }

            if ((state & DrawStates.TexCoord) == DrawStates.TexCoord)
            {
                TexCoordBuffer.Bind(BufferTarget.ArrayBuffer);
                TexCoordBuffer.UseTexCoordPointer(texCoordSize, TexCoordStride, TexCoordOffset);
            }

            if ((state & DrawStates.Normal) == DrawStates.Normal)
            {
                NormalBuffer.Bind(BufferTarget.ArrayBuffer);
                NormalBuffer.UseNormalPointer(NormalStride, NormalOffset);
            }

            //TODO this needs to become static/part of some graphics context class.
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            IndexBuffer.Bind(BufferTarget.ElementArrayBuffer);
            GL.DrawElements(DrawMode, IndexBuffer.Length, (DrawElementsType)IndexBuffer.DataType, 0);
            IndexBuffer.Unbind(BufferTarget.ElementArrayBuffer);
        }
Beispiel #2
0
        protected void Dispose(bool disposing)
        {
            if (mDisposed)
            {
                return;
            }

            if (disposing)
            {
                PositionBuffer?.Dispose();
                NormalBuffer?.Dispose();
                TangentBuffer?.Dispose();
                TexCoord0Buffer?.Dispose();
                TexCoord1Buffer?.Dispose();
                Color0Buffer?.Dispose();

                foreach (var subMesh in SubMeshes)
                {
                    subMesh.Dispose();
                }
            }

            GL.DeleteVertexArray(VertexArrayId);
            GL.Finish();

            mDisposed = true;
        }
Beispiel #3
0
        public BufferPointer GetBufferRenderer(string bufferName, string varNameInShader)
        {
            if (bufferName == strPosition)
            {
                using (var buffer = new PositionBuffer(varNameInShader))
                {
                    buffer.Alloc(positions.Count);
                    unsafe
                    {
                        var array = (vec3 *)buffer.FirstElement();
                        for (int i = 0; i < positions.Count; i++)
                        {
                            array[i] = positions[i];
                        }
                    }

                    return(buffer.GetBufferPtr());
                }
            }
            else if (bufferName == strColor)
            {
                using (var buffer = new ColorBuffer(varNameInShader))
                {
                    buffer.Alloc(normals.Count);
                    unsafe
                    {
                        var array = (vec3 *)buffer.FirstElement();
                        for (int i = 0; i < normals.Count; i++)
                        {
                            array[i] = normals[i];
                        }
                    }

                    return(buffer.GetBufferPtr());
                }
            }
            else if (bufferName == strNormal)
            {
                using (var buffer = new NormalBuffer(varNameInShader))
                {
                    buffer.Alloc(normals.Count);
                    unsafe
                    {
                        var array = (vec3 *)buffer.FirstElement();
                        for (int i = 0; i < normals.Count; i++)
                        {
                            array[i] = normals[i];
                        }
                    }

                    return(buffer.GetBufferPtr());
                }
            }
            else
            {
                return(null);
            }
        }
Beispiel #4
0
        public PropertyBufferPtr GetProperty(string bufferName, string varNameInShader)
        {
            if (bufferName == strPosition)
            {
                using (var buffer = new PositionBuffer(varNameInShader))
                {
                    buffer.Alloc(positions.Length);
                    unsafe
                    {
                        var array = (vec3 *)buffer.FirstElement();
                        for (int i = 0; i < positions.Length; i++)
                        {
                            array[i] = positions[i];
                        }
                    }

                    return(buffer.GetBufferPtr() as PropertyBufferPtr);
                }
            }
            else if (bufferName == strColor)
            {
                using (var buffer = new ColorBuffer(varNameInShader))
                {
                    buffer.Alloc(colors.Length);
                    unsafe
                    {
                        var array = (vec3 *)buffer.FirstElement();
                        for (int i = 0; i < colors.Length; i++)
                        {
                            array[i] = colors[i];
                        }
                    }

                    return(buffer.GetBufferPtr() as PropertyBufferPtr);
                }
            }
            else if (bufferName == strNormal)
            {
                using (var buffer = new NormalBuffer(varNameInShader))
                {
                    buffer.Alloc(normals.Length);
                    unsafe
                    {
                        var array = (vec3 *)buffer.FirstElement();
                        for (int i = 0; i < normals.Length; i++)
                        {
                            array[i] = normals[i];
                        }
                    }

                    return(buffer.GetBufferPtr() as PropertyBufferPtr);
                }
            }
            else
            {
                return(null);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Updates the buffers in GPU memory.
        /// </summary>
        /// <param name="gl">The GL.</param>
        public void UpdateGeometry(OpenGL gl)
        {
            var verts = Vertices.SelectMany(v => v.to_array()).ToArray();
            var norms = Normals.SelectMany(v => v.to_array()).ToArray();

            VertexBuffer.BindBuffer(gl);                                                      // gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VertexBufferId.Value);
            VertexBuffer.SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, verts, Usage, 3); // gl.BufferData(OpenGL.GL_ARRAY_BUFFER, verts, (uint)Usage);
            NormalBuffer.BindBuffer(gl);                                                      // gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, NormalBufferId.Value);
            NormalBuffer.SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, norms, Usage, 3); // gl.BufferData(OpenGL.GL_ARRAY_BUFFER, norms, (uint)Usage);
        }
Beispiel #6
0
    void UpdateMesh()
    {
        Tribuffer.SetCounterValue(0);

        // common
        marchingCS.SetInt("GridRes", GridRes);
        marchingCS.SetFloat("Time", Time.time);
        marchingCS.SetFloat("NoiseInterval", NoiseInterval);
        marchingCS.SetFloat("IsoLevel", IsoLevel);
        marchingCS.SetBool("EnableSmooth", EnableSmooth);
        marchingCS.SetVector("CenterPos", CenterPos);
        marchingCS.SetFloat("GridW", GridW);

        kernel = marchingCS.FindKernel("UpdateVoxel");
        marchingCS.SetTexture(kernel, "VoxelTexture", VoxelTexture);
        marchingCS.Dispatch(kernel, GridRes, GridRes, GridRes);

        kernel = marchingCS.FindKernel("CalculateTriangle");
        marchingCS.SetTexture(kernel, "VoxelTexture", VoxelTexture);
        marchingCS.SetBuffer(kernel, "Tribuffer", Tribuffer);
        marchingCS.Dispatch(kernel, GridRes / 8, GridRes / 8, GridRes / 8);

        ComputeBuffer.CopyCount(Tribuffer, CountBuffer, 0);
        int[] countArr = { 0 };
        CountBuffer.GetData(countArr);
        int count = countArr[0]; // the actual num

        if (count != 0)
        {
            kernel = marchingCS.FindKernel("ExtractData");
            marchingCS.SetInt("TriangleCount", count);
            marchingCS.SetBuffer(kernel, "NewTriBuffer", Tribuffer);
            marchingCS.SetBuffer(kernel, "PosBuffer", PosBuffer);
            marchingCS.SetBuffer(kernel, "NormalBuffer", NormalBuffer);
            marchingCS.SetBuffer(kernel, "IndexBuffer", IndexBuffer);
            marchingCS.Dispatch(kernel, (int)Mathf.Ceil(count / 10f), 1, 1);
        }

        Vector3[] posArr = new Vector3[count * 3];
        PosBuffer.GetData(posArr, 0, 0, count * 3);
        Vector3[] normalArr = new Vector3[count * 3];
        NormalBuffer.GetData(normalArr, 0, 0, count * 3);


        int[] indexArr = new int[count * 3];
        IndexBuffer.GetData(indexArr, 0, 0, count * 3);

        mesh.Clear();
        mesh.vertices  = posArr;
        mesh.normals   = normalArr;
        mesh.triangles = indexArr;
    }
Beispiel #7
0
 private void OnDisable()
 {
     Tribuffer.Release();
     Tribuffer.Dispose();
     PosBuffer.Release();
     PosBuffer.Dispose();
     NormalBuffer.Release();
     NormalBuffer.Dispose();
     IndexBuffer.Release();
     IndexBuffer.Dispose();
     CountBuffer.Release();
     CountBuffer.Dispose();
 }
Beispiel #8
0
        public PropertyBufferPtr GetProperty(string bufferName, string varNameInShader)
        {
            if (bufferName == strPosition)
            {
                using (var positionBuffer = new PositionBuffer(varNameInShader))
                {
                    positionBuffer.Alloc(1);
                    unsafe
                    {
                        var positionArray = (CubePosition *)positionBuffer.FirstElement();
                        positionArray[0] = this.position;
                    }

                    return(positionBuffer.GetBufferPtr() as PropertyBufferPtr);
                }
            }
            else if (bufferName == strColor)
            {
                using (var colorBuffer = new ColorBuffer(varNameInShader))
                {
                    colorBuffer.Alloc(1);
                    unsafe
                    {
                        var colorArray = (CubeColor *)colorBuffer.FirstElement();
                        colorArray[0] = this.color;
                    }

                    return(colorBuffer.GetBufferPtr() as PropertyBufferPtr);
                }
            }
            else if (bufferName == strNormal)
            {
                using (var normalBuffer = new NormalBuffer(varNameInShader))
                {
                    normalBuffer.Alloc(1);
                    unsafe
                    {
                        var normalArray = (CubeNormal *)normalBuffer.FirstElement();
                        normalArray[0] = this.normal;
                    }

                    return(normalBuffer.GetBufferPtr() as PropertyBufferPtr);
                }
            }
            else
            {
                return(null);
            }
        }
Beispiel #9
0
        private bool mDisposed = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!mDisposed)
            {
                if (disposing)
                {
                    PositionBuffer.Dispose();
                    NormalBuffer?.Dispose();
                    TextureCoordinateChannel0Buffer?.Dispose();
                    ElementBuffer.Dispose();
                    GL.DeleteVertexArray(Id);
                }

                mDisposed = true;
            }
        }
Beispiel #10
0
        VertexBuffers.BufferPointer IModel.GetNormalBufferRenderer(string varNameInShader)
        {
            using (var buffer = new NormalBuffer(varNameInShader))
            {
                buffer.Alloc(this.normals.Length);
                unsafe
                {
                    vec3 *array = (vec3 *)buffer.FirstElement();
                    for (int i = 0; i < this.normals.Length; i++)
                    {
                        array[i] = this.normals[i];
                    }
                }

                return(buffer.GetRenderer());
            }
        }
Beispiel #11
0
        protected void Dispose(bool disposing)
        {
            if (disposing)
            {
                PositionBuffer?.Dispose();
                NormalBuffer?.Dispose();
                TangentBuffer?.Dispose();
                TexCoordBuffer?.Dispose();
                TexCoord2Buffer?.Dispose();
                ColorBuffer?.Dispose();

                foreach (var indexTable in IndexTables)
                {
                    indexTable.Dispose();
                }
            }

            GL.DeleteVertexArray(VertexArrayID);
        }
Beispiel #12
0
        public void Unload()
        {
            if (VertexBuffer != null)
            {
                VertexBuffer.Unload();
            }

            if (TexCoordBuffer != null)
            {
                VertexBuffer.Unload();
            }

            if (NormalBuffer != null)
            {
                NormalBuffer.Unload();
            }

            if (IndexBuffer != null)
            {
                IndexBuffer.Unload();
            }
        }
        public void Update(float[] vertices, uint[] indexes, float[] colors, float[] normals, float[] lighting, int vertexCount)
        {
            Bind();

            ElementBuffer.Bind();
            ElementBuffer.SetData(sizeof(uint) * indexes.Length, indexes);

            VertexBuffer.Bind();
            VertexBuffer.SetData(sizeof(float) * vertices.Length, vertices);

            ColorBuffer.Bind();
            ColorBuffer.SetData(sizeof(float) * colors.Length, colors);

            NormalBuffer.Bind();
            NormalBuffer.SetData(sizeof(float) * normals.Length, normals);

            LightingBuffer.Bind();
            LightingBuffer.SetData(sizeof(float) * lighting.Length, lighting);

            VertexCount = vertexCount;

            Unbind();
        }
Beispiel #14
0
 public void Dispose()
 {
     VertexBuffer.Dispose();
     NormalBuffer.Dispose();
     IndexBuffer.Dispose();
 }