2

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

4

2 回答 2

3
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]);
}

你不能glBindTexture()在一glBegin()/glEnd()对内跟注:

GL_INVALID_OPERATION如果在 的执行和 的相应执行glBindTexture之间执行,则生成.glBeginglEnd

最后一次成功 glBindTexture()可能是在最近的SOIL_load_OGL_texture()通话中。这就是为什么tex_1tex_2似乎包含相同的纹理数据:tex_2永远不会反弹。

于 2013-04-17T17:48:45.643 回答
1

正如之前的答案:您不能在 glBegin/glEnd 之间更改纹理。

那你能做什么?

于 2013-04-17T18:12:38.530 回答