0

I am learning OpenGL and while working with textures I came across a problem. When I try to render a texture I simply see a black box instead of the texture image. My code is as follows:

my loadTexture function:

/* Function to load Textures */
void loadTextures(char* filename)
{
    stbi_set_flip_vertically_on_load(1);
    unsigned int logoTexture;
    unsigned char* logo_buffer = NULL;
    int width = 400, height = 400, BPP;
    glGenTextures(1, &logoTexture);
    glBindTexture(GL_TEXTURE_2D, logoTexture);
    logo_buffer = stbi_load(filename, &width, &height, &BPP, 0);
    if(logo_buffer == NULL)
    {
        cout<<"\nError"<<endl;
    }
    else 
    {
        cout<<"\nSuccess"<<endl;
    }
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, logo_buffer);
    stbi_image_free(logo_buffer);
}

The display function where I am drawing the texture:

glClearColor(255.0, 250.0, 250.0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glLoadIdentity();

glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
    glTexCoord2f(250, 250);
    glVertex3f(250.0, 250.0, 0.0);
    glTexCoord2f(650, 250);
    glVertex3f(650.0, 250.0, 0.0);
    glTexCoord2f(650, 550);
    glVertex3f(650.0, 550.0, 0.0);
    glTexCoord2f(250, 550);
    glVertex3f(250.0, 550.0, 0.0);
glEnd();

I see a black rectangle but no texture on it. Help will be highly appreciated.

malik727
  • 169
  • 8
  • 1
    Your texcoords are _way off_, and together with `GL_CLAMP_TO_EDGE`, you will only sample texels an the very right border of the texture. – derhass May 03 '20 at 12:30
  • Can you please elaborate ... actually I have set my window canvas so that bottom left is 0,0 and the coordinates are measured in px – malik727 May 03 '20 at 12:36
  • And how have you set up that the texture coordinates are in pixels? The projection matrix will not affect texture coords. – derhass May 03 '20 at 12:51
  • @Gingitsune The texture coordinates have to be in range [0, 1]. See [How do opengl texture coordinates work?](https://stackoverflow.com/questions/5532595/how-do-opengl-texture-coordinates-work) – Rabbid76 May 03 '20 at 12:58
  • @Rabbid76: Well, legacy GL _has_ a texture matrix which could do what is desired here, but I hardly doubt that it was used, hence my question for clarification. – derhass May 03 '20 at 16:42

0 Answers0