SOIL:使用Xcode在C ++和OpenGL中“无法打开文件" [英] SOIL: 'Unable to open file' in C++ and OpenGL with Xcode

查看:236
本文介绍了SOIL:使用Xcode在C ++和OpenGL中“无法打开文件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SOIL加载纹理文件.我尝试从项目文件中加载图像,也尝试从文件系统中加载图像.当我尝试从项目SOIL_last_result()加载图像时,出现错误:

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.

这是整个代码:

#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;
}

推荐答案

好吧,经过数小时的搜索和使用Xcode,我终于明白了...

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

第一个问题是Xcode项目导航器中的图像资源在buildung之后不会自动出现在您的应用程序路径中.因此,您必须在项目导航器中单击项目(它是项目导航器中所有文件的根目录).然后选择您的目标,单击选项卡构建阶段".现在,重要的是添加一个全新的构建阶段!单击添加构建阶段"按钮,然后选择添加复制文件".您将获得一个新的复制文件"元素.目标应自动为资源".现在,单击"+"并添加要在应用程序中使用的所有图像文件.现在,它们将在构建时复制到您的应用程序路径,或者,如果已设置,则复制到子路径.如果未设置子路径,则图像的路径将是应用程序的根目录,因此您可以通过"filename.png"或其他任何方式访问文件.

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.

第二个问题是EXC_BAD_ACCESS ... LoadGLTextures()函数放在错误的地方...新代码(只是更改了main函数,所以我只粘贴了main)是:

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;
}

这篇关于SOIL:使用Xcode在C ++和OpenGL中“无法打开文件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