Manish Pansiniya’s Blog

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

Archive for the ‘OpenGL’ Category

Easiest Method to Find Point on Circle

without comments

Following is the code to detect whether point is in circle or not :

public static bool IsPointInCircle(UnitPoint center, float radius, UnitPoint testPoint, float halflinewidth)
        {
            float dist = Distance(center, testPoint);
            if ((dist >= radius – halflinewidth) && (dist <= radius + halflinewidth))
                return true;
            return false;
        }

 

Parameter contains Center Point, radius and test point. halflinewidth is same as threshold value. So that it will check the point for Circle with some width provided in halflinewidth.

It finds distance from center and if that distance somehow in between radius and halflinewidth radius. :) quite easy. Let me know if you need more detail into that.

Written by Manish

March 27, 2009 at 1:00 pm

Posted in .NET, .NET 3.0, Algorithm, OpenGL

Viewport and Ortho in OpenGL

without comments

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 : www.opentk.com

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));

GL.MatrixMode(MatrixMode.Modelview);

GL.LoadIdentity();

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 , ,

Simple Fill Polygon in OpenGL

without comments

I am wondering around to find the fill polygon routine in OpenGL. I have closed shape as I am developing CAD/CAM application. And i needed to fill the close shape. In start, it seems difficult, but it is as simple as to eat PIZZA

Just convert the close shape to polygon and call following code to fill the polygon .

GL.Color3((System.Drawing.Color)color);
GL.Disable(EnableCap.Texture2D);
GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
GL.Begin(BeginMode.Polygon);
for (int j = 0; j < sides; j++)
{
GL.Vertex2(xPoints[j], yPoints[j]);
}
GL.End();
GL.Enable(EnableCap.Texture2D);
GL.Flush();
GL.Finish();

 

That’s all and the thing work like charm.

Simple but thought that it should be shared !!!

Written by Manish

March 20, 2009 at 3:45 pm

Posted in .NET, .NET 3.0, OpenGL