[英]My OpenGL program only uses the last texture loaded (C++)
我的裝載機有這個問題:
int loadTexture(char *file)
{
// Load the image
SDL_Surface *tex = IMG_Load(file);
GLuint t;
cout << "Loading image: " << string(file) << "\n";
if (tex) {
glGenTextures(1, &t); // Generating 1 texture
glBindTexture(GL_TEXTURE_2D, t); // Bind the texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, tex->w, tex->h, 0, GL_RGB, GL_UNSIGNED_BYTE, tex->pixels); // Map texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Set minifying parameter to linear
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Set magnifying parameter to linear
SDL_FreeSurface(tex); // Free the surface from memory
cout << " > Image loaded: " << string(file) << "\n\n";
return t; // return the texture index
}
else {
cout << " > Failed to load image: " << IMG_GetError() << "\n\n";
SDL_FreeSurface(tex); // Free the surface from memory
return -1; // return -1 in case the image failed to load
}
}
它可以很好地加載圖像,但是在繪制對象時僅使用最后加載的圖像:
textTest = loadTexture("assets/test_texture_64.png");
textTest2 = loadTexture("assets/test_texture2_64.png");
textTest3 = loadTexture("assets/test_texture3_64.png");
textTest4 = loadTexture("assets/test_texture4_64.png");
紋理文件: http : //i.imgur.com/2K9NsZF.png
正在運行的程序: http : //i.imgur.com/5FMrA1b.png
在繪制對象之前,我使用glBindTexture(GL_TEXTURE_2D,t),其中t是我要使用的紋理的名稱。 我是OpenGL和C ++的新手,所以在這里很難理解這個問題。
加載紋理時,應檢查loadTexture是否返回不同的紋理ID。 然后,您需要確保使用glBindTexture(...)
將正確的紋理綁定到對象上,這表示您已經在做。
您現在如何繪制對象? 是否涉及多重紋理化? 在繪制對象之前和之后,請確保正確調用glPushMatrix
/ glPopMatrix
。
盡管您沒有glEnable
和glDisable
GL_TEXTURE_2D
但從裝載機的角度看對我來說是正確的,但這並不重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.