Textureloading加载蓝色而不是实际颜色的纹理

加载方式:

bool TextureManager::LoadBMP(const char *path, unsigned int &texture)
{
    std::fstream hFile(path, std::ios::in | std::ios::binary);
    if (!hFile.is_open())
    {
        throw std::invalid_argument("Error: File Not Found.");
        return false;
    }

    hFile.seekg(0, std::ios::end);
    int Length = hFile.tellg();
    hFile.seekg(0, std::ios::beg);
    std::vector<uint8_t> FileInfo(Length);
    hFile.read(reinterpret_cast<char*>(FileInfo.data()), 54);

    if (FileInfo[0] != 'B' && FileInfo[1] != 'M')
    {
        hFile.close();
        throw std::invalid_argument("Error: Invalid File Format. Bitmap Required.");
        return false;
    }

    if (FileInfo[28] != 24 && FileInfo[28] != 32)
    {
        hFile.close();
        throw std::invalid_argument("Error: Invalid File Format. 24 or 32 bit Image Required.");
        return false;
    }

    short BitsPerPixel = FileInfo[28];
    int width = FileInfo[18] + (FileInfo[19] << 8);
    int height = FileInfo[22] + (FileInfo[23] << 8);
    uint32_t PixelsOffset = FileInfo[10] + (FileInfo[11] << 8);
    uint32_t size = ((width * BitsPerPixel + 31) / 32) * 4 * height;
    std::vector<unsigned char> Pixels(size);

    hFile.seekg(PixelsOffset, std::ios::beg);
    hFile.read(reinterpret_cast<char*>(Pixels.data()), size);
    hFile.close();

    /*******************GENERATING TEXTURES*******************/

    glGenTextures(1, &texture);             // Generate a texture
    glBindTexture(GL_TEXTURE_2D, texture); // Bind that texture temporarily

    GLint mode = GL_RGB;                   // Set the mode

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    // Create the texture. We get the offsets from the image, then we use it with the image's
    // pixel data to create it.
    glTexImage2D(GL_TEXTURE_2D, 0, mode, width, height, 0, mode, GL_UNSIGNED_BYTE, Pixels.data());

    // Unbind the texture
    glBindTexture(GL_TEXTURE_2D, NULL);

    // Output a successful message
    std::cout << "Texture \"" << path << "\" successfully loaded.\n";

    return true; // Return success code
}


渲染方式:

void Game::Render()
{
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, TextureID);

    glUniform1i(glGetUniformLocation(program->Id, "img"), 0);

    glBegin(GL_QUADS);
    glTexCoord2f(1, 0);              //gl_MultiTexCoord0
    glVertex3f(-1, 1, -4);    //gl_Vertex
    glTexCoord2f(0, 0);
    glVertex3f(-1, -1, -4);
    glTexCoord2f(0, 1);
    glVertex3f(1, -1, -4);
    glTexCoord2f(1, 1);
    glVertex3f(1, 1, -4);
    glEnd();
}


顶点着色器:

#version 120
uniform vec3 color;
varying vec2 texcoord;

void main()
{
        gl_Position=gl_ModelViewProjectionMatrix*gl_Vertex;
        texcoord=gl_MultiTexCoord0.xy;
}


片段着色器:

#version 120
uniform sampler2D img;
varying vec2 texcoord;


void main()
{
        vec4 texcolor=texture2D(img,texcoord);
    gl_FragColor=texcolor;
}


实际质地:



我的输出结果:

最佳答案

您的红色和蓝色通道在图像文件中交换。

若要更正此问题,请使用其他像素传输格式(BGR):

GLint pixel_mode    = GL_BGR;                   // Image File
GLint internal_mode = GL_RGB;                   // OpenGL

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// Create the texture. We get the offsets from the image, then we use it with the image's
// pixel data to create it.
glTexImage2D(GL_TEXTURE_2D, 0, internal_mode, width, height, 0, pixel_mode, GL_UNSIGNED_BYTE, Pixels.data());

关于c++ - Textureloading加载蓝色而不是实际颜色的纹理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27334152/

10-13 07:11