2014-07-04 1 views
-1

내가하려는 것의 Wavefront OBJ 파일을로드하려고합니다. 나는 그것을 가지고있다. 렌더링 될 때 얼굴 중 일부가 누락되거나 잘못된 꼭지점 위치가 있습니다. 내가 그들을 로딩하는 방식은 아마 너무 최적화되어 있지 않다. 나는 LWJGL을 사용하고 있으며 OpenGL 바인딩 (제 바인딩이라고 생각합니다)을 사용하여 제 모델을 렌더링합니다. 여기 http://i1291.photobucket.com/albums/b560/DandDMC/javaw2014-07-0415-52-54-99_zpsdf14fb6c.pngWavefront OBJ로드 오류 : 누락 및 부정확 한 얼굴

가 로딩 방식의 : 여기 http://i1291.photobucket.com/albums/b560/DandDMC/blender2014-07-0415-28-40-23_zps0fbfcebb.png

그것이 게임 내 모습이다 : 여기

public static Model loadModel(String fileLocation) 
{ 
    File file = new File(fileLocation); 

    if(!file.exists()) 
    { 
     try 
     { 
      throw new FileNotFoundException("The file named: " + fileLocation + " doesn't exist!"); 
     } 
     catch (FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    ArrayList<Vertex> vertices = new ArrayList<Vertex>(); 
    ArrayList<Vertex> texVertices = new ArrayList<Vertex>(); 
    ArrayList<Face> faces = new ArrayList<Face>(); 

    try 
    { 
     BufferedReader r = new BufferedReader(new FileReader(file)); 
     String s = ""; 

     int refIndex = 1; 

     int vertIndex = 1; 
     int texVertIndex = 1; 

     while((s = r.readLine()) != null) 
     {    
      String[] split = s.split(" "); 

      if(split[0].equals("v")) 
      { 
       vertices.add(new Vertex(Double.parseDouble(split[1]), Double.parseDouble(split[2]), Double.parseDouble(split[3]), vertIndex)); 
       vertIndex ++; 
      } 
      else if(split[0].equals("vt")) 
      { 
       texVertices.add(new Vertex(Double.parseDouble(split[1]), Double.parseDouble(split[2]), 0.0, texVertIndex)); 
       texVertIndex ++; 
      } 
      else if(split[0].equals("f")) 
      { 
       ArrayList<Integer> vert = new ArrayList<Integer>(); 
       ArrayList<Integer> texVert = new ArrayList<Integer>(); 

       for(int i = 1; i < split.length; i ++) 
       { 
        String[] fSplit = split[i].split("/"); 

        vert.add(Integer.parseInt(fSplit[0])); 
        texVert.add(Integer.parseInt(fSplit[1])); 
       } 

       faces.add(new Face(vert, texVert)); 
      } 
      else if(split[0].equals("#") || split[0].equals("o") || split[0].equals("mtllib") || split[0].equals("usemtl") || split[0].equals("s")) 
      { 
       // Don't have a use for as of now 
      } 
      else 
      { 
       throw new Exception("The syntax at line: " + refIndex + " is incorrect."); 
      } 

      refIndex ++; 
     } 

     r.close(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

    return new Model(vertices, texVertices, faces); 
} 

얼굴 클래스의 다음

는 모델과 같이하도록되어 무엇 (매우 간단합니다) :

}

정점 클래스도 간단 :

package com.glh.model; 

public class Vertex 
{ 
public double x; 
public double y; 
public double z; 
public int index; 

public Vertex(double x, double y, double z, int index) 
{ 
    this.x = x; 
    this.y = y; 
    this.z = z; 
    this.index = index; 
} 

public Vertex() 
{ 
    this(0.0, 0.0, 0.0, 0); 
} 
} 

그리고 렌더링하는 방법은 모델 클래스는 단지 모든 정점과 얼굴의 목록을 보유하고

public void renderModel_tri(Model m) 
{ 
    // The model is much smaller in Blender 
    glScalef(0.3F, 0.3F, 0.3F); 

    glBegin(GL_QUADS); 

    for(Face f : m.faces) 
    { 
     ArrayList<Vertex> vertices = f.getVertexPositions(m); 
     ArrayList<Vertex> texVertices = f.getTextureVertexPositions(m); 

     for(int i = 0; i < vertices.size(); i ++) 
     { 
      Vertex vert = vertices.get(i); 
      Vertex texVert = texVertices.get(i); 

      glTexCoord2d(texVert.x, texVert.y); 
      glVertex3d(vert.x, vert.y, vert.z); 
     } 
    } 

    glEnd(); 
} 

.

package com.glh.model; 

import java.util.*; 

public class Model 
{ 
public ArrayList<Vertex> vertices; 
public ArrayList<Vertex> texVertices; 
public ArrayList<Face> faces; 

public Model(ArrayList<Vertex> vertices, ArrayList<Vertex> texVertices, ArrayList<Face> faces) 
{ 
    this.vertices = vertices; 
    this.texVertices = texVertices; 
    this.faces = faces; 
} 
} 
+0

가장 최근의 질문을 삭제 한 이유는 무엇입니까? 당신이 관심이 있다면 나는 적절한 대답을 가지고있다. – chqrlie

+0

Woops, 가장 최근의 논평은 나의 의심을 확인 했으므로 삭제하지 않을 것이라고 생각 했으므로 어디에서든 삭제 취소 버튼을 볼 수는 없으므로 아마 내가 오후 PM 일 수있다. xH – David

+0

undelete로 투표를했지만, 나는 그것을 지원할 14 명의 다른 회원이 있을지 의심 스럽다. 나는 너에게 PM도하는 법을 모른다. – chqrlie

답변

0

찾았습니다 ... 내 모델은 삼각형으로 구성되어 있으며 게임에서 QUADS를 그리는 중입니다. 내가 변경 : glBegin (GL_QUADS); ~에 glBegin (GL_TRIANGLES);

+0

'GL_TRIANGLE_FAN'을 사용할 수 있습니다.그렇다면 얼굴의 정점 수에 상관없이 작동합니다. 이게 유일한 문제였습니까? 나는 당신의 질문을 어제 보았을 때 이것을 보았습니다. 그러나 그 그림을 근거로, 나는 이것이 당신 혼자서 얻은 결과를 설명했다고 생각하지 않았습니다. –

+0

QUADS를 TRIANGLES와 바꾸기 전에 모델이 나에게 더 많은 문제가 있다고 생각하게했습니다. 그러나 그것은 내가 발견 한 유일한 문제이며, 모델은 완벽하게 렌더링됩니다. 그리고 'GL_TRIANGLES_FAN'을 시도했는데 작동하지만 삼각형으로 만든 모델로만 시도했습니다. 블렌더에서 Ctrl + T를 눌러서 GL_TRIANGLES로 렌더링하는 것만으로도 많은 차이가 있습니다. – David