[SOLVED] Problem storing vertices in an ArrayList

Started by iToSK, September 21, 2006, 19:52:08

Previous topic - Next topic

iToSK

[EDIT] Thanks to kevglass I have solved it, i needed an coords = new float[3]; inside my while loop[EDIT]

I have reached the stage where i want something more interesting than cubes so wrote my own loader for a simple file format using elias4444s .obj loader as reference. Currently in my format each line represents a vertex and contains only the x y and z coordinates separated by commas. Every three vertices define a triangle. I can read and parse the file fine (I think) and am attempting to store each coordinate as a float[3] in an ArrayList. However, when I loop through the ArrayList to draw my object all of the coordinates seem to have the same value. The last 3 x, y and z values that I entered. This is my simple test file:
1.000000,1.000000,1.300000
1.000000,1.000000,-1.000000
-1.000000,1.000000,1.000000

This is my Model class:
package game;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import org.lwjgl.opengl.GL11;

public class Model {
	
    private ArrayList vertices = new ArrayList();;
    private int glcalllist;
    
    public Model(String path) {
    	try {
    		String newline;
    		float[] coords = new float[3];
    		String[] coordstext = new String[3];
    		
    		FileReader fr = new FileReader(path);
    		BufferedReader br = new BufferedReader(fr);
    		
    		System.out.println("Reading model file");
    		int count = 0;
    		while (((newline = br.readLine()) != null)) {
                coordstext = newline.split(",");
                for (int i=0;i<3;i++) {
                   coords[i] = Float.valueOf(coordstext[i]).floatValue();
                }
                System.out.println(count + ":" + coords[0] + "," + coords[1] + "," + coords[2]);
                vertices.add(coords);
                count++;
    		}
    		System.out.println("Finished reading model file");
    	} catch (IOException e) {
    		System.out.println("IO Exception");
    		System.exit(0);
    	}
    	genList();
    }

    public void genList() {
    	System.out.println("Generating glcalllist");
    	glcalllist = GL11.glGenLists(1);
    	GL11.glNewList(glcalllist,GL11.GL_COMPILE);
    	GL11.glBegin(GL11.GL_TRIANGLES);
    	float[] vertex;
    	float x,y,z;
    	for(int i=0;i<vertices.size();i++) {
    		vertex = (float[])vertices.get(i);
    		x = vertex[0];
    		y = vertex[1];
    		z = vertex[2];
    		GL11.glVertex3f(x,y,z);
    		System.out.println(i + ":" + x + "," + y + "," + z);
    	}
    	GL11.glEnd();

        GL11.glEndList();
        System.out.println("Finished generating glcalllist");
    }
    public void drawModel() {
    	GL11.glCallList(glcalllist);
    }
}

And this is the output I am getting from my game:
Reading model file
0:1.0,1.0,1.3
1:1.0,1.0,-1.0
2:-1.0,1.0,1.0
Finished reading model file
Generating glcalllist
0:-1.0,1.0,1.0
1:-1.0,1.0,1.0
2:-1.0,1.0,1.0
Finished generating glcalllist

Can anyone see what I am missing? Also any comments on my code? Anything I should be doing differently?
Thanks

darkprophet

I wouldn't suggest doing the file format like that, because:

a) You can't reuse vertices, leading to higher memory usage (on vram and ram) and a bigger file size
b) You can't re-order indices to obtain a better cache miss rate, you want to be able to keep the biggest number of vertices in the cache.

Both of those can be done via indices, like through glDrawElements and glDrawRangeElements.

DP :)