代码之家  ›  专栏  ›  技术社区  ›  AlexVestin

窗口中出现的glTexImage2D切片和对齐问题

  •  3
  • AlexVestin  · 技术社区  · 8 年前

    我正在使用加载文件

    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

    (图像坐标为N10E099)

    1 回复  |  直到 8 年前
        1
  •  9
  •   Quentin    8 年前

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

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    

    此对齐值 4 默认情况下,由使用 每当读取纹理数据并将其发送到GPU时。

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

    glPixelStorei(GL_PACK_ALIGNMENT, 1); .