-2

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 :

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?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • You can't load a ".png" file like that! `fread` just reads the [PNG](https://en.wikipedia.org/wiki/Portable_Network_Graphics) but doesn't decode it. I recommend to use a library like [STB](https://stb.handmade.network/). See [How to load a bmp on GLUT to use it as a texture?](https://stackoverflow.com/questions/12518111/how-to-load-a-bmp-on-glut-to-use-it-as-a-texture/50641676#50641676) – Rabbid76 Dec 04 '19 at 15:24
  • You need to bind the texture, enable texturing (fixed function pipeline) or use a shader that does texturing (shader pipeline), and draw some shape like a quad. Don't forget the shape you draw needs texture coordinates. – user253751 Dec 05 '19 at 11:10

1 Answers1

-2

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

ich.
  • 17
  • 4
  • No, it is not is loaded correctly, because it is a png file. The png file is read but it is not decoded. – Rabbid76 Dec 04 '19 at 15:24
  • i have used soil before but my problem is not with loading the texture. its with displaying it. i dont know how to do that. i just want the source code. i dont want to get into too much detail of opengl. just want to display an image on the screen. – Hurrairah Nasir Dec 04 '19 at 15:25
  • @HurrairahNasir *"i dont want to get into too much detail of opengl."* - you have to. There is no "short" solution (except [Legacy OpenGL](https://www.khronos.org/opengl/wiki/Legacy_OpenGL) - `glBegin`/`glEnd` sequence). Read a tutorial e.g. [LearnOpenGL - Textures](https://learnopengl.com/Getting-started/Textures). – Rabbid76 Dec 04 '19 at 15:28
  • I will give you example Code when i am home. Though understanding in necessary, when Working With OpenGL. You can use it as Basis but i highly recommend going through a Tutorial and learning the basics – ich. Dec 04 '19 at 16:03