Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/pqt/autorib/ProcessRIB.java


1   //AutoRIB
2   // Copyright © 1998 - 2002, P W Quint
3   //
4   // Contact: autorib00@aol.com
5   //
6   // This library is free software; you can redistribute it and/or
7   // modify it under the terms of the GNU General Public
8   // License as published by the Free Software Foundation; either
9   // version 2 of the License, or (at your option) any later version.
10  //
11  // This library is distributed in the hope that it will be useful,
12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  // General Public License for more details.
15  //
16  // You should have received a copy of the GNU General Public
17  // License along with this library; if not, write to the Free Software
18  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  
20  package org.pqt.autorib;
21  import java.io.*;
22  import java.util.*;
23  import org.pqt.autorib.rib.*;
24  import org.pqt.autorib.instr.*;
25  import org.pqt.autorib.tokenizer.*;
26  import org.pqt.autorib.globals.*;
27  import org.pqt.autorib.util.Bounds3D;
28  
29  /** this class contains a single method - but it is the key method of
30   * the program. This will read in the RIB file, one frame at a time, apply
31   * the instructions to it and write the revised RIB out*/
32  public class ProcessRIB {
33      
34      
35      /** process the RIB file with the given name, using the instructions file
36       * given, taking account of the command line options (which are, by the
37       * time this method is called, set in the static Globals class)
38       * @param instrName the name of the instructions file to read in
39       * @param nameIn the name of the RIB file to read in */
40      
41      public static void  doProcessRIB(
42      String instrName, String nameIn, String nameOut) throws FormatException,
43      FileNotFoundException, IOException {
44          Enumeration i;
45          File tempFile = null;
46          InstrWReader instr = new InstrWReader(instrName); //set up the instrWReader
47          RIBWReader rib = null; //leave as null til we hav checked the command line
48          //options
49          String nameMaps;
50          
51          OutputStreamWriter os = new OutputStreamWriter(System.out);
52          instr.read();
53          if (Globals.isDebug) {
54              instr.write(os); //this is a debugging step
55              os.flush();
56          }
57          if (Globals.stdout.value) //we should write output to standard out
58              rib = new RIBWReader(nameIn, System.out);
59          else if (Globals.pipe.value) // or read from and write to standard streams
60              rib = new RIBWReader(System.in, System.out, "stdin");
61          else {
62              //work out what the name of a map file would be, if we need it
63              if (nameOut == null) {
64                  int t = nameIn.lastIndexOf('.');
65                  if (t >= 0) {
66                      nameOut = nameIn.substring(0, t) + '_' +
67                      nameIn.substring(t);
68                  }
69                  else {
70                      nameOut = nameIn + '_';
71                  }
72              }
73              int t = nameOut.lastIndexOf('.');
74              if (t >= 0)
75                  nameMaps = nameOut.substring(0, t) + 
76                  (nameOut.substring(0,t).endsWith("_") ? "" : "_") +
77                  "maps" + nameOut.substring(t);
78              else
79                  nameMaps = nameOut + "_maps";
80              if (Globals.sepfile.value) //if sepfile is set we use a mapfile
81                  rib = new RIBWReader(nameIn, nameOut, nameMaps);
82              else
83                  rib = new RIBWReader(nameIn, nameOut);
84          }
85          rib.readGlobalHeader();
86          //Look for a GlobalRIB instruction and process the current header
87          instr.process(rib, rib.globalHeader, rib.globalOptions,
88          InstrGlobals.GLOBALRIB);
89          if (rib.moreFrames) //rib.moreframes will have been set by readGlobalHeader above
90          {
91              rib.writeGlobalOptions();
92              //this writes just the non options part of the header ie. the camera
93              //transforms and any options set with Option
94              rib.writePartGlobalHeader();
95              rib.mapList = new Vector(5, 5);
96              i = instr.addedLights.elements();
97              rib.lights = new Vector(5, 5);
98              while (i.hasMoreElements())
99                  rib.lights.addElement(i.nextElement());
100             rib.readFrame(); //read frame also reads the world
101             //see if we need to add RIB to the frame header
102             rib.addWorldRIB = instr.addedLights;
103             //this ensures added lights appear in the right place
104             instr.process(rib, rib.frameHeader, rib.frameOptions,
105             InstrGlobals.FRAMERIB);
106             //this checks for tightClipping, LightDome and SixSnaps
107             instr.processMisc(rib, rib.frameOptions);
108             //first of all write out the gloal Options set with the 'Option' RIB call
109             rib.writeFrameOptions();
110             //rib.write("TransformBegin\n");
111             //write out just the camera transforms
112             rib.writePartFrameHeader();
113             //rib.write("TransformEnd\n");
114             if (!Globals.lightOption.read)
115                 //not a test render for a shadow map so do all the lights & objects
116             {
117                 //make sure output from now on goes to the map file (which is the same
118                 //as the normal file if the sepfile option not set)
119                 rib.switchToMap();
120                 i = rib.objects.elements();
121                 while(i.hasMoreElements()) //and then the objects
122                     instr.process(rib, (RIBObjectGroup) i.nextElement());
123                 i = rib.lights.elements();
124                 while(i.hasMoreElements()) //do the lights
125                     instr.process(rib, (RIBLight)i.nextElement());
126                 processMaps(rib.mapList, rib);
127                 rib.switchToMain(); //go back to the main file
128                 rib.write("\nFrameBegin " + Integer.toString(rib.frameNo)
129                 + "\n");
130                 rib.writeRestFrameHeader();
131                 rib.writeWorld();
132                 rib.write("FrameEnd #Frame number "
133                 + Integer.toString(rib.frameNo) + "\n");
134             }
135             else
136                 //this is a test render for a shadow map
137             {
138                 i = rib.objects.elements();
139                 while(i.hasMoreElements()) //do objects to ensure displacement
140                     instr.process(rib, (RIBObjectGroup) i.nextElement());
141                 //loop through until we find the right light
142                 i = rib.lights.elements();
143                 while (i.hasMoreElements()) {
144                     RIBLight light = (RIBLight) i.nextElement();
145                     if (light.lightHandle ==
146                     ((Integer) Globals.lightOption.value.firstElement()).intValue()) {  //only process the single light specified
147                         rib.switchToMap();
148                         instr.process(rib, light);
149                         rib.switchToMain();
150                         break;
151                     }
152                 }
153             }
154         }
155     else //where no frame specified
156     {
157         rib.lights = new Vector(5, 5);
158         rib.readWorld();
159         //add the frame options to the global options
160         instr.process(rib, rib.globalHeader, rib.globalOptions,
161         InstrGlobals.FRAMERIB);
162         //this checks for tightClipping, LightDomes and SixSnaps
163         instr.processMisc(rib, rib.globalOptions);
164         rib.writeGlobalOptions();
165         //this writes just the non options part of the header ie. the camera
166         //transforms and any options set with Option
167         rib.writePartGlobalHeader();
168         rib.mapList = new Vector(5, 5);
169         i = instr.addedLights.elements();
170         while (i.hasMoreElements())
171             rib.lights.addElement(i.nextElement());
172         rib.addWorldRIB = instr.addedLights;
173         //the above ensures any added lights are in the right place
174         if (!Globals.lightOption.read) //not a test render
175         {
176             //this is pretty much the same sequence as the frame version above
177             rib.switchToMap();
178             i = rib.objects.elements();
179             while(i.hasMoreElements())
180                 instr.process(rib, (RIBObjectGroup) i.nextElement());
181             i = rib.lights.elements();
182             while(i.hasMoreElements())
183                 instr.process(rib, (RIBLight)i.nextElement());
184             processMaps(rib.mapList, rib);
185             rib.switchToMain();
186             rib.write("\nFrameBegin 0\n");
187             rib.write("Identity\n");
188             rib.writeRestGlobalHeader();
189             rib.writeWorld();
190             rib.write("FrameEnd\n");
191         }
192         else {
193             i = rib.lights.elements();
194             while (i.hasMoreElements()) {
195                 RIBLight light = (RIBLight) i.nextElement();
196                 if (light.lightHandle ==
197                 ((Integer) Globals.lightOption.value.firstElement()).intValue()) {
198                     rib.switchToMap();
199                     instr.process(rib, light);
200                     rib.switchToMain();
201                     break;
202                 }
203             }
204         }
205     }
206     rib.flushOut();
207     //write out any saved Prefs
208     InstrSavePref.writeSavedPrefs(instr);
209     //write out a list of names and associated numbers
210     if (Globals.listNumbers.value) {
211         OutputStreamWriter o = new OutputStreamWriter(System.out);
212         if (Globals.listByName.value)
213             instr.nameNumberManager.write(o, true);
214         else
215             instr.nameNumberManager.write(o, false);
216         o.flush();
217     }
218 }
219     
220     /** Write out RIB to generate all the maps in the given list in the right
221      *  order
222      * @param vector the list of maps (MapRec) 
223      * @param rib the rib file we are using*/
224     public static void processMaps(Vector vector, RIBWReader rib) throws 
225     FormatException, IOException{
226         Object array[] = vector.toArray();
227         Arrays.sort(array);
228         InstrMapRec mr;
229         for (int i = 0; i < array.length; i++) {
230             mr = (InstrMapRec) array[i];
231             mr.write(rib);
232         }
233     }
234 
235 }
236 
237 
238