2014-03-28 5 views
0

그래서 obj 파일을로드하는 코드를 직접 작성하고 있습니다. 13/45/76 7,776분의 445 F (2 예)문자열 나누기

F 1/2/3 4/5/6 7/8/9

: 는하지만이 형식의 문자열을 분할하는 것을 시도하고있다/5566 677/7/45

슬래시를 제외한 모든 위치 다음에 공백이있는 3 개의 숫자 그룹 3 개가 있습니다. 현재로서는이 코드가 있습니다. 프로그램의이 시점에서

는 이미 오프 'F'뽑아했지만 문자열 앞에 공간이 그

"1/2/3 4/5/6 7/8/9"같은

두 번째 그룹 ("g2") 만 작동하지 않습니다. 그것이 돌아오고있다, "1/2/3 7/8"

버퍼는 내가 나누고있는 문자열입니다.

// Divide into groups 
       // Create groups of 1/1/1, 2/2/1, 3/3/1 Ex 
       // At this point the buffer = SPACEHEREx/y/z u/v/w xn/yn/zn 
       string g1 = buffer.substr(1, buffer.find(' ', 1) - 1); // Pos 1 - First space 
       string g2 = buffer.substr(buffer.find(' ', 1) + 1, buffer.find(' ', buffer.find(' ', 1) + 1) - 1); // First space - Second space 
       string g3 = buffer.substr(buffer.find(' ', buffer.find(' ', 1) + 1) + 1, buffer.size()); // Second space - End 
+1

**'.obj' 스펙을 살펴보십시오 **'f' 라인은 정확한 포맷을 보장하지 않습니다. 그래서 공백으로 나누고 '/'로 나눕니다. [여기에서보십시오] (http://www.martinreddy.net/gfx/3d/OBJ.spec). – CodeAngry

답변

0

이 당신에게 도움이 될 수 있습니다

http://www.rastertek.com/dx11tut08.html

그것은로드하고 .OBJ 파일을 렌더링하는 방법을 설명합니다.

// Initialize the indexes. 
    vertexIndex = 0; 
    texcoordIndex = 0; 
    normalIndex = 0; 
    faceIndex = 0; 

    // Open the file. 
    fin.open(filename); 

    // Check if it was successful in opening the file. 
    if(fin.fail() == true) 
    { 
     return false; 
    } 

    // Read in the vertices, texture coordinates, and normals into the data structures. 
    // Important: Also convert to left hand coordinate system since Maya uses right hand coordinate system. 
    fin.get(input); 
    while(!fin.eof()) 
    { 
     if(input == 'v') 
     { 
      fin.get(input); 

      // Read in the vertices. 
      if(input == ' ') 
      { 
       fin >> vertices[vertexIndex].x >> vertices[vertexIndex].y >> vertices[vertexIndex].z; 

       // Invert the Z vertex to change to left hand system. 
       vertices[vertexIndex].z = vertices[vertexIndex].z * -1.0f; 
       vertexIndex++; 
      } 

      // Read in the texture uv coordinates. 
      if(input == 't') 
      { 
       fin >> texcoords[texcoordIndex].x >> texcoords[texcoordIndex].y; 

       // Invert the V texture coordinates to left hand system. 
       texcoords[texcoordIndex].y = 1.0f - texcoords[texcoordIndex].y; 
       texcoordIndex++; 
      } 

      // Read in the normals. 
      if(input == 'n') 
      { 
       fin >> normals[normalIndex].x >> normals[normalIndex].y >> normals[normalIndex].z; 

       // Invert the Z normal to change to left hand system. 
       normals[normalIndex].z = normals[normalIndex].z * -1.0f; 
       normalIndex++; 
      } 
     } 

     // Read in the faces. 
     if(input == 'f') 
     { 
      fin.get(input); 
      if(input == ' ') 
      { 
       // Read the face data in backwards to convert it to a left hand system from right hand system. 
       fin >> faces[faceIndex].vIndex3 >> input2 >> faces[faceIndex].tIndex3 >> input2 >> faces[faceIndex].nIndex3 
        >> faces[faceIndex].vIndex2 >> input2 >> faces[faceIndex].tIndex2 >> input2 >> faces[faceIndex].nIndex2 
        >> faces[faceIndex].vIndex1 >> input2 >> faces[faceIndex].tIndex1 >> input2 >> faces[faceIndex].nIndex1; 
       faceIndex++; 
      } 
     } 

     // Read in the remainder of the line. 
     while(input != '\n') 
     { 
      fin.get(input); 
     } 

     // Start reading the beginning of the next line. 
     fin.get(input); 
    } 

    // Close the file. 
    fin.close(); 

편집 :가에서 문제를 해결하기 위해 여기에

은 (당신이 읽으려고) 얼굴 ("F"포함) OBJ 파일에서 라인을 읽는 방법의 조각입니다

std::string buffer = " 1/2/3 4/5/6 7/8/9"; 
    string g1 = buffer.substr(1, buffer.find(' ', 1) - 1); // Pos 1 - First space 
    string g2 = buffer.substr(buffer.find(' ', 1) + 1, g1.length()); // First space - Second space 
    string g3 = buffer.substr(buffer.find(' ', buffer.find(' ', 1) + 1) + 1, g1.length()); // Second space - End 

동작 예 : http://ideone.com/07aVQO

문제는 0 번째 파라미터이었다 여기 코멘트 가능한 해결책. 입력 문자열의 위치가 아니라 길이 여야합니다.

+0

외부 리소스의 가장 중요한 사항을 포함하십시오. 외부 리소스가 오프라인이되거나 화염에 빠지거나 아이스크림 판매 사이트로 변경되거나 즐겨 찾는 하위 리디렉션으로 리디렉션 될 수 있습니다. – Zeta

+0

은 질문에 대답해야하는 부분을 포함하려고했습니다. – puelo

+0

도움이 되었으니 지금하고 있습니다. 더 쉽고 효율적입니다.그러나 지금은 호기심으로 어떻게 그 끈을 어떻게 나눌까요? –

1

아직 게시물에 대한 평판이 없습니다. 사과드립니다.

분명히 다른 줄로 나누는 것이 좋습니다. 심지어 buffer.find 부분을 가져 와서 위치 이름을 알맞게 설정하십시오. 쉽게 읽을 수있을뿐만 아니라 디버깅도 약간 쉬울 것입니다.

약간의 추가 int이 너무 많은 경우 디버깅이 완료되면 다시 병합하십시오.

시도 할 수있는 또 다른 방법은 "x/y/z"를 유지하고 "split by '/'"함수를 호출 한 다음 적절한 변수에 저장하는 것입니다. 나는 .obj 파일을 "f vertex/texture/normal vertex/texture/normal ...."라고 생각합니다.

+0

+1이 당신의 코멘트 평판 기금에갑니다. – CodeAngry