我正在尝试使用 SOIL 加载纹理文件。我尝试从项目文件中加载图像,也尝试从文件系统中加载图像。当我尝试从项目中加载我的图像时,SOIL_last_result()
出现错误:
无法打开文件
但应用程序无论如何都会运行。
当我在我的系统中输入图像的完整路径时,我得到(当我构建项目时):
我不知道那里出了什么问题。我读到它可能是代码中存在任何其他错误,这就是我在这一点上遇到问题的原因。
这是整个代码:
#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;
}