Exemple #1
0
        public void SetVertices(Vector3f[] verts)
        {
            if (fVertexBufferObject != null)
            {
                fVertexBufferObject.Dispose();
                fVertexBufferObject = null;
            }


            // Write the vertex data to the buffer
            GCHandle dataPtr = GCHandle.Alloc(verts, GCHandleType.Pinned);
            int dataSize = Marshal.SizeOf(typeof(Vector3f)) * verts.Length;
            fVertexBufferObject = new VertexBufferObject(GI);
            fVertexBufferObject.Bind();
            fVertexBufferObject.Size = dataSize;
            
            try
            {
                fVertexBufferObject.Bind();
                fVertexBufferObject.Write(dataPtr.AddrOfPinnedObject(), 0, dataSize);
            }
            finally
            {
                fVertexBufferObject.Unbind();
                dataPtr.Free();
            }
       }
Exemple #2
0
 public void CreateIndexBuffer(int[] indices)
 {
     this._indexVbo = VertexBufferObject.Create(BufferTarget.ElementArrayBuffer);
     _indexVbo.Bind();
     _indexVbo.StoreData(indices);
     this._indexCount = indices.Length;
 }
Exemple #3
0
        private void Generate()
        {
            _shader = ShaderManager.GetOrCreateShader("slab_block", Shaders.slab_vertex, Shaders.slab_fragment);
            foreach (var face in BlockFaceMethods.All)
            {
                var vao = BlockFaceVertices.CreateVao(face, SlabHeight);
                vao.Bind();
                var vbo = new VertexBufferObject();
                vao.LinkBuffer(vbo);
                vbo.Bind(BufferTarget.ArrayBuffer);
                vbo.SetData(BufferTarget.ArrayBuffer, MaxFaces * InstanceFloatDataLength, BufferUsageHint.StreamDraw);
                var builder = new AttributePointerBuilder(vao, InstanceDataLength, 3);
                builder.AddPointer(3, true);
                builder.AddPointer(1, true);
                builder.AddPointer(1, true);
                builder.AddPointer(1, true);
                builder.AddPointer(1, true);
                builder.AddPointer(1, true);
                VertexBufferObject.Unbind(BufferTarget.ArrayBuffer);
                VertexArrayObject.Unbind();

                var i = (int)face;

                _vaos[i]        = vao;
                _dataBuffers[i] = vbo;
                var mapper = _mappers[i];
                mapper.Vao = vao;
                mapper.Vbo = vbo;
            }
        }
Exemple #4
0
            public void CreateAttribute(int attribute, float[] data, int attrSize)
            {
                VertexBufferObject dataVbo = VertexBufferObject.Create(BufferTarget.ArrayBuffer);

                dataVbo.Bind();
                dataVbo.StoreData(data);
                GL.VertexAttribPointer(attribute, attrSize, VertexAttribPointerType.Float, false, attrSize * BYTES_PER_FLOAT, 0);
                dataVbo.Unbind();
                _dataVbos.Add(dataVbo);
            }
Exemple #5
0
            public void CreateIntAttribute(int attribute, int[] data, int attrSize)
            {
                VertexBufferObject dataVbo = VertexBufferObject.Create(BufferTarget.ArrayBuffer);

                dataVbo.Bind();
                dataVbo.StoreData(data);
                GL.VertexAttribIPointer(attribute, attrSize, VertexAttribIntegerType.Int, attrSize * BYTES_PER_INT, System.IntPtr.Zero);

                dataVbo.Unbind();
                _dataVbos.Add(dataVbo);
            }
