Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp with UV on my obj loader for android
    primarykey
    data
    text
    <p>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().</p> <p>I managed to read vertex information and face info and I can successfully load a mesh without any textures.</p> <p>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 ...')</p> <p>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(<a href="http://blog.jayway.com/2009/12/03/opengl-es-tutorial-for-android-part-i/" rel="nofollow">jayway</a>, and the nehe ports) it will still come out wrong.</p> <p>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 :(</p> <p>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.</p> <p>Heres the code I'm using:</p> <pre><code>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&lt;Float&gt; vert = new Vector&lt;Float&gt;(); Vector&lt;Float&gt; uv = new Vector&lt;Float&gt;(); Vector&lt;Short&gt; ind = new Vector&lt;Short&gt;(); Vector&lt;Short&gt; map = new Vector&lt;Short&gt;(); 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&lt;3; repeat++){ buildWord=""; do{ data = rd.read(); c = (char)data; buildWord+=c; }while((char)data!=' ' &amp;&amp; (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&lt;3; repeat++){ buildWord=""; do{ data = rd.read(); if(!uvdata &amp;&amp; (char)data == '/'){ uvdata = true; }else{ c = (char)data; buildWord+=c; } }while((char)data!=' ' &amp;&amp; (char)data!='\n' &amp;&amp; (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 &amp;&amp; (char)data == '/'){ uvdata = true; } }while((char)data!=' ' &amp;&amp; (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&lt;2; repeat++){ buildWord=""; do{ data = rd.read(); c = (char)data; buildWord+=c; }while((char)data!=' ' &amp;&amp; (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&lt;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&lt;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&lt;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&lt;ret.indices.length; i++){ ret.indices[i] = (short) (ind.get(i)-1); } ret.MapUVCoordinates(); ret.fillBuffers(); return ret; }} </code></pre> <p>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:</p> <pre><code> 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload