Esempio n. 1
0
        /// <summary>
        /// Gets an OpenGL Shader Program from the resource cache - if the program is
        /// not already cached, an attempt will be made to load it.
        /// </summary>
        /// <param name="programName">The name of the program.</param>
        /// <returns>The ID of the OpenGL Shader Program object.</returns>
        public uint GetShaderProgram(string programName)
        {
            if (Disposing || Disposed)
            {
                return(0);
            }

            // If the shader program has previously been loaded, return its GL
            // ID
            //
            if (ShaderPrograms.ContainsKey(programName))
            {
                return(ShaderPrograms[programName]);
            }

            // We reached here, program has not yet been cached, need to
            // compile it from sources
            //
            string fragmentSource = File.ReadAllText(Environment.CurrentDirectory + @"\Content\Shaders\OpenGL\" + programName + @"\fragment.glsl");
            string vertexSource   = File.ReadAllText(Environment.CurrentDirectory + @"\Content\Shaders\OpenGL\" + programName + @"\vertex.glsl");

            uint compiledProgramId = GlUtil.CompileShaderProgram(vertexSource, fragmentSource);

            ShaderPrograms.Add(programName, compiledProgramId);

            return(compiledProgramId);
        }
Esempio n. 2
0
        /// <summary>
        /// Issues a draw command to this batch.
        /// </summary>
        /// <param name="spriteName">The name of the sprite.</param>
        /// <param name="rect">The rectangle space to draw the sprite at.</param>
        public void Draw(string spriteName, Rectanglei rect)
        {
            Rectanglei spriteRect = Atlas.GetSpriteUV(spriteName);

            VboDrawContents.AddRange(GlUtil.MakeVboData(rect));
            VboUvContents.AddRange(GlUtil.MakeVboData(spriteRect));

            VertexCount += 12;
        }
Esempio n. 3
0
        /// <summary>
        /// Loads a sprite atlas from supplied files on disk.
        /// </summary>
        /// <param name="filename">
        /// The filename of the atlas, the extension will be ignored.
        /// </param>
        /// <returns>
        /// A <see cref="GlSpriteAtlas"/> created from the supplied data.
        /// </returns>
        public static GlSpriteAtlas LoadAtlas(string filename)
        {
            // Read texture atlas information and bitmap data
            //
            string atlasPath  = Path.GetDirectoryName(filename);
            string atlasNoExt = Path.GetFileNameWithoutExtension(filename);

            var atlasBmp       = (Bitmap)Image.FromFile(atlasPath + @"\" + atlasNoExt + ".png");
            var atlasJson      = File.ReadAllText(atlasPath + @"\" + atlasNoExt + ".json");
            var atlasNodeArray = JArray.Parse(atlasJson);

            var atlasMap = new Dictionary <string, Rectanglei>();

            foreach (JToken token in atlasNodeArray)
            {
                string key                 = token.Value <string>("Name").ToLower();
                string boundsCsv           = token.Value <string>("Bounds");
                var    rectangleComponents = new List <int>();

                foreach (string boundComponent in boundsCsv.Split(','))
                {
                    rectangleComponents.Add(Convert.ToInt32(boundComponent));
                }

                atlasMap.Add(
                    key,
                    new Rectanglei(
                        rectangleComponents[0],
                        rectangleComponents[1],
                        rectangleComponents[2],
                        rectangleComponents[3]
                        )
                    );
            }

            // Read out atlas dimensions
            //
            Vector2 atlasDimensions = new Vector2(
                atlasBmp.Width,
                atlasBmp.Height
                );

            // Load the bitmap into OpenGL
            //
            int glTextureId = GlUtil.LoadBitmapTexture(atlasBmp);

            // Dispose the atlas resource
            //
            atlasBmp.Dispose();

            return(new GlSpriteAtlas(atlasDimensions, glTextureId, atlasMap));
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GlSpriteBatch"/> class.
        /// </summary>
        /// <param name="atlasFullPath"></param>
        /// <param name="glProgramId"></param>
        public GlSpriteBatch(string atlasFullPath, uint glProgramId)
        {
            Atlas       = GlUtil.LoadAtlas(atlasFullPath);
            GlProgramId = glProgramId;

            GlCanvasResolutionUniformId = GL.GetUniformLocation(
                GlProgramId,
                "CanvasResolution"
                );

            GlUvMapResolutionUniformId = GL.GetUniformLocation(
                GlProgramId,
                "UvMapResolution"
                );

            VertexCount     = 0;
            VboDrawContents = new List <float>();
            VboUvContents   = new List <float>();
        }