예제 #1
0
        /// <summary>
        /// Loads the texture with a bitmap.
        /// </summary>
        /// <param name="bmp">Bitmap that contains the pixel data.</param>
        public void Load(Bitmap bmp)
        {
            if (bmp == null)
            {
                throw new ArgumentNullException("bmp");
            }
            Rectangle area     = new Rectangle(0, 0, bmp.Width, bmp.Height);
            var       lockData = bmp.LockBits(area, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            OpenGL.Invoke(() =>
            {
                this.Bind();

                GL.TexImage2D(
                    this.Target,
                    0,
                    this.IsSRGB ? PixelInternalFormat.Srgb8Alpha8 : PixelInternalFormat.Rgba,
                    bmp.Width, bmp.Height,
                    0,
                    OpenTK.Graphics.OpenGL4.PixelFormat.Bgra,
                    PixelType.UnsignedByte,
                    lockData.Scan0);
                bmp.UnlockBits(lockData);

                this.GenerateMipMaps();
            });
            this.Width  = bmp.Width;
            this.Height = bmp.Height;
        }
            public PPSTools()
            {
                OpenGL.Invoke(() =>
                {
                    this.vertexBuffer = new Buffer(BufferTarget.ArrayBuffer);
                    this.vertexBuffer.SetData <float>(BufferUsageHint.StaticDraw, new[]
                    {
                        -1.0f, -1.0f,
                        -1.0f, 1.0f,
                        1.0f, -1.0f,
                        1.0f, 1.0f
                    });

                    this.vao = new VertexArray();
                    this.vao.Bind();
                    this.vertexBuffer.Bind();

                    GL.EnableVertexAttribArray(0);
                    GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 0, 0);

                    VertexArray.Unbind();

                    this.frameBuffer = new FrameBuffer();
                });
            }
예제 #3
0
 /// <summary>
 /// Instantiates a new frame buffer.
 /// </summary>
 public FrameBuffer()
 {
     OpenGL.Invoke(() =>
     {
         GL.GenFramebuffers(1, out this.id);
     });
 }
예제 #4
0
 /// <summary>
 /// Instantiates a new render buffer.
 /// </summary>
 public RenderBuffer()
 {
     OpenGL.Invoke(() =>
     {
         GL.GenRenderbuffers(1, out this.id);
     });
 }
예제 #5
0
        private void LoadSide(Bitmap bmp, int offset, TextureTarget target)
        {
            Rectangle area = new Rectangle(0, offset * bmp.Width, bmp.Width, bmp.Width);

            /*
             * using(var tmp = new Bitmap(bmp.Width, bmp.Width))
             * {
             *      using(var g = Graphics.FromImage(tmp))
             *      {
             *              g.DrawImageUnscaled(bmp, new Point(0, offset * bmp.Width));
             *      }
             *      tmp.RotateFlip(RotateFlipType.RotateNoneFlipXY);
             *      using (var g = Graphics.FromImage(bmp))
             *      {
             *              g.DrawImageUnscaled(bmp, area);
             *      }
             * }
             */
            var lockData = bmp.LockBits(area, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            OpenGL.Invoke(() =>
            {
                this.Bind();
                GL.TexImage2D(
                    target,
                    0,
                    PixelInternalFormat.Rgba,
                    bmp.Width, bmp.Width,
                    0,
                    OpenTK.Graphics.OpenGL4.PixelFormat.Bgra,
                    PixelType.UnsignedByte,
                    lockData.Scan0);
            });
            bmp.UnlockBits(lockData);
        }
예제 #6
0
        private void Initialize()
        {
            OpenGL.Invoke(() =>
            {
                this.vertexBuffer = new Buffer(BufferTarget.ArrayBuffer);
                this.vertexBuffer.SetData <float>(BufferUsageHint.StaticDraw, new[]
                {
                    -1.0f, -1.0f,
                    -1.0f, 1.0f,
                    1.0f, -1.0f,
                    1.0f, 1.0f
                });

                this.vao = new VertexArray();
                this.vao.Bind();
                this.vertexBuffer.Bind();

                GL.EnableVertexAttribArray(0);
                GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 0, 0);

                VertexArray.Unbind();

                this.quadShader = new PostProcessingShader("void main() { fragment = texture(inputTexture, vec2(uv.x, uv.y)); }");
            });
        }
