0
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
int width, height, channels;
unsigned char* image = stbi_load("file.jpg", &width, &height, &channels, 0);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    
glBegin(GL_POLYGON);
glVertex2f(0,0);
glVertex2f(100, 0);
glVertex2f(100,100);
glVertex2f(0,100);
glEnd();
    
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(image);

glPopAttrib();
    
glfwSwapBuffers(window);
    
glfwPollEvents();

this correctly produces the square but the square does not have the texture applied to it, only white. texture file i was using.

[1

Also, here is the code which sets my window up:

static void WindowSetup()
{
    glViewport((GLint)0, (GLint)0, SCREEN_WIDTH, SCREEN_HEIGHT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Jo Ca
  • 1
  • 2
  • Is the image even read? Is `image` a valid address or is it `nullptr`? What happens if you change the number of channels form 0 to 4: `stbi_load("file.jpg", &width, &height, &channels, 4);`? – Rabbid76 Apr 29 '20 at 19:34
  • @Rabbid76 , ah. Well, I did a quick if check to see if the image was a nullptr and it seems it is. Not sure what the problem is but I now know it originates at reading the image data. Any ideas? – Jo Ca Apr 29 '20 at 19:38
  • The image is read form the current working directory. Use an absolut path instead of the relative path. What is your os? – Rabbid76 Apr 29 '20 at 19:40
  • @Rabbid76 Huzzah! Changing the directory name to an absolute path worked! – Jo Ca Apr 29 '20 at 19:44

1 Answers1

0

You have to set the texture coordinates, by glTexCoord:

glBegin(GL_POLYGON);
glTexCoord2f(0, 1);
glVertex2f(0, 0);
glTexCoord2f(1, 1);
glVertex2f(100, 0);
glTexCoord2f(1, 0);
glVertex2f(100, 100);
glTexCoord2f(0, 0);
glVertex2f(0, 100);
glEnd();

If the texture coordinates are not set, then all the texture coordinates associated to the vertices are the default coordinates (0, 0) and just one point of the texture is wrapped on the entire primitive.
See also How do opengl texture coordinates work?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thanks for the comment. It unfortunately seemed not to aid my issue though. I suspect I will need this solution and to change something about my window setup code as the matrix mode is changed and the coordinate system is changed in that which may cause some errors. If it helps, I have added to my question the code in which I think may be problematic. I assume the reason that my square appears blank is due to what you mentioned and the way the window is initialised. – Jo Ca Apr 29 '20 at 19:24
  • Unfortunately not. It is still a blank white square, i have updated my Question for extra info @Rabbit76 – Jo Ca Apr 29 '20 at 19:27