Java/OpenGL 从 obj 模型加载和渲染顶点时缺少三角形

Java/OpenGL missing triangles when loading and rendering vertexes from obj model

我正在尝试从 OBJ 文件加载顶点并渲染它们。但是只有一些三角形在渲染。

模型的外观: Blender model

它是如何呈现的: Rendered model

我的代码:

  GL2 gl = glad.getGL().getGL2();
  gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );
   
  gl.glRotatef(0.5f, 1f, 1f, 1f);
   
  gl.glColor3f(1, 0, 0);
  gl.glBegin(GL2.GL_TRIANGLE_STRIP); // Start Drawing The Cube
  try (InputStream in = new FileInputStream("D:\untitled.obj")) {
    
    final IOBJParser parser = new OBJParser();
    final OBJModel model = parser.parse(in);

    
    for(int i = 0; i < model.getVertices().size(); i++) {
        
        
        System.out.println(model.getVertices().get(i).x + " " + model.getVertices().get(i).y + " " + model.getVertices().get(i).z);
        
        gl.glNormal3f(model.getNormals().get(i).x,model.getNormals().get(i).y, model.getNormals().get(i).z);
        gl.glVertex3f(model.getVertices().get(i).x,model.getVertices().get(i).y, model.getVertices().get(i).z);
        
        
        
        
    }
    
    } catch (FileNotFoundException ex) {
        Logger.getLogger(EventListener.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(EventListener.class.getName()).log(Level.SEVERE, null, ex);
    
}
  
  gl.glEnd();
  gl.glFlush();

(我知道这远不是最有效的方法)

我正在使用 JOGL 库进行渲染,java-data-front 用于加载 obj 文件和解析它。模型已正确加载,但未渲染。

(自答)

我对每个对象、网格和面都有太多循环。然后像@Rabbid76 所说的那样获取引用和索引。

代码:

        GL2 gl = glad.getGL().getGL2();
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );
   
  gl.glRotatef(0.5f, 1f, 1f, 1f);
   
  gl.glColor3f(1, 0, 0);
  gl.glBegin(GL2.GL_TRIANGLE_STRIP); // Start Drawing The Cube
  try (InputStream in = new FileInputStream("D:\untitled.obj")) {
    
    final IOBJParser parser = new OBJParser();
    final OBJModel model = parser.parse(in);

     for (OBJObject object : model.getObjects()) {
    for (OBJMesh mesh : object.getMeshes()) {
    for (OBJFace face : mesh.getFaces()) {
        for (OBJDataReference reference : face.getReferences()) {
    final OBJVertex vertex = model.getVertex(reference);
    gl.glVertex3f(vertex.x, vertex.y, vertex.z);
     if (reference.hasNormalIndex()) {
    final OBJNormal normal = model.getNormal(reference);
    gl.glNormal3f(normal.x, normal.y, normal.z);
}
if (reference.hasTexCoordIndex()) {
    final OBJTexCoord texCoord = model.getTexCoord(reference);
    gl.glTexCoord3f(texCoord.u, texCoord.v, texCoord.w);
}
}
    }
    }
    }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(EventListener.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(EventListener.class.getName()).log(Level.SEVERE, null, ex);
    
}
  
  gl.glEnd();
  gl.glFlush();