2013-03-03 101 views
0

我試圖編寫一個類來加載對象到我的程序/問題是,OpenGL只加載所有對象的最後一個紋理。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; 
    } 
+0

請停止使用AUX加載圖像。如果您使用的是使用AUX的教程,請停止使用該教程並[找到更好的教程。](http://www.opengl.org/wiki/Getting_Started#Tutorials_and_How_To_Guides) – 2013-03-03 11:33:11

回答

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; 

    } 
+0

非常感謝您的回答。但是我仍然需要調用if(!is_loaded){..在draw函數中阻塞一次,沒有這個紋理不顯示。 – DarkSun 2013-03-03 12:58:19

+0

@DarkSun:那麼您可能在創建並激活OpenGL上下文之前創建「對象」實例。在這種情況下,按需加載顯示功能中的資源是一種可能的解決方案。 – datenwolf 2013-03-03 15:21:24

0

現在,您的第一次抽籤()調用加載你的紋理,你只要必須在每次繪製之前調用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.. 
} 

如果這個 - > 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-> draw()時要調用最後一個loadtexture(),它通過調用glBindTexture()來激活最後一個紋理。