[英]Problems with multiple textures in 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;
}
為什么要動態分配紋理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;
}
現在,您的第一個draw()調用已加載紋理,您只需在每次繪制之前調用glBindTexture(GL_TEXTURE_2D,your_genic_buffer)。
void Object::draw()
{
if(!is_loaded)
{
loadTexture();
is_loaded = true;
}
glBindTexture(GL_TEXTURE_2D, this->texture_buffer);
// Your vertex things..
}
例如,如果this-> 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()激活您的最后一個紋理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.