Ejemplo n.º 1
0
        public Material Clone()
        {
            var m = new Material(Shader);

            m._values = new Dictionary<string, object>(_values);
            m.Name = Name;

            return m;
        }
Ejemplo n.º 2
0
        private static void LoadMaterials(XmlNode main)
        {
            XmlNode material = main.SelectSingleNode("Materials");
            XmlNodeList materials = material.SelectNodes("Material");
            string basePath = material.Attributes["basePath"].Value;

            foreach (XmlNode item in materials)
            {
                string name = item.Attributes["gameName"].Value;
                string vshader = item.Attributes["vshader"].Value;
                string fshader = item.Attributes["fshader"].Value;

                XmlNodeList @params = item.SelectNodes("Param");

                Shader shader = new Shader();
                shader.LoadVertexFile(basePath + "//" + vshader);
                shader.LoadFragmentFile(basePath + "//" + fshader);
                shader.Link();
                Material mat = new Material(shader);
                foreach (XmlNode param in @params)
                {
                    string paramName = param.Attributes["name"].Value;
                    string paramType = param.Attributes["type"].Value;
                    string paramValue = param.Attributes["value"].Value;

                    mat.SetParameter(paramName, paramType, paramValue);
                }

                _materials[name] = mat;
            }
        }
Ejemplo n.º 3
0
        public SpriteBatch()
        {
            dipQueue = new Queue<SpriteBatch.BatchItem>(25);

            vbuffer = new VertexBuffer(1024);
            Diagnostics.DiagnosticsCenter.Instance.Add(vbuffer);

            Shader defaultShader = new Shader();
            defaultMaterial = new Material (defaultShader);

            if (Shader.Version < 3.3f)
            {
                defaultShader.LoadVertexSource(@"#version 120
                uniform mat4 WPV;
                attribute vec2 vposition;
                attribute vec4 vcolor;
                attribute vec2 vtexcoord;
                varying vec4 fcolor;
                varying vec2 ftexcoord;
                void main(void)
                {
                    fcolor = vcolor;
                    ftexcoord = vtexcoord;
                    gl_Position = WPV * vec4(vposition, 0, 1);
                }");

                defaultShader.LoadFragmentSource(@"#version 120
                                          uniform sampler2D colorTexture;
                                          varying vec4 fcolor; varying vec2 ftexcoord;

                                          void main(void) { gl_FragColor = texture2D(colorTexture, ftexcoord) * fcolor; }");

            }
            else
            {
                defaultShader.LoadVertexSource(@"#version 330 core
                                    uniform mat4 WPV;
                                    in vec2 vposition; in vec4 vcolor; in vec2 vtexcoord;
                                    out vec4 fcolor; out vec2 ftexcoord;
                                    void main(void) {
                                    fcolor = vcolor;
                                    ftexcoord = vtexcoord;
                                    gl_Position = WPV * vec4(vposition, 0, 1);
                                    }");
                defaultShader.LoadFragmentSource(@"#version 330 core
                                          uniform sampler2D colorTexture;
                                          in vec4 fcolor; in vec2 ftexcoord;
                                          out vec4 color;
                                          void main(void) { color = texture(colorTexture, ftexcoord) * fcolor; }");
            }
            defaultShader.Link();
            BindShader(defaultShader, "vposition", "vcolor", "vtexcoord");

            int[] p = new int[4];
            GL.GetInteger(GetPName.Viewport, p);

            proj = Matrix4.CreateOrthographicOffCenter(p[0], p[2], p[3], p[1], 1, -1);

            pixelTex = new Texture();

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 0);

            float[] pix = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, 1, 1, 0,
               PixelFormat.Bgra, PixelType.Float, pix);
        }
Ejemplo n.º 4
0
 public void DrawTexture(Texture texture, Material material, RectangleF destinationRectangle, Color4 color)
 {
     DrawTexture(texture, material, destinationRectangle.X, destinationRectangle.Y, destinationRectangle.Width, destinationRectangle.Height, RectangleF.Empty, color);
 }
Ejemplo n.º 5
0
        public void End()
        {
            if (!began) throw new Exception("Call begin first");

            if (current == null)
            {
                current = defaultMaterial;
            }

            if (dipQueue.Count != 0)
            {

                vbuffer.Bind();
                vbuffer.UpdateVertexBuffer();
                vbuffer.UpdateIndexBuffer();

                do
                {
                    var b = dipQueue.Dequeue();
                    int count = (dipQueue.Count > 0 ? dipQueue.Peek().startIndex : vbuffer.IndexOffset) - b.startIndex;

                    Material material = b.material;
                    if (material == null)
                    {
                        material = defaultMaterial;
                    }
                    if (current != material)
                    {
                        current = material;
                        ResetShader(current.Shader);
                    }

                    Matrix4 m = trans * proj;
                    current.SetParameter("WPV", m);
                    current.SetShaderUniforms();

                    //GL.BindTexture(TextureTarget.Texture2D, b.texture);
                    b.texture.Bind(0);
                    FlushBuffer(b.mode, b.startIndex, count);
                    elementsCounter += count;
                } while (dipQueue.Count > 0);
                last = new SpriteBatch.BatchItem() { texture = null, startIndex = -1, mode = BeginMode.Triangles };
            }
            began = false;
        }
Ejemplo n.º 6
0
 public void DrawTexture(Texture texture, Material material, Vector2 position, RectangleF sourceRectangle, Color4 color)
 {
     DrawTexture(texture, material, position.X, position.Y, sourceRectangle, color);
 }
Ejemplo n.º 7
0
 public void DrawTexture(Texture texture, Material material, Vector2 position, Color4 color)
 {
     DrawTexture(texture, material, position.X, position.Y, RectangleF.Empty, color);
 }
Ejemplo n.º 8
0
 public void DrawTexture(Texture texture, Material material, RectangleF destinationRectangle, RectangleF sourceRectangle, Color4 color,
     float rotation, Vector2 origin,
     bool flipHorizontally = false, bool flipVertically = false)
 {
     DrawTexture(texture, material, destinationRectangle.X, destinationRectangle.Y, destinationRectangle.Width, destinationRectangle.Height, sourceRectangle, color, rotation, origin.X, origin.Y, flipHorizontally, flipVertically);
 }
Ejemplo n.º 9
0
 public void DrawTexture(Texture texture, Material material, Vector2 position, RectangleF sourceRectangle, Color4 color,
     float rotation, Vector2 origin, Vector2 scale,
     bool flipHorizontally = false, bool flipVertically = false)
 {
     DrawTexture(texture, material, position.X, position.Y, sourceRectangle, color, rotation, origin.X, origin.Y, scale.X, scale.Y, flipHorizontally, flipVertically);
 }
Ejemplo n.º 10
0
 public void DrawTexture(Texture texture, Material material, float x, float y, RectangleF sourceRectangle, Color4 color,
     float rotation = 0.0f, float xOrigin = 0.5f, float yOrigin = 0.5f, float xScale = 1, float yScale = 1,
     bool flipHorizontally = false, bool flipVertically = false)
 {
     DrawTexture(texture,material, x, y,
         sourceRectangle != RectangleF.Empty ? sourceRectangle.Width * xScale : texture.Size.Width * xScale,
         sourceRectangle != RectangleF.Empty ? sourceRectangle.Height * yScale : texture.Size.Height * yScale,
         sourceRectangle, color, rotation, xOrigin, yOrigin, flipHorizontally, flipVertically);
 }
Ejemplo n.º 11
0
        public unsafe void DrawTexture(Texture texture, Material material, float x, float y, float width, float height, RectangleF sourceRectangle, Color4 color,
            float rotation = 0.0f, float xOrigin = 0.5f, float yOrigin = 0.5f,
            bool flipHorizontally = false, bool flipVertically = false)
        {
            if (texture == null)
                throw new ArgumentException("texture");
            TryPush(texture, material, BeginMode.Triangles);

            if (sourceRectangle.IsEmpty)
            {
                tempRect.X = 0;
                tempRect.Y = 0;
                tempRect.Width = texture.Size.Width;
                tempRect.Height = texture.Size.Height;
            }
            else
                tempRect = sourceRectangle;

            texCoordTL = texture.GetTextureCoordinate(new Vector2(tempRect.X, tempRect.Y));
            texCoordBR = texture.GetTextureCoordinate(new Vector2(tempRect.Right, tempRect.Bottom));

            if (flipVertically)
            {
                float temp = texCoordBR.Y;
                texCoordBR.Y = texCoordTL.Y;
                texCoordTL.Y = temp;
            }
            if (flipHorizontally)
            {
                float temp = texCoordBR.X;
                texCoordBR.X = texCoordTL.X;
                texCoordTL.X = temp;
            }

            #region Add vertices

            float dx = -xOrigin * width;
            float dy = -yOrigin * height;

            float sin = 0;
            float cos = 1;

            if (rotation != 0)
            {
                sin = (float)Math.Sin(rotation);
                cos = (float)Math.Cos(rotation);
            }

            int offset = vbuffer.VertexOffset / vbuffer.Stride;
            int* ind = vbuffer.GetIndexPointerToFill(6);
            float* vert = vbuffer.GetVertexPointerToFill(4);

            *(ind++) = offset; *(ind++) = offset + 1; *(ind++) = offset + 2;
            *(ind++) = offset; *(ind++) = offset + 2; *(ind++) = offset + 3;

            *(vert++) = x + dx * cos - dy * sin;
            *(vert++) = y + dx * sin + dy * cos;
            *(vert++) = color.R; *(vert++) = color.G; *(vert++) = color.B; *(vert++) = color.A;
            *(vert++) = texCoordTL.X; *(vert++) = texCoordTL.Y;

            *(vert++) = x + (dx + width) * cos - dy * sin;
            *(vert++) = y + (dx + width) * sin + dy * cos;
            *(vert++) = color.R; *(vert++) = color.G; *(vert++) = color.B; *(vert++) = color.A;
            *(vert++) = texCoordBR.X; *(vert++) = texCoordTL.Y;

            *(vert++) = x + (dx + width) * cos - (dy + height) * sin;
            *(vert++) = y + (dx + width) * sin + (dy + height) * cos;
            *(vert++) = color.R; *(vert++) = color.G; *(vert++) = color.B; *(vert++) = color.A;
            *(vert++) = texCoordBR.X; *(vert++) = texCoordBR.Y;

            *(vert++) = x + dx * cos - (dy + height) * sin;
            *(vert++) = y + dx * sin + (dy + height) * cos;
            *(vert++) = color.R; *(vert++) = color.G; *(vert++) = color.B; *(vert++) = color.A;
            *(vert++) = texCoordTL.X; *(vert++) = texCoordBR.Y;

            #endregion Add vertices
        }
Ejemplo n.º 12
0
 private bool TryPush(Texture tex, Material material, BeginMode mode)
 {
     if (last.texture != tex || last.mode != mode || last.material != material)
     {
         last = new BatchItem() { texture = tex, mode = mode, startIndex = vbuffer.IndexOffset, material = material };
         dipQueue.Enqueue(last);
         return true;
     }
     return false;
 }