java - 无法弄清楚这个 Obj 文件解析器有什么问题

标签 java android opengl-es wavefront

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

最佳答案

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

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

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

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

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

关于java - 无法弄清楚这个 Obj 文件解析器有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13977021/

相关文章:

java - android中TextView首字母大写并放大

java - 如何使用堆转储识别 Java 内存分析器中对象的引用持有者

java - android旋转屏幕导致文字颜色更改为默认

android - 无法在 Gradle 中编译新的 Material 库

objective-c - 在 iPhone 上用手指画直线

java - SwingWorker 中的错误处理

java - 如何从 Log4j 中过滤特定的异常?

android - 世博会保存日历事件

objective-c - 检索视频 ram 使用 iphone

android - LibGDX - 颜色选择器