예제 #7
0
        /// <summary>
        /// Instantiates a new model mesh.
        /// </summary>
        /// <param name="indexes">The indexes of the mesh.</param>
        /// <param name="vertices">The vertices of the mesh.</param>
        public ModelMesh(uint[] indexes, Vertex[] vertices)
            : this()
        {
            if (indexes == null)
            {
                throw new ArgumentNullException("indexes");
            }
            if (vertices == null)
            {
                throw new ArgumentNullException("vertices");
            }

            this.indexes  = indexes;
            this.vertices = vertices;

            OpenGL.Invoke(() =>
            {
                this.vertexArray  = new VertexArray();
                this.vertexBuffer = new Buffer(BufferTarget.ArrayBuffer);
                this.indexBuffer  = new Buffer(BufferTarget.ElementArrayBuffer);

                this.vertexBuffer.SetData(BufferUsageHint.StaticDraw, this.vertices);
                this.indexBuffer.SetData(BufferUsageHint.StaticDraw, this.indexes);

                this.vertexArray.Bind();

                GL.EnableVertexAttribArray(0);
                GL.EnableVertexAttribArray(1);
                GL.EnableVertexAttribArray(2);
                GL.EnableVertexAttribArray(3);
                GL.EnableVertexAttribArray(4);
                GL.EnableVertexAttribArray(5);

                this.vertexBuffer.Bind();

                // Position
                GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vertex.Size, 0);

                // Normal
                GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, Vertex.Size, 12);

                // UV 1
                GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, Vertex.Size, 24);

                // UV 2
                GL.VertexAttribPointer(3, 2, VertexAttribPointerType.Float, false, Vertex.Size, 32);

                // Tangent
                GL.VertexAttribPointer(4, 3, VertexAttribPointerType.Float, false, Vertex.Size, 40);

                // BiTangent
                GL.VertexAttribPointer(5, 3, VertexAttribPointerType.Float, false, Vertex.Size, 52);

                // Bind element buffer
                this.indexBuffer.Bind();

                VertexArray.Unbind();
            });
        }
예제 #8
0
 /// <summary>
 /// Generates mip maps
 /// </summary>
 public void GenerateMipMaps()
 {
     OpenGL.Invoke(() =>
     {
         this.Bind();
         GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
     });
 }
예제 #9
0
 /// <summary>
 /// Sets the size of the render buffer.
 /// </summary>
 /// <param name="width">Width of the buffer.</param>
 /// <param name="height">Height of the buffer.</param>
 public void SetSize(int width, int height)
 {
     OpenGL.Invoke(() =>
     {
         this.Bind();
         GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent24, width, height);
         RenderBuffer.Unbind();
     });
     this.Width  = width;
     this.Height = height;
 }
예제 #10
0
        /// <summary>
        /// Instantiates a new texture
        /// </summary>
        /// <param name="target">Texture target</param>
        protected Texture(TextureTarget target)
        {
            this.isSRGB = Texture.UseSRGB;

            this.target = target;
            OpenGL.Invoke(() => { this.id = GL.GenTexture(); });

            this.WrapS = TextureWrapMode.Repeat;
            this.WrapT = TextureWrapMode.Repeat;
            this.WrapR = TextureWrapMode.Repeat;

            this.Filter = Filter.Nearest;
        }
예제 #11
0
 /// <summary>
 /// Disposes the texture
 /// </summary>
 /// <param name="disposing"></param>
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (this.id != 0)
         {
             OpenGL.Invoke(() =>
             {
                 GL.DeleteTexture(this.id);
             });
             this.id = 0;
         }
     }
 }
예제 #12
0
        /// <summary>
        /// Sets the render targets.
        /// </summary>
        /// <param name="depthBuffer">Depth buffer for this frame buffer</param>
        /// <param name="textureList">Array of textures that should be used as a render target.</param>
        public void SetTextures(RenderBuffer depthBuffer, params Texture2D[] textureList)
        {
            this.depthBuffer = depthBuffer;
            this.textures    = textureList;
            OpenGL.Invoke(() =>
            {
                // Setup depth buffer
                GL.BindFramebuffer(FramebufferTarget.Framebuffer, this.id);
                int depthBufferID = 0;
                if (this.depthBuffer != null)
                {
                    depthBufferID = this.depthBuffer.Id;
                }
                GL.FramebufferRenderbuffer(
                    FramebufferTarget.Framebuffer,
                    FramebufferAttachment.DepthAttachment,
                    RenderbufferTarget.Renderbuffer,
                    depthBufferID);

                if (this.textures != null)
                {
                    DrawBuffersEnum[] drawBuffers = new DrawBuffersEnum[this.textures.Length];
                    for (int i = 0; i < this.textures.Length; i++)
                    {
                        GL.FramebufferTexture(
                            FramebufferTarget.Framebuffer,
                            FramebufferAttachment.ColorAttachment0 + i,
                            this.textures[i].Id, 0);
                        drawBuffers[i] = DrawBuffersEnum.ColorAttachment0 + i;
                    }

                    GL.DrawBuffers(drawBuffers.Length, drawBuffers);
                }
                else
                {
                    GL.DrawBuffers(0, new DrawBuffersEnum[0]);
                }

                FramebufferErrorCode error;
                if ((error = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer)) != FramebufferErrorCode.FramebufferComplete)
                {
                    throw new InvalidOperationException("Failed to bind FrameBuffer: " + error);
                }
            });
        }
