-1
我在C++中用Assimp編寫Modelloader。問題在於模型的紋理沒有顯示在程序中。我認爲該模型具有紋理。 下面是相關代碼:Assimp no Textures
頂點着色器:
#version 330
in vec4 position;
uniform mat4 matrix;
layout(location = 1) in vec2 texCoords;
out vec2 TexCoord;
void main()
{
gl_Position=matrix*position;
TexCoord = vec2(texCoords);
}
Fragmet着色器:
#version 330
out vec2 TexCoord;
out vec3 color;
uniform sampler2D tex;
void main(){
color = texture(tex, TexCoord).rgb;
};
網類:
if(scene->HasMaterials()){
for(unsigned int i = 0; i < scene->mNumMaterials; i++){
const aiMaterial* material = scene->mMaterials[i];
int texIndex = 0;
GLint texture;
aiString path;
if(material->GetTexture(aiTextureType_DIFFUSE, texIndex, &path)==AI_SUCCESS){
std::string textureDir = path.data;
std::string fullPath = directory + '\\' + textureDir;
std::cout << fullPath << std::endl;
LoadTexture(fullPath,&t);
}
}
}
而且LoadTexture方法:
GLint Mesh::MeshEntry::LoadTexture(std::string path, struct TextureHandle *T){
ilInit();
ilEnable(IL_ORIGIN_SET);
ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
ILuint ImageNameID;
ilGenImages(1,&ImageNameID);
ilBindImage(ImageNameID);
if(!ilLoadImage(path.c_str())){
return 0;
}
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);
T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);
GLuint textureid;
glGenTextures(1,&textureid);
glBindTexture(GL_TEXTURE_2D,textureid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, t.DestFormat, t.w, t.h, 0, t.DestFormat, t.DestType, t.p);
glGenerateMipmap(GL_TEXTURE_2D);
GLint texloc = (glGetUniformLocation(ma.getProgrammID(),"tex"));
glUniform1i(texloc,0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,textureid);
return 1;
}
我知道這段代碼不是最好的。所以我需要你的幫助:)。
Habe你檢查t.w,t.h,t.p是否包含加載後的正確數據? – BDL
我懷疑這歸結於片段着色器中的一個錯字:'out vec2 TexCoord;'。這需要在'vec2 TexCoord'中,以便它與頂點着色器連接。 –
首先感謝您的回答:)我已經改變了「進」到「進」,但沒有任何事情發生。我會看看現在的數據是否正確。 – user5239643