Ejemplo n.º 1
0
        public OpenGLVertexArray(GL gl)
        {
            _gl            = gl;
            _vertexBuffers = new List <VertexBuffer>();

            _rendererID = (RendererID)_gl.CreateVertexArray();
            Bind();
        }
Ejemplo n.º 2
0
        public unsafe OpenGLVertexBuffer(GL gl, uint count)
        {
            _gl         = gl;
            _rendererID = (RendererID)_gl.CreateBuffer();
            Bind();

            int size = Marshal.SizeOf <T>();

            _gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(size * count), null, GLEnum.DynamicDraw);
        }
 public NiPersistentSrcTextureRendererData()
 {
     palette       = null;
     numMipmaps    = (uint)0;
     bytesPerPixel = (uint)0;
     numPixels     = (uint)0;
     padNumPixels  = (uint)0;
     numFaces      = (uint)0;
     platform      = (PlatformID)0;
     renderer      = (RendererID)0;
 }
Ejemplo n.º 4
0
        public OpenGLVertexBuffer(GL gl, ReadOnlySpan <T> vertices)
        {
            _gl         = gl;
            _rendererID = (RendererID)_gl.CreateBuffer();
            Bind();

            int size = Marshal.SizeOf <T>();

            _gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(vertices.Length * size), vertices,
                           GLEnum.StaticDraw);
        }
Ejemplo n.º 5
0
        public OpenGLIndexBuffer(GL gl, ReadOnlySpan <T> indices)
        {
            _gl    = gl;
            _count = (uint)indices.Length;

            _rendererID = (RendererID)_gl.CreateBuffer();
            Bind();
            int size = Marshal.SizeOf <T>();

            _gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(_count * size), indices,
                           GLEnum.StaticDraw);
        }
Ejemplo n.º 6
0
        public override unsafe void Invalidate()
        {
            if (_rendererID != 0)
            {
                Dispose(true);
            }

            _rendererID = (RendererID)_gl.CreateFramebuffer();
            Bind();

            _gl.CreateTextures(TextureTarget.Texture2D, 1, out uint generatedId);
            _colorAttachmentRendererID = (RendererID)generatedId;
            _gl.BindTexture(TextureTarget.Texture2D, _colorAttachmentRendererID);
            _gl.TexImage2D(TextureTarget.Texture2D, 0, (int)GLEnum.Rgba8, _specs.Width, _specs.Height, 0, GLEnum.Rgba,
                           PixelType.UnsignedByte, null);

            _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
            _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);

            _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
                             (int)TextureMinFilter.Linear);
            _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,
                             (int)TextureMagFilter.Linear);

            _gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0,
                                     TextureTarget.Texture2D, _colorAttachmentRendererID, 0);

            _gl.CreateTextures(TextureTarget.Texture2D, 1, out generatedId);
            _depthAttachmentRendererID = (RendererID)generatedId;
            _gl.BindTexture(TextureTarget.Texture2D, _depthAttachmentRendererID);
            _gl.TexStorage2D(TextureTarget.Texture2D, 1, SizedInternalFormat.Depth24Stencil8, _specs.Width,
                             _specs.Height);
            _gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, GLEnum.DepthStencilAttachment,
                                     TextureTarget.Texture2D, _depthAttachmentRendererID, 0);

            Logger.Assert(_gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer) == GLEnum.FramebufferComplete,
                          "Framebuffer is incomplete");

            Unbind();
        }
Ejemplo n.º 7
0
        public OpenGLTexture2D(GL gl, uint width, uint height)
        {
            _gl            = gl;
            _path          = "Generated";
            _textureTarget = TextureTarget.Texture2D;
            _width         = width;
            _height        = height;

            _internalFormat = InternalFormat.Rgba8;
            _dataFormat     = PixelFormat.Rgba;

            _gl.CreateTextures(_textureTarget, 1, out uint generatedId);
            _rendererID = (RendererID)generatedId;

            _gl.TextureStorage2D(_rendererID, 1, (GLEnum)_internalFormat, _width, _height);
            Bind();

            _gl.TexParameter(_textureTarget, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
            _gl.TexParameter(_textureTarget, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);

            _gl.TexParameter(_textureTarget, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            _gl.TexParameter(_textureTarget, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
        }
Ejemplo n.º 8
0
        public OpenGLShader(GL gl, string vertexSource, string fragmentSource)
        {
            _gl = gl;
            uint?vertexShader = CreateShader(ShaderType.VertexShader, vertexSource);

            if (!vertexShader.HasValue)
            {
                return;
            }

            uint?fragmentShader = CreateShader(ShaderType.FragmentShader, fragmentSource);

            if (!fragmentShader.HasValue)
            {
                return;
            }

            _rendererID = (RendererID)_gl.CreateProgram();
            _gl.AttachShader(_rendererID, vertexShader.Value);
            _gl.AttachShader(_rendererID, fragmentShader.Value);

            bool result = LinkProgram(_rendererID);

            if (!result)
            {
                _gl.DeleteShader(vertexShader.Value);
                _gl.DeleteShader(fragmentShader.Value);
                return;
            }

            _gl.DetachShader(_rendererID, vertexShader.Value);
            _gl.DetachShader(_rendererID, fragmentShader.Value);
            _gl.DeleteShader(vertexShader.Value);
            _gl.DeleteShader(fragmentShader.Value);

            GetUniforms();
        }
Ejemplo n.º 9
0
        public unsafe OpenGLTexture2D(GL gl, string path)
        {
            _gl            = gl;
            _path          = path;
            _textureTarget = TextureTarget.Texture2D;
            Image <Rgba32> img = (Image <Rgba32>)Image.Load(_path);

            img.Mutate(x => x.Flip(FlipMode.Vertical));
            _width  = (uint)img.Width;
            _height = (uint)img.Height;
            bool withAlpha = img.Metadata.GetPngMetadata().ColorType == PngColorType.RgbWithAlpha;

            // TODO: Figure out every possible combination with SixLabors api and create mappings
            _internalFormat = withAlpha ? InternalFormat.Rgba8 : InternalFormat.Rgb8;
            _dataFormat     = withAlpha ? PixelFormat.Rgba : PixelFormat.Rgb;

            fixed(void *data = &MemoryMarshal.GetReference(img.GetPixelRowSpan(0)))
            {
                _gl.CreateTextures(_textureTarget, 1, out uint generatedId);
                _rendererID = (RendererID)generatedId;

                _gl.TextureStorage2D(_rendererID, 1, (GLEnum)_internalFormat, _width, _height);
                Bind();

                _gl.TexParameter(_textureTarget, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
                _gl.TexParameter(_textureTarget, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);

                _gl.TexParameter(_textureTarget, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                _gl.TexParameter(_textureTarget, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);

                _gl.TextureSubImage2D(_rendererID, 0, 0, 0, _width, _height, _dataFormat, PixelType.UnsignedByte, data);
                _gl.GenerateMipmap(_textureTarget);
            }

            img.Dispose();
        }