DisplacementMap :P

Java Cool Dude

New member
Well I kinda try to take advantage of both Java and Java3D to come up with something nice "not as nice as if it was written with a language that takes advantage of vertex and pixel shaders*, so I coded a little demo that basically extracts any picture's colors (as integers) and stock them into three different arrays, Red,Green and Blue.
Then it passes those arrays to a class that generates vertices based on the rgb values of each pixel: Let's say a white point has R=G=B =255, (R+G+B)/3 =(255 + 255 +255)/3 will be the height of the pixel. But as Java3D deals with colors as floats, the height will be determined as (255 + 255 + 255)/(255*3) to get a max displacement of 1f(well multiplying the vertex.z by a scalar will result in an infinite choice).
Once the vertices locations are computed, it's rendering time....pics please:
Capture0.jpg
Capture1.jpg
Capture2.jpg
Capture4.jpg

The advantage of this technic are noticeable in case someone would like to create large terrains with minimal effort(a la infinite terrain demo :D)
Code:
    int progress =0, lim =2;
    String percent = "";
    for(int yStep =0; yStep<imageHeight-2; yStep++)
      for(int xStep =0; xStep<imageWidth; xStep++)
      {
         percent = Double.toString(((double)(progress)/(double)(arrayOfPixels.length*4 -8*imageWidth))*100);
        if(((double)(progress)/(double)(arrayOfPixels.length*4 -8*imageWidth))*100>= 99.99){
		 percent = "100";
		 lim = 3;
        }

        System.out.print("\rMaking quads..." + percent.substring(0,lim) + "% completed");

        points[progress]     = new Point3f(xStart + (float)xStep*step,
                                           yStart - (float)yStep*step,
                                           arrayOfPixels[yStep*imageWidth + xStep]/4f);
        colors[progress]     = new Color3f((float)(red[yStep*imageWidth + xStep])/255f,
                                           (float)(green[yStep*imageWidth + xStep])/255f ,
                                           (float)(blue[yStep*imageWidth  + xStep])/255f);
        myQuads.setColor(progress, colors[progress]);


        points[progress + 1] = new Point3f(xStart + (float)xStep*step,
                                           yStart - (float)(yStep+1)*step,
                                           arrayOfPixels[(yStep+1)*imageWidth + xStep]/4f);
        colors[progress + 1] = new Color3f((float)(red[(yStep+1)*imageWidth + xStep])/255f,
                                           (float)(green[(yStep+1)*imageWidth + xStep])/255f ,
                                           (float)(blue[(yStep+1)*imageWidth  + xStep])/255f);
        myQuads.setColor(progress + 1, colors[progress + 1]);


        points[progress + 2] = new Point3f(xStart + (float)(xStep+1)*step,
                                           yStart - (float)(yStep+1)*step,
                                           arrayOfPixels[(yStep+1)*imageWidth + xStep+1]/4f);
        colors[progress + 2] = new Color3f((float)(red[(yStep+1)*imageWidth   + xStep+1])/255f,
                                           (float)(green[(yStep+1)*imageWidth + xStep+1])/255f ,
                                           (float)(blue[(yStep+1)*imageWidth  + xStep+1])/255f);
        myQuads.setColor(progress + 2, colors[progress + 2]);


        points[progress + 3] = new Point3f(xStart + (float)(xStep+1)*step,
                                           yStart - (float)yStep*step,
                                           arrayOfPixels[yStep*imageWidth + xStep+1]/4f);
        colors[progress + 3] = new Color3f((float)(red[yStep*imageWidth   + xStep+1])/255f,
                                           (float)(green[yStep*imageWidth + xStep+1])/255f ,
                                           (float)(blue[yStep*imageWidth  + xStep+1])/255f);
        myQuads.setColor(progress + 3, colors[progress + 3]);
        progress+=4;
      }
 
Last edited:
Ah, that's pretty cool, basically presampled displacement mapping. :) I have thought about doing something like that, but haven't got around to do it.

Now we want to see dynamic tesselation with lod too ;)
 
Humus said:
Ah, that's pretty cool, basically presampled displacement mapping. :) I have thought about doing something like that, but haven't got around to do it.

Now we want to see dynamic tesselation with lod too ;)
Actually I was about to ask you if you can translate my code into c++/c, but I'm not sure if you guys have pixelGrabber() as we do in java.
Oh and yes, I created some water animation "waves propagation" and I'll be posting pics soon :D
 
wavy.jpg

wavy2.jpg

Sorry for the poor jpg quality:(
Anyways, I use this formula to make the waves:
Code:
points[progress].z  = .05f*(float)(((extremeDist-dist1)/extremeDist)*Math.cos(w*time - (w/v)*dist1));
Where w stands for 2Pi/Period, v velocity of propagation, dist1 distance between the disturbance source and the point
 
I'd probably use a vertex shader program for waves. . . Without dynamic displacement mapping, it's kinda necessary.
 
Ostsol said:
I'd probably use a vertex shader program for waves. . . Without dynamic displacement mapping, it's kinda necessary.
I wish java3d supported the shaders you're talking about :(
Bah, the engineers at sun already stated they'll be supporting it soon, and by the time it's supported, I'll be giving Humus and NitroGl serious competition :D
:D j\k.....
 
Hmm ... time to start the anti-competive marketing business machine to force you out of the market :evil:
 
Back
Top