问题描述
我正在制作自己的地形图,并且我一直在使用NASA的.hgt文件.
I'm working on making my own topographic map, and I have been using .hgt files from NASA.
我正在使用加载文件
void MapImage::load_map_file(const char* filename) {
std::ifstream file(filename, std::ios::in | std::ios::binary);
if (!file) {
std::cout << "Error opening file!" << std::endl;
}
std::vector<short> tempHeight(TOTAL_SIZE);
unsigned char buffer[2];
int x, y;
for (int i = 0; i < TOTAL_SIZE; i++) {
if (!file.read(reinterpret_cast<char*>(buffer), sizeof(buffer))) {
std::cout << "Error reading file!" << std::endl;
}
tempHeight[i] = (buffer[0] << 8) | buffer[1];
}
height = tempHeight;
}
然后使用以下命令将它们添加到内存位图中:
And then adding them to an inmemory bitmap using:
void MapImage::loadTextureImage() {
img_tex = 0;
glGenTextures(1, &img_tex);
int x, y, w, h;
w = h = SRTM_SIZE;
unsigned char* img;
img = (unsigned char *)malloc(3 * w * h);
memset(img, 0, sizeof(img));
int g = 0;
double height_color;
/*
for(int i = 0; i < TOTAL_SIZE; i++){
height_color = (float)height[i] / 10.0;
g = (height_color * 255);
if (g>255)g = 255;
img[i * 3 + 2] = (unsigned char)0;
img[i * 3 + 1] = (unsigned char)g;
img[i * 3 + 0]= (unsigned char)0;
}
*/
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; ++j) {
x = i;
y = (h - 1) - j;
height_color = (float)height[j + (i * w)] / 10.0;
g = (height_color * 255);
if (g>255)g = 255;
img[(x + y*w) * 3 + 2] = (unsigned char)0;
img[(x + y*w) * 3 + 1] = (unsigned char)g;
img[(x + y*w) * 3] = (unsigned char)0;
}
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, img_tex);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGB,
w,
h,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
img
);
}
但是,这样会导致图像被切成角,像这样
However this results in a image with the corner sliced, like this
在loadTextureImage()中使用注释的版本看起来略有不同,但是切片了相同的角.
Using the commented version in the loadTextureImage() looks slightly different, however with the same corner sliced.
有人暗示发生了什么吗?我尝试将图像用作纹理,并加载到stbi库中,并且效果很好,所以我不确定哪里出了问题.
Does anyone have a hint to whats going on? I've tried using a image as a texture, loading with the stbi library, and that works fine, so I'm not sure where it's going wrong.
(图像坐标为N10E099)
(the coordinates for the image is N10E099)
推荐答案
这看起来像是行对齐错误,由3宽度的颜色数据引起.尝试在 glTexImage2D
之前使用以下调用:
This looks like row misalignment, caused by the 3-wide colour data. Try using the following call just before glTexImage2D
:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
每当读取纹理数据发送到GPU时, glTexImage2D
及其朋友都会使用此对齐值,默认情况下为 4
.
This alignment value, which is 4
by default, is used by glTexImage2D
and friends whenever texture data is read to be sent to the GPU.
无法验证它与数据的实际外观是否匹配,因此在像您这样的情况下,某行未在4字节边界处结束,下一行的前几个字节将被跳过,从而导致这种对角线变形.
There is no verification that it matches what the data actually looks like, so in cases like yours where a row doesn't end on a 4-byte boundary, the first few bytes of the next row will be skipped, leading to this diagonal distortion.
另一个方向(从GPU到客户端内存)的纹理数据传输通过 glPixelStorei(GL_PACK_ALIGNMENT,1);
进行对齐.
Texture data transfers in the other direction (from the GPU to client memory) are aligned via glPixelStorei(GL_PACK_ALIGNMENT, 1);
.
这篇关于glTexImage2D切片和对齐问题出现在窗口中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!