www.xbdev.net xbdev - software development
Saturday December 15, 2018
home | about | contact | Donations

     
 

Slice of Java.

Its simple and its powerful...

 

 

 

Lots and lots of triangles - Reading in data

by Ben Kenwright


 

 

Well we can do simple cubes, and sphere and other such shapes.  But we need to go further!  We want a level in there, or possibly a 3D model of a bunny rabbit..hehe  And well, its way to hard to manually enter all the vertice points in...so we have to go and get our data from an external file.

I've chosen a simple 'raw' file format.  Its a slightly modified version of raw, as it also contains the colour of each triangle, as well as the 3 vector values for the 3 corners.

 

Lets see what this amazingly simple file format looks like:

 

maze1.txt (Download Maze1.txt)

-6.000000 20.250000 -23.250000      -6.250000 20.250000 -40.000000      -6.500000 0.750000 -40.000000       FF0000

-6.750000 0.500000 -23.250000        -6.000000 20.250000 -23.250000      -6.500000 0.750000 -40.000000       FF0000

7.000000 20.250000 -40.000000        7.000000 20.500000 -23.250000        7.000000 1.000000 -40.000000       FF0000

7.250000 1.000000 -23.250000          7.000000 1.000000 -40.000000          7.000000 20.500000 -23.250000      FF0000

-6.190597 20.119310 5.750000          -6.190597 0.380692 5.750000            6.690597 20.119310 5.750000         00FF00

-6.190597 0.380692 5.750000            6.690597 0.380692 5.750000             6.690597 20.119310 5.750000          00FF00

6.690597 20.119310 5.750000           6.690597 0.380692 5.750000             6.690597 20.119310 -6.000000         00FF00

6.690597 0.380692 5.750000             6.690597 0.380692 -6.000000            6.690597 20.119310 -6.000000         00FF00

6.690597 20.119310 -6.000000          6.690597 0.380692 -6.000000            -6.190597 20.119310 -6.000000       00FF00

6.690597 0.380692 -6.000000            -6.190597 0.380692 -6.000000          -6.190597 20.119310 -6.000000       00FF00

-6.190597 20.119310 -6.000000        -6.190597 0.380692 -6.000000          -6.190597 20.119310 5.750000         00FF00

....etc etc....

 

 

 

