8

I would like to write a simple ray tracer using WPF. It is a learning project and thus I favour configurability over performance (otherwise I'd go for C++).

I still want relatively fast pixel drawing. A previous question on StackOverflow contains code to achieve this in WPF, by obtaining a GDI bitmap. From the relatively little I know about Windows programming,

  1. GDI is slow
  2. DirectX is fast
  3. WPF uses DirectX underneath (not sure which parts of WPF though)

Is it possible to obtain pixel-level access using DirectX (not GDI) through the WPF Canvas (or similar)?

I will also consider suggestions for incorporating DirectX API calls within a WPF window (alongside other WPF controls) if that is possible.

Thanks in advance.

Community
  • 1
  • 1
Gigi
  • 28,163
  • 29
  • 106
  • 188
  • Simplest would be to create a command line application that renders something simple like a tetrahedron and save it to a png. Once you have that you would have solved a lot of problems related to the raytracer itself. Then you can worry about drawing to the screen etc. – Indy9000 Sep 25 '12 at 12:09
  • @Gigi: I don't think it's wise to attempt to write directly to the screen in WPF - see here: http://blogs.msdn.com/b/greg_schechter/archive/2006/05/02/588934.aspx – Surfbutler Sep 25 '12 at 12:18
  • 2
    I think using a `WritableBitmap` could be a good idea. – CodesInChaos Sep 25 '12 at 12:24
  • 1
    @Surfbutler - that relates to using GDI, which the Gigi has already ruled out – Kieren Johnstone Sep 25 '12 at 12:30
  • @KierenJohnstone: I stand by the article I previously linked to - it indicates direct writes to the primary (screen) aren't allowed, and would be slow anyway (see the last section 'Drawing To and Reading From the Screen' – Surfbutler Sep 25 '12 at 14:10
  • @Surfbutler I think the article you linked to is talking about something different - i.e. directly writing to video memory as opposed to writing to a window (or component thereof), which is what I want to do. Besides, the author is warning against something GDI-specific. – Gigi Sep 25 '12 at 17:26
  • @Gigi: ok fair enough, just didn't want you running into problems - I also vote for WritableBitmap! – Surfbutler Sep 25 '12 at 17:33

3 Answers3

5

Interesting, but with raytracing, writing the pixels to the screen will (should) not be the slow part. You can use WriteableBitmap for the purpose, though. It's certainly quick enough for what you want.

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.aspx

(For info, I use it in this emu/IDE - http://0x10c-devkit.com/ - and it can refresh a low res display with great performance. There's the source to that on the github repository, the LEM1802 plugin.)

Ah, this bit: https://github.com/kierenj/0x10c-DevKit/blob/master/PluginAPI/NyaElektriska.LEM1802/GPU.cs - see UpdateDisplay.

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
  • You raise a valid point. I will do some research based on the links provided, and probably go with this. – Gigi Sep 25 '12 at 13:37
  • Just to go along with the answer, I found this link helpful when attempting my implementation: http://tipsandtricks.runicsoft.com/CSharp/WpfPixelDrawing.html – Gigi Sep 25 '12 at 17:53
3

Another solution is WriteableBitmapEx. It extends the builtin WriteableBitmap.

henon
  • 2,022
  • 21
  • 23
Joel Lucsy
  • 8,520
  • 1
  • 29
  • 35
2

There is an open Source Project Called Direct Canvas wich is A hardware accelerated, 2D drawing API that supports vector graphics, multimedia files, extensible pixel shaders, blending modes and more!

http://directcanvas.codeplex.com/

Demo http://www.youtube.com/user/jdollah69#p/u

Pascalsz
  • 1,092
  • 11
  • 10
  • Unless the raytracing is done in a pixel shader, that's no use, and there will definitely be no use for vector graphics, multimedia files or blending modes. Dude wants to write pixels, that's all – Kieren Johnstone Sep 25 '12 at 12:32