[英]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.