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:
The advantage of this technic are noticeable in case someone would like to create large terrains with minimal effort(a la infinite terrain demo )
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:
The advantage of this technic are noticeable in case someone would like to create large terrains with minimal effort(a la infinite terrain demo )
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: