为什么我的高度图生成的地形纹理不正确?
我正在用2D短裤构建高度图。生成顶点的代码是
MeshVertex v; // has position, normal, and texCoord fields
v.position = glm::vec3(
(float) x * 150,
height * 64,
(float) z * 150
);
当我生成地形网格的索引时,我遵循Learnopengl.com上的教程,在这里我循环每行,循环每一列,然后为两个三角形的两组索引分配每个单元格。
for(unsigned int i = 0; i < mapSize-1; i++) // for each row a.k.a. each strip
{
for(unsigned int j = 0; j < mapSize; j++) // for each column
{
for(unsigned int k = 0; k < 2; k++) // for each side of the strip
{
indices.push_back(j + mapSize * (i + k));
}
}
}
最后,我认为gl_triangle_strip
基于索引0、1、2;根据Wikipedia,2、1、3
。假设基于0的编号,这是否意味着顶点1和4共享纹理坐标,而顶点2和3共享纹理坐标?我正在使用以下代码来构建我的纹理坐标
switch(index % 6) {
case 0:
v.texCoords = glm::vec2(0, (bandHeight * 1) + bandHeight);
break;
case 1:
case 4:
v.texCoords = glm::vec2(0, (bandHeight * 1));
break;
case 2:
case 3:
v.texCoords = glm::vec2(1, (bandHeight * 1));
break;
case 5:
v.texCoords = glm::vec2(1, (bandHeight * 1) + bandHeight);
break;
}
。第一个纹理是第0行,第二个纹理是第1行等。这就是全部,带有1
,这意味着我们正在查看第二个纹理,我只是为了进行测试而进行了硬编码。
当我渲染地形网格时,每6个看起来只有1个四分之一。其余的扭曲,我不确定为什么。基于循环正在处理的索引,生成纹理坐标的正确公式是什么?
这是地形的完整图片,每个单元具有与当前单元格的纹理生成的纹理坐标。
编辑,
由于有一些票数可以将问题关闭为“不足以复制的代码”,这是a 链接与完整存储库。您需要从Carnivores2中自己的Huntdat副本2;由于版权原因,我无法提供。不过,可以通过一些Google搜索轻松找到它。
它使用Mingw 8编译Windows 10,尽管Cmakelists文件应该足够容易,可以自定义到其他平台。
找到生成地形的文件 >。
I'm building a heightmap out of a 2D array of shorts. The code to generate the vertices is
MeshVertex v; // has position, normal, and texCoord fields
v.position = glm::vec3(
(float) x * 150,
height * 64,
(float) z * 150
);
When I generate the indices for the terrain mesh, I followed the tutorial on learnopengl.com, where I loop through each row, loop through each column, and assign the two sets of indices for the two triangles in each cell.
for(unsigned int i = 0; i < mapSize-1; i++) // for each row a.k.a. each strip
{
for(unsigned int j = 0; j < mapSize; j++) // for each column
{
for(unsigned int k = 0; k < 2; k++) // for each side of the strip
{
indices.push_back(j + mapSize * (i + k));
}
}
}
Lastly, I thought GL_TRIANGLE_STRIP
draws the tris in a counter clockwise direction, based on indices 0, 1, 2; 2, 1, 3
as per wikipedia. Assuming 0-based numbering, wouldn't this mean that vertices 1 and 4 share a texture coordinate and vertices 2 and 3 share a texture coordinate? I'm building my texture coords based on this with the following code
switch(index % 6) {
case 0:
v.texCoords = glm::vec2(0, (bandHeight * 1) + bandHeight);
break;
case 1:
case 4:
v.texCoords = glm::vec2(0, (bandHeight * 1));
break;
case 2:
case 3:
v.texCoords = glm::vec2(1, (bandHeight * 1));
break;
case 5:
v.texCoords = glm::vec2(1, (bandHeight * 1) + bandHeight);
break;
}
I don't know that it's relevant, but the bandHeight
variable is due to my textures being a variable length vertical image of subimages, where the first texture is row 0, second texture is row 1, etc. That's all that is, with 1
meaning we're looking at the second texture, which I've hardcoded just for testing.
When I render the terrain mesh, only one quad in every 6 looks right. The rest are warped, and I'm not sure why. What is the right formula for generating texture coordinates based on which index the loop is processing?
Here's what a full picture of the terrain looks like, with each cell having the texture coordinates generated with the current cell's intended texture.
Edit
As there have been some votes to close the question as "not enough code to reproduce", here's a link to the full repository. You'll need your own copy of HUNTDAT from Carnivores2; I can't provide that due to copyright reasons. It can easily be found through some google searches, though.
It compiles on Windows 10 with MinGW 8, although the cmakelists file should be easy enough to customize to other platforms.
The file that generates the terrain is found here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论