Exemple #6
0
        private static VertexArrayObject LoadVao()
        {
            var vao = new VertexArrayObject();
            var vbo = new VertexBufferObject();

            vao.Bind();
            vbo.Bind(BufferTarget.ArrayBuffer);
            vao.LinkBuffer(vbo);
            vbo.SetData(BufferTarget.ArrayBuffer, Vertices, BufferUsageHint.StaticDraw);
            var builder = new AttributePointerBuilder(vao, 3, 0);

            builder.AddPointer(3, false);
            VertexBufferObject.Unbind(BufferTarget.ArrayBuffer);
            VertexArrayObject.Unbind();
            return(vao);
        }
        public VertexArrayObject CreateAndSetupVertexArrayObject(VertexBufferObject<Vertex3V3N> vertexBuffer, VertexBufferObject<uint> elementBuffer)
        {
            var vertexArrayObject = _resourceFactory.CreateVertexArrayObject();

            vertexArrayObject.Bind();
            vertexBuffer.Bind();
            elementBuffer.Bind();

            var dummy = new Vertex3V3N();
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, dummy.SizeInBytes, dummy.PositionOffset);
            GL.EnableVertexAttribArray(0);

            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, dummy.SizeInBytes, dummy.NormalOffset);
            GL.EnableVertexAttribArray(1);

            vertexArrayObject.Unbind();
            vertexBuffer.Unbind();
            elementBuffer.Unbind();

            return vertexArrayObject;
        }
Exemple #8
0
        public void Render()
        {
            if (isValid < 0)
            {
                CheckValidity();
            }

            if (isValid > 0)
            {
                Log.WriteLine("Cannot render. Renderable is not valid.", LogType.Warning);
                return;
            }

            //if (shader.Uniforms.Count(uniform => uniform.Type == UniformType.Texture2D) != textures.Length) {
            //    Log.WriteLine("Cannot render. Texture count does not match uniform texture count in shader.");
            //    return;
            //}

            Clean();

            shader.Bind();
            shader.EnableVertexAttributes();

            this.uniformAssigner.AssignUniforms(this.shader, this.shaderUniformAssignmentHandler);

            vbo.Bind();
            shader.AssignVertexAttributePointers();
            vbo.Release();

            GLHandler.Instance.Render(ibo);

            foreach (Texture textureUniform in this.uniformAssigner.TextureUniforms)
            {
                textureUniform.Release();
            }

            shader.DisableVertexAttributes();
            shader.Release();
        }
        public VertexArrayObject CreateAndSetupVertexArrayObject(VertexBufferObject <Vertex3V3N> vertexBuffer, VertexBufferObject <uint> elementBuffer)
        {
            var vertexArrayObject = _resourceFactory.CreateVertexArrayObject();

            vertexArrayObject.Bind();
            vertexBuffer.Bind();
            elementBuffer.Bind();

            var dummy = new Vertex3V3N();

            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, dummy.SizeInBytes, dummy.PositionOffset);
            GL.EnableVertexAttribArray(0);

            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, dummy.SizeInBytes, dummy.NormalOffset);
            GL.EnableVertexAttribArray(1);

            vertexArrayObject.Unbind();
            vertexBuffer.Unbind();
            elementBuffer.Unbind();

            return(vertexArrayObject);
        }
Exemple #10
0
        private void Generate()
        {
            _shader = ShaderManager.GetOrCreateShader("cubic_block", Shaders.block_vertex, Shaders.block_fragment);
            _vao    = new VertexArrayObject(GenTransformedVertices(), Indices);
            _vao.Bind();
            _dataBuffer = new VertexBufferObject();
            _vao.LinkBuffer(_dataBuffer);
            _dataBuffer.Bind(BufferTarget.ArrayBuffer);
            _dataBuffer.SetData(BufferTarget.ArrayBuffer, MaxBlocks * InstanceFloatDataLength,
                                BufferUsageHint.StreamDraw);
            var builder = new AttributePointerBuilder(_vao, InstanceDataLength, 3);

            builder.AddPointer(3, true);
            builder.AddPointer(1, true);
            builder.AddPointer(1, true);
            builder.AddPointer(1, true);
            builder.AddPointer(1, true);
            VertexBufferObject.Unbind(BufferTarget.ArrayBuffer);
            VertexArrayObject.Unbind();

            _mapper.Vao = _vao;
            _mapper.Vbo = _dataBuffer;
        }
