0

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.

PhaTty-FU
  • 33
  • 7
  • Your question could use some clarity. What kind of problem do you have, what does the result look like? Also if a cube is not working, I'd recommend just starting with an obj of a single triangle, and seeing if you can get that to work. If so, then try two distinct triangles, etc. See how far you can get. – Tim May 14 '12 at 20:20
  • I have been working with rectangles and textures for a while now. The cube draws jagged and randomly. I have tried using TRIANGLES TRIANGLE_FAN TRIANGLE_STRIPS Fan has done the best. I'm guessing my problem is with the indices and the drawing method I am using. I tried to add the .OBJ file but I'm a newb to forums so I don't know why its not on here. Anyways the faces are in threes for example f 1 2 3f 5 6 8...etc – PhaTty-FU May 14 '12 at 20:27
  • Debug and check the data in the arraylists is correct / matches the data in the .obj file. If it does, you can eliminate your parser as the problem Alternatively, hard code the data in (`float[] vertices = new float[]{1,0,0.0,-1.0 etc}` ) and work on the opengl stuff till you have that working, then work on the parser – James Coote May 14 '12 at 20:50
  • I did a check when renderer creates the cube. It has a proper count and all values are in the order they were parsed (-1 from the indices) QQ – PhaTty-FU May 14 '12 at 21:03
  • Any chance this could be caused by blender exporting a whack OBJ file? – PhaTty-FU May 14 '12 at 21:20
  • I created a method to make an array with length of 36 and set each vertex in it based on the faces index value. Ill edit my post to show you. I then used drawarrays instead of elements and I ended up with lines so short some very long flying off the "cube". Does this help? – PhaTty-FU May 14 '12 at 23:36

1 Answers1

1

After tons of facerolling and facepalming I realized that I need to change UNSIGNED_BYTE to UNSIGNED_SHORT in the drawelements call (since I was using a short). Lesson learned.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
PhaTty-FU
  • 33
  • 7