开发者

Choosing textures in open GL

Hi guys and girls the problem I have is I have sucessfully loaded 3 BMP textures (or at least I hope I have using char* textureFilenames[textureCount] = {"cement.bmp","hedge.bmp","sky.bmp"}; and I'm applying it currently using

    glTexCoord2f(0.0,0.0);
    glVertex3f(-150.0, 0.0, -150.0);
    glTexCoord2f(1.0,0.0);
    glVertex3f(-150.0, 0.0, 150.0);
    glTexCoord2f(1.0,1.0);
    glVertex开发者_如何学Python3f(150.0, 0.0, 150.0);
    glTexCoord2f(0.0,1.0);
    glVertex3f(150.0, 0.0, -150.0);

however it currently only picks up the sky.bmp is there anyway i can select one of the others?


OpenGL is a state machine. The current texture is part of the OpenGL state. The last texture you bind with glBindTexture() will be used until you bind another.

glBindTexture(GL_TEXTURE_2D, cement_texture_id);

// ... following geometry will use the cement texture

glBindTexture(GL_TEXTURE_2D, hedge_texture_id);

// ... hedge texture

glBindTexture(GL_TEXTURE_2D, sky_texture_id);

// ... sky texture

The "OpenGL RedBook" has a chapter on texture mapping that covers the basics.


You mistake lies in your lack of understanding of OpenGL. OpenGL is not a scene graph! It's best to think OpenGL to be a set of drawing tools to paint on a canvas called the frame buffer.

So in using OpenGL you must put your mind in a state similar to if you's draw a picture with pencils, eraser, brush and paint. First you prepare your tools: Textures are like "sheets of colour", meshes of vertices are like some delicate "brush".

Like an artist the very fist step is to prepare your tools. You prepare your geometry (i.e. the meshes), if you use Vertex Buffer Objects you load them into fast memory with glBufferData, and your paint and dye, the textures. This is what you do in the "init" phase (I prefer to do this on demand, so that users don't see a "loading" screen).

First you load all your objects (geometry in VBOs, textures etc.); you do this exactly once for each required object, i.e. once an object is prepared (i.e. complete) you don't have to re-upload it.

Then in every drawing iteration for each object you want to draw you bind the needed OpenGL objects to their targets, then perform the drawing calls, which will then be performed using the currently bound objects.

i.e. something like this, please use common sense to fill in the lacking functions in your mind:

struct texture; // some structure holding texture information, details don't matter here
struct geometry; // structure holding object geometry and cross references

texture *textures;
geometry *geometries;

texture * load_texture(char const *texture_name)
{
    texture *tex;

    if( texture_already_loaded(textures, texture_name) )
        tex = get_texture(texture_name);
    else
        tex = load_texture_data(textures, texture_name);

    return tex;
}

geometry * load_geometry(char const *geometry_name)
{
    geometry * geom;

    if( geometry_already_loaded(geometries, geometry_name) )
        geom = get_geometry(geometry_name);
    else
        geom = load_geometry_data(geometries, geometry_name)

    if( geom->texture_name )
        geom->texture = load_texture(geom->texture_name);

    return geom;
}

void onetime_initialization()
{
    for(geometry_list_entry * geom = geometry_name_list; geom ; geom = geom->next)
        geom->geometry = geometry_load(geom->name);
}

void drawGL()
{
    glViewport(...);
    glClearColor(...);
    glClear(...);

    glMatrixMode(GL_PROJECTION);
    // ...

    glMatrixMode(GL_MODELVIEW);
    // ...

    for(geometry_list_entry * geom = geometry_name_list; geom ; geom = geom->next)
    {
        glMatrixMode(GL_MODELVIEW); // this is not redundant!
        glPushMatrix();

        apply_geometry_transformation(geom->transformation); // calls the proper glTranslate, glRotate, glLoadMatrix, glMultMatrix, etc.

        glBindTexture(GL_TEXTURE_2D, geom->texture->ID);
        draw_geometry(geom);

        glMatrixMode(GL_MODELVIEW); // this is not redundant!
        glPopMatrix();
    }

   // ...

   swapBuffers();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