簡體   English   中英

紋理圖集紋理坐標數學

[英]Texture Atlas Texture Coordinates math

我正在為opengl寫一個lwjgl紋理圖集,並且我的紋理圖集創建工作正常,但是當我進行渲染時,該對象出現了,但是它是完全黑色的。 我認為這是因為紋理坐標位於紋理圖集的空白區域。 由於我沒有正確設置混合,並且因為對象是我渲染的第一件事,所以Alpha變為完全黑色。 我知道這一點是因為我在0,0,0渲染了一個測試四邊形,並且可以看到對象的輪廓。 當我以其他順序渲染它們時,輪廓不在那兒,因為那時混合工作正常。 這是我的紋理坐標移動代碼,用於更改坐標m_height和m_width分別是整個紋理圖集和x,y,w的高度和寬度,並且是紋理在圖集x中的位置,寬度和高度, y是從左上角開始的,texCoordinate2D是從左下角開始的,這就是為什么要執行m_height-yh。 它在正常紋理而不是圖集下工作良好。

public TexCoordinate2D getTextureCoord(int x, int y, int w, int h,
                                       TexCoordinate2D coord) {
    int nx = x;
    int ny = (m_height - y) - h;
    //to fix m_repeat
    float cx = coord.getX();//going to fix repeat here
    float cy = coord.getY();
    int cpx = (int) (nx + (cx * w));
    int cpy = (int) (ny + (cy * h));
    float ncoordx =  cpx/(float) m_width;
    float ncoordy =  cpy/(float) m_height;
    System.out.println("Rect: " + x + " " + y + " " + w + " " + h);
    System.out.println("Coord: x: " + coord.getX() + " y: " + coord.getY());
    System.out.println("Coord--> pixel: x: " + cpx + " y: " + cpy);
    System.out.println("Pixel-->Coord: x: " + ncoordx + " y: " + ncoordy);
    TexCoordinate2D newCoord = new TexCoordinate2D(ncoordx, ncoordy);
    m_coordinates.add(newCoord);
    return newCoord;
}

這是一些打印輸出

Rect: 0 0 512 512 #The rectangle from the top left corner of atlas
Coord: x: 0.5004405 y: 0.55040383 #The input coord from bottom left corner of texture
Coord--> pixel: x: 256 y: 3865 #The pixel location in the atlas of the input coord  the from bottom left corner
Pixel-->Coord: x: 0.0625 y: 0.9436035 #The coordinates in the atlas (The finished product)
Rect: 3072 0 256 256 #Starts again
Coord: x: 0.56088686 y: 0.5795429
Coord--> pixel: x: 3215 y: 3988
Pixel-->Coord: x: 0.7849121 y: 0.9736328
Rect: 2560 0 512 512
Coord: x: 0.18178056 y: 0.35738176
Coord--> pixel: x: 2653 y: 3766
Pixel-->Coord: x: 0.6477051 y: 0.9194336

那是什么錯誤呢?

(0,0)在OpenGL規范化紋理坐標的左下角,而(1,1)在右上角。

從您的示例輸出中,我看到:

Rect: 1024 0 512 512
Coord: x: 0.737651 y: 0.30223513
Coord--> pixel: x: 1401 y: 4249
Pixel-->Coord: x: 0.34212455 y: 1.0376068  (texture coordinates ?)

在此示例中,您使用的紋理坐標不在規范化范圍內。 發生這種情況時,其行為取決於紋理環繞狀態。 可以簡單地固定坐標,可以使用邊框顏色,可以重復紋理( 默認 ),可以使用鏡像等。

給定默認的紋理環繞行為(重復),您的T坐標(1.0376068)本質上變為(0.0376068)。 在紋理圖集的背景下進行紋理包裹幾乎沒有意義,因此坐標超出范圍可能意味着您做錯了:)

您還需要注意,您的紋理坐標需要與texel中心對齊,或者當您從偏心位置采樣時,地圖集中附近的texel可能會流血。 由於除了最簡單的幾何圖形之外,幾乎不可能使屏幕上的每個像素都映射到texel中心,因此通常的解決方案是將其與之配合使用...在圖集中每個紋理周圍放置邊框,即使最接近的紋理像素靠近紋理的邊緣,紋理過濾永遠不會將其放在相鄰紋理的樣本上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM