0

可能重复:
使用 openGL、SOIL 加载图像

在运行下面的源代码时,它只显示一个没有图像的 wite quad。我试图打开一个名为:foto 的图像。我将图像文件放在程序的文件夹中。

bool* keyStates = new bool[256];
GLuint texture[0];

void resize(int height, int width) {
    const float ar = (float) width / (float) height;
    glViewport(0, 10, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 90.0);
    //gluLookAt(0, 2, 0, -1, 1, -3, 0, 1, 0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}
void keyOperations (void) {
    if (!keyStates['a']) {}
}
static void Draw(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0f,0.0f,-5.0f);
    texture[0] = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture
    (
        "foto.png",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
    );
// allocate a texture name

    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
    glEnd();
    glutSwapBuffers();
}
void keyPressed (unsigned char key, int x, int y) {
    keyStates[key] = false;
}
void keyUp (unsigned char key, int x, int y) {
    keyStates[key] = true;
}
int main(int argc, char **argv)
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInit(&argc, argv);
    glutInitWindowSize(600, 600);
    glutCreateWindow("ugh fml");
    glutReshapeFunc(resize);
    glutDisplayFunc(Draw);
    glutKeyboardFunc(keyPressed);
    glutKeyboardUpFunc(keyUp);
    /////////////////////////////////////
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glutMainLoop();
}
4

1 回答 1

0

如果图像文件(foto.png)不在程序的工作目录中,我会得到相同的行为。但是当我将图像添加到程序的工作目录时,四边形的纹理正确。尝试 1) 将纹理图像添加到程序的工作目录或 2) 将程序的工作目录更改为纹理文件所在的目录。

于 2013-01-08T20:41:21.903 回答