예제 #13
0
 /// <summary>
 /// Instantiates a new Texture2D
 /// </summary>
 /// <param name="width">Width of the texture</param>
 /// <param name="height">Height of the texture</param>
 /// <param name="internalFormat">Internal Format</param>
 /// <param name="pixelFormat">Pixel Format</param>
 /// <param name="pixelType">Pixel Type</param>
 public Texture2D(int width, int height, PixelInternalFormat internalFormat, OpenTK.Graphics.OpenGL4.PixelFormat pixelFormat, PixelType pixelType)
     : this()
 {
     OpenGL.Invoke(() =>
     {
         this.Bind();
         GL.TexImage2D(
             this.Target,
             0,
             internalFormat,
             width, height,
             0,
             pixelFormat,
             pixelType,
             IntPtr.Zero);
     });
     this.Width  = width;
     this.Height = height;
 }
예제 #14
0
        /// <summary>
        /// Loads the texture with RGBA8 data.
        /// </summary>
        public void Load(byte[] pixels, int width, int height)
        {
            OpenGL.Invoke(() =>
            {
                this.Bind();
                GL.TexImage2D(
                    this.Target,
                    0,
                    this.IsSRGB ? PixelInternalFormat.Srgb8Alpha8 : PixelInternalFormat.Rgba,
                    width, height,
                    0,
                    OpenTK.Graphics.OpenGL4.PixelFormat.Bgra,
                    PixelType.UnsignedByte,
                    pixels);
                this.GenerateMipMaps();
            });

            this.Width  = width;
            this.Height = height;
        }
