简体   繁体   English

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

[英]How to display a loaded texture on the screen using openGL

I am using the function :我正在使用该功能:

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

in display() function :display()函数中:

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

how do i make the texture appear on the screen?如何让纹理出现在屏幕上? what do i need to do after i have binded the texture?绑定纹理后我需要做什么?

If your Texture is loaded correctly, what you need is a shader pipeline containing a Vertex shader and a Fragment shader.如果您的纹理正确加载,您需要的是一个包含顶点着色器和片段着色器的着色器管道。 Then you can Render a rectangle and apply the Texture in the Fragment shader Stage.然后你可以渲染一个矩形并在片段着色器阶段应用纹理。

Btw.顺便提一句。 SOIL is great lib for loading textures SOIL 是加载纹理的好库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM