I have been looking for days to find an answer to my question. I Have looked through text books checked several websites etc... from what I can see my code SHOULD be rendering properly. I have thrown my face on my keyboard repeatedly trying to find the errors :(. Here it is. I have a simple .OBJ cube from blender that has been set to triangles so I have 3 indices per face. I have a parser parsing the 8 total vertices and 36 indices. I just don't know what to do anymore here is my code... please help! Thanks in advance.
enter code here
this is the .OBJ file
# Blender v2.62 (sub 0) OBJ File: ''
# www.blender.org
mtllib tcubetest.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
usemtl Material
s off
f 5 1 4
f 5 4 8
f 3 7 8
f 3 8 4
f 2 6 3
f 6 7 3
f 1 5 2
f 5 6 2
f 5 8 6
f 8 7 6
f 1 2 3
f 1 3 4
this is the parsing being done.
enter code here
Context context;
public ArrayList<Float> v = new ArrayList<Float>();
public ArrayList<Short> f = new ArrayList<Short>();
Parser(Context context) {
this.context = context;
BufferedReader reader = null;
String line = null;
try { // try to open file
reader = new BufferedReader(new InputStreamReader(context
.getResources().getAssets().open("tcubetest.obj")));
} catch (IOException e) {
}
try {
while ((line = reader.readLine()) != null) {
if (line.startsWith("v")) {
processVLine(line);
} else if (line.startsWith("f")) {
processFLine(line);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void processVLine(String line) {
String[] tokens = line.split("[ ]+"); // split the line at the spaces
int c = tokens.length;
for (int i = 1; i < c; i++) { // add the vertex to the vertex array
v.add(Float.valueOf(tokens[i]));
}
}
private void processFLine(String line) {
String[] tokens = line.split("[ ]+");
int c = tokens.length;
if (tokens[1].matches("[0-9]+")) {// f: v
//if (c == 4) {
for (int i = 1; i < c; i++) {
Short s = Short.valueOf(tokens[i]);
s--;
f.add(s);
}
}
}
//THIS IS THE CUBE CLASS
public class Cube {
FloatBuffer vertBuff;
ShortBuffer faceBuff;
float[] verts;
short[] faces;
Cube(ArrayList<Float> vertices, ArrayList<Short> facesa) {
verts = new float[vertices.size()];
faces = new short[facesa.size()];
for (int index = 0; index < verts.length; index++) {
verts[index] = vertices.get(index);
}
for (int index = 0; index < faces.length; index++) {
faces[index] = facesa.get(index);
}
ByteBuffer vbb = ByteBuffer.allocateDirect(verts.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertBuff = vbb.asFloatBuffer();
vertBuff.put(verts);
vertBuff.position(0);
vbb = ByteBuffer.allocateDirect(faces.length * 4);
vbb.order(ByteOrder.nativeOrder());
faceBuff = vbb.asShortBuffer();
faceBuff.put(faces);
faceBuff.position(0);
}
public void draw(GL10 gl) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
gl.glColor4f(1.0f, 0.0f, 0.0f, .2f);
gl.glDrawElements(GL10.GL_TRIANGLES, faces.length,
GL10.GL_UNSIGNED_BYTE, faceBuff);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
} used to make array for draw array
enter code here
public void makeArray(float ver[], short fac[]){
finals = new float[fac.length];
for(int index = 0; index < fac.length; index++){
finals[index] = ver[fac[index]];
}
ByteBuffer vbb = ByteBuffer.allocateDirect(finals.length *4);
vbb.order(ByteOrder.nativeOrder());
vertBuff = vbb.asFloatBuffer();
vertBuff.put(finals);
vertBuff.position(0);
}
I have been looking through this ever since the first post... trying anything I can think of. The code itself isn't that complicated, isn't there someone out there that knows what I am doing wrong? Is it possibly a problem with the file being exported improperly?
Okay update time. I manually loaded vertices and indices that should work based on tutorials... used the same draw setup for the Cube class and it had the same problems of not all the faces completely drawing(only about half of them it looks) so this is what I know: My parser works; my loading is "working" by that I mean it is consistent with manually entered values; That I have tried what appears to me to be EVERYTHING. I have spent months teaching myself programming from scratch and made it this far without asking ANYONE any questions. I could really use the help right now though, thanks again everyone.