【发布时间】:2015-07-20 12:13:42
【问题描述】:
我正在尝试使用 libGdx 中的 PolygonSpriteBatch 渲染一个简单的 2D 风景轮廓,以在屏幕上绘制填充的多边形。我不知道为什么,但代码似乎只是在渲染每个其他多边形,从而导致屏幕上出现条纹图案(见下面的屏幕截图 - 背景是红色的,风景是绿色的)。
我用来绘制风景的代码如下所示。这将遍历世界坐标中的一组相邻点(使用landarray.getOnScreen() 检索),计算屏幕坐标,为每个段创建一个PolygonRegion 并绘制它们。我使用的纹理只是一个 100x100 像素的纯白色方块。我没有使用任何相机或视图,只是将世界坐标动态转换为屏幕坐标并直接绘制到图形屏幕。
请注意,我正在从使用 Slick2D 的工作版本重新编写此游戏(因此在此阶段不使用相机),并且下面的代码或多或少没有改变(除了翻转 y 轴高度计算)工作版本,它绘制了一个连续的景观。
public void drawPoly() {
System.out.println("Mountains - drawShape()");
// Calculate screen bottom-edge Y taking into account values = bHEIGHT
int scrBtmY = scrTopY - Gdx.graphics.getHeight();
if (FastMath.abs(scrBtmY) == bHEIGHT) { // Wrap-around bug at y == bHEIGHT
scrBtmY = 0;
}
// Draw the land masses on-screen that are in the results list
polyBatch.begin();
polyBatch.setColor(landColour);
ArrayList<Landmass> masses = landarray.getOnScreen(scrLeftX, scrTopY, scrBtmY);
for (Landmass lm : masses) {
/* Calculate the on-screen start and end x and y values taking into account
wrapping in 'buffer space'
Wrap around - low negative numbers (> -25) are 'just off screen' start or
end points, large ones are wrap-around points
*/
int sx = (int) lm.start.x - scrLeftX;
if (sx < -Settings.Physics.LANDSITEWIDTH) {
sx = sx + bWIDTH;
}
int sy = (int) lm.start.y - scrBtmY;
int ex = (int) lm.end.x - scrLeftX;
if (ex < -Settings.Physics.LANDSITEWIDTH) { // Wrap around
ex = ex + bWIDTH;
}
int ey = (int) lm.end.y - scrBtmY;
ex = ex - 1; // In case over-lapping regions is causing stripes
System.out.format("start (%d, %d)\t end (%d, %d)%n", sx, sy, ex, ey);
// Construct and draw the polygon
float[] vertices = new float[8];
vertices[0] = sx;
vertices[1] = 0;
vertices[2] = ex;
vertices[3] = 0;
vertices[4] = ex;
vertices[5] = ey;
vertices[6] = sx;
vertices[7] = sy;
short[] triangles = new short[] {0, 1, 2, 0, 2, 3};
PolygonRegion polyReg = new PolygonRegion(new TextureRegion(textureSolid), vertices, triangles);
polyBatch.draw(polyReg, sx, 0);
}
polyBatch.end();
}
我已经包含了一个调试行来打印各个多边形(陆地段)的屏幕坐标,并且一次 drawPoly() 迭代的输出如下所示。这显示了应该在屏幕上绘制的每个段的开始 (x, y) 和结束 (x, y) 坐标。从输出来看,绘制的多边形看起来彼此相邻,因此它们之间不应有间隙。
Mountains - drawShape()
start (53, 265) end (77, 358)
start (78, 358) end (102, 376)
start (103, 376) end (127, 406)
start (128, 406) end (152, 371)
start (153, 371) end (177, 429)
start (178, 429) end (202, 447)
start (203, 447) end (227, 433)
start (228, 433) end (252, 415)
start (253, 415) end (277, 478)
start (278, 478) end (302, 490)
start (303, 490) end (327, 524)
start (328, 524) end (352, 519)
start (353, 519) end (377, 551)
start (378, 551) end (402, 556)
start (403, 556) end (427, 543)
start (428, 543) end (452, 470)
start (453, 470) end (477, 505)
start (478, 505) end (502, 482)
start (503, 482) end (527, 513)
start (528, 513) end (552, 476)
start (553, 476) end (577, 479)
start (578, 479) end (602, 460)
start (603, 460) end (627, 475)
start (628, 475) end (652, 426)
start (653, 426) end (677, 453)
start (-22, 449) end (2, 406)
start (3, 406) end (27, 357)
start (28, 357) end (52, 265)
我在这个网站上搜索了一系列帖子,这些帖子帮助我获得了上面的代码,但没有看到任何有这个特定问题的帖子。
请注意,我仅将其作为桌面版本运行,并为此启用了 Java 8。使用 libGdx 1.5.6 和 1.6.0 进行测试,结果相同。
我已经制作了一个独立版本的代码来复制问题,以防有人想在家尝试。这使用 libGdx 1.6.0 和(我假设)Java 6,并显示了上述问题(至少在我的 PC 上)。这些文件可以在这里找到:java source、png image 和 whole project 压缩文件。
有人知道为什么我会在屏幕上出现条纹效果吗?
【问题讨论】:
-
我开发了一种解决方法,使用带有 ShapeType.Filled 的 ShapeRenderer 并使用我计算的顶点为每个段绘制两个三角形。但是,这并不能回答上述问题。