gpt4 book ai didi

c++ - 窗口中出现 glTexImage2D 切片和对齐问题

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:41 27 4
gpt4 key购买 nike

我正在制作我自己的地形图,我一直在使用 NASA 的 .hgt 文件。

我正在使用

加载文件
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;
}

然后使用以下方法将它们添加到内存位图中:

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
);
}

然而,这会导致图像的角被切开,像这样 Sliced image

在 loadTextureImage() 中使用注释版本看起来略有不同,但是切角相同。 Sliced image 2

有人知道发生了什么事吗?我试过使用图像作为纹理,使用 stbi 库加载,效果很好,所以我不确定哪里出了问题。

(图片坐标为N10E099)

最佳答案

这看起来像是行错位,由 3-wide 颜色数据引起。尝试在 glTexImage2D 之前使用以下调用:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

此对齐值默认为 4,每当读取纹理数据以发送到 GPU 时,glTexImage2D 和 friend 都会使用该对齐值。

没有验证它是否与数据的实际外观相匹配,所以在像你这样的情况下,一行不以 4 字节边界结束,下一行的前几个字节将被跳过,从而导致这种对角线失真。

另一个方向(从 GPU 到客户端内存)的纹理数据传输通过 glPixelStorei(GL_PACK_ALIGNMENT, 1); 对齐。

关于c++ - 窗口中出现 glTexImage2D 切片和对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45486668/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com