예제 #15
0
        // +X, +Y, +Z, -X, -Y und -Z

        /// <summary>
        /// Loads the cube texture.
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="stream"></param>
        /// <param name="extensionHint"></param>
        protected override void Load(AssetLoadContext manager, System.IO.Stream stream, string extensionHint)
        {
            using (var bmp = new Bitmap(stream))
            {
                this.Bind();
                LoadSide(bmp, 0, TextureTarget.TextureCubeMapPositiveX);
                LoadSide(bmp, 1, TextureTarget.TextureCubeMapPositiveY);
                LoadSide(bmp, 2, TextureTarget.TextureCubeMapPositiveZ);

                LoadSide(bmp, 3, TextureTarget.TextureCubeMapNegativeX);
                LoadSide(bmp, 4, TextureTarget.TextureCubeMapNegativeY);
                LoadSide(bmp, 5, TextureTarget.TextureCubeMapNegativeZ);
            }
            OpenGL.Invoke(() =>
            {
                this.Bind();
                GL.TexParameter(this.Target, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                GL.TexParameter(this.Target, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            });
        }
예제 #16
0
 /// <summary>
 /// Loads the 2d texture.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="stream"></param>
 /// <param name="extensionHint"></param>
 protected override void Load(AssetLoadContext context, Stream stream, string extensionHint)
 {
     if (extensionHint == ".dds")
     {
         Log.WriteLine(LocalizedStrings.LoadingUncompressedBitmapFromStream);
         OpenGL.Invoke(() =>
         {
             if (LoadDDS(stream) != this.Target)
             {
                 throw new InvalidDataException("Could not load texture: Invalid texture format.");
             }
         });
     }
     else
     {
         Log.WriteLine(LocalizedStrings.LoadingUncompressedBitmapFromStream);
         using (var bmp = new Bitmap(stream))
             this.Load(bmp);
     }
     this.Filter = Filter.LinearMipMapped;
 }
예제 #17
0
        /// <summary>
        /// Disposes the mesh.
        /// </summary>
        public void Dispose()
        {
            OpenGL.Invoke(() =>
            {
                if (this.vertexBuffer != null)
                {
                    this.vertexBuffer.Dispose();
                }
                if (this.indexBuffer != null)
                {
                    this.indexBuffer.Dispose();
                }
                if (this.vertexArray != null)
                {
                    this.vertexArray.Dispose();
                }
            });

            this.vertexBuffer = null;
            this.indexBuffer  = null;
            this.vertexArray  = null;
        }
예제 #18
0
        /// <summary>
        /// Sets the data of this texture.
        /// </summary>
        /// <param name="pixels">Contains all Width*Height pixels.</param>
        /// <param name="format">Format of pixels</param>
        /// <param name="type">Type of pixels</param>
        public void SetData(byte[] pixels, PixelFormat format, PixelType type)
        {
            OpenGL.Invoke(() =>
            {
                this.Bind();
                GL.TexSubImage2D(
                    this.Target,
                    0,
                    0,
                    0,
                    this.Width,
                    this.Height,
                    format, type,
                    pixels);

                GL.GetTexImage(
                    this.Target,
                    0,
                    format,
                    type,
                    pixels);
                pixels[0] = pixels[0];
            });
        }
예제 #19
0
        private void Load(AssetLoadContext context, Scene scene)
        {
            Node[] nodes = new[] { scene.RootNode };
            if (scene.RootNode.ChildCount > 0)
            {
                nodes = scene.RootNode.Children;
            }

            List <ModelMesh> meshList = new List <ModelMesh>();

            for (int i = 0; i < nodes.Length; i++)
            {
                Node node = nodes[i];
                if (!node.HasMeshes)
                {
                    continue;
                }

                for (int j = 0; j < node.MeshCount; j++)
                {
                    Mesh     mesh     = scene.Meshes[node.MeshIndices[j]];
                    Vertex[] vertices = new Vertex[mesh.VertexCount];

                    Assimp.Material assimpMaterial  = null;
                    Texture2D       diffuseTexture  = null;
                    Texture2D       specularTexture = null;
                    Texture2D       emissiveTexture = null;
                    Texture2D       normalMap       = null;
                    if (scene.HasMaterials && mesh.MaterialIndex >= 0)
                    {
                        assimpMaterial  = scene.Materials[mesh.MaterialIndex];
                        diffuseTexture  = LoadTexture(context, assimpMaterial.GetTexture(Assimp.TextureType.Diffuse, 0));
                        specularTexture = LoadTexture(context, assimpMaterial.GetTexture(Assimp.TextureType.Specular, 0));
                        emissiveTexture = LoadTexture(context, assimpMaterial.GetTexture(Assimp.TextureType.Emissive, 0));

                        bool usedSRGB = Texture.UseSRGB;
                        Texture.UseSRGB = false;                         // We need "default" texture loading because normal maps are already linear space
                        normalMap       = LoadTexture(context, assimpMaterial.GetTexture(Assimp.TextureType.Normals, 0));

                        Texture.UseSRGB = usedSRGB;
                    }

                    Vector3D[] texcoord0 = null;
                    Vector3D[] texcoord1 = null;

                    if (mesh.HasTextureCoords(0))
                    {
                        texcoord0 = mesh.GetTextureCoords(0);
                    }
                    if (mesh.HasTextureCoords(1))
                    {
                        texcoord1 = mesh.GetTextureCoords(1);
                    }

                    uint[] indices = mesh.GetIndices();
                    for (int k = 0; k < mesh.VertexCount; k++)
                    {
                        Vertex vertex = new Vertex();

                        vertex.Position = new Vector3(
                            mesh.Vertices[k].X,
                            mesh.Vertices[k].Y,
                            mesh.Vertices[k].Z);
                        if (mesh.HasNormals)
                        {
                            vertex.Normal = new Vector3(
                                mesh.Normals[k].X,
                                mesh.Normals[k].Y,
                                mesh.Normals[k].Z);
                        }
                        if (texcoord0 != null)
                        {
                            vertex.UV = new Vector2(
                                texcoord0[k].X,
                                texcoord0[k].Y);
                        }
                        if (texcoord1 != null)
                        {
                            vertex.UV2 = new Vector2(
                                texcoord1[k].X,
                                texcoord1[k].Y);
                        }

                        if (mesh.HasTangentBasis)
                        {
                            vertex.Tangent = new Vector3(
                                mesh.Tangents[k].X,
                                mesh.Tangents[k].Y,
                                mesh.Tangents[k].Z);

                            vertex.BiTangent = new Vector3(
                                mesh.BiTangents[k].X,
                                mesh.BiTangents[k].Y,
                                mesh.BiTangents[k].Z);
                        }

                        vertices[k] = vertex;
                    }

                    OpenGL.Invoke(() =>
                    {
                        ModelMesh modelMesh       = new ModelMesh(indices, vertices);
                        modelMesh.DiffuseTexture  = diffuseTexture;
                        modelMesh.SpecularTexture = specularTexture;
                        modelMesh.EmissiveTexture = emissiveTexture;
                        modelMesh.NormalMap       = normalMap;
                        meshList.Add(modelMesh);
                    });
                }
            }
            this.meshes = meshList.ToArray();
        }