我在纹理方面遇到了一点问题。
我通过assimp加载网格,效果很好,模型看起来应该是这样的……但它是黑色的……
所以,首先我加载着色器,然后开始加载网格,包括顶点、面和 uv。为了将其保持在最低限度,我将仅显示与纹理相关的代码。
texID = glGetUniformLocation(shaderProgram, "myTextureSampler");
loadScene(modelPath);
loadScene 现在将导入我们通过模型路径获得的整个模型场景。
所以,我认为这不是问题的第一部分,但它可能是,也许我错过了一些东西,而调试一切似乎都很好。我使用 FreeImage 库寻找纹理。
bool loadGLTextures(const aiScene* scene)
{
/* unload image first */
if (pImage)
{
FreeImage_Unload(pImage);
pImage = 0;
}
if (scene->HasTextures())
{
std::cerr << "Support for meshes with embedded textures is not (yet)
implemented" << std::endl;
return false;
}
/* getTexture Filenames and Numb of Textures */
for (unsigned int m=0; m < scene->mNumMaterials; m++)
{
int texIndex = 0;
aiReturn texFound = AI_SUCCESS;
aiString path; // filename
while (texFound == AI_SUCCESS)
{
texFound = scene->mMaterials[m]->GetTexture(aiTextureType_DIFFUSE,
texIndex, &path);
textureIdMap[path.data] = NULL;
//fill map with textures, pointers still NULL yet
texIndex++;
}
}
int numTextures = textureIdMap.size();
/* create and fill array with GL texture ids */
textureIds = new GLuint[numTextures];
glGenTextures(numTextures, textureIds); /* Texture name generation */
/* get iterator */
std::map<std::string, GLuint*>::iterator itr = textureIdMap.begin();
for (int i=0; i < numTextures; i++)
{
std::string filename = (*itr).first; // get filename
(*itr).second = &textureIds[i]; // save texture id for filename in map
itr++; // next texture
std::string fileloc = basepath + filename; /* Loading of image */
// Check the file signature and deduce its format.
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(fileloc.c_str(), 0);
// If still unknown, try to guess the file format from the file extension.
if(fif == FIF_UNKNOWN)
{
fif = FreeImage_GetFIFFromFilename(fileloc.c_str());
}
// If still unkown, return failure.
if(fif == FIF_UNKNOWN)
{
return false;
}
// Check that the plugin has reading capabilities and load the file.
if(FreeImage_FIFSupportsReading(fif))
{
pImage = FreeImage_Load(fif, fileloc.c_str());
}
if (pImage != 0) /* If no error occured: */
{
glBindTexture(GL_TEXTURE_2D, textureIds[i]);
unsigned int width = pImage ? FreeImage_GetWidth(pImage) : 0;
unsigned int height = pImage ? FreeImage_GetHeight(pImage) : 0;
const BYTE *bits = pImage ? FreeImage_GetBits(pImage) : 0;
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);
// Initialise texture with image data.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, bits);
}
else
{
/* Error occured */
std::cerr << "Couldn't load image: " + fileloc << std::endl;
}
}
//return success;
return true;
}
获得纹理后,我选择网格,但唯一相关的部分是 uv 部分和绑定,所以让我们跳到材质加载 - 因为还没有光,我保留了它最低限度。
void initMaterial(const aiMaterial* mtl)
{
int texIndex = 0;
aiString texPath; //contains filename of texture
if(AI_SUCCESS == mtl->GetTexture(aiTextureType_DIFFUSE, texIndex, &texPath))
{
//bind texture
GLuint texId = *textureIdMap[texPath.data];
glBindTexture(GL_TEXTURE_2D, texId);
glActiveTexture(GL_TEXTURE0);
}
}
因此,以下部分也适用于顶点和法线,但目前它们对我来说并不重要,顶点有效,而纹理则不...
glUseProgram(shaderProgram);
glUniform1i(texID, 0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, tbo);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDisableVertexAttribArray(1);
为了完成,着色器,它们现在保持基本状态,只是为了测试纹理。
顶点着色器
layout(location = 1) in vec2 vertexUV;
// Output data ; will be interpolated for each fragment.
out vec2 UV;
void main(){
....
// UV of the vertex. No special space for this one.
UV = vertexUV;
}
碎片着色器
// Interpolated values from the vertex shaders
in vec2 UV;
// Ouput data
out vec3 color;
// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;
void main(){
// Output color = color of the texture at the specified UV
color = texture2D( myTextureSampler, UV ).rgb;
}
所以,就是这样。正如我所说,我没有收到任何错误,一切都正常加载并显示,但我只得到黑色而不是纹理。