2014-11-21 82 views
0

我使用OpenGl ES 2開始一個新的Android項目,基於官方頁面中提供的示例,並且我想在此應用中使用來自Blender的一些項目。我應該在Blender中導出這個項目的格式以便於它在android項目中的處理?我應該用什麼庫來處理這個文件?從Blender導出項目在Android項目中使用

我想要的東西,這讓我用這樣的一類:

public class Triangle { 

    private final String vertexShaderCode = 
      "uniform mat4 uMVPMatrix;" + 
      "attribute vec4 vPosition;" + 
      "void main() {" + 
      " gl_Position = uMVPMatrix * vPosition;" + 
      "}"; 

    private final String fragmentShaderCode = 
      "precision mediump float;" + 
      "uniform vec4 vColor;" + 
      "void main() {" + 
      " gl_FragColor = vColor;" + 
      "}"; 

    private final FloatBuffer vertexBuffer; 
    private final int mProgram; 
    private int mPositionHandle; 
    private int mColorHandle; 
    private int mMVPMatrixHandle; 

    // number of coordinates per vertex in this array 
    static final int COORDS_PER_VERTEX = 3; 
    static float triangleCoords[] = {0.0f, 0.622008459f, 0.0f, -0.5f, -0.311004243f, 0.0f, 0.5f, -0.311004243f, 0.0f}; 
    private final int vertexCount = triangleCoords.length/COORDS_PER_VERTEX; 
    private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex 

    float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 0.0f }; 

    public Triangle() { 
     // initialize vertex byte buffer for shape coordinates 
     ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4); 
     // use the device hardware's native byte order 
     bb.order(ByteOrder.nativeOrder()); 

     // create a floating point buffer from the ByteBuffer 
     vertexBuffer = bb.asFloatBuffer(); 
     // add the coordinates to the FloatBuffer 
     vertexBuffer.put(triangleCoords); 
     // set the buffer to read the first coordinate 
     vertexBuffer.position(0); 

     // prepare shaders and OpenGL program 
     int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode); 
     int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode); 

     mProgram = GLES20.glCreateProgram();    // create empty OpenGL Program 
     GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program 
     GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program 
     GLES20.glLinkProgram(mProgram);     // create OpenGL program executables 

    } 

    public void draw(float[] mvpMatrix) { 
     // Add program to OpenGL 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 triangle coordinate data 
     GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); 

     // 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"); 
     MyGLRenderer.checkGlError("glGetUniformLocation"); 

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

     // Draw the triangle 
     GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); 

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

而是硬編碼爲頂點的數組的值,我從文件中讀取此值(相同的顏色和其他組件)。

Blender的每個3D項目都應該有它的類,除了draw()方法之外,我還想要其他方法,如walk(),jump()等,以便與觸摸事件相關聯。

任何人都可以提供一個提示呢?我甚至接受教程的指示,因爲我無法在Google上找到這樣的東西(可能是因爲我不知道除android opengl tutorial之外的其他搜索)。

+0

繼承人如何加載.obj文件http://stackoverflow.com/questions/22316440/how-to-handle-index-buffer-in-opengl-es-1-1/22316797#22316797 – JRowan 2014-11-21 23:13:36

回答

0

你需要的是一個Android遊戲引擎,它將從Blender導入對象。最近在這個領域有很多的發展,所以我寫了這個article,總結了他們。有些是針對Java的,有些是C++,既有開源的,也有開源的。有很多很好的選擇,它取決於對你來說重要的事情。例如,JMonkeEngine SDK與Blender緊密結合。我最喜歡的是PowerVR SDK,因爲它具有自己的輕量級和可定製的目標文件格式(POD)。