2012-07-31 2 views
0

여기 내 문제입니다. 임 자산에서 웨이브 프론트 obj 파일을 읽으려고하고 분명히 제대로 읽습니다. 그러나 내가 8 개의 삼각형 중 glsurfaceview 2에 객체를 그릴 때 끌기를하지 않고 왜 그 이유를 모르겠습니다.android opengl gles20에 웨이브 프론트 파일 그리기

# Blender v2.63 (sub 0) OBJ File: '' 
# www.blender.org 
mtllib octahedron.mtl 
o Mesh_Object001.001 
v 0.000000 -1.000000 -0.000000 
v 1.000000 0.000000 0.000000 
v 0.000000 -0.000000 1.000000 
v -1.000000 0.000000 0.000000 
v 0.000000 1.000000 0.000000 
v 0.000000 0.000000 -1.000000 
usemtl 
s off 
f 1 2 3 
f 4 1 3 
f 5 4 3 
f 2 5 3 
f 2 1 6 
f 1 4 6 
f 4 5 6 
f 5 2 6 

이이 파일을 읽을 수있는 코드입니다 :

public WaveFrontObj(InputStream file) throws IOException, WaveFrontObjExeption { 
    color = new float[] { 1f, 0f, 0f, 1.0f }; 

    Log.i(MyApp_Tag, "WaveFrontObj: Starting to read file"); 

    BufferedReader br = new BufferedReader(new InputStreamReader(file)); 

    ArrayList<Float> vertexinfoarraylist = new ArrayList<Float>(); 
    ArrayList<Short> indexinfoarraylist = new ArrayList<Short>(); 

    while (br.ready()) { 
     // Some string fixes 
     String line = br.readLine().trim(); 

     while (line.contains(" ")) { 
      line = line.replace(" ", " "); 
     } 
     // End string fixes 

     // Comments lines should be avoided. 
     if (line.startsWith("#")) 
      continue; 

     // Wavefront file body 
     if (line.startsWith("v")) { 
      String[] point_value = line.split(" "); 

      Log.i(MyApp_Tag, "Vertex array lentth: " + point_value.length 
        + " at line " + line); 

      for (int i = 1; i < point_value.length; i++) { 
       try { 
        vertexinfoarraylist.add(Float 
          .parseFloat(point_value[i])); 
       } catch (Exception ex) { 
        throw new WaveFrontObjExeption("Unrecognized string \"" 
          + point_value[i] + "\" ant line \"" + line 
          + "\""); 
       } 
      } 
     } else if (line.startsWith("f")) { 
      String[] face_value = line.split(" "); 

      Log.i(MyApp_Tag, "Index array length: " + face_value.length 
        + " at line " + line); 

      for (int i = 1; i < face_value.length; i++) { 
       try { 
        indexinfoarraylist.add(Short.parseShort(face_value[i])); 
       } catch (Exception ex) { 
        throw new WaveFrontObjExeption("Unrecognized string \"" 
          + face_value[i] + "\" ant line \"" + line 
          + "\""); 
       } 
      } 
     } else { 
      // avoid this lines for now 
      // the end result should be a exception for 
      // Unrecognized command. 
      continue; 
     } 
    } 

    if (!(vertexinfoarraylist.size() > 0 && indexinfoarraylist.size() > 0)) { 
     throw new WaveFrontObjExeption("Object has no vertex or index inf."); 
    } else { 

     float[] vertexarrary = new float[vertexinfoarraylist.size()]; 
     short[] indexarrary = new short[indexinfoarraylist.size()]; 

     for (int i = 0; i < vertexinfoarraylist.size(); i++) { 
      vertexarrary[i] = ((Float) vertexinfoarraylist.get(i)); 
      Log.i(MyApp_Tag, "Vertex Info #" + (i + 1) + ": " 
        + vertexinfoarraylist.get(i)); 
     } 

     for (int i = 0; i < indexinfoarraylist.size(); i++) { 
      indexarrary[i] = ((Short) indexinfoarraylist.get(i)); 
      Log.i(MyApp_Tag, "Index Info #" + (i + 1) + ": " 
        + indexinfoarraylist.get(i)); 
     } 

     vertexCount = vertexinfoarraylist.size(); 
     vertexBuffer = ByteBuffer 
       .allocateDirect(
         BYTES_PER_FLOAT * COORDS_PER_VERTEX * vertexCount) 
       .order(ByteOrder.nativeOrder()).asFloatBuffer(); 
     vertexBuffer.put(vertexarrary).position(0); 

     indexCount = vertexinfoarraylist.size(); 
     indexBuffer = ByteBuffer 
       .allocateDirect(
         BYTES_PER_SHORT * COORDS_PER_VERTEX * indexCount) 
       .order(ByteOrder.nativeOrder()).asShortBuffer(); 
     indexBuffer.put(indexarrary).position(0); 

     int vertexShader = GameRenderer.loadShader(GLES20.GL_VERTEX_SHADER, 
       vertexShaderCode); 
     int fragmentShader = GameRenderer.loadShader(
       GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode); 

     // create empty OpenGL ES Program 
     mProgram = GLES20.glCreateProgram(); 

     // add the vertex shader to the opengl program 
     GLES20.glAttachShader(mProgram, vertexShader); 

     // add the fragment shader to the opengl program 
     GLES20.glAttachShader(mProgram, fragmentShader); 

     // creates an executable OpenGL ES program 
     GLES20.glLinkProgram(mProgram); 
    } 
} 

