opengl 只使用最后加载的纹理,我不知道为什么。这是我的代码:
GLuint loadTex(const char* c) {
GLuint temp = SOIL_load_OGL_texture(
c,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
if (0 == temp)
{
printf("SOIL loading error: '%s'\n", SOIL_last_result());
//return 0;
}
//glGenTextures(1, &temp);
glBindTexture(GL_TEXTURE_2D, temp);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return temp;
}
void drawObject(std::vector<Level::Triangle> faces, GLuint &resID, double x, double y, double z ){
glColor3f(1, 1, 1);
//glColor3f(0, 1, 0);
glTranslatef(x, y, z);
glBegin(GL_TRIANGLES);
//glBindTexture(GL_TEXTURE_2D, resID);
//glActiveTexture(GL_TEXTURE1);
for (int i = 0; i < faces.size(); i++){
Level::Triangle t = faces[i];
glTexCoord2f(t.p1.tx, t.p1.ty);
//glTexCoord2f(0.0, 0.0);
glVertex3f(t.p1.x, t.p1.y, t.p1.z);
glTexCoord2f(t.p2.tx, t.p2.ty);
//glTexCoord2f(0.0, 1.0);
glVertex3f(t.p2.x, t.p2.y, t.p2.z);
glTexCoord2f(t.p3.tx, t.p3.ty);
//glTexCoord2f(1.0, 1.0);
glVertex3f(t.p3.x, t.p3.y, t.p3.z);
}
glEnd();
glTranslatef(-x, -y, -z);
}
纹理被加载到一个 c++ 向量类数组中,然后传递给 drawObject 函数。绘制对象时只使用最后加载的纹理,我不知道为什么。是的,每次 SOIL 加载纹理时,它都会分配一个新的 GLuint,是的,纹理在使用之前就已加载。