3

I'm working on a school project right now. It's meant to be a 3D editing software, (like a very minimized version of Maya). I have to write it in C#, using the 'Windows Application Project'. I intend to implement all the 3D stuff myself, from projections to shading. My question is, how can I get direct access to a single pixel in the C# windows application? I know I'm going to have a main view port in the window. But I haven't decided yet how it will be made. Is there a built in object that I can use, that will let me define the boundaries of the view port, and then paint each pixel individually? (I'm just shooting in the dark here, I don't know much about C#. I mostly program in C)

Thanks, Malki.

4

5 回答 5

3

Using the CreateGraphics() method, this should work:

Bitmap b = new Bitmap(Width, Height, this.CreateGraphics());
//pixel is:
Color c = b.GetPixel(x, y);

To set a pixel to a specific colour, use this instead of Color c = b.GetPixel(x,y):

b.SetPixel(x, y, c); // where c is a Color

If you want a viewport, place a panel or a PictureBox (maybe with Dock: Fill), then use:

Bitmap b = new Bitmap(viewport.Width, viewport.Height, viewport.CreateGraphics());

instead of the first line used previously.

But from what you want to do, I think it would be better to use the OnPaint event:

void pnlViewport_Paint(object sender, PaintEventArgs e) {
    if ( e.ClipRectange.Width < 1 || e.ClipRectangle.Height < 1 ) return;
    Bitmap b = new Bitmap(e.ClipRectangle.Width, e.ClipRectangle.Height, e.Graphics);
    // ...
}

This event fires every time the control needs painted. The first line checks whether the area being drawn is empty - this will not just save CPU time, but your application may crash - you are getting it to make a 0x0 bitmap.

EDIT: Yes, this is resizable, if Dock = DockStyle.Fill; As your window resizes, the control expands to fill the space. It is then repainted - firing the event.

EDIT 2: As pointed out by others, this is still slow. It does sound like it is a requirement to do the 3D drawing yourself, so maybe SDL.NET (which I think can use hardware acceleration) is the way to go. It even has a (slow) SurfaceControl to use.

于 2009-01-24T16:41:25.210 回答
2

Christian Graus has several good graphic-manipulation articles on www.CodeProject.com. They should answer your questions.

于 2009-01-24T16:34:26.457 回答
1

3D 编辑器应用程序不仅需要在屏幕上绘图 - 它还需要非常快地完成。.NET 中的标准方法是使用 Bitmap 和 Graphics 类,如person-b所述。但这会很慢。您可以通过使用Bitmap.LockBits()和巧妙的优化来提高速度,但总而言之,我怀疑您是否仍会获得所需的性能。你会吃掉所有的 CPU,并且仍然会卡在不到 10-20 FPS(数字是一个疯狂的猜测)。硬件加速是真正的方法。shuggycouk的链接方向正确。

不幸的是,硬件方法也会消除投影和阴影,因为这就是硬件的用途。所以我不知道你是否可以使用它......也许在.NET中寻找关于“覆盖”的信息。这就是所有媒体播放器软件显示电影的方式。它肯定比标准 GDI/GDI+ 快得多,而且仍然是 2D 的。或者,也许您可​​以使用 DirectX 做一个棘手的解决方法 - 使用它来绘制您自己定义的 2D 形状。;)

于 2009-01-24T17:00:03.910 回答
0

如果你正在做 3d,那么利用现在几乎所有 GPU 提供的硬件加速是有意义的。如果您乐于使用特定于 Windows 的 API,那么 DirectX 的 3D API 现在会通过托管包装器公开。还有主要是为游戏设计的XNA工具集,但它提供了大量示例,说明如何从 c# 进行 Direct-X 3D 加速绘图。

于 2009-01-24T16:50:32.150 回答
0

如果您需要像素速度,high-speed-graphing-control-for-net-or-mfc可能会提供一些提示。

于 2009-01-24T18:15:34.973 回答