Ejemplo n.º 1
0
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);

                Engine.scene = new Scene();

                font = new Font("Assets/OpenSans-Bold.ttf");

                bump_test = new GameObject();
                bump_test.components.Add(new Sprite());

                bump_test.sprite.width = 512;
                bump_test.sprite.height = 512;

                bump_test.sprite.material = new Material();

                Shader bump = new Shader();

                bump.name = "Bump shader";
                bump.vertexProgram = File.ReadAllText("Assets/vertex.glsl");
                bump.fragmentProgram = File.ReadAllText("Assets/fragment.glsl");
                bump.fullRecompile();

                bump_test.sprite.material.shader = bump;

                Texture tex = new Texture("Assets/tex.png");
                Texture nrm = new Texture("Assets/tex_nrm.png");
                Sampler2D smp_tex = new Sampler2D(tex);
                Sampler2D smp_nrm = new Sampler2D(nrm);

                bump_test.sprite.material.parameters["light_c"] = (int)1;

                bump_test.sprite.material.parameters["texture"] = smp_tex;
                bump_test.sprite.material.parameters["normal"] = smp_nrm;
                bump_test.sprite.material.parameters["color"] = new Vec4(1.0f, 1.0f, 1.0f, 1.0f);
                bump_test.sprite.material.parameters["ambient"] = new Vec4(0.0f, 0.0f, 0.0f, 1.0f);
                bump_test.sprite.material.parameters["light_p[0]"] = new Vec4(256.0f, 256.0f, 150.0f, 1.0f);
                bump_test.sprite.material.parameters["light_col[0]"] = new Vec4(1.0f, 1.0f, 1.0f, 1.0f);
                bump_test.sprite.material.parameters["light_int[0]"] = (float)0.3f;

                bump_test.sprite.material.parameters["amount"] = (float)1.0f;
            }
Ejemplo n.º 2
0
        public static void drawTexture(Texture texture, Vector position, Vector center, Vector scale, int angle, int width, int height)
        {
            Vector[] vertexBuf = new Vector[4];
            Vector[] normalsBuf = new Vector[4];
            Vector[] textureBuf = new Vector[4];

            vertexBuf[0].x = -center.x; vertexBuf[0].y = -center.y;
            vertexBuf[1].x = width - center.x; vertexBuf[1].y = -center.y;
            vertexBuf[2].x = width - center.x; vertexBuf[2].y = height - center.y;
            vertexBuf[3].x = -center.x; vertexBuf[3].y = height - center.y;

            textureBuf[0] = new Vector(0, 0);
            textureBuf[1] = new Vector(1, 0);
            textureBuf[2] = new Vector(1, 1);
            textureBuf[3] = new Vector(0, 1);

            //Verts
            int vId = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, vId);
            GL.BufferData<Vector>(BufferTarget.ArrayBuffer, new IntPtr(Marshal.SizeOf(new Vector()) * 2 * vertexBuf.Length), vertexBuf, BufferUsageHint.StreamDraw);
            GL.BufferSubData<Vector>(BufferTarget.ArrayBuffer, new IntPtr(0), new IntPtr(Marshal.SizeOf(new Vector()) * 2 * vertexBuf.Length), vertexBuf);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            //Textures
            int tId = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, tId);
            GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(sizeof(float) * 2 * textureBuf.Length), textureBuf, BufferUsageHint.StaticDraw);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            GL.PushMatrix();

            if (width <= 0) width = 1;
            if (height <= 0) height = 1;

            Texture tex = new Texture();
            Bitmap bitmap = new Bitmap(texture.bitmap, width, height);
            tex.LoadFromBitmap(bitmap);

            Engine.shaders.guiShader.begin();
            Engine.shaders.guiShader.pass("texture", tex);
            Engine.shaders.guiShader.pass("color", new Vec4(1, 1, 1, 1));

            GL.Translate(position.x, position.y, 0);
            GL.Rotate(angle, 0, 0, 1);

            GL.Scale(scale.x, scale.y, 1);

            //Биндим текстурные координаты
            GL.EnableClientState(ArrayCap.TextureCoordArray);
            GL.BindBuffer(BufferTarget.ArrayBuffer, tId);
            GL.TexCoordPointer(2, TexCoordPointerType.Float, 0, 0);

            //Биндим геометрию
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vId);
            GL.VertexPointer(2, VertexPointerType.Float, 0, 0);

            //Рисуем
            GL.DrawArrays(PrimitiveType.Quads, 0, vertexBuf.Length);

            Engine.shaders.guiShader.passNullTex("texture");
            Engine.shaders.guiShader.end();

            GL.DeleteTexture(tex.ID);

            bitmap.Dispose();
            tex.Dispose();

            GL.PopMatrix();

            //Отключаем все
            GL.DisableClientState(ArrayCap.VertexArray);
            GL.DisableClientState(ArrayCap.TextureCoordArray);

            GL.DeleteBuffer(vId);
            GL.DeleteBuffer(tId);
        }
