Does anybody have an example of applying the old “Mode 7” pseudo-3D effect to 2D graphics rendered by Java2D? Just thought I’d ask before blowing the dust off my old math books to refresh my memory on affine transformations.
You won’t easily be able to do it with Java2D as perspective projection is not an Affine Transform. The SNES was able to do it with it’s affine transform because it could be modified per-scanline something you can’t really do with Java2D.
You need to give more details. Mode-7 alone is simply a point sampled bitmap that can be rotated. As noted all other effects come from changing hardware parameters at desired scanlines.
Thanks guys. I’m just trying to simulate the pseudo-perspective effect that the overworld map has in Final Fantasy 3/6. Nostalgia goggles help me overlook the fact that it looks quite horrid.
Some quick googling turned up this implementation in C. I ported it to Java (it’s all of maybe 20 lines), although I’m having a tough time figuring out the optimal values for the parameters for the look I want.
In my first-pass Java version, I pass it an input BufferedImage (the background, painted normally) and an output BufferedImage. I’m currently basically doing this:
static int[] inRGB = new int[w * h];
static int[] outRGB = new int[w * h];
public static void getMode7(BufferedImage in, BufferedImage out, ...) {
in.getRGB(0,0, w,h, inRGB,0, w);
for (int y=0; y<h; y++) {
... stuff ...
for (int x=0; x<w; x++) {
... stuff ...
}
}
out.setRGB(0, 0, w,h, outRGB,0, w);
out.flush();
}
Unfortunately, this is unusably slow, taking about ~0.05 seconds per call to getMode7() for a 512x512 image. My naive timing of sections of this method seem to show that the getRGB() and setRGB() calls are the bottleneck. Not to mention that there’s no chance of my “out” image being accelerated. Is there a better way do do this sort of thing?
Don’t use setRGB/getRGB they are slow, just grab the underlying buffers and modify directly:
int[] inRGB = ((DataBufferInt)in.getRaster().getDataBuffer()).getData();
once again dont use setRGB / get RGB
rather use
int pixelsInput[]=((DataBufferInt)(in.getRaster().getDataBuffer())).getData();
int pixelsOutput[]=((DataBufferInt)(out.getRaster().getDataBuffer())).getData();
int ofs=0;
for(int y=0;y<height;y++)
{
int ofsY=y*width;
for(int x=0;x<width;x++)
{
int ofs=x+ofsY;
int pixel=pixelsInput[ofs];
...
perform per pixel operation here
...
pixelsOutput[ofs]=pixel;
}
}
EDIT: I am 1 minute late !
Yes, I just found the solution myself in an older post by Riven. Seems more than fast enough now.
Cheers guys!
hi all,
i’m very interested in Mode 7, do
you have a sample of code, or a demo to show/give please please ?
thanx
I would be pretty interested in results too.