help needed

Started by ug02070, November 18, 2004, 01:32:48

Previous topic - Next topic

ug02070

got the following code to parse one line of a raw file, but i am struggling to get a loop to work that will go through the entire raw file, any help will be greatly appreciated

private boolean render() {
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);         // Clear The Screen And The Depth Buffer

      GL11.glLoadIdentity();                  // Reset The Current Modelview Matrix
      try{
         BufferedReader br = new BufferedReader(new FileReader("alien.raw"));
         Vector coords = new Vector();
         String currentLine = br.readLine();

         while(currentLine != null) {
            StringTokenizer st = new StringTokenizer(currentLine);
            while(st.hasMoreTokens()) {
               String coord = st.nextToken();
               coords.add(coord);
            }
            currentLine = br.readLine();
         }
      
   GL11.glBegin(GL11.GL_TRIANGLES);

   for (int x = 0;x<coords.size();x+=9) {
      for (x = 0;x<coords.size();x+=9) {
         float x1 = Float.parseFloat("" + coords.elementAt(x));
         float y1 = Float.parseFloat("" + coords.elementAt(x+1));
         float z1 = Float.parseFloat("" + coords.elementAt(x+2));

         float x2 = Float.parseFloat("" + coords.elementAt(x+3));
         float y2 = Float.parseFloat("" + coords.elementAt(x+4));
         float z2 = Float.parseFloat("" + coords.elementAt(x+5));

         float x3 = Float.parseFloat("" + coords.elementAt(x+6));
         float y3 = Float.parseFloat("" + coords.elementAt(x+7));
         float z3 = Float.parseFloat("" + coords.elementAt(x+8));
         
         GL11.glVertex3f(x1, y1, z1);
         GL11.glVertex3f(x2, y2, z2);
         GL11.glVertex3f(x3, y3, z3);
      }
      
      
   }
      
      GL11.glEnd();
      }catch(IOException e)
      {
         System.out.println("error");
      }   
      return true;            
   }


i am sorry if i am annoying a few of you with my lack of knowledge but i assure you that i am trying, i want to do it though because i love java nad i want to learn more :)

ap_kelly

One thing to note when posting code is to use the "code" tags, that way you won't get smiley faces in your printouts.

for (int x = 0;x<coords.size();x+=9) {
for (x = 0;x<coords.size();x+=9) {
float x1 = Float.parseFloat("" + coords.elementAt(x));
float y1 = Float.parseFloat("" + coords.elementAt(x+1));
float z1 = Float.parseFloat("" + coords.elementAt(x+2));

float x2 = Float.parseFloat("" + coords.elementAt(x+3));
float y2 = Float.parseFloat("" + coords.elementAt(x+4));
float z2 = Float.parseFloat("" + coords.elementAt(x+5));

float x3 = Float.parseFloat("" + coords.elementAt(x+6));
float y3 = Float.parseFloat("" + coords.elementAt(x+7));
float z3 = Float.parseFloat("" + coords.elementAt(x+8));

GL11.glVertex3f(x1, y1, z1);
GL11.glVertex3f(x2, y2, z2);
GL11.glVertex3f(x3, y3, z3);
}
}


I'm not sure I like the look of the double for loops where with the same variable "x" being incremented by both. I have no experience processing RAW files but it would seem to me that one of these loops is redundant.

Andy.

ug02070

ok thanks everyone for your help, i will get on with it now, thanks :)

aaron

Hi ug,

Maybe a loop structured like this would work?

           /*
             * For each triangle (3 vertices * 3 floats per vertex == 9)
             */
            for (int i = 0; i < coords.size(); i++) {
                /*
                 * For each vertex in this triangle (3 floats per vertex)
                 */
                for (int j = 0; j < 3; j++) {
                    float x = Float.parseFloat((String) coords.elementAt(9*i+3*j+0));
                    float y = Float.parseFloat((String) coords.elementAt(9*i+3*j+1));
                    float z = Float.parseFloat((String) coords.elementAt(9*i+3*j+2));

                    GL11.glVertex3f(x, y, z);
                }
            }


- Aaron