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 3609300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:36:36+00:00 2026-05-18T21:36:36+00:00

I’m trying to make my first android game and to do this I made

  • 0

I’m trying to make my first android game and to do this I made an OBJ loader which takes a resource id and uses the InputStream returned by Context.getResources().openRawResource().

I managed to read vertex information and face info and I can successfully load a mesh without any textures.

See the problem is that the textures are not mapped correctly. I managed to read the information from the file by putting all the numbers after ‘vt’ into an array as well as the indexes in f after the first ‘/’ (ie ‘f 1/1 …’)

I’m not exactly sure what I’m supposed to do with the numbers though…if I just put everything into a bytebuffer and continue as I normally would the way I learned from these two tutorials(jayway, and the nehe ports) it will still come out wrong.

My thoughts are that the ‘vt’ info in the file is out of order(hence the need for the ‘/x’ in the ‘f’ data) and I’m supposed to put it in order somehow before creating the bytebuffer from the array….I don’t know how to do that 🙁

So I ask, can anyone offer me some help please? I know there are other obj loaders out there on the internet but I already made this one and I know how it works, not only that but its a better learning experience for me.

Heres the code I’m using:

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import android.content.Context;
import android.util.Log;
import com.snakeinalake.catchthemoney.Mesh.CMeshInfo;

public class CMeshParserOBJ extends CMeshParser{

 public CMeshParserOBJ(){
  type = "OBJ";  
 }

