Esempio n. 1
0
        public GLBufferDynamic(GLBufferSettings settings, int elementSize, int startCapacity = 8192)
        {
            Settings    = settings;
            BufferSize  = startCapacity;
            ElementSize = elementSize;

            BufferId = GL.GenBuffer();
            Bind();
            GL.BufferData(Settings.Target, BufferSize, IntPtr.Zero, Settings.Hint);
            Unbind();
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="nginz.GLBuffer{T}"/> class.
        /// </summary>
        /// <param name="settings">The buffer settings.</param>
        /// <param name="buffer">The buffer initialization array.</param>
        public GLBuffer(GLBufferSettings settings, IList <T> buffer)
        {
            // Set the settings
            Settings = settings;

            // Set the buffer
            Buffer = buffer;

            // Calculate the buffer size
            BufferSize = Marshal.SizeOf(buffer [0]) * Buffer.Count;

            // Generate the buffer
            BufferId = GL.GenBuffer();

            // Bind the buffer
            Bind();

            // Set the buffer data
            GL.BufferData(Settings.Target, BufferSize, Buffer.ToArray(), Settings.Hint);

            // Unbind the data
            Unbind();
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="nginz.SpriteBatch"/> class.
        /// </summary>
        /// <param name = "game">The game.</param>
        /// <param name="shader">Shader.</param>
        public SpriteBatch(Game game, ShaderProgram shader = null)
        {
            // Set the game
            Game = game;

            // Compile predefined shaders if no shader program is given
            if (shader == null) {
                var vertShader = new VertexShader (vert_source);
                var fragShader = new FragmentShader (frag_source);
                Program = new ShaderProgram (vertShader, fragShader);
                Program.Link ();
            } else
                Program = shader;

            // Create the optimal buffer settings
            var settings = new GLBufferSettings {
                AttribSize = 0,
                Hint = BufferUsageHint.DynamicDraw,
                Normalized = false,
                Offset = 0,
                Target = BufferTarget.ArrayBuffer,
                Type = VertexAttribPointerType.Float
            };

            // Create temp variables for indices
            int indPtr = 0;
            var tempIndices = new uint[MAX_INDICES];

            // Fill temporary indices
            for (uint i = 0; i < MAX_VERTICES; i += 4) {

                // Triangle 1
                tempIndices [indPtr++] = i;
                tempIndices [indPtr++] = i + 1;
                tempIndices [indPtr++] = i + 2;

                // Triangle 2
                tempIndices [indPtr++] = i + 1;
                tempIndices [indPtr++] = i + 3;
                tempIndices [indPtr++] = i + 2;
            }

            // Set camera
            InternalCamera = new Camera (60f, game.Resolution, 0, 16, type: ProjectionType.Orthographic);

            // Set current texture
            CurrentTexture = Texture2D.Dot;

            // Generate array buffer object
            abo = GL.GenVertexArray ();

            // Create vertex buffer object
            vbo = new GLBufferDynamic<Vertex2D> (settings, Vertex2D.Size, MAX_VERTICES);

            // Create index buffer object
            ibo = new GLBuffer<uint> (GLBufferSettings.DynamicIndices, tempIndices);

            // Initialize vertices
            Vertices = new Vertex2D[MAX_VERTICES];

            framebuffer = new Framebuffer (FramebufferTarget.Framebuffer, Game.Resolution.Width, game.Resolution.Height)
                            .AttachTexture (FboAttachment.DiffuseAttachment, DrawBuffersEnum.ColorAttachment0, PixelInternalFormat.Rgb, PixelFormat.Rgb, PixelType.UnsignedByte, InterpolationMode.Linear);
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="nginz.SpriteBatch"/> class.
        /// </summary>
        /// <param name = "game">The game.</param>
        /// <param name="shader">Shader.</param>
        public SpriteBatch(Game game, ShaderProgram shader = null)
        {
            // Set the game
            Game = game;

            // Compile predefined shaders if no shader program is given
            if (shader == null)
            {
                var vertShader = new VertexShader(vert_source);
                var fragShader = new FragmentShader(frag_source);
                Program = new ShaderProgram(vertShader, fragShader);
                Program.Link();
            }
            else
            {
                Program = shader;
            }

            // Create the optimal buffer settings
            var settings = new GLBufferSettings {
                AttribSize = 0,
                Hint       = BufferUsageHint.DynamicDraw,
                Normalized = false,
                Offset     = 0,
                Target     = BufferTarget.ArrayBuffer,
                Type       = VertexAttribPointerType.Float
            };

            // Create temp variables for indices
            int indPtr      = 0;
            var tempIndices = new uint[MAX_INDICES];

            // Fill temporary indices
            for (uint i = 0; i < MAX_VERTICES; i += 4)
            {
                // Triangle 1
                tempIndices [indPtr++] = i;
                tempIndices [indPtr++] = i + 1;
                tempIndices [indPtr++] = i + 2;

                // Triangle 2
                tempIndices [indPtr++] = i + 1;
                tempIndices [indPtr++] = i + 3;
                tempIndices [indPtr++] = i + 2;
            }

            // Set camera
            InternalCamera = new Camera(60f, game.Resolution, 0, 16, type: ProjectionType.Orthographic);

            // Set current texture
            CurrentTexture = Texture2D.Dot;

            // Generate array buffer object
            abo = GL.GenVertexArray();

            // Create vertex buffer object
            vbo = new GLBufferDynamic <Vertex2D> (settings, Vertex2D.Size, MAX_VERTICES);

            // Create index buffer object
            ibo = new GLBuffer <uint> (GLBufferSettings.DynamicIndices, tempIndices);

            // Initialize vertices
            Vertices = new Vertex2D[MAX_VERTICES];

            framebuffer = new Framebuffer(FramebufferTarget.Framebuffer, Game.Resolution.Width, game.Resolution.Height)
                          .AttachTexture(FboAttachment.DiffuseAttachment, DrawBuffersEnum.ColorAttachment0, PixelInternalFormat.Rgb, PixelFormat.Rgb, PixelType.UnsignedByte, InterpolationMode.Linear);
        }
Esempio n. 5
0
 public static GLBuffer<uint> ToGLBuffer(this IList<uint> @this, GLBufferSettings? settings = null)
 {
     return new GLBuffer<uint> (settings ?? GLBufferSettings.StaticIndices, @this);
 }
Esempio n. 6
0
 public static GLBuffer<Vector2> ToGLBuffer(this IList<Vector2> @this, GLBufferSettings? settings = null)
 {
     return new GLBuffer<Vector2> (settings ?? GLBufferSettings.StaticDraw2FloatArray, @this);
 }