如何用透视绘制位图?
我需要用透视图绘制 Windows 位图,例如:
alt text http://img410 .imageshack.us/img410/3905/example.png
仅使用 Windows API C 函数(无 MFC、无 DirectX 等)
I need to paint a Windows bitmap with perspective, in example:
alt text http://img410.imageshack.us/img410/3905/example.png
only using Windows API C functions (no MFC, no DirectX, etc.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实现此算法。
Implement this algorithm.
如果您无法使用 OpenGL(或 Direct3D),那么您可能必须编写自己的光栅化器。 我还要指出,这是一个糟糕的想法,如果您可以使用 OpenGL 或 Direct3D,那么您将为自己节省大量工作,即使很难让这些东西与您的应用程序一起工作。 是的,您可以使用 OpenGL 或 Direct3D 渲染为纹理,而无需创建屏幕上下文。
从理论上讲,您将对正方形上的每个点应用透视投影(查找该术语)。 这是每个点,不仅仅是角点,而是之间的所有点。 通常,如果您仅将变换应用于四个点,人们会发现失真是可以接受的,所以我们这样做。
一旦通过使用透视变换将 3D 点转换为 2D 点,您就可以绘制多边形,将像素从纹理复制到目标上下文中。 您需要循环遍历目标中的每个像素并计算哪个源像素最终到达那里。 如果它仅绕图片中显示的 X 轴旋转,那么您将使事情变得容易得多,因为目标中的每一行都对应于源中的一行。 《德军总部》(Wolfenstein) 利用相同的技巧(Y 轴除外)在 286 上进行实时透视渲染,《毁灭战士》(Doom)也这么做了。
如果您仍然想继续但不知道我在说什么,请查找一本告诉您如何制作自己的毁灭战士克隆的旧书。 这些书有您正在寻找的代码。 一两个现已在线提供,请访问此 wiki 并查看“3D 游戏编程的黑艺术”或“Windows 游戏编程大师的技巧”。 可能还有一两个人在那里。
If you can't use OpenGL (or Direct3D) then you're probably going to have to write your own rasterizer. I'm also going to point out that this is a terrible idea, and if you can use OpenGL or Direct3D then you'll save yourself an enormous amount of work, even if it's really hard to get these things to work with your app. Yes, you can use OpenGL or Direct3D to render into a texture without creating an on-screen context.
On the theoretical side, you're applying a perspective projection (look up that term) to each of the points on your square. That's each point, not just the corners but all the points between. People find the distortion acceptable if you just apply the transformation to the four points, usually, so we do that instead.
Once your 3D points are transformed into 2D points by use of a perspective transformation, you draw the polygon, copying pixels from your texture into your destination context. You'll want to loop across each pixel in the destination and calculate which source pixel ends up there. If it's only rotated about the X axis as it appears in the picture, then you'll make things a lot easier as each row in the destination corresponds to a row in the source. Wolfenstein exploited the same trick (except about the Y axis) for perspective rendering in real time on a 286, Doom did too.
If you still want to continue but don't know what I'm saying, look up one of those old books that tells you how to make your own Doom-clone. Those books have the code you're looking for. One or two is now available online, go to this wiki and look for "Black art of 3D game programming" or "Tricks of the Windows game programming gurus". One or two others might be there.