Esempio n. 1
0
        public void SetData(Bitmap bitmap)
        {
            Bind();

            Size = bitmap.Size;

            var data = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadOnly,
                System.Drawing.Imaging.PixelFormat.Format32bppArgb
                );

            GL.TexImage2D(
                TextureTarget.Texture2D,
                0,
                PixelInternalFormat.Rgba,
                bitmap.Width,
                bitmap.Height,
                0,
                OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
                PixelType.UnsignedByte,
                data.Scan0
                );
            GLDebug.CheckErrors();

            bitmap.UnlockBits(data);
        }
Esempio n. 2
0
        public void DrawOne()
        {
            if (!Ready)
            {
                return;
            }

            VAO.Bind();
            VBO.Bind();
            GL.DrawArrays(PrimitiveType.Triangles, 0, Vertices.Count);
            GLDebug.CheckErrors();
            VAO.Unbind();
            VBO.Unbind();
        }
Esempio n. 3
0
 public static void Init()
 {
     Shader.Init();
     DefaultProgram = new ShaderProgram(Shader.DefaultVertex, Shader.DefaultFragment);
     while (DefaultProgram.GetUniformLocation("instancePosition") == -1 && GraphicsSettings.MaxBatchInstances > 0)
     {
         GLDebug.IgnoreError();
         GraphicsSettings.MaxBatchInstances -= 4;
         Console.WriteLine("Failed to compile default program, trying batch size of " + GraphicsSettings.MaxBatchInstances);
         DefaultProgram.Dispose();
         Shader.CompileDefaults();
         DefaultProgram = new ShaderProgram(Shader.DefaultVertex, Shader.DefaultFragment);
     }
     if (GraphicsSettings.MaxBatchInstances <= 0)
     {
         Console.WriteLine("Yo this GPU is f****d.");
     }
 }
Esempio n. 4
0
        public Shader(Shader.ShaderType type, string name, string code)
        {
            code   = code.Replace("<instances>", GraphicsSettings.MaxBatchInstances.ToString());
            Name   = name;
            Source = code;
            ID     = GL.CreateShader((OpenTK.Graphics.OpenGL.ShaderType)type);
            GLDebug.CheckErrors();

            GL.ShaderSource(ID, code);
            GL.CompileShader(ID);
#if DEBUG
            var log = GL.GetShaderInfoLog(ID);
            if (log != null && log != "")
            {
                throw new ShaderCompileException(this, log);
            }
#endif
            GLDebug.CheckErrors();
        }
Esempio n. 5
0
        public ShaderProgram(params Shader[] shaders)
        {
            Shaders = shaders;
            ID      = GL.CreateProgram();

            foreach (var shader in shaders)
            {
                GL.AttachShader(ID, shader.ID);
            }
            GLDebug.CheckErrors();

            GL.LinkProgram(ID);
            GLDebug.CheckErrors();

            foreach (var shader in shaders)
            {
                GL.DetachShader(ID, shader.ID);
            }
            GLDebug.CheckErrors();
        }
Esempio n. 6
0
        static void SetAttributes()
        {
            var slot = 0;

            //position
            GL.EnableVertexAttribArray(slot);
            GL.VertexAttribPointer(slot, 3, VertexAttribPointerType.Float, false, Vertex.Size, 0);
            GLDebug.CheckErrors();
            slot++;

            //uv
            GL.EnableVertexAttribArray(slot);
            GL.VertexAttribPointer(slot, 2, VertexAttribPointerType.Float, false, Vertex.Size, 12);
            GLDebug.CheckErrors();
            slot++;

            //color
            GL.EnableVertexAttribArray(slot);
            GL.VertexAttribPointer(slot, 4, VertexAttribPointerType.Float, false, Vertex.Size, 20);
            GLDebug.CheckErrors();
            slot++;
        }
Esempio n. 7
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            GraphicsSettings.Init();
            GLDebug.Init();
            ShaderProgram.Init();
            Primitive.Init();

            testLayer = new GameObjectLayer(ClientSize.Width, ClientSize.Height);


            testTexture = Texture2D.FromFile("testball.png");
            var testSprite = new Sprite(testTexture, new Vector2(32, 32),
                                        new Vector2(0, 0),
                                        new Vector2(16, 0),
                                        new Vector2(32, 0),
                                        new Vector2(48, 0),
                                        new Vector2(64, 0),
                                        new Vector2(80, 0),
                                        new Vector2(96, 0),
                                        new Vector2(112, 0)
                                        );

            var rand = new Random();

            for (int i = 0; i < 20000; i++)
            {
                var newObj = new SpriteObject(testSprite, new Vector3(rand.Next(0, ClientSize.Width), rand.Next(0, ClientSize.Height), 0));
                newObj.Layer        = testLayer;
                newObj.Frame        = rand.Next(0, 7);
                newObj.AnimationFPS = 15f;
            }

            CursorVisible  = true;
            FPSSampleStart = DateTime.Now;
        }
