OpenGL中多个纹理的问题

时间:2013-03-03 11:21:58

标签: c++ opengl glut textures

我正在尝试编写一个用于将对象加载到程序中的类/问题是OpenGL只加载了所有对象的最后一个纹理。

以下是代码:

game.cpp类:

object =  new Object("table.obj", "wood.bmp",0); // a new object - obj file, texture and 
object1 = new Object("aa.obj", "cloth.bmp",1);   // texture number

object->draw();
object1->draw();

对象类:

AUX_RGBImageRec *texture;

 Object::Object(char* filename, char* texname, int num) {
 tex_num = num;
 is_loaded = false;
.... some vertex stuff here
texture = auxDIBImageLoadA(texname);
}

 void Object::draw() {
if(!is_loaded) {
     loadTexture();
     is_loaded = true;
 }
 ... vertex stuff again
 void Object::loadTexture() {
    GLuint *tex = new GLuint[10];
 unsigned int names[10];
 glGenTextures(1, tex);
 glBindTexture(GL_TEXTURE_2D, tex[tex_num]);
 cout << tex_num << endl;
// glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 glTexImage2D(GL_TEXTURE_2D, 0, 3,
         texture->sizeX,
         texture->sizeY,
         0, GL_RGB, GL_UNSIGNED_BYTE,
         texture->data);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 // free memory
 delete tex;
 cout << tex_num << endl;
  }

2 个答案:

答案 0 :(得分:1)

为什么要动态分配纹理ID变量?为什么在需要时在loadTexture的末尾删除它,在绘图时实际引用纹理? OpenGL不会将纹理与几何本身神奇地关联起来。

以下更改:

- Object::Object(char* filename, char* texname, int num) {
+ Object::Object(char* filename, char* texfilename) {

-     tex_num = num;
     is_loaded = false;
    .... some vertex stuff here
-    texture = auxDIBImageLoadA(texname);
+    loadTexture(texfilename);

}

class Object {
/*...*/
+     GLuint texID;
}

void Object::draw() {
-if(!is_loaded) {
-     loadTexture();
-     is_loaded = true;
- }

+  glBindTexure(GL_TEXTURE_2D, texID);
+  draw_geometry();
}

- void Object::loadTexture() {
+ void Object::loadTexture(char const * const texfilename) {

 -    GLuint *tex = new GLuint[10];
 -    unsigned int names[10];
 -    glGenTextures(1, tex);
 -    glBindTexture(GL_TEXTURE_2D, tex[tex_num]);
 -    cout << tex_num << endl;

 +    glGenTexture(1, &texID);
 +    glBindTexture(GL_TEXTURE_2D, texID);
 +    cout << texID << endl;

 +    // Image is a yet to implement class offering image loading
 +    // in a RAII fashion.
 +    Image img = Image::fromFile(texfilename);

 -   // glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 -    glTexImage2D(GL_TEXTURE_2D, 0, 3,
 -            texture->sizeX,
 -            texture->sizeY,
 -            0, GL_RGB, GL_UNSIGNED_BYTE,
 -            texture->data);

 +   glPixelStorei(GL_UNPACK_ALIGNMENT, img.alignment());
 +    glTexImage2D(GL_TEXTURE_2D, 0,
 +            img.glinternalformat(), // don't use a channel count here!
 +            img.width(),
 +            img.height(),
 +            0,
 +            img.glformat(),
 +            img.gltype(),
 +            img.data() );

     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

-     // free memory
-     delete tex;
-     cout << tex_num << endl;
+     cout << texID << endl;

  }

答案 1 :(得分:0)

现在您的第一个draw()调用加载了纹理,您只需在每次绘制之前调用glBindTexture(GL_TEXTURE_2D,your_generated_buffer)。

void      Object::draw()
{
       if(!is_loaded)
       {
            loadTexture();
            is_loaded = true;
       }
       glBindTexture(GL_TEXTURE_2D, this->texture_buffer);
       // Your vertex things..
}

如果this-&gt; texture_buffer表示你生成的纹理缓冲区,例如,你正在做什么是奇怪的,你正在分配一个GLuint *数组?我宁愿向你推荐这个:

void Object::loadTexture()
{
     glGenTextures(1, &this->texture_buffer);
     glBindTexture(GL_TEXTURE_2D, this->texture_buffer);
     glTexImage2D(GL_TEXTURE_2D, 0, 3,
                  texture->sizeX,
                  texture->sizeY,
                  0, GL_RGB, GL_UNSIGNED_BYTE,
                  texture->data);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}

希望这会有所帮助:)

PS:如果你想知道为什么只应用了最后一个纹理,那是因为OpenGL的工作方式是,他绘制了你想要绘制的内容,这要归功于当前激活的纹理,灯光,......以及你最后一次调用object1-&gt; draw(),调用最后一个loadtexture(),它通过调用glBindTexture()来激活你的最后一个纹理。