2

I have a have following code which is supposed to display an image but the image never appears.

GLuint tex_2d = SOIL_load_OGL_texture (
    "ImageName.tga",
    SOIL_LOAD_AUTO,
    SOIL_CREATE_NEW_ID,
    SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
glColor3f(0.0f,1.0f,.50f);
glBindTexture(GL_TEXTURE_2D, tex_2d);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
    glTexCoord2d(0,0);        glVertex3f(factor*BOARD_BREADTH/2,-factor*BOARD_LENGTH/2,0);
    glTexCoord2d(0,1);        glVertex3f(factor*BOARD_BREADTH/2,factor*BOARD_LENGTH/2,0);
    glTexCoord2d(1,1);        glVertex3f(-factor*BOARD_BREADTH/2,factor*BOARD_LENGTH/2,0);
    glTexCoord2d(1,0);        glVertex3f(-factor*BOARD_BREADTH/2,-factor*BOARD_LENGTH/2,0);


glEnd();

But I only get a green rectangle as output. There is no compilation error.

pranphy
  • 1,787
  • 4
  • 16
  • 22
  • 1
    Did you check if the `tex_2d` actually is not 0 (i.e. if the image loading failed)? Also, `glColor` call will have an impact on the texture rendering in fixed pipeline, do you know that? – Bartek Banachewicz Feb 21 '13 at 11:29
  • it seems there is the problem. It displays the imgae when the full path like `C:\\Images\\Imagename.tga`is given which of course is not always possible. So I want to know which relative path should I use for the image file? I am using **Code::Blocks** 10.05 as my IDE – pranphy Feb 22 '13 at 18:47
  • Are you asking me how to construct relative path for a file? *Srsly?* `..` is "up one directory". So if your code is in `/whatever/bin/` and image in `/whatever/images/`, the path will be `../images/imagename.tga`. – Bartek Banachewicz Feb 22 '13 at 18:58

1 Answers1

2

SOIL_load_OGL_texture returns correct OpenGL texture identifier if the loading succeeds, 0 if it fails. You should always check for that!

In your case, if the wrong path caused the problem, use relative paths. Here's example folder structure:

root/
--- data/
-------- music/
-------- images/
------------ texture.tga
--- bin/
-------- debug/
------------ program.exe

In that case, the relative path would be "../../data/images/texture.tga". Note how we go up twice (by .., to get to root/, then go into data/images/.

That way if you keep the folder structure, it doesn't matter where the root/ resides on disk.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135