简体   繁体   English

如何在 DirectX 中绘制橡皮筋矩形?

[英]How do Draw Rubber band rectangle in DirectX?

How can I draw a 2D rubber band rectangle in DirectX?如何在 DirectX 中绘制 2D 橡皮筋矩形? Preferable using C#.最好使用 C#。

The classic way to do rubberbanding is to:做橡皮筋的经典方法是:

  • Draw the regular scene, ie the "background"绘制常规场景,即“背景”
  • Switch to "XOR mode", where your pen will XOR the pixels already in the framebuffer.切换到“XOR 模式”,您的笔将对帧缓冲区中已有的像素进行 XOR。 It is important that the mode here is XOR, since XOR is losslessly "revertable", unlike for instance an add operation.这里的模式是 XOR 很重要,因为 XOR 是无损“可恢复的”,这与添加操作不同。
  • While "rubberbanding":虽然“橡皮筋”:
    • Draw the rubberband rectangle in its current coordinates在其当前坐标中绘制橡皮筋矩形
    • On the next frame, draw the rectangle again, still using XOR, using its previous coordinates.在下一帧,再次绘制矩形,仍然使用 XOR,使用其先前的坐标。 This removes the rectangle, leaving the framebuffer intact.这将删除矩形,保持帧缓冲区完好无损。
    • Update the rectangle's coordinates更新矩形的坐标

Details on how to do this "with DirectX" are a bit harder ... I believe DirectDraw, the "old school" way of doing 2D in DirectX is obsolete, so I guess you must be using Direct3D.关于如何“使用 DirectX”执行此操作的详细信息有点困难......我相信 DirectDraw,在 DirectX 中进行 2D 的“老派”方式已经过时了,所以我猜你一定在使用 Direct3D。 Unfortunately I'm not sure on the details, there.不幸的是,我不确定那里的细节。 It might be that you're out of luck with finding pre-defined functionality to do XOR drawing, and need to roll your own.可能是您找不到预定义的功能来进行 XOR 绘图,并且需要自己动手。 Hopefully someone can provide better details on that.希望有人可以提供更好的细节。 Apologies if you were already up to speed on the rubberbanding theory itself.抱歉,如果您已经了解橡皮筋理论本身。

You need two point variables (each one holding x and y coordinates).您需要两个点变量(每个变量都包含 x 和 y 坐标)。 Let's call them FirstPoint and SecondPoint.我们称它们为 FirstPoint 和 SecondPoint。

On mousedown, store the current mouse position to FirstPoint and SecondPoint.在鼠标按下时,将当前鼠标位置存储到 FirstPoint 和 SecondPoint。 As long as the mouse is down, whenever it moves, update SecondPoint with the new mouse position.只要鼠标按下,无论何时移动,都会用新的鼠标位置更新 SecondPoint。

Then each frame if the mouse is down, draw a rectangle based on the two corners, FirstPoint and SecondPoint.然后每帧如果鼠标按下,则根据两个角,FirstPoint 和 SecondPoint 绘制一个矩形。 You'll need a tiny bit of math to detect if they are top-left/bottom-right or top-right/bottom-left and which one is which, but then you just draw a rectangle between them, or two triangles.你需要一点数学来检测它们是左上角/右下角还是右上角/左下角,哪个是哪个,但是你只需要在它们之间画一个矩形,或者两个三角形。 You would of course have to switch to orthographic (2D) mode before drawing.您当然必须在绘图之前切换到正交 (2D) 模式。

I'm not sure about unwind's answer, I don't even know how to do an XOR operation and it seems overly complicated, unless you specifically want the rectangle to be a negative of the background (which is what the XOR operation would do, I believe).我不确定 unwind 的答案,我什至不知道如何进行 XOR 操作,而且看起来过于复杂,除非您特别希望矩形成为背景的负数(这就是 XOR 操作会做的事情,我相信)。 Furthermore, if you are redrawing the scene each frame, there is no reason to do some silly XOR back and forth in order to erase the previous rectangle;此外,如果您每帧都重新绘制场景,则没有理由来回执行一些愚蠢的 XOR 操作以擦除前一个矩形; it would already have been written over by drawing the new frame.通过绘制新框架,它已经被覆盖了。

I would personally prefer to draw a rectangle with a solid outline and translucent fill;我个人更喜欢画一个有实心轮廓和半透明填充的矩形; for this, set the current color to have an alpha value of maybe 128 (or 0.5f) and draw the rectangle between the two points, and then change the polygon draw mode to line and set the color solid, and draw another rectangle.为此,将当前颜色设置为 alpha 值可能为 128(或 0.5f)并在两点之间绘制矩形,然后将多边形绘制模式更改为线条并将颜色设置为纯色,并绘制另一个矩形。

Finally, I'm sure you would want to react to the rubberband;最后,我相信你会想要对橡皮筋做出反应; when the mouse is released, do whatever you need based on the FirstPoint and SecondPoint variables.释放鼠标后,根据 FirstPoint 和 SecondPoint 变量执行您需要的任何操作。 Also your draw code should only be drawing the rectangle while the mouse is down, so it will stop being drawn.此外,您的绘制代码应该只在鼠标按下时绘制矩形,因此它将停止绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM