Watching through some Java beginner game development videos, at least four out of five of them are rendering their game pixel by pixel (using rasters, data buffers and what have you).
My question is exactly why that is, since I haven’t been able to find a straight answer by myself.
Thanks in advance from a wondering newbie!
That’s odd you found such a high frequency of videos using that method.
It’s not a method I would ever recommend to a beginner, but I’ve written a few [unreleased] libraries that deal with everything at the pixel level… draws images, lines, polygons, etc.
It’s really cool and easy to create effects as well, blurring is stupid easy, writing shaders is fun… it’s a waste of time but fun nonetheless.
Is there no performance to gain from it neither? Thanks for the reply
Nope. Hardware is still faster.
www.rel.phatcode.net/junk.php?id=54
There are two simillar effects in there. 1 is a lightet textured twisting cylinder using opengl, the other is just a flat shaded twisting cylinder using pure software rasters.
The one that uses opengl beats the crap out of the software rendered one.
It’s potentially a lot faster than java2d - and also in its own way - fun.
OpenGL, which uses the gpu (hardware), is hundreds even thousands of times faster.
The coolest pixel-by-pixel library I’ve ever used is by far Pulp Core… VERY fast and had tons of sweet effects… also loved the API. Very powerful.
Rendering Pixel by Pixel, isn’t all this happening behind the scenes during Buffering? And really doesn’t all rendering happen Pixel by Pixel?
Yes. But what the OP is referring to is rendering through the CPU having the programmer hands on control over the rendering pixel by pixel.
Of course you can have pixel control with OpenGL as well but it isn’t as straightforward and requires OpenGL know-how as opposed to “how do I loop through an array” know-how.
Manipulating pixels has its place. Take the classics Worms & Lemmings as good examples. You can also use it to achieve some fancy effects whilst using 2d, such as lighting or colourising sprites on the fly. It can be a better, faster, easier and a less restrictive option than lots of Java2d Graphics method calls.
Of course it’s not going to be faster than using hardware, but at the end of the day if you are using 2d you are using 2d.
The first half of jonjava’s post makes a great point:
I particularly agree with the ‘fun’ part. It is fun, and great to play around with. If you are adept at 2d graphics, and not yet a 3d master, you should have a play around with pixel manipulation.
nerb.
Pixel control with OpenGL is very simple with glDrawPixels, it becomes more complicated with more modern methods (pixel shaders, …). I used glDrawPixels (JOGL) when mixing raycasting with OpenGL, I passed a direct NIO buffer instead of an array, it was really simple but slower than letting OpenGL handle the rasterization.
Yeah glDrawPixels is slow unfortunately =/
glDrawPixels is slow but you still get a noticeable speedup compared to pure Java2D.
Speaking of which? What is a good way to do a fullscreen pixel by pixel effect in java2d?
Has to be fast. I’m going to try some oldskool demo effects in java.
Thanks.
Probably more realistic for performance to render per-pixel to a small image, and scale it (hoping that Java2D will use hardware scaling).
You can access the backing pixels like so: (for int type images)
int[] argb = ((DataBufferInt)image.getData().getDataBuffer()).getData();
Thanks! Works with BufferedImage too right?
Also yo nerb below.
Yep.
That’s a copy of the pixels.
Use this instead:
int[] argb = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
Here’s my code to efficiently modify the pixels of an image which is drawn on the screen. I think it’s the most efficient way.
Initialization at the start of the application or resize of window
int backWidth = ....
int backHeight = ....
int[] pixels = new int[backWidth * backHeight]; // use this
MemoryImageSource renderBufferSource = new MemoryImageSource( backWidth, backHeight, pixels, 0, backWidth );
renderBufferSource.setAnimated( true );
Image renderBuffer = Toolkit.getDefaultToolkit().createImage( renderBufferSource );
After you’re done writing to pixels, this updates the image:
renderBufferSource.newPixels();
And drawing of course:
gr.drawImage( renderBuffer, 0, 0, null );
I hope that helps.