我在C ++中用Assimp编写了一个Modelloader。问题是模型的纹理没有在程序中显示。我很肯定模特有纹理。 以下是相关代码:
顶点着色器:
#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 Shader:
#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;
}
我知道这段代码不是最好的。所以我需要你的帮助:)。
答案 0 :(得分:0)
现在有效。装载机没有问题。路径错误,因此IlLoadImage无法加载文件。我的“fullPath”字符串是这样的:model \ nano \ nanosuit.obj \ glass_dif.png
但它应该是:“model \\ nano \\ arm_dif.png”
在TexCoord之前应该有一个“in”而不是“out”。
顺便说一下。非常感谢你的回答:)