简体   繁体   中英

Mapping a texture with OpenGL using SOIL

Trying to complete a basic texture map to surface using OpenGL and SOIL but I am not generating anything.

GLuint textureID[5];

glutInitWindowPosition(0, 50);
windowID[0] = glutCreateWindow("orthogonal projection, cubes");

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-400, 400, -400, 400, -500, 500);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glutKeyboardFunc(Keyboard);
glutDisplayFunc(DrawWindowOne);

textureID[0] = SOIL_load_OGL_texture("assets/faceA.png", 
              SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);

void DrawWindowOne()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);   
    glClear(GL_COLOR_BUFFER_BIT);   

    glViewport(0, 0, 250, 250); 

    glMatrixMode(GL_MODELVIEW); 

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glEnable(GL_TEXTURE_2D);

    glBindTexture(GL_TEXTURE_2D, textureID[0]);

    glBegin(GL_QUADS);
    glNormal3f(0.0, 0.0, 1.0); // front face
    glTexCoord2f(0.0, 0.0);     glVertex3f(-a,-a, a);
    glTexCoord2f(0.0, 1.0);     glVertex3f(-a, a, a);
    glTexCoord2f(1.0, 1.0);     glVertex3f( a, a, a);
    glTexCoord2f(1.0, 0.0);     glVertex3f( a,-a, a);
    glEnd();

    glDisable(GL_TEXTURE_2D);
}

The face draws in blue, however, and I get no texture. I have a second window, where apart from position the only differance is that I am using Frustrum as opposed to Orthogonal

windowID[1] = glutCreateWindow("Perspective projection using glFrustum");

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-60, 60, -60, 60, 60, 200);
gluLookAt(0, 0, 120, 0, 0, 0, 0, 1, 0);

and the texture draws fine.

The problem was that I was loading the textures all at once, where it appears they need to be loaded after each window is initialized to be available for that windows draw code.

#pragma region Initialise Window One
glutInitWindowPosition(0, 50);
windowID[1] = glutCreateWindow("orthogonal projection, cubes");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-400, 400, -400, 400, -500, 500);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glutKeyboardFunc(Keyboard);
glutDisplayFunc(DrawWindowOne);
LoadTextures();
#pragma endregion

#pragma region Initialise Window Two
glutInitWindowPosition(0, 450);
windowID[1] = glutCreateWindow("Perspective projection using glFrustum, ellipsoids");

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-60, 60, -60, 60, 60, 200);
gluLookAt(0, 0, 120, 0, 0, 0, 0, 1, 0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glutKeyboardFunc(Keyboard);
glutDisplayFunc(DrawWindowTwo);
LoadTextures();
#pragma endregion

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