0
我想为我的3D引擎采样高度图。可能发生的情况是我的地形上有更多的顶点比我的图像中的像素更多。我创建下面的方法:插值图像像素
public float interpolateFromImage(BufferedImage image, float x, float y){
这需要所述图像和所述x和y坐标。坐标以百分比表示。 我的方法是计算附近像素和给定坐标之间的距离。这工作得很好,但我最终与此:
可以清楚地看到,边缘不像他们应该是光滑的。
我用下面的代码:
int topLeftX = (int) (x * (image.getWidth()-1)); //index of topLeft pixel
int topLeftY = (int) (y * (image.getHeight()-1)); //index of topLeft pixel
float[] distances = new float[4];
float total = 0;
for(int j = topLeftX ; j < topLeftX + 2; j++){ //run through all 4 nearby pixels
for(int k = topLeftY ; k < topLeftY + 2; k++){
float dist = (float) Math.sqrt( //pythagoras for the distance
(x- j/ (float)(image.getWidth()-1)) *
(x- j/ (float)(image.getWidth()-1)) +
(y- k/ (float)(image.getHeight()-1)) *
(y- k/ (float)(image.getHeight()-1)));
if(dist < 0.001){
return new Color(image.getRGB(j,k)).getRed();
}
distances[(j-topLeftX) * 2 + (k-topLeftY)] = (1f/image.getWidth())/(dist * dist);
total += distances[(j-topLeftX) * 2 + (k-topLeftY)];
}
}
float h = 0;
for(int j = topLeftX ; j < topLeftX + 2; j++){
for(int k = topLeftY ; k < topLeftY + 2; k++){
float p = distances[(j-topLeftX) * 2 + (k-topLeftY)]/total;
h+= new Color(image.getRGB(j,k)).getRed() * p;
}
}
return h;
有谁知道我需要怎么改变我的代码? 我很乐意提供任何建议和帮助:)
姆姆是的......我知道它为什么看起来像:)我想用我自己做的东西。似乎不是最好的主意。谢谢!:) – Luecx
你可以随时进一步试验,这是学习的最佳方式。 –