2

I'm trying to load a texture file with SOIL. I tried to load an image from the project files and I also tried to load an image from the file system. When I try to load my image from the project SOIL_last_result() gives me the error:

Unable to open file

But the application runs anyway.

When I type in the full path to an image in my system I get (when I build the project):

lazy

I don't know whats going wrong there. I was reading that it could be, that there are any other errors in the code and that's why I get problems at this point.

Here is the whole code:

#include <stdlib.h>
#include <stdio.h>
#include <GLUT/glut.h>
#include "SOIL.h"

float boxX = 0.0f;
float boxY = 0.0f;

bool* keyStates = new bool[256];
bool* keySpecialStates = new bool[246];

int windowId = 0;

GLuint texture[1];

int LoadGLTextures()
{
    /* load an image file directly as a new OpenGL texture */
    texture[0] = SOIL_load_OGL_texture
    (
     //"Data/colonel_hapablap.jpg",
     "/Users/Thomas/Pictures/colonel_hapablap.jpg",
     SOIL_LOAD_AUTO,
     SOIL_CREATE_NEW_ID,
     SOIL_FLAG_INVERT_Y
    );

    if(texture[0] == 0)
    {
        printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
        return false;
    }


    // Typical Texture Generation Using Data From The Bitmap
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

    return true;
}

// key is pressed
void keyPressed(unsigned char key, int x, int y)
{
    keyStates[key] = true;
}

// key is released
void keyUp(unsigned char key, int x, int y)
{
    keyStates[key] = false;
}

void keySpecialPressed(int key, int x, int y)
{
    keySpecialStates[key] = true;
}

void keySpecialUp(int key, int x, int y)
{
    keySpecialStates[key] = false;
}

void keyOperations(void)
{
    if(keySpecialStates[GLUT_KEY_LEFT])
    {
        boxX -= 0.1f;
    }

    if(keySpecialStates[GLUT_KEY_RIGHT])
    {
        boxX += 0.1f;
    }

    if(keySpecialStates[GLUT_KEY_UP])
    {
        boxY += 0.1f;
    }

    if(keySpecialStates[GLUT_KEY_DOWN])
    {
        boxY -= 0.1f;
    }

    if(keyStates['q'])
    {
        glutDestroyWindow(windowId);
        exit(0);
    }
}

void display(void)
{
    keyOperations();

    int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
    int windowHeight = glutGet(GLUT_WINDOW_HEIGHT);

    if(boxX > windowWidth)
    {
        boxX -= windowWidth;
    }

    if(boxX < 0)
    {
        boxX = windowWidth;
    }

    if(boxY > windowHeight)
    {
        boxY -= windowHeight;
    }

    if(boxY < 0)
    {
        boxY = windowHeight;
    }

    glEnable(GL_TEXTURE_2D);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glTranslatef(boxX, boxY, 0.0f);

    glBegin(GL_QUADS);

    LoadGLTextures();

    //glColor3f(0.0f, 1.0f, 0.0f);
    glVertex2f(0.0f,   128.0f);
    glVertex2f(128.0f, 128.0f);
    glVertex2f(128.0f, 256.0f);
    glVertex2f(0.0f,   256.0f);
    glEnd();

    glPopMatrix();

    glutSwapBuffers();
}

void reshape(int width, int height)
{
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, width, 0, height);
    glMatrixMode(GL_MODELVIEW);
}

void idle(void)
{
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(640, 480);

    windowId = glutCreateWindow("GLUT Program");

    //glutFullScreen();

    glutKeyboardFunc(keyPressed);
    glutKeyboardUpFunc(keyUp);
    glutSpecialFunc(keySpecialPressed);
    glutSpecialUpFunc(keySpecialUp);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);

    glutMainLoop();
    return EXIT_SUCCESS;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
hapablap
  • 379
  • 6
  • 23
  • Is `colonel_hapablap.jpg` actually all lower-case on-disk? If you grabbed the image [from here](http://simpsons.wikia.com/wiki/File:Colonel_hapablap.jpg) and didn't rename it then the "c" is capitalized. Depending on your filesystem that might be a problem. – genpfault Oct 22 '12 at 14:41
  • Why are you loading the textures inside the display callback? It should be done before you begin to render. – ravuya Oct 22 '12 at 18:14
  • @ genpfault: Yes it's all lower case. I copied the path and the filename so that there should be no mistakes. @ravuya I've put the [code]LoadGLTextures();[/code] now outside. I try to learn OpenGL and all that stuff from websites and at the moment I don't know much about it. – hapablap Oct 22 '12 at 21:55
  • I'm still on the problem but I think I know what's going wrong, I'm just still searching for a solution. I've tried to put the image file in different directions and read it from there. Inside the project, in a subdirectory of the project, in my home folder. The problem is: the program is running in a different folder, so the version with just the filename doesn't work. Absolute path doesn't work because I have no permission. So now I need to know how to add image resources to a Xcode C++ project so that it copies it to the running programs folder. I'm still searching. – hapablap Oct 22 '12 at 23:17

1 Answers1

1

Ok, after hours of searching and playing with Xcode, I finally got it ...

First problem was that the image resources in the projects navigator in Xcode are not automatically in your applications path after buildung. So you have to click on your project in your project navigator (it's the root of all files in the project navigator). Then select your target, click on the tab "Build Phases". Now it's important to add a completely new build phase! Click on "Add Build Phase" button and select "Add copy files". You will get a new "Copy files" element. Destination should be automatically "Resources". Now click on the "+" and add all image files you want to use in your application. They will now be copied on build to your application path or, if you have set up, to the subpath. If you set up no subpath the path to your image will be the root directory of your app so you can just access the file via "filename.png" or whatever.

Second problem was the EXC_BAD_ACCESS ... The LoadGLTextures() function was just on the wrong place ... The new code (just the main function changed, so I just paste the main) is:

int main(int argc, char** argv)
{
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(640, 480);

    windowId = glutCreateWindow("GLUT Program");

    LoadGLTextures();

    //glutFullScreen();

    glutKeyboardFunc(keyPressed);
    glutKeyboardUpFunc(keyUp);
    glutSpecialFunc(keySpecialPressed);
    glutSpecialUpFunc(keySpecialUp);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);

    glutMainLoop();
    return EXIT_SUCCESS;
}
hapablap
  • 379
  • 6
  • 23