像素阵列 CPU 密集型 [英] Pixels Array Too CPU Intensive

查看:29
本文介绍了像素阵列 CPU 密集型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从事 Java 2D 游戏已有一段时间了.它是一个具有像素阵列(整数)的光栅图形系统.

I have been working on a Java 2D game for a little while. It is a raster graphics system with an array of pixels (integers).

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

然后我分别创建一个整数数组,使用屏幕对象(从外部图像文件渲染,例如 .png)对其进行操作,然后将该数组复制到我的主要数组中,该数组投影在屏幕上.我发现在迭代中使用数组复制方法没有性能差异(并且真的没想到).无论如何,图形渲染良好,游戏进展顺利.

I then create an array of integers separately, manipulate it with the screen objects (rendered from external image files, such as .png), then copy that array into my main one, which is projected on the screen. I found no performance difference (and really didn't expect to) in using array copying methods over iteration. Regardless, the graphics render well and the game is coming along swimmingly.

但是,我发现这非常占用 CPU.我的活动监视器显示应用程序使用了超过 100% 的 CPU.这显然是因为我每次更新(每秒 60 次)都要遍历一个像素数组(76k 个整数).

However, I have found this to be extremely CPU intensive. My activity monitor says that the application is using more than 100% of my CPU. This is obviously because I am iterating through a pixel array (76k integers) each update (60 times per second).

我选择这种技术是出于教育目的.这是一个个人项目,我只是想深入了解 Java 图形.我决不喜欢这种渲染技术.

I chose this technique for educational purposes. This is a personal project and I simply wanted to get insight into Java graphics. I am, by no means, married to this rendering technique.

我的问题分为三个相关部分...

My question comes in three related parts...

  1. 显然有更好的方法来做到这一点.哪些库/框架会做得更好?
  2. 这些库本质上会做同样的事情(遍历像素),只是以更有效的方式?
  3. 有没有一种方法可以在不使用外部工具的情况下优化此技术,或者它是否不值得?

推荐答案

OpenGL 最常与 3D 图形应用程序相关联,但它也可用于 2D 应用程序.使用正交投影和带纹理的四边形来代替精灵和背景图像,您可以为完全没有 3D 元素的游戏构建图形和界面,尽管在某种意义上说,绘图正在完成3D.

OpenGL is most often associated with 3D graphics applications, but it can also be used for 2D applications. Using an orthographic projection and textured quads to take the place of sprites and background images, you can construct the graphics and interface for a game with absolutely no 3D elements at all, despite the fact that the drawing is being done, in a sense, in 3D.

我没有在 Java 中使用 OpenGL 的经验.但是,我知道可以使用 LWJGL 库和其他可能的库.如果您想大幅提高图形性能,就应该检查一下.

I have no experience using OpenGL in Java. However, I know that it is possible using the LWJGL library and possibly others. You should check it out if you want to drastically improve your graphics performance.

为了回答您的第二个问题,此应用程序中的 OpenGL 实际上与您的方法非常不同,使用与在 3D 中绘制纹理多边形相同的技术,而不是简单的块图像传输到帧缓冲区,尽管像素数据 可以访问和操作.

To answer your second question, OpenGL in this application actually works very differently from your approach, using the same techniques it would use to draw textured polygons in 3D rather than simple block image transfers into a framebuffer, though pixel data can be accessed and manipulated.

这是一个使用 OpenGL 和 C 的 2D 图形示例,来自一个我从未完成的游戏项目,但可以作为我正在谈论的内容的一个很好的视觉示例.在这种情况下,我没有使用正交投影,而是使用透视矩阵,通过在 3D 空间中绘制不同深度的各个图层来获得视差效果.

Here's an example of 2D graphics using OpenGL with C, from a game project I never finished but nevertheless may serve as a good visual example of what I'm talking about. In this case, I did not use an orthographic projection, but rather a perspective matrix to get parallax effects by drawing the various layers at different depths in 3D space.

这篇关于像素阵列 CPU 密集型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