PDA

View Full Version : Is Open GL a good platform for drawing 3D lines?



plnelson
02-16-2016, 08:32 AM
I'm responsible for an industrial application written for Windows in C# for WPF/.Net that controls a bank of industrial lasers. The app already exists and runs, and for its graphics output it uses the basic line-drawing and 3D transform API's in WPF.

Graphics performance is terrible so I want to replace that part with a hardware-accelerated API such as OpenGL 3D or Direct3D.
N.B. I only need to draw lines (fully 3D transformed, with perspective) and text, not lighted, shaded surfaces. Also N.B. this is not the same as wireframe - the lines do not necessarily represent surfaces and I do NOT want polygons tesselated into triangles. Just think of these as thousands of lines that can be rotated, scaled, translated and (if desired) viewed in perspective. The lines should be able to assume specific colors and the text is there to label details of what's being represented.

Questions:

1. Does OpenGL have a 3D line-drawing primitive?

2. How easy is OpenGL to integrate with WPF? I want to run the OpenGL part in some sort of WPF control like a Panel or Canvas so it integrates well with the surrounding UI and so I can mouse over the graphics to draw rectangular regions of interest, etc.

3. Where can I see some sample hello world code that shows how to integrate this with WPF (not Windows Forms)?

Thanks in advance.

GClements
02-16-2016, 11:03 AM
1. Does OpenGL have a 3D line-drawing primitive?

Yes.

To get decent performance, you don't want to be making a function call for each line segment. You ideally need an array of vertex coordinates so that you can tell OpenGL to draw many line segments with a single call.

Also, if the coordinates aren't changing continually, you want to avoid having to transfer all of the vertex coordinates from system memory to video memory every frame. This basically means explicitly managing the copying of data between system memory and video memory so that you only need to copy the portions which have changed.

In short, you get better performance if you structure the data around what's convenient for the hardware than if you design the data structure in isolation then deal with rendering as an afterthought. Common OOP practices tend to work against you here.

I have no experience with WPF so I can't answer the WPF-related aspects.

Carmine
02-16-2016, 10:09 PM
Of course OpenGL can draw 3D lines. It draws points, lines, and polygons. And it does so very quickly. How many lines are you talking about? You don't even have to worry about hardware acceleration unless there are hundreds of thousands of lines. Try googling 'WPF OpenGL' to get some sample code. One link that looked promising to me was THIS (http://www.codeproject.com/Articles/265903/Using-OpenGL-in-a-WPF-Application).

carlamengo
02-23-2016, 12:11 PM
If your going the OpenGL route, search for the nuget package 'OpenTK'.
We used it as a base to build a editor for a application in the automotive segment, the application is written in C#, gui is WPF and we use MVVM as pattern,
but we had to host the OpenTK Control in a winforms host.

If you only have to draw a couple of thousands lines just go for the old OpenGL immediate mode and dont invest in modern OpenGL.
code to draw one line:
GL.Begin(PrimitiveType.Lines);
GL.Color4(color.R, color.G, color.B, color.A);
GL.Vertex3(start.X, start.Y, start.Z);
GL.Vertex3(end.X, end.Y, end.Z);
GL.End();

Your other problem will be rendering text, there is no support for drawing texts in OpenGL.
So you need to use a third party tool. At the moment we use a modified QuickFont implementation (search the nuget package), but we plan to replace it with a own font rendering engine in the (not so near) future.

plnelson
03-23-2016, 01:46 PM
. . .
but we had to host the OpenTK Control in a winforms host . . .
. . .

Your other problem will be rendering text, there is no support for drawing texts in OpenGL.
So you need to use a third party tool. At the moment we use a modified QuickFont implementation (search the nuget package), but we plan to replace it with a own font rendering engine in the (not so near) future.

Why the Winforms host? That just adds another layer of complexity to the problem. Is it really necessary for OpenGL?

Also, If I can layer a WPF canvas over the window (/panel/canvas/whatever) that has the graphics I can make the overlay canvas transparent and put text in that using WPF, since I don't need the text to be 3D or rotatable.

My other option that I'm exploring is Direct3D and it's going to come down to which can be more seamlessly integrated with WPF.