가 그리고 이것은 OpenGL을 GLES20에 객체를 그리는 코드입니다

은 OBJ 파일 (면체)입니다 :

public void draw(float[] mvpMatrix) { 
    // Add program to OpenGL ES environment 
    GLES20.glUseProgram(mProgram); 

    // get handle to vertex shader's vPosition member 
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); 

    // Enable a handle to the triangle vertices 
    GLES20.glEnableVertexAttribArray(mPositionHandle); 

    // Prepare the vertex coordinate data 
    GLES20.glVertexAttribPointer(mPositionHandle, 
      COORDS_PER_VERTEX, 
      GLES20.GL_FLOAT, 
      false, 
      0, // No space between vertex 
      vertexBuffer); 


    GLES20.glEnableVertexAttribArray(mPositionHandle); 

    // get handle to fragment shader's vColor member 
    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); 

    // Set color for drawing the triangle 
    GLES20.glUniform4fv(mColorHandle, 1, color, 0); 

    // get handle to shape's transformation matrix 
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); 

    // Apply the projection and view transformation 
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0); 

    // Draw the object 
    GLES20.glDrawElements(GLES20.GL_TRIANGLE_FAN, indexBuffer.capacity(), GLES20.GL_UNSIGNED_SHORT, indexBuffer); 
    //GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP, indexBuffer.capacity(), GLES20.GL_UNSIGNED_SHORT, indexBuffer); 
    //GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); 

    // Disable vertex array 
    GLES20.glDisableVertexAttribArray(mPositionHandle); 
} 
+0

GL_TRIANGLES 대신 입력을 기반으로 GL_TRIANGLE_FAN을 사용해야한다고 생각합니다. 문제는 확실하지 않습니다. – vmpstr

+0

세 가지 옵션을 모두 사용했지만 nether가 사용되었습니다. – Enriquillo

+0

저는 Java에 익숙하지 않지만, 과거에 그 문제가 있었을 때 사용할 수있는 것보다 적은 수의 vertex/indecies를 전달하고있었습니다. 내 제안은 그 값 (glDrawElements뿐만 아니라 꼭지점을 삽입 할 때 COORDS_PER_VERTEX)을 출력하고 숫자가 올바른지 수동으로 확인하는 것입니다. 또한 어딘가에있는 경우 컬링을 해제하십시오 ... – vmpstr

답변

0

이 콘텐츠는 의견에 위층 포함되어 있습니다 :

다른 하나는 형식이 indecies를 1 기반으로 지정하지만 코드에서 0 기반이므로 indices를 뺄 때 얼굴을 파싱해야한다는 것입니다.