是OpenGL的新手。是否可以在地形上包裹单个图像?
以下代码会生成图片图块,但我想在整个地形上添加单个图块。
glBindTexture(GL_TEXTURE_2D, land);
for (int z = 0; z < MAP_Z-1; z++)
{
glBegin(GL_TRIANGLE_STRIP);
for (int x = 0; x < MAP_X-1; x++)
{
glTexCoord2f(0.0f, 0.0f);
glVertex3f(terrain[x][z][0], terrain[x][z][1], terrain[x][z][2]);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(terrain[x+1][z][0], terrain[x+1][z][1], terrain[x+1][z][2]);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(terrain[x][z+1][0], terrain[x][z+1][1], terrain[x][z+1][2]);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(terrain[x+1][z+1][0], terrain[x+1][z+1][1], terrain[x+1][z+1][2]);
}
glEnd();
}
答案 0 :(得分:4)
问题是您当前正在将纹理映射到每个方块。当你越过地形时,你需要改变你的glTexCoord2f。只需将x和z坐标除以for循环中地形的宽度和高度。
for (int z = 0; z < MAP_Z-1; z++)
{
glBegin(GL_TRIANGLE_STRIP);
for (int x = 0; x < MAP_X-1; x++)
{
glTexCoord2f((float)x/(float)MAP_X, (float)z/(float)MAP_Z);
...
... // Other code with (x+1) and (z+1) in the glTexCoord2f
}
}