Manish Pansiniya's Blog

.NET, C#, Javascript, ASP.NET and lots more…:)

Posts Tagged ‘ortho projection

Viewport and Ortho in OpenGL

leave a comment »

We are developing CAD Application in .NET language. After some analysis we confirm the use of OpenTK library as a OpenGL wrapper+utility.

You can get OpenTK Information from :

It is really great library and use for developing rapid opengl application. It also acts as .NET wrapper and so object oriented.

Now, the basic problem in startup we had to set our camera for the 2D view. We have tried different option but failed to setup properly the same. After some trial and error, I found how it can work.

Viewport basically, set the view area of your application while Ortho is setting projection area which is visible on the screen. But one thing to note that whatever you provide in Ortho is set as your unit on the screen.

Suppose, you want to show user Inch on the screen. and 96 px=1 Inch is your unit. You will do following to setup the screen.

OpenTK.Graphics.GL.Viewport(0, 0,(int)Math.Abs(viewPort.Width),(int)Math.Abs(viewPort.Height));



Glu.Ortho2D(ortho.Left, ortho.Right, ortho.Bottom, ortho.Top);

Now, for screen which draws in inch would require you to setup viewport and ortho as above. In above viewport rectangle will be the screen resolution or your control’s rectangle. Suppose you are making widows application and the drawing area is 1024×790 then it should be provided into Viewport.

In ortho rectangle I will provide ( 0,0, 1024/96,790/96) rectangle and that will be your projection to draw. so suppose the rectangle you have given is (0,0,5,5) and you draw line from (0,0) to (5,5), it should start from left top and end to right bottom part. And your drawing dimension/unit is between 0 to 5.  Quite easy!!!

Written by Manish

March 22, 2009 at 7:07 pm

Posted in .NET, OpenGL

Tagged with , ,