Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9020615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:03:55+00:00 2026-06-16T05:03:55+00:00

I have trouble figuring out what’s wrong with my obj file parser for android,

  • 0

I have trouble figuring out what’s wrong with my obj file parser for android, it just draws some skewed triangles instead of cube shape.
Code for the parser

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){

        }

Code for drawing it using opengl
Initialization

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);

And the 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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:03:56+00:00Added an answer on June 16, 2026 at 5:03 am

    No need to code what others have done already. Check this

    What are the sizes of mCoordsSize, mNormalDataSize, mTextureCoordinateDataSize?

    Also move GLES20.glEnableVertexAttribArray(mPositionHandle); before passsing the pointer:

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

    Try to isolate if the problem is in your obj parsing code or in your drawing code. For example generate the vertex data yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble figuring out to call methods that I have in other
I'm having some trouble figuring this out. I have an unordered list menu that
I'm having trouble figuring out how to solve this issue. I have a file
I'm having trouble figuring out how to use soundfonts on android (that have a
I'm having some trouble figuring out how to do this. I want to have
i'm having some trouble figuring out how to save unicode into a file in
I have been having some trouble figuring out how exactly I get a process's
I have some trouble of figuring out how to use group_concat to produce certain
I have some trouble figuring out why the following crashes (MSVC9): //// the following
I have created my own blocking queue and I'm having some trouble figuring out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.