Create a 2D - polygon as SceneElement

Feb 20, 2012 at 5:15 PM

I have to draw many 2D polygons, can i create each polygon as a independently SceneElement object?  I also need to modify the any vertex of any polygon.

Can i do this?

Coordinator
Feb 21, 2012 at 8:46 AM

You can indeed - there is in fact a Polygon primitive that contains a set of vertices, UVs, normals and faces that you can use for this. There is also a very basic sample that I'm working on named TwoDDrawingSample in the WPF samples that creates an orthographic projection and does some 2D drawing, this will be a good starting point - I'll update the sample tonight to include some polygon drawing.

Feb 21, 2012 at 2:14 PM

Hi, thanks for your reply.

As you say, indeed there is a Polygon primitive. But, i would like to know if this primitive or any other primitives (like Lines, Circles, etc.) could be a SceneElement (a node in the scene graph).

Coordinator
Feb 21, 2012 at 2:46 PM

Polygon is actually a Scene Element, and happily sits in the scene graph.

You can derive your own elements, just derive from Scene Graph, and optionally implement any of the following interfaces:

IBindable - Implement this interface if you need anything to be bound as part of the scene graph. For example, lights are IBindable.
IRenderable - Implement this interface to do any rendering.
IFreezable - IF you want to improve performance, implement this interface if any drawing can be 'frozen' (i.e. code that doesn't change that can be put into a display list).
IHasObjectSpace - Anything that implements this is transformed into object space before rendering.

There are other interfaces like these that you can implement if you choose - that essentially augment the functionality of the scene element, you can also look over existing scene elements (Light, Polygon, Quadric, NurbsSurface etc) as a starting point - does this help?

Feb 21, 2012 at 3:47 PM

Hi Dave,

This helps me a lot. Now i have additional questions:

  • Any SceneElement is able to be selected with the mouse? i mean can i select with the mouse a SceneElement in the screen (picking) and what about other mouse events (click, doubleclick, mouse_over...)?
  • The polygon primitive of SceneGraph (not OpenGL primitive) could be non-convex?
  • If i do not have a SceneGraph, does the OpenGL primitives (point, line, etc.) could have mouse events also? (click, double_click, mouse_over, etc.)
  • What is the functional difference between the Winforms and WPF versions of your OpenGL component?

I am asking all these questions because i am starting the developing of a little CAD application and i am looking for the graphics framework that allows me to do the following:

  • Select graphic objects through mouse events. Not only your SceneGraph primitives but OpenGL primitives too.
  • Manage the camera in 2D and 3D.  I want to be able to make zooming, panning.
  • Be able to export the content of the control to a image (JPEG, BMP, PNG).
Coordinator
Feb 23, 2012 at 8:45 AM

Hi,

Here are the answers to your questions:

  • Any SceneElement is able to be selected with the mouse? i mean can i select with the mouse a SceneElement in the screen (picking) and what about other mouse events (click, doubleclick, mouse_over...)?
    The Scene object doesn't do anything to handle mouse events, it only provides DoHitTest - which gives you enough to handle mouse events in your own application. The only Scene Elements that will be tested are those which implement IBoundable - as the hit test occurs over the objects bounding volume.
  • The polygon primitive of SceneGraph (not OpenGL primitive) could be non-convex?
    The polygon primitive renders convex polygons only, it doesn't render concave polygons. 
  • If i do not have a SceneGraph, does the OpenGL primitives (point, line, etc.) could have mouse events also? (click, double_click, mouse_over, etc.)
    OpenGL objects do not participate in any mouse events, if you are not using the scene graph you will need to implement picking by hand. This means rendering the view as normal, then rendering in GL_PICK mode and pushing a 'name' before you render each element. If you look over the Scene.DoHitTest code this will give you a starting point if you need a baseline. 
  • What is the functional difference between the Winforms and WPF versions of your OpenGL component?
    There are no functional differences - the differences are only in how they get the pixels onto the screen - the WinForms version has a control with a window handle, the WPF version has a WPF control in the visual tree - asides from this they are exactly the same and expose exactly the same functionality. Performance-wise they are comparable. 

I am asking all these questions because i am starting the developing of a little CAD application and i am looking for the graphics framework that allows me to do the following:
I want SharpGL to be usable in many situations. To this end, anything it doesn't do for you I am happy to implement. None of the features that you are missing will be difficult to incorporate. 

  • Select graphic objects through mouse events. Not only your SceneGraph primitives but OpenGL primitives too.
    This is generally very application-specific. If you are going to be making a CAD application, with a variety of primitives, I strongly recommend using the SceneGraph and creating your own SceneElements for your specific objects. The reason is:
    * You can quickly support serialization
    * You can leverage existing functionality (picking, display lists, object space etc)
    * Your custom classes are less 'tied' to OpenGL, meaning in theory you could switch to a different rendering system and keep large amounts of the code. 
    Without the Scene Graph you will need to do picking by hand. 
  • Manage the camera in 2D and 3D.  I want to be able to make zooming, panning.
    Take a look at the ArcBallCamera. If you pass it mouse events, it allows you to pan around an object in 3D. If you need a simple 2D pan/zoom camera that works in a similar way I can write one for you as a baseline. 
  • Be able to export the content of the control to a image (JPEG, BMP, PNG).
    You can get the gl pixels and then write them but if it would help then I'll add 'RenderToFile' to the OpenGL object.  

If you need to be able to manipulate vertices directly, then I think we need to consider something akin to widgets - these are scene elements that are associated with a low level object - for example a Vertex Grabber widget would be a scene element, render as a small red box around a vertex and be draggable - it would then move its associated vertex. SharpGL 1.83 had vertices that supported picking and more but having ALL vertices in the scene support this proved cumbersome, in most cases people need vertices to be absolutely simple and very fast. Instead, we can create overlays or widgets that allow very primitive objects (vertices, normals etc) to be manipulated via the mouse.

Why don't you describe in detail one of the primitives you need, what you need to do with it and how you want to view it, and I'll put together a basic example that shows how to build upon the Scene Graph to handle what you need :)

