简体   繁体   English

将SRTM * .hgt文件作为纹理传递到OpenGL着色器会导致图像歪斜

[英]Passing SRTM *.hgt file as texture to OpenGL shader results in skewed image

I am writing terrain renderer that interprets NASA SRTM *.hgt files to get height data. 我正在编写用于解释NASA SRTM * .hgt文件以获取高度数据的地形渲染器。 I implemented LOD technique described here: http://www.pheelicks.com/2014/03/rendering-large-terrains/ . 我实现了此处描述的LOD技术: http : //www.pheelicks.com/2014/03/rendering-large-terrains/

My terrain mesh has 1024 width and height. 我的地形网格物体的宽度和高度为1024。 The height map is 1201 x 1201. I load the height map into memory and the send it to shader as a texture. 高度图为1201 x1201。我将高度图加载到内存中,并将其作为纹理发送到着色器。 Then I sample it in vertex shader using current vertex position which x and z component is in range [-512, 512]. 然后,使用x和z分量在[-512,512]范围内的当前顶点位置在顶点着色器中对其进行采样。 Next I divide it by 1024 which gives me vector with x and z in range [-0.5, 0.5]. 接下来,我将其除以1024,得到x和z在[-0.5,0.5]范围内的向量。 The upper left corner of the mesh is at (-0.5, -0.5) so I add vec2(0.5, 0.5) to "normalize" it. 网格的左上角位于(-0.5,-0.5),因此我添加了vec2(0.5, 0.5)以对其进行“归一化”。

When sampling a jpeg file with the calculated co-ordinates it all looks as expected, but when I'm sampling the heightmap it looks like this: 当使用计算出的坐标对jpeg文件进行采样时,一切看起来都与预期的一样,但是当我对高度图进行采样时,它看起来像这样: 在此处输入图片说明

with this strange diagonal line. 用这条奇怪的对角线。 After thinkig for a while what's wrong with this image I discovered that the heightmap is actually just skewed (or my sampling is somehow skewed...) and on the left you can see the part that should be on the right, like here: 在思考了片刻之后,这张图片出了什么问题,我发现高度图实际上只是偏斜了(或者我的采样有些偏斜了……),在左侧,您可以看到应该在右侧的部分,如下所示: 在此处输入图片说明

I have no idea what's going on here. 我不知道这是怎么回事。 Considering the fact that the jpeg image was laying perfectly on the mesh with the same sampling code I thought that it cannot be something wrong with the sampling method. 考虑到使用相同的采样代码jpeg图像可以完美地放置在网格上的事实,我认为采样方法不会有什么问题。

All the code is available on the github repository: https://github.com/0ctothorp/terrain_rendering/tree/lod2 in the "lod2" branch. 所有代码都可以在github存储库上找到:“ lod2”分支中的https://github.com/0ctothorp/terrain_rendering/tree/lod2

The most interesting part is probably the sampling code: 最有趣的部分可能是采样代码:

sample_ = texture(heightmap, (position.xz / meshSize) + vec2(0.5f, 0.5f)).r;
position.y = sample_ / 50.0f;

and how I pass heightmap to shader: 以及如何将heightmap传递给着色器:

GL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, GL_R16I, 1201, 1201, 0, GL_RED_INTEGER, GL_SHORT, 
                      hmData->data()));

Apparently your data is tightly packed, whereas OpenGL expects it to be 4-byte aligned by default. 显然,您的数据是紧密打包的,而OpenGL期望默认情况下它是4字节对齐的。 You can override the expected alignment with the following prior to the glTexImage2D call: glTexImage2D调用之前,可以使用以下命令覆盖预期的对齐方式:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM