Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a char with a texture and its properties.
 /// </summary>
 public BitmapChar(int charId, Texture texture, float xOffset, float yOffset, float xAdvance)
 {
     CharId = charId;
     XAdvance = xAdvance;
     Texture = texture;
     XOffset = xOffset;
     YOffset = yOffset;
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a char with a texture.
 /// </summary>
 public BitmapChar(Texture texture)
 {
     CharId = 0;
     XAdvance = texture.Width;
     Texture = texture;
     XOffset = 0;
     YOffset = 0;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a movie with the first frame and the default number of frames per second.
 /// </summary>
 public MovieClip(Texture texture, float fps) : base(texture)
 {
     _defaultFrameDuration = 1.0f / fps;
     Loop = true;
     _playing = true;
     _totalTime = 0.0f;
     _currentTime = 0.0f;
     _currentFrame = 0;
     _textures = new List<Texture>();
     // TODO _sounds = new List<>;
     _durations = new List<float>();        
     AddFrame(texture);
 }
Ejemplo n.º 4
0
        private void ParseFontData(Stream fontTextureData, Stream fontXmlData)
        {
            if (fontTextureData == null || fontXmlData == null)
            {
                throw new InvalidOperationException("Font parsing requires texture and font XML to be set");
            }
            TextureLoader texLoader = new TextureLoader();
            GLTexture tex = texLoader.LoadFromStream(fontTextureData);
            _fontTexture = new SubTexture(tex);

            _chars = new Dictionary<int,BitmapChar>();
            _helperImage = new Image(_fontTexture);

            XmlDocument xml = new XmlDocument();
            xml.Load(fontXmlData);

            float scale = _fontTexture.Scale;
            ParseAndLoadChars(xml, scale);
            ParseAndLoadKerning(xml, scale);
            ParseAndLoadInfo(xml);
            ParseAndLoadCommon(xml);
        }
Ejemplo n.º 5
0
        protected void InitImage(Texture texture)
        {
            if (texture == null)
            {
                throw new Exception("texture cannot be null!");
            }

            Rectangle frame = texture.Frame;
            float width = (frame != null) ? frame.Width : texture.Width;
            float height = (frame != null) ? frame.Height : texture.Height;
            bool pma = texture.PremultipliedAlpha;

            Init(width, height, 0xFFFFFF, pma);

            _vertexData.Vertices[1].TexCoords.X = 1.0f;
            _vertexData.Vertices[2].TexCoords.Y = 1.0f;
            _vertexData.Vertices[3].TexCoords.X = 1.0f;
            _vertexData.Vertices[3].TexCoords.Y = 1.0f;

            _texture = texture;
            _vertexDataCache = new VertexData(4, pma);
            _vertexDataCacheInvalid = true;
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Adds a frame to the end of the animation with a certain texture, using the default duration (1/fps).
 /// </summary>
 public void AddFrame(Texture texture)
 {
     AddFrame(texture, NumFrames);   
 }
Ejemplo n.º 7
0
 /// <summary>
 ///  Sets the texture of a certain frame.
 /// </summary>
 public void SetTexture(Texture texture, int position)
 {
     _textures[position] = texture;
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Adds a frame with a certain texture, duration and sound.
 /// </summary>
 public void AddFrame(Texture texture, int position, float duration, object sound = null)
 {
     _totalTime += duration;
     _textures.Insert(position, texture);
     _durations.Insert(position, duration);
     //TODO [_sounds insertObject:(sound ? sound : [NSNull null]) atIndex:position];
 }
Ejemplo n.º 9
0
 // TODO this is not used
 private void DestroyFramebufferForTexture(Texture texture)
 {
     uint framebuffer;
     if (FramebufferCache.TryGetValue(texture.Name, out framebuffer))
     {
         GL.DeleteFramebuffers(1, ref framebuffer);
         FramebufferCache.Remove(texture.Name);
     }
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes a subtexture with a region (in points) of another texture, using a frame rectangle
 /// to place the texture within an image. If 'rotated' is 'true', the subtexture will show the base
 /// region rotated by 90 degrees (CCW). If frame is null, it will use the whole texture.
 /// </summary>
 public SubTexture(Texture texture, Rectangle region = null, Rectangle frame = null, bool rotated = false)
 {
     Init(texture, region, frame, rotated);
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Indicates if specific quads can be added to the batch without causing a state change.
 /// A state change occurs if the quad uses a different base texture, has a different 'Smoothing',
 /// 'Repeat' or 'Tinted' setting, or if the batch is full (one batch can contain up to 8192 quads).
 /// </summary>
 public bool IsStateChange(bool tinted, Texture texture, float alpha, bool premultipliedAlpha, uint blendMode, int numQuads)
 {
     if (_numQuads == 0)
     {
         return false;
     }
     else if (_numQuads + numQuads > 8192)
     {
         return true;
     }
     else if (_texture == null && texture == null)
     {
         return _premultipliedAlpha != premultipliedAlpha || BlendMode != blendMode;
     }
     else if (_texture != null && texture != null)
     {
         return _tinted != (tinted || alpha != 1.0f) ||
         _texture.Name != texture.Name ||
         BlendMode != blendMode;
     }
     return true;
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Adds a quad or image to the batch, using custom alpha and blend mode values (ignoring the
        /// quad's original values) and transforming each vertex by a certain transformation matrix.
        /// Make sure you only add quads with an equal state.
        /// </summary>
        public void AddQuad(Quad quad, float alpha, uint blendMode, Matrix matrix = null)
        {
            if (matrix == null)
            {
                matrix = quad.TransformationMatrix;
            }

            if (_numQuads + 1 > _capacity)
            {
                Capacity = _capacity < 8 ? 16 : _capacity * 2;
            }

            if (_numQuads == 0)
            {
                _texture = quad.Texture;
                _premultipliedAlpha = quad.PremultipliedAlpha;
                BlendMode = blendMode;
                _vertexData.SetPremultipliedAlpha(_premultipliedAlpha, false);
            }

            int vertexID = _numQuads * 4;

            _tinted = alpha != 1.0f || quad.Tinted;

            quad.CopyVertexDataTo(_vertexData, vertexID, _tinted);
            _vertexData.TransformVertices(matrix, vertexID, 4);

            if (alpha != 1.0f)
            {
                _vertexData.ScaleAlphaBy(alpha, vertexID, 4);
            }

            _syncRequired = true;
            _numQuads++;
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Adds a frame with a certain texture and duration.
 /// </summary>
 public void AddFrame(Texture texture, float duration)
 {
     AddFrame(texture, NumFrames, duration);
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Initialize a quad with a texture mapped onto it
 /// </summary>
 /// <param name="texture">The texture to use. Use for example the TextureLoader class to load one.</param>
 public Image(Texture texture)
 {
     InitImage(texture);
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Activates the optimal shader program for the current settings; alpha and matrix uniforms are
        /// passed to the program right away, and the texture (if available) is bound.
        /// Parameters:
        /// mvpMatrix: The modelview-projection matrix used for rendering. Any vertex will be multiplied with this matrix.
        /// premultipliedAlpha:  Indicates if the color values of texture and vertices use premultiplied alpha.
        /// alpha: The alpha value with which every vertex color will be multiplied.
        /// useTinting: Set to true if you dont use textures or want to use alpha.
        /// texture: The texture that's projected onto the quad, or 'null' if there is none.
        /// </summary>
        public void PrepareToDraw(Matrix mvpMatrix, bool premultipliedAlpha, float alpha, bool useTinting, Texture texture = null)
        {
            bool hasTexture = texture != null;

            string programName;
            if (hasTexture)
            {
                programName = useTinting ? "SparrowAlphaTextureProgram" : "SparrowTextureProgram";
            }
            else
            {
                programName = "SparrowQuadProgram";
            }

            if (_currentProgram == null || _currentProgram != SparrowSharpApp.GetProgram(programName))
            {
                if (SparrowSharpApp.Programs.ContainsKey(programName))
                {
                    _currentProgram = SparrowSharpApp.GetProgram(programName);
                }
                else
                {
                    string vertexShader = VertexShaderString(hasTexture, useTinting);
                    string fragmentShader = FragmentShaderString(hasTexture, useTinting);
                    _currentProgram = new Program(vertexShader, fragmentShader);
                    SparrowSharpApp.RegisterProgram(programName, _currentProgram);
                }

                _aPosition = _currentProgram.Attributes["aPosition"];

                if (_currentProgram.Attributes.ContainsKey("aColor"))
                {
                    _aColor = _currentProgram.Attributes["aColor"];
                }
                if (_currentProgram.Attributes.ContainsKey("aTexCoords"))
                {
                    _aTexCoords = _currentProgram.Attributes["aTexCoords"];
                }

                _uMvpMatrix = _currentProgram.Uniforms["uMvpMatrix"];

                if (_currentProgram.Uniforms.ContainsKey("uAlpha"))
                {
                    _uAlpha = _currentProgram.Uniforms["uAlpha"];
                }
            }

            Matrix4 glkMvpMatrix = mvpMatrix.ConvertToMatrix4();
            GL.UseProgram(_currentProgram.Name);
            GL.UniformMatrix4(_uMvpMatrix, false, ref glkMvpMatrix); // TODO check; was glUniformMatrix4fv(_uMvpMatrix, 1, NO, glkMvpMatrix.m);

            if (useTinting)
            {
                if (premultipliedAlpha)
                {
                    GL.Uniform4(_uAlpha, alpha, alpha, alpha, alpha);
                }
                else
                {
                    GL.Uniform4(_uAlpha, 1.0f, 1.0f, 1.0f, alpha);
                }
            }

            if (hasTexture)
            {
                GL.ActiveTexture (TextureUnit.Texture0);
                GL.BindTexture (TextureTarget.Texture2D, texture.Name);
            }
        }
Ejemplo n.º 16
0
        override protected void ActivateWithPass(int pass, Texture texture, Matrix mvpMatrix)
        {
            UpdateParamaters(pass, (int)texture.NativeWidth, (int)texture.NativeHeight);
            bool isColorPass = _enableColorUniform && pass == NumPasses - 1;
            BlurProgram program = isColorPass ? _tintedProgram : _program;

            GL.UseProgram(program.Name);
            Matrix4 mvp = mvpMatrix.ConvertToMatrix4();
            GL.UniformMatrix4(program.UMvpMatrix, false, ref mvp);

            GL.Uniform4(program.UOffsets, 1, _offsets);
            GL.Uniform4(program.UWeights, 1, _weights);

            if (isColorPass)
            {
                GL.Uniform4(program.UColor, 1, _color);
            }
        }
Ejemplo n.º 17
0
 override protected void ActivateWithPass(int pass, Texture texture, Matrix mvpMatrix)
 {
     GL.UseProgram(_program.Name);
     Matrix4 mvp = mvpMatrix.ConvertToMatrix4();
     GL.UniformMatrix4(_program.Uniforms["uMvpMatrix"], false, ref mvp);
 }
Ejemplo n.º 18
0
 public Bunny(Texture texture) : base(texture)
 {
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Subclasses must override this method and use it to activate their shader program.
 /// The 'ActivateWithPass' call directly precedes the call to 'GL.DrawElements'.
 /// </summary>
 protected abstract void ActivateWithPass(int pass, Texture texture, Matrix mvpMatrix);
Ejemplo n.º 20
0
        /// <summary>
        /// Adds another quad batch to this batch, using custom alpha and blend mode values (ignoring the
        /// batch's original values) and transforming each vertex by a certain transformation matrix. Just
        /// like the 'AddQuad' method, you have to make sure that you only add batches with an equal state.
        /// </summary>
        public void AddQuadBatch(QuadBatch quadBatch, float alpha, uint blendMode, Matrix matrix = null)
        {
            int vertexID = _numQuads * 4;
            int numQuads = quadBatch.NumQuads;
            int numVertices = numQuads * 4;

            if (matrix == null)
            {
                matrix = quadBatch.TransformationMatrix;
            }
            if (_numQuads + numQuads > _capacity)
            {
                Capacity = _numQuads + numQuads;
            }
            if (_numQuads == 0)
            {
                _texture = quadBatch.QuadTexture;
                _premultipliedAlpha = quadBatch.PremultipliedAlpha;
                BlendMode = blendMode;
                _vertexData.SetPremultipliedAlpha(_premultipliedAlpha, false);
            }
                
            _tinted = alpha != 1.0f || quadBatch.Tinted;

            quadBatch.VertexData.CopyToVertexData(_vertexData, _tinted, vertexID, numVertices);
            _vertexData.TransformVertices(matrix, vertexID, numVertices);

            if (alpha != 1.0f)
            {
                _vertexData.ScaleAlphaBy(alpha, vertexID, numVertices);
            }

            _syncRequired = true;
            _numQuads += numQuads;
        }
Ejemplo n.º 21
0
 /// <summary>
 /// This method is called directly after 'GL.DrawElements'.
 /// If you need to clean up any resources, you can do so in this method
 /// </summary>
 protected void DeactivateWithPass(int pass, Texture texture)
 {
     // override in subclass
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Resets the batch. The vertex- and index-buffers keep their size, so that they can be reused.
 /// </summary>
 public void Reset()
 {
     _numQuads = 0;
     _syncRequired = true;
     _texture = null;
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Inserts a frame at the specified position. The successors will move down.
 /// </summary>
 public void AddFrame(Texture texture, int position)
 {
     AddFrame(texture, position, _defaultFrameDuration);
 }
Ejemplo n.º 24
0
        protected void Init(Texture texture, Rectangle region = null, Rectangle frame = null, bool rotated = false)
        {
            if (region == null)
                region = new Rectangle(0.0f, 0.0f, texture.Width, texture.Height);

            _parent = texture;
            if (_frame != null)
            {
                _frame = frame.Copy();
            }

            _transformationMatrix = Matrix.Create();
            _width = rotated ? region.Height : region.Width;
            _height = rotated ? region.Width : region.Height;

            if (rotated)
            {
                _transformationMatrix.Translate(0, -1);
                _transformationMatrix.Rotate((float)Math.PI / 2.0f);
            }

            _transformationMatrix.Scale(region.Width / texture.Width, region.Height / texture.Height);

            _transformationMatrix.Translate(region.X / texture.Width, region.Top / texture.Height);
        }
Ejemplo n.º 25
0
        private uint CreateFramebufferForTexture(Texture texture)
        {
            uint framebuffer;
            GL.GenFramebuffers(1, out framebuffer);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer);
            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferSlot.ColorAttachment0, TextureTarget.Texture2D, texture.Name, 0);

            if (GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferErrorCode.FramebufferComplete)
            {
                Debug.WriteLine("Failed to create framebuffer for render texture");
            }
            return framebuffer;
        }