Its not the most complex file format is it..hehe...hopefully you'll be able to glid through this tutorial without to much worry.  The only tricky part is understanding how we do this with java and in an applet :)  As remember now, we have to have the txt file with the .class applet file.  You can't put it on a different server or in a different folder or you'll get a security warning when you try and open it in a browser - and hence you won't get to see your cool maze :(

 

 

Applet Code - Download Applet Source Code

 

   ...etc..etc..

   int m_NumTris;
   Triangle[] m_tri;

 

   /*

   This small function or should I call it a method to be more correct,

   loads in a set of data from a txt file in the same location as the

   .class applet file.

   */

   private void ReadInData()

   { 

      // Our .txt file name

      String strFile = "maze1.txt";

      // We'll read in all the txt file in one go into this buffer

      String buffer = new String();

 

      //-- First we read in all the juicy txt info from our txt file--//

     

      // Well when ever we read or write to a file, we have to test for

      // errors!  Java won't let us compile it otherwise :-/...picky compiler

      try

      {

         URL theURL = new URL(getDocumentBase(),strFile);

         BufferedReader in = new BufferedReader(

                                                                    new InputStreamReader(

                                                                    theURL.openStream()));

         String inputLine;

                 

         while ((inputLine = in.readLine()) != null)

         {

            System.out.println(inputLine);

            buffer += inputLine;

            buffer += "\n";

         }

         in.close();

      }

      catch(java.net.MalformedURLException e) // not really needed

      {

         System.out.println("Exception :-/ ");

      }

      catch(IOException e)

      {}

           

 

      // Second...we've read it all in...but I just want to replace

      // newline's with spaces...and remove commas :)

   

   

      // Mini routine to remove char's

      char charA = '\n';

      char charB = '\"';

      char charC = '\r';

      char charD = '\t';

           

      String withChar = " ";

      String r = "";

      for (int i = 0; i < buffer.length(); i++)

      {

         if (buffer.charAt(i) != charA &&

                             buffer.charAt(i) != charB &&

                                     buffer.charAt(i) != charC &&

                                     buffer.charAt(i) != charD  ) r += buffer.charAt(i);

                         else r+= withChar;

      }

      buffer = r;

      buffer.trim();

           

 

      //-- Third we'll parse the data we now have in our buffer that we

      // read in --//

           

      StringTokenizer st;

      st = new StringTokenizer(buffer, " ");

      int tokenCount = st.countTokens();

           

      int tokens = 0;

      String[] str = new String[5000]; // Big buffer

      while( st.hasMoreTokens() )

      {

                     str[tokens] = st.nextToken();

                     tokens++;

      }

           

      // At this point, str[] is just a big array of string

      // values...such as 1, 32, 22, "0xFFFFF"...etc...all seperate

      // and one after the other where we can put seperate them.

           

      /*

      // Test code...dump all the parsed values

      for( int i=0; i<tokens; i++ )

      {

                     System.out.println( str[i] );

      }

      */

     

      float scalex = 3.0f;

      float scaley = 1.0f;

      float scalez = 3.0f;

           

      // Allocate memory so we can store all the values from the txt file into our

      // array.  I divide by 6, as there are 10 tokens of data per line :)

     

      m_tri = new Triangle[ tokens/10 ];

           

      int c=0;

      int i=0;

      while( c < tokens-1 )

      {

         // Had to fix these few lines so we could still use our java

         // applet on versions 1.0...as parseFloat wasn't introduced till 1.2

                     /*

         float x0  = Float.parseFloat( str[c]   )   * scalex;

         float y0  = Float.parseFloat( str[c+1] )   * scaley;

         float z0  = Float.parseFloat( str[c+2] )   * scalez;

         float x1  = Float.parseFloat( str[c+3] )   * scalex;

         float y1  = Float.parseFloat( str[c+4] )   * scaley;

         float z1  = Float.parseFloat( str[c+5] )   * scalez;

         float x2  = Float.parseFloat( str[c+6] )   * scalex;

         float y2  = Float.parseFloat( str[c+7] )   * scaley;

         float z2  = Float.parseFloat( str[c+8] )   * scalez;

         int   col = Integer.parseInt( str[c+9],16);

         c+=10; // 10 values per line

         */

           

         float x0  = new Float( str[c]   ).floatValue()   * scalex;

         float y0  = new Float( str[c+1] ).floatValue()   * scaley;

         float z0  = new Float( str[c+2] ).floatValue()   * scalez;

         float x1  = new Float( str[c+3] ).floatValue()   * scalex;

         float y1  = new Float( str[c+4] ).floatValue()   * scaley;

         float z1  = new Float( str[c+5] ).floatValue()   * scalez;

         float x2  = new Float( str[c+6] ).floatValue()   * scalex;

         float y2  = new Float( str[c+7] ).floatValue()   * scaley;

         float z2  = new Float( str[c+8] ).floatValue()   * scalez;

         int   col = Integer.parseInt( str[c+9],16);

         c+=10; // 10 values per line

                 

         // Debug Code

         /*

         System.out.println( " x0:" + x0 + " y0:"+ y0 + " z0: "+z0 +

                             " x1:" + x1 + " y1:"+ y1 + " z1: "+z1 +

                             " x2:" + x2 + " y2:"+ y2 + " z2: "+z2 +

                             " colour:"+ col );

         */

        

         int red = ( col >> 16 ) & 0xFF;

         int green = ( col >> 8 ) & 0xFF;

         int blue = ( col  ) & 0xFF;

         Color colr = new Color(red,green,blue);

         m_tri[i] = new Triangle(x0,y0,z0,  x1,y1,z1, x2,y2,z2,  colr);

         i++;

                 

      }// End while loop

     

      m_NumTris = i;

     

   }// End of ReadInData() function

     

   ...etc...etc

 

 

 

You would call the member function 'ReadInData(..)' once at the startup of the applet - which is in the init(..) function.  And then we stash away the triangle values for use in rendering or maze.  Its not a very high poly count maze...but its starting to look more and more exciting. 

Remember where only clipping in one direction as well, the z near plane - we could improve our code further ...and we possibly will by clipping to the viewing frustrum - as we only want to send poly's and vertices to our renderer stage that we can see. 

But our render seems to be handling it pretty well up to now, so we'll let it carry on for a bit long...taking the strain for us :)

 

 

 

 

 

 
 Visitor: 9534626  { 209.237.238.175 } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.