开发者

How will I add multiple textures without glaux?

there's a problem which I don't know how to solve. I know how to get multiple textures in my 3D openGL and MVC++ 2010 game but the way is extremely slow. The more textures I load the more the game gets slower. It has been 2 weeks since I got stuck with this. I searched many sites but none of them helped me.

I don't want glaux because it keeps giving me errors and is said to have memory leaks. This is the way I load .RAW textures:

GLuint texture; //ID to bind w开发者_Python百科ith

    GLuint LoadTexture(const char *filename, int imW, int imH){
    unsigned char*data;
    FILE*file;

    file=fopen(filename,"rb");
    if(file==NULL)return 0;
    data=(unsigned char*)malloc(imW*imH*3);
    fread(data,imW*imH*3,1,file);
    fclose(file);

    glGenTextures(1,&texture);
    glBindTexture(GL_TEXTURE_2D,texture);
    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D,0x8191,1); //0x8191 means GL_GENERATE_MIPMAP
    glTexImage2D(GL_TEXTURE_2D,0,3,imW,imH,0,GL_RGB,GL_UNSIGNED_BYTE,data);
    free(data);
    return texture;
    }

I have opengl 2 but I use "glTexParameterf(GL_TEXTURE_2D,0x8191,1);" to build mipmaps because "gluBuild2DMipmaps" is slower than that.

void model_room(float x, float y, float x2, float y2, float z, float z2){
LoadTexture("tex_floor.raw",512,512);
model_floor(x,y,x2,y2,z,7,7);
glDeleteTextures(1,&texture);

LoadTexture("tex_ceiling.raw",512,512);
model_floor(x,y,x2,y2,z2,4,4);
glDeleteTextures(1,&texture);

LoadTexture("tex_wall.raw",512,512);
model_wall(x,y,x,y2,z,z2,1,1);
glDeleteTextures(1,&texture);

LoadTexture("tex_tiles.raw",512,512);
model_wall(x,y2,x2,y2,z,z2,1,1);
glDeleteTextures(1,&texture);

LoadTexture("tex_diamondtiles.raw",512,512);
model_wall(x2,y2,x2,y,z,z2,1,1);
glDeleteTextures(1,&texture);

LoadTexture("tex_castlepoint.raw",512,512);
model_wall(x2,y,x,y,z,z2,5,3);
glDeleteTextures(1,&texture);
}

Over here the code loads the textures and then deletes them every frame. It might not be a good idea but for now it's okay. For "model_wall" and "model_floor", this is the code:

    void model_floor(float x, float y, float x2, float y2, float z, float tc1, float tc2){
glBegin(GL_QUADS);
    glTexCoord2f(0,     0);     glVertex3f(x,  y,  z);
    glTexCoord2f(tc1,   0);     glVertex3f(x,  y2, z);
    glTexCoord2f(tc1,   tc2);   glVertex3f(x2, y2, z);
    glTexCoord2f(0,     tc2);   glVertex3f(x2, y,  z);
glEnd();
    }

    void model_wall(float x, float y, float x2, float y2, float z1, float z2, float tch, float tcv){
glBegin(GL_QUADS);
    glTexCoord2f(0,     0);     glVertex3f(x,   y,  z2);
    glTexCoord2f(tch,   0);     glVertex3f(x2,  y2, z2);
    glTexCoord2f(tch,   tcv);   glVertex3f(x2,  y2, z1);
    glTexCoord2f(0,     tcv);   glVertex3f(x,   y,  z1);
glEnd();
    }

I just want a good way to load textures without slowing down the game and it's not important to only load raw files but anything that is recommended. I'm not a Win Base programmer so I may want some explanation on those type of codes. Any help would be appreciated. Thanks in advance!


It might not be a good idea to load and destroy all textures every frame? Definitely not a good idea and surely the cause of your performance drop! That's what texture objects are for (the IDs you get from glGenTextures). These are handles to the textures in video memory. So you load all your textures at start, then just bind the neccessary texture (using glBindTexture) before drawing (before the model_wall calls). And then at the end of your program you destroy them using glDeleteTextures.

By your mentioning of glaux, I assume you are using quite old learning ressources, maybe you should look for a better tutorial/book if you got this practice from there.


Loading the textures every frame is a terrible idea, and it's probably why your game slows down. Just load them once and then make them active when using them.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