OpenGL不会绘制我的纹理

时间:2015-08-05 16:16:15

标签: opengl textures bmp texture2d

我的问题是OpenGL 3.3没有绘制我的纹理。我检查了它是否是我的着色器的问题(它无法正常加载)但是着色器没问题。然后,我检查了它是否是UV坐标的问题,但我看到使用Fragment Shader的代码就可以了:

#version 330 core

out vec3 outColor;

in DATA {
    vec2 UV;
} fs_in;

void main() {
    outColor = vec3(fs_in.UV.x, fs_in.UV.y, 0.0);
}

最后,我使用了片段着色器的这段代码,但它不起作用:

#version 330 core

out vec3 outColor;

uniform sampler2D mySampler;

in DATA {
    vec2 UV;
} fs_in;

void main() {
    outColor = texture(mySampler, fs_in.UV).rgb;
}

加载BMP图像并将其提供给OpenGL的代码如下(我在网站上找到它):

GLuint loadTexture(const char *lpszTexturePath) {
    unsigned char header[54];
    unsigned int dataPos;
    unsigned int width, height;
    unsigned int imageSize;
    unsigned char * data;

    FILE *file;

    fopen_s(&file, lpszTexturePath, "rb");

    if(!file)
        printf("Image could not be opened\n"); return 0;

    if(fread(header, 1, 54, file) != 54) {
        printf("Not a correct BMP file\n");
        return false;
    }

    if(header[0] != 'B' || header[1] != 'M') {
        printf("Not a correct BMP file\n");
        return 0;
    }

    dataPos = *(int*)&(header[0x0A]);
    imageSize = *(int*)&(header[0x22]);
    width = *(int*)&(header[0x12]);
    height = *(int*)&(header[0x16]);

    if(imageSize == 0)    imageSize = width*height * 3;
    if(dataPos == 0)      dataPos = 54;

    data = new unsigned char[imageSize];
    fread(data, 1, imageSize, file);

    fclose(file);

    GLuint textureID;
    glGenTextures(1, &textureID);

    glBindTexture(GL_TEXTURE_2D, textureID);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);

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

    return textureID;
}

部分主要代码: GLuint uTextureID = loadTexture(" image.bmp");

while(!glfwWindowShouldClose(lpstWndID)) {
    glfwPollEvents();

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(uProgID);

    glBindTexture(GL_TEXTURE_2D, uTextureID);

    object.renderObject();

    glfwSwapBuffers(lpstWndID);
}

1 个答案:

答案 0 :(得分:0)

天哪啊。错误非常......非常......自己看看......

纹理加载器的第10行:

if(!file)
        printf("Image could not be opened\n"); return 0;

有人看到一些额外的东西吗? YEAH,它在IF BLOCK外面有一个返回...这个函数正在返回0 ...

谢谢(我是愚蠢的xD)