1

我很难弄清楚我的 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);
4

1 回答 1

2

无需编写其他人已经完成的代码。检查这个

mCoordsSize、mNormalDataSize、mTextureCoordinateDataSize 的尺寸是多少?

同时移动 GLES20.glEnableVertexAttribArray(mPositionHandle); 在传递指针之前:

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

尝试隔离问题是在您的 obj 解析代码中还是在您的绘图代码中。例如自己生成顶点数据。

于 2012-12-21T07:02:31.033 回答