Xenland's picture

Up to date example on how to draw a line in a GameWindow?

I have been looking around the net for a working example of something simple I can play with, Drawing circles, triangle, lines, etc but I can't seem to find anything simple that works, I've tried some VB.NET examples and attempted to convert the code into C# with MonoDevelop, I tried a few other OpenTK examples I found from the tutorials area and only the rotating box one worked, Except that one was dealing with 3d and animation elements, and that’s a little ahead of my game to fooling around with that to make any progress with OpenTK. A thousand thank yous!

Also I have 0.1 Bitcoins for a tip-- if you are willing to accept Bitcoins.


Comments

Comment viewing options

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

Oh yeah so far My code is this with a GameWindow

using System;
using OpenTK;
using OpenTK.Graphics;
 
class OpenGlTest1{
 
	public static void Main(){
		GameWindow GLWindow = new GameWindow(1200,700);
		GLWindow.Run();
 
	}
}
flopoloco's picture

Here's a simple one.

using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics.OpenGL;
 
namespace DrawLines
{
	class Program : GameWindow
	{
		public Program()
		{
			Size = new Size(1024, 768);
			GL.LineWidth(4f);
		}
 
		protected override void OnResize(EventArgs e)
		{
			// Set orthographic rendering (useful when you want 2D)
			GL.MatrixMode(MatrixMode.Projection);
			GL.LoadIdentity();
			GL.Ortho(this.ClientRectangle.Left, this.ClientRectangle.Right,
				this.ClientRectangle.Bottom, this.ClientRectangle.Top, -1.0, 1.0);
			GL.Viewport(this.ClientRectangle.Size);
		}
 
		protected override void OnRenderFrame(FrameEventArgs e)
		{
			//if (time == 0f)
			GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
			GL.MatrixMode(MatrixMode.Modelview);
      		GL.LoadIdentity();
 
			GL.Begin(BeginMode.Lines);
 
			GL.Vertex2(0, 0);
			GL.Vertex2(Width, Height);
 
			GL.Vertex2(Width, 0);
			GL.Vertex2(0, Height);
 
 
      		GL.End();
 
 
			SwapBuffers();
		}
 
		public static void Main(string[] args)
		{
			using (Program p = new Program())
			{
				p.Run();
			}
		}
	}
}

The real deal if you want to draw circle then you have to do it hardcore but if you're interested then I can write the drawing method.

Xenland's picture

This code worked great!

I see on my screen there are two lines going in an X pattern (I hope thats what was intended) I learned alot about programming I didn't know just from your simple example, For instance I noticed that you called the GameWindow into a Program() from inside of the Main() What is the terminology of this practice called so I can do more research on its' useage.

Also if you could be so kind to write a drawing Circle example?

All my knowledge is going towards my "Tutorials for Beginners By a Beginner" You see I am very knowledgeable with PHP, Actionscript 3 and Java but C# seems to be slightly different and I noticed there isn't any clear tutorials for C# users on Ubuntu Linux so I am writing up tutorials from Console printing, Networking, and Drawing.
I appreciate every bit of help thanks :) and I would love to link back to the original source of the information I got if that is an issue.

My tutorials will always be free and opensource: http://pipboy3000.com/list_tutorials.php?category=2

flopoloco's picture

Hello, good idea to create tutorials so even more people could find OpenTK useful and learn it.

Here is an updated version

using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
 
namespace DrawLines
{
	class Program : GameWindow
	{
		public class Shapes
		{
			public static void DrawCircle(float x, float y, float radius, int segments)
			{
				// http://slabode.exofire.net/circle_draw.shtml
				GL.Begin(BeginMode.LineLoop);
 
				for (int i = 0; i < segments; i++)
				{
					float theta = (2.0f * (float)Math.PI * (float)i) / (float)segments;
					float xx = radius * (float)Math.Cos(theta);
					float yy = radius * (float)Math.Sin(theta);
					GL.Vertex2(x + xx, y + yy);
				}
 
				GL.End();
			}
 
			public static void DrawLine(float x1, float y1, float x2, float y2)
			{
				GL.Begin(BeginMode.Lines);
				GL.Vertex2(x1, y1);
				GL.Vertex2(x2, y2);
      			GL.End();
			}
 
			public static void DrawBox(float x, float y, float size)
			{
				float half = (size/2);
				float x1 = x - half;
				float y1 = y - half;
				float x2 = x + half;
				float y2 = y + half;
 
				GL.Begin(BeginMode.Lines);
				GL.Vertex2(x1, y1); GL.Vertex2(x2, y1); // bottom
				GL.Vertex2(x1, y2); GL.Vertex2(x2, y2); // upper
				GL.Vertex2(x1, y1); GL.Vertex2(x1, y2); // left
				GL.Vertex2(x2, y1); GL.Vertex2(x2, y2); // right
				GL.End();
			}
		}
 
		public Program()
		{
			Size = new Size(1024, 768);
			GL.LineWidth(4f);
		}
 
		protected override void OnResize(EventArgs e)
		{
			// Set orthographic rendering (useful when you want 2D)
			GL.MatrixMode(MatrixMode.Projection);
			GL.LoadIdentity();
			GL.Ortho(this.ClientRectangle.Left, this.ClientRectangle.Right,
				this.ClientRectangle.Bottom, this.ClientRectangle.Top, -1.0, 1.0);
			GL.Viewport(this.ClientRectangle.Size);
		}
 
		protected override void OnRenderFrame(FrameEventArgs e)
		{
			//if (time == 0f)
			GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
			GL.MatrixMode(MatrixMode.Modelview);
      		GL.LoadIdentity();
 
			// Draw line
			GL.Color3(Color.Red);
			Shapes.DrawLine(10f, 10f, 400f, 400f);
 
			// Draw box
			GL.Color3(Color.Yellow);
			Shapes.DrawBox(400f, 400f, 100f);
 
			// Draw circles
			GL.Color3(Color.Cyan);
			Shapes.DrawCircle(600f, 600f, 100f, 32);
			GL.Color3(Color.Orange);
			Shapes.DrawCircle(400f, 600f, 100f, 16);
 
			SwapBuffers();
		}
 
		public static void Main(string[] args)
		{
			using (Program p = new Program())
			{
				p.Run();
			}
		}
	}
}

Note: By the moment I write this I think that the box drawing method could use a line loop instead to get better results, but see for yourself if you can actually modify it.

By instantiating a variable inside using, you can get more precise on how you want garbage collector to behave. You might not use it (it will work also without it), but it's a common practice in OpenTK (also you might see it file handling, or other data manipulation practices).
http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx

Another thing you might wonder why I instantiate the variable Program p, this is because Main() is static, static methods (every static method) can't communicate with the class they belong, they only communicate with other static members (properties/methods) as long as they are accessible.

Xenland's picture

That last code worked great as well too! Thanks, I can not wait till I get to the drawing parts of my tutorials section, this stuff is fun!