Exemple #11
0
        public override void Init()
        {
            var width  = RenderContext.Current.ScreenPixelSize.X;
            var height = RenderContext.Current.ScreenPixelSize.Y;

            GBuffer             = new FrameBuffer(width, height);
            GBuffer.ObjectLabel = nameof(GBuffer);
            //GBuffer.InitNormal();

            // R11fG11fB10f --> rgba16f
            // Warning: R11fG11fB10f has no sign bit!

            GPosition = new RendererTexture(nameof(GPosition), TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb16f, width, height, 0, PixelFormat.Rgb, PixelType.Float, IntPtr.Zero);
            GPosition.SetNearestFilter();
            GBuffer.DestinationTextures.Add(GPosition);
            GBuffer.BindTexture(GPosition, FramebufferAttachment.ColorAttachment0);

            GNormal = new RendererTexture(nameof(GNormal), TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb16f, width, height, 0, PixelFormat.Rgb, PixelType.Float, IntPtr.Zero);
            GNormal.SetNearestFilter();
            GBuffer.DestinationTextures.Add(GNormal);
            GBuffer.BindTexture(GNormal, FramebufferAttachment.ColorAttachment1);

            GAlbedoSpec = new RendererTexture(nameof(GAlbedoSpec), TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
            GAlbedoSpec.SetNearestFilter();
            GBuffer.DestinationTextures.Add(GAlbedoSpec);
            GBuffer.BindTexture(GAlbedoSpec, FramebufferAttachment.ColorAttachment2);

            GMaterial = new RendererTexture(nameof(GMaterial), TextureTarget.Texture2D, 0, PixelInternalFormat.R11fG11fB10f, width, height, 0, PixelFormat.Rgb, PixelType.Float, IntPtr.Zero);
            GMaterial.SetNearestFilter();
            GBuffer.DestinationTextures.Add(GMaterial);
            GBuffer.BindTexture(GMaterial, FramebufferAttachment.ColorAttachment3);

            GL.DrawBuffers(4, new DrawBuffersEnum[]
            {
                DrawBuffersEnum.ColorAttachment0,
                DrawBuffersEnum.ColorAttachment1,
                DrawBuffersEnum.ColorAttachment2,
                DrawBuffersEnum.ColorAttachment3,
            });

            // var rboDepth = new RenderBuffer(gBuffer, RenderbufferStorage.DepthComponent, FramebufferAttachment.DepthAttachment);
            // rboDepth.ObjectLabel = nameof(rboDepth);

            // Attach default Forward Depth Buffer to this Framebuffer, so both share the same depth informations.
            var fwPipe = RenderContext.Current.GetPipeline <ForwardRenderPipeline>();

            GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, fwPipe.FrameBuffer.RenderBuffer.Handle);

            GBuffer.Check();

            _DefLightShader = new RendererShader("Shaders/deferred-shading.vert", "Shaders/deferred-shading.frag", null, false);
            if (Renderer.Current.UseShadows)
            {
                _DefLightShader.SetDefine("USE_SHADOW");
            }
            _DefLightShader.Compile();
            _DefLightShader.SetInt("gPosition", 0);
            _DefLightShader.SetInt("gNormal", 1);
            _DefLightShader.SetInt("gAlbedoSpec", 2);
            _DefLightShader.SetInt("gMaterial", 3);

            vbo = new VertexBufferObject();
            vbo.Create();
            vbo.Bind();

            var layout = VertexLayoutDefinition.CreateDefinitionFromVertexStruct <VertexDataPos2UV>();

            vao = new VertexArrayObject(layout.BindToShader(_DefLightShader), vbo);
            vao.Create();

            vao.SetData(BufferData.Create(_vertices));
        }
Exemple #12
0
        public void SetTextureCoordinates(TextureCoordinates[] coords)
        {
            if (fTextureBufferObject != null)
            {
                fTextureBufferObject.Dispose();
                fTextureBufferObject = null;
            }


            // Write the vertex data to the buffer
            GCHandle dataPtr = GCHandle.Alloc(coords, GCHandleType.Pinned);
            int dataSize = Marshal.SizeOf(typeof(TextureCoordinates)) * coords.Length;
            fTextureBufferObject = new VertexBufferObject(GI);
            fTextureBufferObject.Bind();
            fTextureBufferObject.Size = dataSize;

            try
            {
                fTextureBufferObject.Bind();
                fTextureBufferObject.Write(dataPtr.AddrOfPinnedObject(), 0, dataSize);
            }
            finally
            {
                fTextureBufferObject.Unbind();
                dataPtr.Free();
            }
        }