Ejemplo n.º 3
0
        public void shapeFromTexture(Texture tex)
        {
            byte[] data;

            using (MemoryStream ms = new MemoryStream())
            {
                tex.bitmap.Save(ms, ImageFormat.Png);
                data = ms.ToArray();
                ms.Dispose();
            }

            uint[] bytes = new uint[data.Length];

            for (int i = 0; i < data.Length; i++)
                bytes[i] = data[i];

            Vertices verts = PolygonTools.CreatePolygon(bytes, tex.bitmap.Width, true);

            vertices.Clear();

            for (int i = 0; i < verts.Count; i++)
            {
                vertices.Add(new Vector((float)verts[i].X, (float)verts[i].Y));
            }
        }
Ejemplo n.º 4
0
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            openFameDlg.ShowDialog();
            string[] fname = openFameDlg.FileNames;

            for (int i = 0; i < fname.Length; i++)
            {
                if (File.Exists(fname[i]))
                {
                    Texture tex = new Texture();
                    tex.LoadFromFile(fname[i]);
                    Value.Add(tex);

                    PictureBox img = new PictureBox();
                    img.Parent = panel;
                    img.Width = 128;
                    img.Height = 128;
                    img.SizeMode = PictureBoxSizeMode.Zoom;
                    img.Tag = tex;
                    img.Click += img_Click;

                    MemoryStream ms = new MemoryStream();
                    ms.Write(tex.bytes, 0, tex.bytes.Length);
                    ms.Seek(0, SeekOrigin.Begin);
                    Bitmap bmp = new Bitmap(ms);
                    ms.Dispose();

                    img.Image = bmp;
                }
            }
        }
