2012-12-20 42 views
1

我無法弄清楚我的obj文件解析器出現了什麼問題,它只是畫出一些傾斜的三角形而不是立方體形狀。 代碼分析器無法弄清楚這個Obj文件解析器中有什麼問題

public ObjLoader(Context context){ 
     loaderContext = context; 
     am = context.getAssets(); 
     InputStream file = getFile("3dsmax.obj"); 
     BufferedReader br = new BufferedReader(new InputStreamReader(file)); 

     String str; 

     ArrayList<Float> tempModelVertices = new ArrayList<Float>(); 
     ArrayList<Float> tempTextureVertices = new ArrayList<Float>(); 
     ArrayList<Float> tempNormalVertices = new ArrayList<Float>(); 
     ArrayList<Integer> facesM = new ArrayList<Integer>(); 
     ArrayList<Integer> facesT = new ArrayList<Integer>(); 
     ArrayList<Integer> facesN = new ArrayList<Integer>(); 

     try { 
      while((str = br.readLine())!=null){ 
       if(str.startsWith("f")){ 
        String[] strAr = str.replaceAll("f", "").trim().split(" "); 
        for(String s : strAr){ 
         String[] cornerAr = s.split("/"); 
         facesM.add(Integer.parseInt(cornerAr[0].trim())-1); 
         facesT.add(Integer.parseInt(cornerAr[1].trim())-1); 
         facesN.add(Integer.parseInt(cornerAr[2].trim())-1); 
        } 
       } 
       else if(str.startsWith("vt")){ 
        String[] strAr = str.replaceAll("vt", "").trim().split(" "); 
        tempTextureVertices.add(Float.valueOf(strAr[0].trim())); 
        tempTextureVertices.add(-1*Float.valueOf(strAr[1].trim())); 
       } 
       else if(str.startsWith("vn")){ 
        String[] strAr = str.replaceAll("vn", "").trim().split(" "); 
        tempNormalVertices.add(Float.valueOf(strAr[0].trim())); 
        tempNormalVertices.add(Float.valueOf(strAr[1].trim())); 
        tempNormalVertices.add(Float.valueOf(strAr[2].trim())); 
       } 
       else if(str.startsWith("v")){    
        String[] strAr = str.replaceAll("v", "").trim().split(" "); 
        tempModelVertices.add(Float.valueOf(strAr[0].trim())); 
        tempModelVertices.add(Float.valueOf(strAr[1].trim())); 
        tempModelVertices.add(Float.valueOf(strAr[2].trim()));  
       } 
      } 
      //Log.v(LOG_TAG, "v :"+ String.valueOf(v) + "vt :"+ String.valueOf(vt) + "vn :"+ String.valueOf(vn) + "f :"+ String.valueOf(f)); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      Log.v(LOG_TAG, "error"); 
     } 
     Log.v(LOG_TAG, "vt " + String.valueOf(tempTextureVertices.size()) + " vn " + String.valueOf(tempNormalVertices.size()) + " v " + String.valueOf(tempModelVertices.size())); 

     modelVertices = new float[facesM.size()]; 
     textureVertices = new float[facesT.size()]; 
     normalVertices = new float[facesN.size()]; 

     for(int i=0; i<facesM.size(); i++){ 
      modelVertices[i] = tempModelVertices.get(facesM.get(i)); 
     } 
     for(int i=0; i<facesT.size(); i++){ 
      textureVertices[i] = tempTextureVertices.get(facesT.get(i)); 
     } 
     for(int i=0; i<facesN.size(); i++){ 
      normalVertices[i] = tempNormalVertices.get(facesN.get(i)); 
     } 
     for(float f: modelVertices){ 

     } 

代碼使用OpenGL的 初始化

ObjLoader obj = new ObjLoader(mActivityContext); 
     totalEle = obj.modelVertices.length; 
     // Initialize the buffers. 
     mSquareCoords = ByteBuffer.allocateDirect(obj.modelVertices.length * mBytesPerFloat) 
       .order(ByteOrder.nativeOrder()).asFloatBuffer();  
     mSquareCoords.put(obj.modelVertices).position(0); 

     mSqaureTextureCoords = ByteBuffer.allocateDirect(obj.textureVertices.length * mBytesPerFloat) 
       .order(ByteOrder.nativeOrder()).asFloatBuffer();  
     mSqaureTextureCoords.put(obj.textureVertices).position(0); 

     mSquareNormalCoords = ByteBuffer.allocateDirect(obj.normalVertices.length * mBytesPerFloat) 
       .order(ByteOrder.nativeOrder()).asFloatBuffer();  
     mSquareNormalCoords.put(obj.normalVertices).position(0); 

而且drawOnFrame

mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix"); 
     mMVMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVMatrix"); 
     mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_Texture"); 
     mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position"); 
     mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_TexCoordinate"); 
     mNormalHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Normal"); 

     if(updateTexture) 
     { 
      if(mTextureDataHandle != null){ 
       GLES20.glDeleteTextures(1, mTextureDataHandle, 0); 
      } 
      mTextureDataHandle = TextureHelper.loadTexture(texture); 
      //mTextureDataHandle = TextureHelper.loadTexture(getFile("coinAnimation/1230025.png")); 
      //mTextureDataHandle = TextureHelper.loadTexture(mActivityContext, textureId); 
      textureWidth = mTextureDataHandle[1]; 
      textureHeight = mTextureDataHandle[2]; 

      updateTexture = false; 
     } 
     //GLES20.glBlendFunc(GLES20.GL_SRC_COLOR, GLES20.GL_DST_ALPHA); 

     // Set the active texture unit to texture unit 0. 
     GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 

     // Bind the texture to this unit. 
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle[0]); 

     // Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0. 
     GLES20.glUniform1i(mTextureUniformHandle, 0); 

     long time = SystemClock.uptimeMillis() % 10000L; 
     float angleInDegrees = (360.0f/10000.0f) * ((int) time); 

     //Identity matrix of the object 
     Matrix.setIdentityM(mModelMatrix, 0); 
     //Matrix.scaleM(mModelMatrix, 0, 0.2f, 0.2f, 1.0f); 
     Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f); 
     //Scaling 
     //Matrix.scaleM(mModelMatrix, 0, 1.0f, 1.0f, 0.0f); 
     //Moving 
     Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, -7.0f); 
     //Rotating 


     // Pass in the position information 
     mSquareCoords.position(0); 
     GLES20.glVertexAttribPointer(mPositionHandle, mCoordsSize, GLES20.GL_FLOAT, false, 
       0, mSquareCoords); 

     GLES20.glEnableVertexAttribArray(mPositionHandle); 

     // Pass in the normal information 
     mSquareNormalCoords.position(0); 
     GLES20.glVertexAttribPointer(mNormalHandle, mNormalDataSize, GLES20.GL_FLOAT, false, 
         0, mSquareNormalCoords); 

     GLES20.glEnableVertexAttribArray(mNormalHandle); 

     mSqaureTextureCoords.position(0); 
     GLES20.glVertexAttribPointer(mTextureCoordinateHandle, mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 
       0, mSqaureTextureCoords); 

     GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle); 

     // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix 
     // (which currently contains model * view). 
     Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); 

     // Pass in the modelview matrix. 
     GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0); 

     // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix 
     // (which now contains model * view * projection). 
     Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); 

     // Pass in the combined matrix. 
     GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); 

     // Draw the cube. 
     GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, totalEle); 

回答

2

無需對碼別人已經做畫它。檢查this

mCoordsSize,mNormalDataSize,mTextureCoordinateDataSize的大小是多少?

也移動GLES20.glEnableVertexAttribArray(mPositionHandle);在傳遞指針之前:

GLES20.glEnableVertexAttribArray(mPositionHandle); 
GLES20.glVertexAttribPointer(mPositionHandle, mCoordsSize, GLES20.GL_FLOAT, false, 
      0, mSquareCoords); 

嘗試隔離問題是否在您的obj解析代碼或您的繪圖代碼中。例如你自己生成頂點數據。

+0

嗨,謝謝你的幫助。我發現了這個問題,它有索引和麪孔。我修好後,它似乎工作到目前爲止:D –

+0

不客氣;) – Trax

相關問題