Select DXF objects in OpenGL rendering orthographic projection

< previous    1 2
overmaster
10/31/2007 5:23 PM

Hi,

Excuse me but I again have problems with OpenGL.

1) I cannot get correct result from method EntitySelector.GetEntitiesInRectangle() but method EntitySelector.GetClosestEntities() works correct. I create Rectangle2D by using mouse then transforms points of rectange how you show me in example then pass this Rectangle2D to EntitySelector.GetEntitiesInRectangle() but method returns IList&lt;IList&lt;DxfEntity&gt;&gt; closestEntities.Count = 0. I don't undestand why method EntitySelector.GetClosestEntities() works correct and method EntitySelector.GetEntitiesInRectangle() not correct. Can you check this in your example and answer me about results?

2) I cannot set white background in OpenGL? How I can do it?

Wout
11/1/2007 6:06 PM

Hi,

1) Make sure the rectangle's first coordinate is the bottom left, and the second coordinate is the top right.
2) Use e.g. GL.ClearColor(0.2f, 0.7f, 0f, 0f); to change the background color where OpenGL is initialized (see the OnHandleCreated override).

Wout

overmaster
11/2/2007 10:19 AM

Hi,

Thanks for your anwer. It's really work but I have question for you.
How calculate real CAD point's coordinates (DxfModel points coordinate) from mouse click point in OpenGL? In GDI I use next calculations
Point3D pointInScreenSpace = new Point3D(mousePoint.X, mousePoint.Y, 0d);
Point3D pointInModelSpace = to2TTransform.GetInverse().Transform(pointInScreenSpace);
How about same calculations in OpenGL?

Wout
11/2/2007 12:39 PM

It is the same with OpenGL, with the minor modification that the screen point must now be defined in clip space (so normalized to -1, 1 as you have seen already). Alternatively you could correct for the viewport transformation in stead of normalizing the screen point, as I've explained before. Note that you should be using a orthographic transformation (not perspective).

Wout

overmaster
11/2/2007 1:57 PM

Sorry but I cannot get correct result from calculating real CAD point's coordinates (DxfModel points coordinate) from mouse click point in OpenGL. I am using next code:

Matrix4D modelViewTransform = GetModelViewTransform();
Matrix4D projectionTransform = GetProjectionTransform();
double clientWidth = (double)ClientSize.Width;
double clientHeight = (double)ClientSize.Height;
Point2D startPoint = new Point2D((double)mouseClickLocation.X / clientWidth * 2d - 1d,
   -(double)mouseClickLocation.Y / clientHeight * 2d + 1d);
Point2D endPoint = new Point2D((double)e.X / clientWidth * 2d - 1d,
   -(double)e.Y / clientHeight * 2d + 1d);
DxfPolyline2D createdPolyline = MathDxf.ConvertToPolyline(
   new Rectangle2D(startPoint.X, startPoint.Y, endPoint.X, endPoint.Y),
   modelViewTransform * projectionTransform);

public static DxfPolyline2D ConvertToPolyline(Rectangle2D rectangle, Matrix4D matrix)
    {
      Matrix4D normalizedMatrix = Matrix4D.Identity;    
      matrix = matrix * normalizedMatrix;      
      Point3D point1 = new Point3D(rectangle.Corner1.X, rectangle.Corner1.Y, 0d);
      point1 = matrix.GetInverse().Transform(point1);   
      Point3D point2 = new Point3D(rectangle.Corner1.X, rectangle.Corner2.Y, 0d);
      point2 = matrix.GetInverse().Transform(point2);  
      Point3D point3 = new Point3D(rectangle.Corner2.X, rectangle.Corner2.Y, 0d);
      point3 = matrix.GetInverse().Transform(point3);   
      Point3D point4 = new Point3D(rectangle.Corner2.X, rectangle.Corner1.Y, 0d);
      point4 = matrix.GetInverse().Transform(point4);    

      DxfPolyline2D resultPolyline = new DxfPolyline2D();
      resultPolyline.Closed = true;
      resultPolyline.Vertices.Add(point1.X, point1.Y);
      resultPolyline.Vertices.Add(point2.X, point2.Y);
      resultPolyline.Vertices.Add(point3.X, point3.Y);
      resultPolyline.Vertices.Add(point4.X, point4.Y);
      return resultPolyline;
    }

Can you say me what I've wrote wrong?

Wout
11/2/2007 2:03 PM

Hi,

Looks like you have reversed the order of model view and projection transform. The projection transform takes place last, which means it has to be first in the matrix multiplication:

C# Code:
DxfPolyline2D createdPolyline = MathDxf.ConvertToPolyline(
   new Rectangle2D(startPoint.X, startPoint.Y, endPoint.X, endPoint.Y),
   projectionTransform * modelViewTransform
);

Also I suggest calculating the inverse matrix just once in stead of for each point again, because it's a fairly expensive operation. Probably you can cache it somewhere.

Wout

overmaster
11/2/2007 2:09 PM

Thanks I think order of matrix not importent. :)

< previous    1 2