Ejemplo n.º 5
0
 void searchTexture()
 {
     if (Engine.scene != null)
     {
         foreach (GameObject o in Engine.scene.objects)
         {
             if (o.rigidbody != null)
             {
                 if (o.rigidbody.vertices == v)
                 {
                     if (o.sprite != null)
                     {
                         if (o.sprite.material != null)
                         {
                             foreach (string s in o.sprite.material.parameters.Keys)
                             {
                                 if (o.sprite.material.parameters[s] is Sampler2D)
                                 {
                                     Sampler2D smp = (Sampler2D)o.sprite.material.parameters[s];
                                     if (smp.texture != null)
                                     {
                                         tex = smp.texture;
                                         toolStripTextBox3.Text = o.sprite.width.ToString();
                                         toolStripTextBox4.Text = o.sprite.height.ToString();
                                         toolStripTextBox1.Text = o.sprite.center.x.ToString();
                                         toolStripTextBox2.Text = o.sprite.center.y.ToString();
                                         break;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 6
0
 private void imgBox_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (imgBox.SelectedItem is Texture)
     {
         tex = ((Texture)imgBox.SelectedItem);
         toolStripTextBox3.Text = tex.bitmap.Width.ToString();
         toolStripTextBox4.Text = tex.bitmap.Height.ToString();
     }
     else
     {
         tex = null;
     }
 }
Ejemplo n.º 7
0
        internal void renderString(Vector position, Vector center, Vector scale, int angle, int width, int height, string text, Color color, int size)
        {
            Vector[] vertexBuf = new Vector[4];
            Vector[] normalsBuf = new Vector[4];
            Vector[] textureBuf = new Vector[4];

            vertexBuf[0].x = -center.x; vertexBuf[0].y = -center.y;
            vertexBuf[1].x = width - center.x; vertexBuf[1].y = -center.y;
            vertexBuf[2].x = width - center.x; vertexBuf[2].y = height - center.y;
            vertexBuf[3].x = -center.x; vertexBuf[3].y = height - center.y;

            textureBuf[0] = new Vector(0, 0);
            textureBuf[1] = new Vector(1, 0);
            textureBuf[2] = new Vector(1, 1);
            textureBuf[3] = new Vector(0, 1);

            //Verts
            int vId = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, vId);
            GL.BufferData<Vector>(BufferTarget.ArrayBuffer, new IntPtr(Marshal.SizeOf(new Vector()) * 2 * vertexBuf.Length), vertexBuf, BufferUsageHint.StreamDraw);
            GL.BufferSubData<Vector>(BufferTarget.ArrayBuffer, new IntPtr(0), new IntPtr(Marshal.SizeOf(new Vector()) * 2 * vertexBuf.Length), vertexBuf);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            //Textures
            int tId = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, tId);
            GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(sizeof(float) * 2 * textureBuf.Length), textureBuf, BufferUsageHint.StaticDraw);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            GL.PushMatrix();

            if (width <= 0) width = 1;
            if (height <= 0) height = 1;

            Bitmap bitmap = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bitmap);

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

            g.Clear(System.Drawing.Color.Transparent);

            font = new System.Drawing.Font(font.FontFamily, size, FontStyle.Regular);
            g.DrawString(text, font, new SolidBrush(System.Drawing.Color.FromArgb(color.a, color.r, color.g, color.b)), new PointF(0, 0));

            if (bitmap != null)
            {
                Texture tex = new Texture();
                tex.LoadFromBitmap(bitmap);

                Engine.shaders.guiShader.begin();
                Engine.shaders.guiShader.pass("texture", tex);
                Engine.shaders.guiShader.pass("color", new Vec4(1, 1, 1, 1));

                GL.Translate(position.x, position.y, 0);
                GL.Rotate(angle, 0, 0, 1);

                GL.Scale(scale.x, scale.y, 1);

                //Биндим текстурные координаты
                GL.EnableClientState(ArrayCap.TextureCoordArray);
                GL.BindBuffer(BufferTarget.ArrayBuffer, tId);
                GL.TexCoordPointer(2, TexCoordPointerType.Float, 0, 0);

                //Биндим геометрию
                GL.EnableClientState(ArrayCap.VertexArray);
                GL.BindBuffer(BufferTarget.ArrayBuffer, vId);
                GL.VertexPointer(2, VertexPointerType.Float, 0, 0);

                //Рисуем
                GL.DrawArrays(PrimitiveType.Quads, 0, vertexBuf.Length);

                Engine.shaders.guiShader.passNullTex("texture");
                Engine.shaders.guiShader.end();

                GL.DeleteTexture(tex.ID);

                bitmap.Dispose();
                g.Dispose();
                tex.Dispose();
            }

            GL.PopMatrix();

            if (bitmap != null)
            {
                //Отключаем все
                GL.DisableClientState(ArrayCap.VertexArray);
                GL.DisableClientState(ArrayCap.TextureCoordArray);
            }

            GL.DeleteBuffer(vId);
            GL.DeleteBuffer(tId);
        }
Ejemplo n.º 8
0
 public void pass(string name, Texture tex)
 {
     int l = GL.GetUniformLocation(program, name);
     GL.ActiveTexture(TextureUnit.Texture0 + tex.ID);
     GL.BindTexture(TextureTarget.Texture2D, tex.ID);
     GL.Uniform1(l, tex.ID);
     GL.ActiveTexture(TextureUnit.Texture0);
     l = 0;
 }
Ejemplo n.º 9
0
 public Sampler2D(Texture tex)
 {
     animated = false;
     texture = tex;
 }