 @Override
 public CMeshInfo loadMesh(Context context, int FileID){
  CMeshInfo ret = new CMeshInfo(type);

  String buildWord = "";
  int data;
  int found = -1;

  Vector<Float> vert = new Vector<Float>();
  Vector<Float> uv = new Vector<Float>();
  Vector<Short> ind = new Vector<Short>();
  Vector<Short> map = new Vector<Short>();

  InputStream is = context.getResources().openRawResource(FileID);
  InputStreamReader rd = new InputStreamReader(is);

  try {
   data = rd.read();
   while(data!=-1){ 
    char c = (char)data;

     if(found == -1){
      if(c == 'v'){    //found vertex data
       data = rd.read();
        if((char)data == ' '){
         found = 1;
        }else if((char)data == 't'){ //found uv map data
         data = rd.read();
         if((char)data == ' '){
          found = 3;
         }
        }else{
         found = -1;        
        }
      }else if(c == 'f'){   //found index data (faces)
       data = rd.read();
       if((char)data == ' '){
        found = 2;
       }else{
        found = -1;        
       }
      }else{
       found = -1;       
      }
     }

     if(found == 1){
      float x = 0,y = 0,z = 0;     
      for(int repeat = 0; repeat<3; repeat++){
       buildWord="";
       do{
        data = rd.read();
        c = (char)data;    
        buildWord+=c;
       }while((char)data!=' ' && (char)data!='\n');
       if(repeat==0)
        x = Float.parseFloat(buildWord.trim());
       else if(repeat==1)
        y = Float.parseFloat(buildWord.trim());
       else if(repeat==2)
        z = Float.parseFloat(buildWord.trim());
      }
      vert.add(x);
      vert.add(y);
      vert.add(z);
      found = -1;
     }

     if(found == 2){
      short v1 = 0, v2 = 0, v3 = 0;
      short map1 = 0, map2 = 0, map3 = 0;
      boolean uvdata = false;
      for(int repeat = 0; repeat<3; repeat++){
       buildWord="";
       do{
        data = rd.read();
        if(!uvdata && (char)data == '/'){
         uvdata = true;        
        }else{
         c = (char)data;    
         buildWord+=c;
        }
       }while((char)data!=' ' && (char)data!='\n' && (char)data!='/');
       if(repeat==0){
        v1 = Short.parseShort(buildWord.trim());
       }else if(repeat==1){
        v2 = Short.parseShort(buildWord.trim());
       }else if(repeat==2){
        v3 = Short.parseShort(buildWord.trim());
       }

       if(uvdata){
        uvdata = false;
        buildWord = "";
        do{
         data = rd.read();
         c = (char)data;    
         buildWord+=c;
         if(!uvdata && (char)data == '/'){
          uvdata = true;        
         }
        }while((char)data!=' ' && (char)data!='\n');
        if(repeat == 0)
         map1 = Short.parseShort(buildWord.trim());
        else if(repeat == 1)
         map2 = Short.parseShort(buildWord.trim());
        else if(repeat == 2)
         map3 = Short.parseShort(buildWord.trim());
        else{
         map1 = 0; map2  = 0; map3 = 0;     
        }
       }
      }       
      ind.add(v1);
      ind.add(v2);
      ind.add(v3);

      map.add(map1);
      map.add(map2);
      map.add(map3);
      found = -1;
     }
     if(found == 3){
      float uvx = 0, uvy = 0;     
      for(int repeat = 0; repeat<2; repeat++){
       buildWord="";
       do{
        data = rd.read();
        c = (char)data;    
        buildWord+=c;
       }while((char)data!=' ' && (char)data!='\n');
       if(repeat==0)
        uvx = Float.parseFloat(buildWord.trim());
       else if(repeat==1)
        uvy = Float.parseFloat(buildWord.trim());
      }
      uv.add(uvx);
      uv.add(uvy);
      found = -1;
     }

    data = rd.read();
   }
   rd.close();

  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }


  ret.vertices = new float[vert.size()];
  for(int i=0; i<ret.vertices.length; i++){
   ret.vertices[i] = vert.get(i);
   Log.d("CATCH", Float.toString(ret.vertices[i]));
  }

  ret.uvMappings = new short[map.size()];
  for(int i=0; i<ret.uvMappings.length; i++){
   ret.uvMappings[i] = (short) (map.get(i)-1);
   Log.d("CATCH", Float.toString(ret.uvMappings[i]));
  }

  ret.uvtex = new float[uv.size()];
  for(int i=0; i<ret.uvtex.length; i++){
   ret.uvtex[i] = uv.get(i);
   Log.d("CATCH", Float.toString(ret.uvtex[i]));
  }

  ret.indices = new short[ind.size()];
  for(int i=0; i<ret.indices.length; i++){
   ret.indices[i] = (short) (ind.get(i)-1);
  }

  ret.MapUVCoordinates();
  ret.fillBuffers();

  return ret;
 }}

The method ‘ret.MapUVCoordiantes’ doesn’t work, it was just my attempt at ordering them myself…I’m not going to post it because it, just, doesn’t do anything.
The method ‘ret.fullBuffers’ is below. It just creates bytebuffers out of the arrays:

    public void fillBuffers(){
  ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
  ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
  ByteBuffer ubb = ByteBuffer.allocateDirect(uvtex.length * 4);

  Log.d("CMeshInfo::fillBuffers", Float.toString(vertices.length * 4));
  Log.d("CMeshInfo::fillBuffers", Float.toString((indices.length * 2)));
  Log.d("CMeshInfo::fillBuffers", Float.toString(uvtex.length * 4));


  vbb.order(ByteOrder.nativeOrder()); //Vertices
  ubb.order(ByteOrder.nativeOrder()); //UV coordinates
  ibb.order(ByteOrder.nativeOrder()); //Indices

  vertBuff = vbb.asFloatBuffer();
  uvBuff = ubb.asFloatBuffer();
  IndBuff = ibb.asShortBuffer();

  vertBuff.put(vertices);
  uvBuff.put(uvtex);
  IndBuff.put(indices);

  vertBuff.position(0);
  uvBuff.position(0);
  IndBuff.position(0);
 }
  • 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-05-18T21:36:36+00:00Added an answer on May 18, 2026 at 9:36 pm

    If I remember correctly those number are indexes into the vertex array.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.