2016-12-07 5 views
13

私はあなたがここにファイルを見つけることができますOpenGLの2のOpenGL-ES 2

を私のAndroidアプリケーションに.objファイルをロードし、使用してそれを表示しようとしています:編集:私はファイルを削除した、テストのために下記の値を含む任意の.objファイルを使用することができます。

stackoverflowには多くの類似の質問がありますが、大きなライブラリを必要としない単純な解決策は見つかりませんでした。

ファイルのみが次の値の型が含まれています:

F

  • グラム
  • V
  • VT
  • VN
  • 私は、[OK]を働いたlibgdxを、試してみましたそれは私が必要とするもののために少し過剰です。

    LWJGLなしでoObjLoader https://github.com/seanrowens/oObjLoaderを試しました。解析は機能しているようですが、単純なシーンにどのように値を表示できますか?

    次のステップは、画像をテクスチャとしてオブジェクトに添付することです。しかし、今のところ私はそのままファイルを表示することができます。

    私はアプリケーション内でこれがこれだけであるため、ファイルを事前変換するようなさまざまなソリューションを公開しています。

    ありがとうございます!

    ステータス更新 基本的な読み込みと表示は、自分自身の回答として示されています。

+3

あなたは、Androidを使用しているのであなたの質問はOpenGL-ES 2を対象とし、OpenGL 2を対象としていない可能性が高いです。この場合は、質問を更新し、それに応じて調整することをお勧めします。 – BDL

答えて

4

私は新しいパーサを書いてしまった、あなたのレンダラで使用するFloatBuffersを構築するために、このように使用することができます:

ObjLoader objLoader = new ObjLoader(context, "Mug.obj"); 

numFaces = objLoader.numFaces; 

// Initialize the buffers. 
positions = ByteBuffer.allocateDirect(objLoader.positions.length * mBytesPerFloat) 
     .order(ByteOrder.nativeOrder()).asFloatBuffer(); 
positions.put(objLoader.positions).position(0); 

normals = ByteBuffer.allocateDirect(objLoader.normals.length * mBytesPerFloat) 
     .order(ByteOrder.nativeOrder()).asFloatBuffer(); 
normals.put(objLoader.normals).position(0); 

textureCoordinates = ByteBuffer.allocateDirect(objLoader.textureCoordinates.length * mBytesPerFloat) 
     .order(ByteOrder.nativeOrder()).asFloatBuffer(); 
textureCoordinates.put(objLoader.textureCoordinates).position(0); 

、ここでは、パーサーです:

public final class ObjLoader { 

    public final int numFaces; 

    public final float[] normals; 
    public final float[] textureCoordinates; 
    public final float[] positions; 

    public ObjLoader(Context context, String file) { 

     Vector<Float> vertices = new Vector<>(); 
     Vector<Float> normals = new Vector<>(); 
     Vector<Float> textures = new Vector<>(); 
     Vector<String> faces = new Vector<>(); 

     BufferedReader reader = null; 
     try { 
      InputStreamReader in = new InputStreamReader(context.getAssets().open(file)); 
      reader = new BufferedReader(in); 

      // read file until EOF 
      String line; 
      while ((line = reader.readLine()) != null) { 
       String[] parts = line.split(" "); 
       switch (parts[0]) { 
        case "v": 
         // vertices 
         vertices.add(Float.valueOf(parts[1])); 
         vertices.add(Float.valueOf(parts[2])); 
         vertices.add(Float.valueOf(parts[3])); 
         break; 
        case "vt": 
         // textures 
         textures.add(Float.valueOf(parts[1])); 
         textures.add(Float.valueOf(parts[2])); 
         break; 
        case "vn": 
         // normals 
         normals.add(Float.valueOf(parts[1])); 
         normals.add(Float.valueOf(parts[2])); 
         normals.add(Float.valueOf(parts[3])); 
         break; 
        case "f": 
         // faces: vertex/texture/normal 
         faces.add(parts[1]); 
         faces.add(parts[2]); 
         faces.add(parts[3]); 
         break; 
       } 
      } 
     } catch (IOException e) { 
      // cannot load or read file 
     } finally { 
      if (reader != null) { 
       try { 
        reader.close(); 
       } catch (IOException e) { 
        //log the exception 
       } 
      } 
     } 

     numFaces = faces.size(); 
     this.normals = new float[numFaces * 3]; 
     textureCoordinates = new float[numFaces * 2]; 
     positions = new float[numFaces * 3]; 
     int positionIndex = 0; 
     int normalIndex = 0; 
     int textureIndex = 0; 
     for (String face : faces) { 
      String[] parts = face.split("/"); 

      int index = 3 * (Short.valueOf(parts[0]) - 1); 
      positions[positionIndex++] = vertices.get(index++); 
      positions[positionIndex++] = vertices.get(index++); 
      positions[positionIndex++] = vertices.get(index); 

      index = 2 * (Short.valueOf(parts[1]) - 1); 
      textureCoordinates[normalIndex++] = textures.get(index++); 
      // NOTE: Bitmap gets y-inverted 
      textureCoordinates[normalIndex++] = 1 - textures.get(index); 

      index = 3 * (Short.valueOf(parts[2]) - 1); 
      this.normals[textureIndex++] = normals.get(index++); 
      this.normals[textureIndex++] = normals.get(index++); 
      this.normals[textureIndex++] = normals.get(index); 
     } 
    } 
} 
+0

こんにちは私は全体のObjLoaderファイルを得ることができますか?私はそれにいくつかの問題があります。 –

+0

こんにちは、私はopenglesに非常に新しいです、私はバッファをレンダリングするために私を導くことができる、私はバッファのおかげでウルのコードを作成することができたが、それをrederに失敗しました。 –

0

objファイルを解析できるので、postが役に立ちます。

もっと簡単な方法がありますが、three.js objloaderを使用してください。 XWalkViewを使用して、あなたのアンドロイドアプリにウェブページを追加してください。 XWalkViewはWebviewと似ていますが、ここではworkviewは複雑なシーンをレンダリングするためには機能しません。 Webページをリモートにしない場合は、ローカルにすることができます。

+0

あなたの答えをありがとうが、私はOpenGL-ESソリューションをご希望です。 –

関連する問題