如何使用openGL在屏幕上显示加载的纹理

问题描述 投票:0回答:1

我正在使用该功能:

GLuint LoadTexture(const char* filename)
{

    GLuint texture;

    int width, height;

    unsigned char* data;

    FILE* file;

    file = fopen(filename, "rb");

    if (file == NULL) return 0;
    width = 1024;
    height = 512;
    data = (unsigned char*)malloc(width * height * 3);
    //int size = fseek(file,);
    fread(data, width * height * 3, 1, file);
    fclose(file);

    for (int i = 0; i < width * height; ++i)
    {
        int index = i * 3;
        unsigned char B, R;
        B = data[index];
        R = data[index + 2];

        data[index] = R;
        data[index + 2] = B;

    }


    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);


    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
    free(data);

    return texture;
}

display()功能中:

GLuint texture;
texture = LoadTexture("bubble.png");
glBindTexture(GL_TEXTURE_2D, texture);

我如何使纹理出现在屏幕上?绑定纹理后该怎么办?

c++ opengl glut opengl-compat
1个回答
0
投票

如果正确加载了纹理,则需要一个包含顶点着色器和片段着色器的着色器管道。然后,您可以渲染一个矩形并在“片段着色器”阶段中应用“纹理”。

顺便说一句。 SOIL是loading纹理

的绝佳库
© www.soinside.com 2019 - 2024. All rights reserved.