2013-05-19 1 views
0

텍스처에 약간 문제가 있습니다. ,OpenGL 텍스처로드 중 (적용되지 않음)

그래서 처음에 나는 쉐이더를로드 한 후 메쉬를로드하기 시작 ..

나는 모델이 정상적으로 방법을 보이는 완벽하게 잘 작동 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; 
} 

우리는 질감을 가지고 후, 난 그렇게 재료 로딩으로 건너 뛸 수 있습니다, 메시 (들)하지만, 관련 될 것이다 자외선 부분과 결합 될 수있는 유일한 부분에 대한 이동 -이있는 한 아직 빛이 없다, 나는 그것을 최소한으로 지켰다. 텍스처하지 않는 동안, 정점이 ... 작동

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); 

쉐이더는 텍스쳐링을 테스트하기 위해 기본으로 유지됩니다.

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; 
} 

// 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; 
} 

vertexshader 그래서, 그것에 대해입니다. 내가 말했듯이, 나는 오류가 없으며, 모든 것이 잘로드되고 표시되지만 텍스처 대신에 나는 흑색만을 얻는다.

+1

너무 많은 코드. 그것을 의문의 여지가있는 부분으로 잘라내십시오. 그것이 어떤 부분인지 모른다면 먼저 디버그 할만큼 충분히 시도하지 않은 것입니다. 예를 들어 셰이더가 작동하는지 모를 경우보다 간단한 테스트 케이스를 만들고 테스트하십시오. 작동하는 경우 여기에 게시 할 필요가 없습니다. 여기 3 마일의 코드를 세척, 헹구고 반복하십시오. – derpface

답변

4

레벨 0 만 지정 했으므로 GL_LINEAR_MIPMAP_LINEAR 미니 필터 필터를 지정했지만 질감이 밉맵이 아닙니다 (이미지가 1x1보다 큰 것으로 가정 함). 몇 가지 옵션이 있습니다.

  1. GL_LINEAR과 같은 다른 필터를 사용하십시오. 이것은 작동이 시작되는지를보기위한 빠른 테스트로서 특히 유용합니다.
  2. 전체 밉맵을 제공하십시오. 각 레벨은 1x1에 도달 할 때까지 너비의 1/2과 이전 레벨의 1/2을가집니다.
  3. GL이 glGenerateMipmap()을 통해 밉맵 레벨을 생성하도록하십시오.
+0

감사합니다!그게 문제 였어, 지금은 왜 모델이 노란색 대신에 파란색인지 알 필요가있어. 하지만 문제는 해결되었습니다. 정말 고마워요! – user2399531