Pregunta

I'm attempting to load two textures and then switch between the two in my display function. I am using the SOIL library to load the textures as such:

tex_2 = SOIL_load_OGL_texture
    (
            "s9.png",
            SOIL_LOAD_AUTO,
            SOIL_CREATE_NEW_ID,
            SOIL_FLAG_MIPMAPS | SOIL_FLAG_COMPRESS_TO_DXT
    );

tex_1 = SOIL_load_OGL_texture
    (
            "s8.png",
            SOIL_LOAD_AUTO,
            SOIL_CREATE_NEW_ID,
            SOIL_FLAG_MIPMAPS | SOIL_FLAG_COMPRESS_TO_DXT
    );

And then I use

glBindTexture(GL_TEXTURE_2D, tex_1) 
or
glBindTexture(GL_TEXTURE_2D, tex_2);

To switch between the two. The problem is I must be loading them incorrectly and I'm not sure how. Whichever texture I load in last (tex_1 in the code above) is the the texture that I get for both tex_1 and tex_2 when I try to switch with glBindTexture. Any ideas?

Before loading the teaxtures I set up blending and turn on texture and sprites

glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE);
glEnable(GL_TEXTURE_2D);
glEnable(GL_POINT_SPRITE);
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

Then in my display function, I loop over all my points which I want to draw and attempt to change the current texture before drawing the point with glVertex3f:

for(int i=0; i<num_particles; i++)
{
    //select texture to use
    if(class[i] == 2.0f)
        glBindTexture(GL_TEXTURE_2D, tex_2);
    else
        glBindTexture(GL_TEXTURE_2D, tex_1);

    glVertex3f(posn[3*i], posn[3*i+1], posn[3*i+2]);
}

The goal is I have two types of points and the sprite to be drawn at each point depends on the class the point is in

¿Fue útil?

Solución

for(int i=0; i<num_particles; i++)
{
    //select texture to use
    if(class[i] == 2.0f)
        glBindTexture(GL_TEXTURE_2D, tex_2);
    else
        glBindTexture(GL_TEXTURE_2D, tex_1);

    glVertex3f(posn[3*i], posn[3*i+1], posn[3*i+2]);
}

You can't call glBindTexture() inside a glBegin()/glEnd() pair:

GL_INVALID_OPERATION is generated if glBindTexture is executed between the execution of glBegin and the corresponding execution of glEnd.

The last successful glBindTexture() was probably in the most recent SOIL_load_OGL_texture() call. That's why tex_1 and tex_2 seem to contain the same texture data: tex_2 is never rebound.

Otros consejos

As previous answers: you cannot change textures between glBegin/glEnd.

What can you do then?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top