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

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

问题描述

我一直在一个Java 2D游戏的一小会儿。这是一个光栅图形系统的像素(整数)的数组。

 私人BufferedImage的图像=新的BufferedImage(宽度,高度,BufferedImage.TYPE_INT_RGB);
私人INT []像素=((DataBufferInt)image.getRaster()getDataBuffer()。)的getData()。
 

我再单独创建一个整数数组,与画面对象操纵它(从外部图像文件,如巴纽渲染),然后复制该数组到我的主要原因之一,这是投影到屏幕上。我没有发现任何性能上的差异(真的没想到)使用阵列复制方法在迭代。无论如何,图形渲染很好,本场比赛是顺顺当当磨磨蹭蹭。

不过,我发现这是非常CPU密集型。我的活动监视器说,该应用程序使用我的CPU 100%以上。这显然​​是因为我通过一个像素阵列(76K整数)的每个更新(每秒60次)迭代。

我选择了这种技术用于教育目的。这是一个个人项目,我只是想获得深入了解Java的图形。我,绝不嫁给这种渲染技术。

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

  1. 显然有更好的方式来做到这一点。什么库/框架将更好地做到这一点?
  2. 在将这些库基本上是做同样的事情(遍历像素),只是以更有效的方式?
  3. 有没有一种方法,我可以优化这个技术,而无需使用外部工具,或者是它只是不值得?

解决方案

的OpenGL是最经常与3D图形应用程序相关联的,但它也可以用于二维应用。使用正投影和纹理的四边形采取精灵和背景图像的地方,可以构建图形和接口的一个游戏与在所有绝对没有3D元素,尽管在图正在做,在某种意义上来说,在3D。

我使用OpenGL在Java中没有经验。但是,我知道,它有可能使用 LWJGL 库以及其它可能的。你应该检查出来,如果​​你想大幅度提高你的图形性能。

要回答你的第二个问题,OpenGL在此应用程序的实际工作非常不同于你的方法,使用它会用它来绘制3D纹理的多边形,而不是简单的块传输图像到帧缓冲区相同的技术,但是像素数据的可以的访问和操作。

下面是使用OpenGL用C,从一个游戏项目,我从来没有完成的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();

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.

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).

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. Obviously there is a better way to do this. What libraries/frameworks would do it better?
  2. Will those libraries essentially do the same thing (loop through the pixels), just in a more efficient way?
  3. Is there a way I can optimize this technique without using external tools, or is it just not worth it?

解决方案

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.

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.

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.

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天全站免登陆