Feb 23, 2012 at 2:47 PM

Hi Dave, thanks for taking your time for the answers.

Ok i explain what i am trying to do:

I am starting to develop an utility application (CAD-like) that calculates geometric properties (area, inertia, centroid, ...) of a cross-section defined by a 2D-polygon as the boundary.  The cross-section boundary could be defined by a convex or non-convex polygon, and could have holes inside (another polygon would be the hole).

  • Then, once the cross-section is defined in 2D, i also want to have the ability to show it in 3D, by extruding the cross-section. I would show this 3D solid model in another form, just to view purposes.
  • I allow the user to enter the list of vertex of the polygon that defines the cross-section.
  • As you can see, i need to provide the functionality to show the cross-section (with its boundary polygon and its holes) in the screen in 2D.
  • I need to provide the ability to select (picking) the boundary polygon or any of the holes polygons.
  • I need to provide the ability to pick any vertex of the cross-section.
  • I need to provide the ability of mouse-over event. So the application can show some information when you are over a vertex, or the polygon. Also to show the 2D position (x, y) where the mouse is.
  • I need to provide to generate an image of the 2D render and the 3D render in order to generate some reports.
  • I need to provide a 2D camera that allows the displacement (panning) and zooming funcionality. I also need a 3D camera for the 3D solid model with the ability to rotate, zoom, etc.. (like a trackball).
  • I need to show a design grid, and the XYZ axis in 2D and 3D.

If you can create an example that shows these kind of functionality, it would be awesome!! and it would helps developers on how to use SharpGL in CAD-like applications.

If you want to see what i am talking about, you can see my latest version of my app (3.2) here: http://sourceforge.net/projects/secciones/  (http://secciones.wordpress.com/). I was developed with .Net 2.0 and winforms. Now i want to develop a new version from the scratch in .Net 4 using your OpenGL component.

Waiting for your comments.

Feb 28, 2012 at 7:55 PM

Hi Dave, any ideas about that? (the last reply) .

Coordinator
Mar 26, 2012 at 10:46 AM

I've got a small example app, when I get a chance I'll send it over :)

Mar 26, 2012 at 11:56 AM

please add it to your sample applications, thanks

Apr 4, 2012 at 6:26 AM
dwmkerr wrote:

You can indeed - there is in fact a Polygon primitive that contains a set of vertices, UVs, normals and faces that you can use for this. There is also a very basic sample that I'm working on named TwoDDrawingSample in the WPF samples that creates an orthographic projection and does some 2D drawing, this will be a good starting point - I'll update the sample tonight to include some polygon drawing.

Can you upload that TwoDDrawingSample in window form sample project??even i want to start CAD like application ,so that it will be helpful for me ...