我正在为opengl编写一个lwjgl纹理地图集,我的纹理地图集工作得很好,但是当我去渲染它时,这个对象会出现,但它是完全黑色的。我认为这是因为我的纹理坐标在我的纹理地图集的空白处。因为我没有正确地设置混合,并且由于对象是我呈现的第一件事,alpha变成完全黑色。我之所以知道这一点,是因为我在0,0,0呈现了一个测试四角,并且可以看到对象的轮廓。当我把它们按另一个顺序呈现时,轮廓就不在那里了,因为那一次混合正常工作。这是我的纹理坐标移位代码,以改变坐标m_height和m_width是整个纹理地图集的高度和宽度,以及x,y,w,以及地图集中纹理的位置、宽度和高度,x,y是从左上角的,texCoordinate2D是从左下角的,这就是为什么我要做which y。它工作良好,与正常的纹理,而不是地图集。
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那这个虫子是什么?
发布于 2013-08-23 02:34:24
(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)。纹理地图集上下文中的纹理包装很少有意义,因此拥有超出范围的坐标可能意味着您做错了什么:)
您还需要注意,您的纹理坐标需要对齐纹理中心或附近的纹理在您的地图集可能会出血,当你从一个非中心的位置取样。由于几乎不可能让屏幕上的每一个像素映射到纹理中心,除了最简单的几何学外,通常的解决方案是接受它。在地图集中的每个纹理周围放置一个边框,这样即使最近的纹理靠近纹理的边缘,纹理过滤也不会从相邻纹理中得到样本。
https://stackoverflow.com/questions/18393235
复制相似问题