我们应该使用 OpenGL 来处理 2D 图形吗?
如果我们想做一个像MS Paint这样的应用程序,我们应该使用OpenGL来渲染图形吗? 我想谈谈使用传统 GDI 与 OpenGL 时的性能。 如果有一些更好的库用于此目的,请看我的一个。
If we want to make an application like MS Paint, should we use OpenGL for render graphics?
I want to mention about performance if using traditional GDI vs. OpenGL.
And if there are exist some better libs for this purpose, please see me one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GDI、X11、OpenGL...都是渲染API,即您通常不使用它们进行图像操作(您可以这样做,但需要一些预防措施)。
在像 MS Paint 这样的绘图应用程序中,如果它是基于像素的,您通常会使用习惯代码或特殊的图像处理库来操作一些图片缓冲区,然后将完整的缓冲区发送到渲染 API。
如果您的数据模型由笔划和单个形状(即矢量图形)组成,那么 OpenGL 会是一个非常好的后端。然而,可能值得研究一些其他矢量图形 API,例如 OpenVG(其当前实现位于 OpenGL 之上,但可能会出现直接在 GPU 上运行的本机实现)。
在您的使用场景中,您不会在当前计算机上遇到任何性能问题,因此不要根据该标准选择 API。在纹理、Alpha 混合等方面,OpenGL 肯定比 GDI 更快。但是,根据系统和 GPU,对于绘制圆弧或使用复杂的缠绕规则填充复杂的自相交多边形等简单的操作,纯 GDI 可能会优于 OpenGL,具体取决于系统和 GPU。
GDI, X11, OpenGL... are rendering APIs, i.e. you usually don't use them for image manipulation (you can do this, but it requires some precautions).
In a drawing application like MS Paint, if it's pixel based, you'll normally manipulate some picture buffer with customary code, or a special image manipulation library, then send the full buffer to the rendering API.
If your data model consists of strokes and individual shapes, i.e. vector graphics, then OpenGL makes a quite good backend. However it may be worth looking into some other API for vector graphics, like OpenVG (which in its current implementations sits on top of OpenGL, but native implementations operating directly on the GPU may come).
In your usage scenario you'll not run into any performance problems on current computers, so don't choose your API from that criteria. OpenGL is definitely faster than GDI when it comes to texturing, alpha blending, etc. However depending on system and GPU pure GDI may outperform OpenGL for so simple things like drawing an arc or filling a complex self intersecting polygon with complex winding rules.
没有充分的理由不为此使用 OpenGL。除非您拥有多年的 GDI 经验,但对 OpenGL 一无所知。
另一方面,OpenGL 在许多情况下可能更优越。如果计算机中有一张相当新的卡,在 GLSL 着色器中合成图层或调整色调/饱和度/亮度/对比度将会快几个数量级(事实上,几乎“立即”)。使用“模糊”笔抚摸自由绘制路径(即一遍又一遍地将精灵与 alpha 透明度混合)将会快几个数量级。在尺寸合理的图像上,大多数过滤器内核应该接近实时运行。使用双线性过滤进行重新缩放在硬件中运行。
这些事情在 512x512 图像上并不重要,因为在这样的分辨率下几乎一切都是瞬时的,但在数码相机的典型 4096x3072(或更大)图像上,它可能非常明显,特别是如果您有 4-6 层。
There is no good reason not to use OpenGL for this. Except maybe if you have years of experience with GDI but don't know a single thing about OpenGL.
On the other hand, OpenGL may very well be superior in many cases. Compositing layers or adjusting hue/saturation/brightness/contrast in a GLSL shader will be several orders of magnitude faster (in fact, pretty much "instantly") if there is a reasonably new card in the computer. Stroking a freedraw path with a "fuzzy" pen (i.e. blending a sprite with alpha transparency over and over again) will be orders of magnitude faster. On images with somewhat reasonable dimensions, most filter kernels should run close to realtime. Rescaling with bilinear filtering runs in hardware.
Such things won't matter on a 512x512 image, as pretty much everything is instantaneous at such resolutions, but on a typical 4096x3072 (or larger) image from your digital camera, it may be very noticeable, especially if you have 4-6 layers.