Exemple #13
0
 public void Bind()
 {
     vertices.Bind();
     vertices.SetPointer();
 }
Exemple #14
0
        public void UpdateVertexBuffer(int PolygonGroupIndex, int UvChannelIndex, List <STGenericMesh> genericObjects, STGenericTextureMap textureMap)
        {
            Init();

            if (Points.Count > 0)
            {
                return;
            }

            if (genericObjects.Count == 0)
            {
                return;
            }

            foreach (var genericObject in genericObjects)
            {
                int divisions = 4;
                int lineWidth = 1;

                System.Drawing.Color uvColor   = Runtime.UVEditor.UVColor;
                System.Drawing.Color gridColor = System.Drawing.Color.Black;

                List <uint> f = new List <uint>();
                int         displayFaceSize = 0;
                if (genericObject.PolygonGroups.Count > 0)
                {
                    if (PolygonGroupIndex == -1)
                    {
                        foreach (var group in genericObject.PolygonGroups)
                        {
                            f.AddRange(group.Faces);
                            displayFaceSize += group.Faces.Count;
                        }
                    }
                    else
                    {
                        if (genericObject.PolygonGroups.Count > PolygonGroupIndex)
                        {
                            f = genericObject.PolygonGroups[PolygonGroupIndex].Faces;
                            displayFaceSize = genericObject.PolygonGroups[PolygonGroupIndex].Faces.Count;
                        }
                    }
                }

                if (genericObject.Vertices.Count == 0 ||
                    genericObject.Vertices[0].TexCoords.Length == 0)
                {
                    return;
                }

                for (int v = 0; v < displayFaceSize; v += 3)
                {
                    if (displayFaceSize < 3 || genericObject.Vertices.Count < 3)
                    {
                        return;
                    }

                    Vector2 v1 = new Vector2(0);
                    Vector2 v2 = new Vector2(0);
                    Vector2 v3 = new Vector2(0);

                    if (f.Count <= v + 2)
                    {
                        continue;
                    }

                    if (genericObject.Vertices.Count > f[v + 2])
                    {
                        v1 = genericObject.Vertices[(int)f[v]].TexCoords[UvChannelIndex];
                        v2 = genericObject.Vertices[(int)f[v + 1]].TexCoords[UvChannelIndex];
                        v3 = genericObject.Vertices[(int)f[v + 2]].TexCoords[UvChannelIndex];

                        v1 = new Vector2(v1.X, 1 - v1.Y);
                        v2 = new Vector2(v2.X, 1 - v2.Y);
                        v3 = new Vector2(v3.X, 1 - v3.Y);

                        DrawUVTriangleAndGrid(v1, v2, v3, divisions, uvColor, lineWidth, gridColor, textureMap);
                    }
                }
            }

            List <float> list = new List <float>();

            for (int i = 0; i < Points.Count; i++)
            {
                list.Add(Points[i].X);
                list.Add(Points[i].Y);
            }

            vao.Bind();

            float[] data = list.ToArray();
            GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * data.Length, data, BufferUsageHint.StaticDraw);
        }
 public void Update(Mesh3V3N mesh)
 {
     _vertexBuffer.Bind();
     _vertexBuffer.Data(mesh.Vertices, BufferUsageHint.StaticDraw);
     _vertexBuffer.Unbind();
 }
        private void SetupGeometryBuffers()
        {
            verts = new VertexBufferObject (VertexBufferType.ArrayBuffer, VertexBufferUsageHint.StaticDraw);
            indices = new VertexBufferObject (VertexBufferType.ElementArrayBuffer, VertexBufferUsageHint.StaticDraw);

            float[] points =
            {
                0.0f, 0.0f, 0.0f,
            };

            ushort[] idx = { 0 };

            verts.push_data (ref points);
            verts.Bind ();
            GL.VertexPointer (3, VertexPointerType.Float, 0, 0);

            indices.push_data (ref idx);
            indices.Bind();
        }