flopoloco's picture

2D In OpenGL

Do any of you know if I can use the power of OpenGL for 2D drawing? As far as I think is that I should look further into projections. Have you ever tried it?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Entropy's picture

I believe the Glu.Ortho2D(...) function will set up your projection matrix for 2D drawing. I think the OpenTK examples will demonstrate its use for you, but let me know if you want some more examples and I'll see what I can dig up.

wI don't have anything too impressive, but I've got a couple of simple 2D projects using OpenTK's OpenGL bindings you can take a look at it you think it'll help.

flopoloco's picture

Yes please, providing me with a simple snippet would be a great help.
Thank you Entropy :)

kanato's picture

I've written a 2D library that uses plugins to use OpenGL or Direct3D for 2D drawing. It's at http://agate.sourceforge.net/. If you browse through the subversion you can find the 2D drawing stuff encapsulated in /trunk/drivers/AgateOTK/. Or you could just use the library (or another 2D library .NET library, there's a few out there). :)

Entropy's picture

@floploco: I'll post something simple when I get home tonight after work (approximately 8 hours from now). Don't expect to be impressed (and certainly don't take any programming tips from me!) but it should prove useful as a working OpenTK implementation.

@kanato: Actually I just discovered Agate last night after posting on this thread. It looks pretty interesting - I've got a project in the pipeline that I think would benefit nicely from using Agate - something a little more major than my current one (which is hardly tiny, either). I had to put it on hold for a while, though in order to a) prove to my prospective artist he wouldn't be wasting his time drawing for me and b) gain more experience in crafting something that's genuinely fun to play.

So I'll take a proper look at Agate myself soon enough.

Entropy's picture

As promised, here's an (extremely basic) 2D OpenTK program, which draws 2D squares and rotates them.

Like I say, it's nothing special, and you won't want to take too many tips from this, but if you're new to OpenGL it might come in handy as a basic working implementation.

Actually, I wouldn't be surprised if I get some criticism for posting this, as I'm probably much less experienced with OpenGL than a few other people on this forum, and I suspect that some of my methods might be a little inefficient or counter-intuitive.

That said, any criticism is welcome - good to know what I could improve.

//#define MOUSECONTROL
#region using statements
using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Math;
using OpenTK.Input;
#endregion
 
namespace OpenTKProject
{
    public class Game : GameWindow
    {
        #region Declarations
 
        const int InitialWidth = 1024;
        const int InitialHeight = 768;
 
        static float angle = 0;
        static float size = 25f;
 
        #endregion
 
        #region Constructor
        public Game()
            : base(InitialWidth, InitialHeight, new GraphicsMode(), "SquareDemo")
        { }
        #endregion
 
        #region Onload
        public override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
 
            GL.ClearColor(Color.Black);
            GL.Enable(EnableCap.Blend);
            GL.Disable(EnableCap.DepthTest);
 
            GL.Viewport(0, 0, Width, Height);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            Glu.Ortho2D(0.0, (double)Width, 0.0, (double)Height);
        }
        #endregion
 
        #region OnResize
        protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
        {
            base.OnResize(e);
            GL.Viewport(0, 0, Width, Height);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            Glu.Ortho2D(0.0, (double)Width, 0.0, (double)Height);
        }
        #endregion
 
        #region OnUpdateFrame
        public override void OnUpdateFrame(UpdateFrameEventArgs e)
        {
            base.OnUpdateFrame(e);
 
#if MOUSECONTROL
            angle += Mouse.XDelta;            
            size += Mouse.WheelDelta;
#else
            angle = angle + 5;
#endif
 
            if (Keyboard[Key.Escape])
                Exit();
        }
        #endregion
 
        #region OnUnload
        public override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
        }
        #endregion
 
        #region OnRenderFrame
        public override void OnRenderFrame(RenderFrameEventArgs e)
        {
            base.OnRenderFrame(e);
 
            GL.Clear(ClearBufferMask.ColorBufferBit);
 
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
 
            GL.Translate(350, 350, 0f);
 
            GL.PushMatrix();
            {
                GL.Rotate(angle, 0, 0, 1);
                GL.Color4(0.7, 0.5, 0.2, 0.5);
                GL.Begin(BeginMode.Quads);
                drawSquare();
                GL.End();
            }
            GL.PopMatrix();
 
            GL.PushMatrix();
            {
                GL.Translate(20, 50, 0);
                GL.Rotate(angle, 0, 0, 1);
                GL.Color4(0.2, 0.7, 0.4, 0.5);
                GL.Begin(BeginMode.LineLoop);
                drawSquare();
                GL.End();
            }
            GL.PopMatrix();
 
            SwapBuffers();
        }
 
        private static void drawSquare()
        {
            GL.Vertex2(-size, -size);
            GL.Vertex2(size, -size);
            GL.Vertex2(size, size);
            GL.Vertex2(-size, size);
        }
        #endregion
 
    }
 
    class Program
    {
        [STAThread]
        static void Main()
        {
            using (Game game = new Game())
            {
                game.Run(30, 30);
            }
 
        }
    }
}

Hope this is helpful!

flopoloco's picture

Nice one Entropy, thanks.

@Kanato
Nice library you built there, I will look further into it. :)

kanato's picture

Thanks, I'm glad you guys are interested in it. If you have questions about agatelib, let me know. I check the forums there just about every day, so even though it looks dead don't let that stop you from posting.