简体   繁体   中英

OpenGL texture loading with soil help need

Here is the code. Please help guys

#include <GL/glut.h>
#include <iostream>
#include <math.h>
#include "SOIL.h"
using namespace std;

GLuint texture[1];

int LoadGLTextures()
{
    texture[0] = SOIL_load_OGL_texture
        (
        "NeHe.bmp",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_INVERT_Y
        );

    if(texture[0] == 0)
        return false;


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

void ChangeSize(int w, int h)
{
    if(h == 0)
    {
        h = 1;
    }
    float ratio = 1.0f * w/h;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0,0,w,h);
    gluPerspective(45,ratio,1,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0,0.0,6.0,
              0.0,0.0,0.0,
              0.0f,1.0f,0.0f);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_TEXTURE_2D);
    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();

    glFlush();
}

void idle(void)
{
    glutPostRedisplay();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
    glutCreateWindow("ZC");

    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(display);
    glutIdleFunc(idle);

    glEnable(GL_DEPTH_TEST);

    glutMainLoop();
}

I dont get errors, but not loading the texture.

You forgot to call LoadGLTextures in your main program. I ran your program with this change:

  glEnable(GL_DEPTH_TEST);

  LoadGLTextures(); //<-- added this

  glutMainLoop();
}

and things worked fine.

I provided my own NeHe.bmp file, since you didn't link to one. It could also fail if your .bmp file is bad in some way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM