UV coordinates to Pixel coordinates
pix.x = (uv.x * texture.width) -0.5
pix.y = ((1-uv.y) * texture.height) -0.5
uv坐标的y轴与图像上的像素坐标相反
对于最近的邻插值,只需舍入像素坐标
对于双线性插值,请从四个相邻像素计算参与百分比并进行加权平均
当UV坐标超出范围时,可以选择如何处理“纹理包裹”:opengl texture Wrapping
这是一些带有“重复”纹理包装的双线性插值的Java代码:
private static int billinearInterpolation(Point2D uv, BufferedImage texture) { uv.x = uv.x>0 ? uv.x%1 : 1+(uv.x%1); uv.y = uv.y>0 ? uv.y%1 : 1+(uv.y%1); double pixelXCoordinate = uv.x * texture.getWidth() - 0.5; double pixelYCoordinate = (1-uv.y) * texture.getHeight() - 0.5; pixelXCoordinate = pixelXCoordinate<0?texture.getWidth()-pixelXCoordinate: pixelXCoordinate; pixelYCoordinate = pixelYCoordinate<0?texture.getHeight()-pixelYCoordinate : pixelYCoordinate; int x = (int) Math.floor(pixelXCoordinate); int y = (int) Math.floor(pixelYCoordinate); double pX = pixelXCoordinate - x; double pY = pixelYCoordinate - y; float[] px = new float[]{(float) (1 - pX), (float) pX}; float[] py = new float[]{(float) (1 - pY), (float) pY}; float red = 0; float green = 0; float blue = 0; float alpha = 0; for (int i = 0; i < px.length; i++) { for (int j = 0; j < py.length; j++) { float p = px[i] * py[j]; if (p != 0) { int rgb = texture.getRGB((x + i)%texture.getWidth(), (y + j)%texture.getHeight()); alpha += (float) ((rgb >> 24) & 0xFF) * p; red += (float) ((rgb >> 16) & 0xFF) * p; green += (float) ((rgb >> 8) & 0xFF) * p; blue += (float) ((rgb >> 0) & 0xFF) * p; } } } return (((int) alpha & 0xFF) << 24) | (((int) red & 0xFF) << 16) | (((int) green & 0xFF) << 8) | (((int) blue & 0xFF) << 0); }