Esempio n. 8
0
 public void BufferData(Vertex[] vertices)
 {
     Count = vertices.Length;
     BufferData(vertices, Vertex.Size * vertices.Length);
     GLDebug.CheckErrors();
 }
Esempio n. 9
0
        public void Draw()
        {
            ShaderProgram.DefaultProgram.Bind();
            var proj = Matrix4.CreateOrthographicOffCenter(0, Size.Width, Size.Height, 0, -1f, 1f);

            ShaderProgram.DefaultProgram.SetUniform("projectionMatrix", ref proj);
            GLDebug.CheckErrors();

            if (batchPositions.Length < GraphicsSettings.MaxBatchInstances * 3)
            {
                Array.Resize(ref batchPositions, GraphicsSettings.MaxBatchInstances * 3);
            }
            if (batchScales.Length < GraphicsSettings.MaxBatchInstances * 3)
            {
                Array.Resize(ref batchScales, GraphicsSettings.MaxBatchInstances * 3);
            }
            if (batchUVPositions.Length < GraphicsSettings.MaxBatchInstances * 2)
            {
                Array.Resize(ref batchUVPositions, GraphicsSettings.MaxBatchInstances * 2);
            }
            if (batchUVSizes.Length < GraphicsSettings.MaxBatchInstances * 2)
            {
                Array.Resize(ref batchUVSizes, GraphicsSettings.MaxBatchInstances * 2);
            }

            Texture2D batchTex   = null;
            Primitive batchPrim  = null;
            var       batchCount = 0;
            var       batchStart = 0;
            var       batchNext  = 0;

            for (var i = 0; i < Objects.Count; i++)
            {
                var obj = Objects[i];
                if (obj is PrimitiveObject)
                {
                    var po = (PrimitiveObject)obj;
                    if (po.Primitive != batchPrim || po.Sprite.Texture != batchTex || batchCount >= GraphicsSettings.MaxBatchInstances)
                    {
                        if (batchPrim != null && batchCount > 0)
                        {
                            _Draw(ShaderProgram, batchPrim, batchTex, batchStart, batchCount);
                        }
                        batchPrim  = po.Primitive;
                        batchTex   = po.Sprite.Texture;
                        batchStart = i;
                        batchCount = 0;
                        batchNext  = 0;
                    }

                    batchPositions[batchNext * 3]     = obj.Position.X;
                    batchPositions[batchNext * 3 + 1] = obj.Position.Y;
                    batchPositions[batchNext * 3 + 2] = obj.Position.Z;

                    batchScales[batchNext * 3]     = po.Scale.X * po.ScaleMultiplier.X;
                    batchScales[batchNext * 3 + 1] = po.Scale.Y * po.ScaleMultiplier.X;
                    batchScales[batchNext * 3 + 2] = po.Scale.Z * po.ScaleMultiplier.X;

                    var uvPos = po.Sprite.Positions[(int)po.Frame];
                    var w     = (float)po.Sprite.Texture.Size.Width;
                    var h     = (float)po.Sprite.Texture.Size.Height;
                    batchUVPositions[batchNext * 2]     = uvPos.X / w;
                    batchUVPositions[batchNext * 2 + 1] = uvPos.Y / h;
                    batchUVSizes[batchNext * 2]         = po.Sprite.Size.X / w;
                    batchUVSizes[batchNext * 2 + 1]     = po.Sprite.Size.Y / h;

                    batchNext++;
                    batchCount++;
                }
            }
            if (batchPrim != null && batchCount > 0)
            {
                _Draw(ShaderProgram, batchPrim, batchTex, batchStart, batchCount);
            }

            ShaderProgram.DefaultProgram.Unbind();
        }
Esempio n. 10
0
 public void SetUniformVec2Array(string name, float[] v, int count)
 {
     GL.Uniform2(GetUniformLocation(name), count, v);
     GLDebug.CheckErrors();
 }
Esempio n. 11
0
 public void SetUniformVec2Array(string name, float[] v)
 {
     GL.Uniform2(GetUniformLocation(name), v.Length, v);
     GLDebug.CheckErrors();
 }
Esempio n. 12
0
 public void SetUniform(string name, ref Matrix4 v)
 {
     GL.UniformMatrix4(GetUniformLocation(name), false, ref v);
     GLDebug.CheckErrors();
 }
Esempio n. 13
0
 public void SetUniform(string name, Vector4 v)
 {
     GL.Uniform4(GetUniformLocation(name), v);
     GLDebug.CheckErrors();
 }
Esempio n. 14
0
 public void SetUniform(string name, float v)
 {
     GL.Uniform1(GetUniformLocation(name), v);
     GLDebug.CheckErrors();
 }