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):
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;
}
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;
}
来源:https://stackoverflow.com/questions/13012026/soil-unable-to-open-file-in-c-and-opengl-with-xcode