00001 /* 00002 File: GCLApp.cc 00003 00004 Function: 00005 00006 Author: Andrew Willmott 00007 00008 Notes: 00009 */ 00010 00011 #include "gcl/GCLApp.h" 00012 #include "gcl/Avars.h" 00013 #include "gcl/Readers.h" 00014 #include <stdlib.h> 00015 00016 #define DBG_COUT if (0) cerr 00017 00018 00019 // --- GCLApp -------------------------------------------------------------- 00020 00021 00029 Char **GCLApp::files; 00030 Int GCLApp::numFiles; 00031 StrConstArray GCLApp::avarNames; 00032 ScalarList GCLApp::avarVals; 00033 00034 GCLApp::GCLApp() : 00035 #ifndef GCL_NO_GL 00036 itsScenePane(0), 00037 #endif 00038 itsCamera(0), 00039 frame(1), 00040 time(0.0), 00041 fps(24.0), 00042 anim(0), 00043 avarList(0) 00044 { 00045 } 00046 00047 Void GCLApp::ReadScene(FileName &sceneFile) 00048 { 00049 scene = SceneReader::Load(sceneFile); 00050 if (!scene) 00051 return; 00052 00053 if (zUp) 00054 scene->Apply(Rotation(vl_x, vl_halfPi)); 00055 else if (zDown) 00056 scene->Apply(Rotation(vl_x, -vl_halfPi)); 00057 00058 if (!noScale) 00059 scene->Normalise(); 00060 } 00061 00062 Void GCLApp::GetFileArgs(Int argc, Char *argv[]) 00063 { 00064 // Save away a pointer to the list of file names 00065 // I've checked the source of libarg.c -- it 00066 // doesn't free anything, so this is safe. 00067 00068 files = argv; 00069 numFiles = argc; 00070 } 00071 00072 Void GCLApp::GetAvarArgs(Int argc, Char *argv[]) 00073 { 00074 Int i; 00075 String suffix; 00076 00077 for (i = 0; i < argc; ) 00078 { 00079 StrConst avarName = argv[i++]; 00080 00081 DBG_COUT << "looking at " << avarName << " suff = " << avarName.Suffix(2) << endl; 00082 00083 suffix = avarName.Suffix(2); 00084 if (suffix == "_c" || suffix == "_v" || suffix == "_q") 00085 { 00086 String stub, newAvar; 00087 00088 stub = avarName.Prefix(-2); 00089 00090 DBG_COUT << "got vector type " << stub << endl; 00091 00092 newAvar.Printf("%s_x", stub.CString()); 00093 avarNames.Append(newAvar.Detach()); 00094 avarVals.Append(atof(argv[i++])); 00095 newAvar.Printf("%s_y", stub.CString()); 00096 avarNames.Append(newAvar.Detach()); 00097 avarVals.Append(atof(argv[i++])); 00098 if (suffix[1] == 'v' || suffix[1] == 'q') 00099 { 00100 newAvar.Printf("%s_z", stub.CString()); 00101 avarNames.Append(newAvar.Detach()); 00102 avarVals.Append(atof(argv[i++])); 00103 } 00104 if (suffix[1] == 'q') 00105 { 00106 newAvar.Printf("%s_w", stub.CString()); 00107 avarNames.Append(newAvar.Detach()); 00108 avarVals.Append(atof(argv[i++])); 00109 } 00110 } 00111 else 00112 { 00113 avarNames.Append(avarName); 00114 avarVals.Append(atof(argv[i++])); 00115 } 00116 } 00117 00118 if (i != argc) 00119 { 00120 cerr << "Error: wrong number of avar arguments." << endl; 00121 exit(1); 00122 } 00123 } 00124 00125 Void GCLApp::SetAvars(scScenePtr scene) 00126 { 00127 Int i; 00128 CreateAvarList theAvarList; 00129 00130 frame = -1; 00131 00132 scene->ApplyAction(theAvarList); 00133 00134 #ifdef DEBUG 00135 cout << "set avars:" << endl; 00136 for (i = 0; i < avarNames.NumItems(); i++) 00137 cout << "avar " << avarNames[i] << " = " << avarVals[i] << endl; 00138 cout << "scene avars:" << endl; 00139 for (i = 0; i < theAvarList.avarList->NumItems(); i++) 00140 cout << theAvarList.avarList->Item(i).name << " = " 00141 << theAvarList.avarList->Item(i).value << endl; 00142 #endif 00143 00144 scene->Set(theAvarList.avarList); 00145 00146 for (i = 0; i < avarNames.NumItems(); i++) 00147 { 00148 StrConst avarName = avarNames[i]; 00149 00150 if (avarName == "frame") 00151 frame = (Int) avarVals[i]; 00152 else if (avarName == "time") 00153 time = avarVals[i]; 00154 else if (itsCamera->CheckAvar(avarName, avarVals[i])) 00155 ; 00156 else if (!theAvarList.avarList->SetAvar(avarNames[i], avarVals[i])) 00157 cerr << "No such avar in scene: " << avarNames[i] 00158 << " (ignoring) " << endl; 00159 } 00160 00161 itsCamera->SetupFromParams(); 00162 } 00163 00164 Void GCLApp::SetAvarsFromAnim(scScenePtr scene) 00165 { 00166 if (!avarList) 00167 { 00168 avarList = (scAvarList*) scene->FindFirst(aAvarList); 00169 00170 if (!avarList) 00171 { 00172 CreateAvarList theAvarList; 00173 00174 cout << "creating avar list" << endl; 00175 scene->ApplyAction(theAvarList); 00176 scene->Set(theAvarList.avarList); 00177 avarList = theAvarList.avarList; 00178 } 00179 } 00180 00181 time = (frame - 1.0) / fps; 00182 if (anim) 00183 anim->SetAvarList(avarList, itsCamera, time); 00184 } 00185 00186 ArgForm *GCLApp::GetOptionList() 00187 { 00188 #ifdef UNFINISHED 00189 return(arg_to_form(0, 00190 "-bgr %F", &grey, 00191 "Set background grey level", 00192 "-bgc %F %F %F", clr, clr + 1, clr + 2, 00193 "Set colour background", 00194 "-defClr %F %F %F", defClr, defClr + 1, defClr + 2, 00195 "Set default model colour", 00196 "-noScale", ARG_FLAG(&noScale), 00197 "Don't rescale scene.", 00198 // "-wire", ARG_FLAG(&wireFrame), 00199 // "Draw wire-frame.", 00200 "-zup", ARG_FLAG(&zUp), 00201 "Z axis is up (default is Y)", 00202 "-zdown", ARG_FLAG(&zDown), 00203 "Z axis is down", 00204 // "-noLight", ARG_FLAG(&noLight), 00205 // "no lighting", 00206 // "-showBF", ARG_FLAG(&showBF), 00207 // "show back faces of polygons", 00208 00209 // "-anim %S", &animFile, "specify animation file", 00210 "-fps %F", &fps, "set frames per sec [default 24]", 00211 // "-start %d", &startFrame, "start frame [1]", 00212 // "-end %d", &endFrame, "end frame [24]", 00213 // "-step %d", &frameStep, "frame step [1]", 00214 00215 "-set", ARG_SUBR(GetAvarArgs), 00216 "Set avar, e.g. -set light 0.5 height 0.2", 00217 00218 // Output options 00219 // "-size %d", &size, 00220 // "Set output size", 00221 "-xysize %d %d", &xsize, &ysize, 00222 "Set output size", 00223 0) 00224 ); 00225 #else 00226 return(0); 00227 #endif 00228 }