纹理有点问题。
我通过assimp加载网格,效果非常好,模型看起来应该是这样......但它是黑色的..
所以,首先我加载着色器然后开始加载网格,包括顶点,面和紫外线。 为了保持最低限度,我只会显示与纹理相关的代码。
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);
glUseProgram(shaderProgram);
glUniform1i(texID, 0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, tbo);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDisableVertexAttribArray(1);
为了完成,着色器,它们现在保持基本,只是为了测试纹理。
vertexshader
fragmentshader
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;
}
所以,就是这样。正如我所说,我没有得到任何错误,一切都很好地显示,但显示但不是纹理我只会变黑。
答案 0 :(得分:4)
您指定GL_LINEAR_MIPMAP_LINEAR
微型过滤器,但您的纹理不是mipmap完成,因为您只指定级别0(我假设您的图像大于1x1)。有几种选择:
GL_LINEAR
。这对于快速测试来说特别有用,以确定它是否开始工作。glGenerateMipmap()
生成mipmap级别。