00001 /* 00002 File: SLRender.cc 00003 00004 Function: 00005 00006 Author: Andrew Willmott 00007 00008 Notes: 00009 */ 00010 00011 #include "gcl/GCLConfig.h" 00012 00013 #ifdef GCL_MESA 00014 #include "cl/ArgParse.h" 00015 00016 #include "gcl/GCLApp.h" 00017 #include "gcl/MesaRenderer.h" 00018 #include "gcl/Readers.h" 00019 00020 #define DBG_COUT if (0) cerr 00021 00022 00023 // --- RenderApp -------------------------------------------------------------- 00024 00025 00026 class RenderApp : public GCLApp 00027 { 00028 public: 00029 00030 Void SetOptions(Int argc, Char **argv); 00031 Int Run(); 00032 Void DrawScenes(); 00033 00034 // options 00035 Char *output; 00036 Int wireFrame; 00037 Int window; 00038 00039 StrConst animFile; 00040 Int startFrame; 00041 Int endFrame; 00042 Int frameStep; 00043 Int noLight; 00044 Int showBF; 00045 Int super; 00046 }; 00047 00048 Void RenderApp::SetOptions(Int argc, Char **argv) 00049 { 00050 ArgForm *arg_format; 00051 Double grey, clr[3], defClr[3]; 00052 Int formats; 00053 Int size; 00054 00055 size = 400; 00056 xsize = -1; 00057 ysize = -1; 00058 output = 0; 00059 grey = 0.5; 00060 clr[0] = -1; 00061 defClr[0] = -1; 00062 animFile = 0; 00063 startFrame = -1; 00064 endFrame = -1; 00065 frameStep = 1; 00066 00067 arg_format = arg_to_form(0, 00068 "", 00069 "Usage: slview file1 file2 ... [options]", 00070 "", ARG_SUBR(GetFileArgs), 00071 "", 00072 "-o %S", &output, 00073 "Set output file", 00074 "-bgr %F", &grey, 00075 "Set background grey level", 00076 "-bgc %F %F %F", clr, clr + 1, clr + 2, 00077 "Set colour background", 00078 "-defClr %F %F %F", defClr, defClr + 1, defClr + 2, 00079 "Set default model colour", 00080 "-noScale", ARG_FLAG(&noScale), 00081 "Don't rescale scene.", 00082 "-wire", ARG_FLAG(&wireFrame), 00083 "Draw wire-frame.", 00084 "-zup", ARG_FLAG(&zUp), 00085 "Z axis is up (default is Y)", 00086 "-zdown", ARG_FLAG(&zDown), 00087 "Z axis is down", 00088 "-noLight", ARG_FLAG(&noLight), 00089 "no lighting", 00090 "-showBF", ARG_FLAG(&showBF), 00091 "show back faces of polygons", 00092 "-super", ARG_FLAG(&super), 00093 "supersample", 00094 00095 "-anim %S", &animFile, "specify animation file", 00096 "-fps %F", &fps, "set frames per sec [default 24]", 00097 "-start %d", &startFrame, "start frame [1]", 00098 "-end %d", &endFrame, "end frame [24]", 00099 "-step %d", &frameStep, "frame step [1]", 00100 00101 "-set", ARG_SUBR(GetAvarArgs), 00102 "Set avar, e.g. -set light 0.5 height 0.2", 00103 "-formats", ARG_FLAG(&formats), 00104 "List supported file formats", 00105 00106 // Output options 00107 "-size %d", &size, 00108 "Set output size", 00109 "-xysize %d %d", &xsize, &ysize, 00110 "Set output size", 00111 0); 00112 00113 if (argc == 1) 00114 { 00115 arg_form_print(arg_format); 00116 cout << endl; 00117 SceneReader::PrintSupportedFormats(cout); 00118 Image::PrintSupportedFormats(cout); 00119 exit(0); 00120 } 00121 00122 if (arg_parse_argv(argc, argv, arg_format) < 0) 00123 exit(1); 00124 00125 if (formats) 00126 { 00127 SceneReader::PrintSupportedFormats(cout); 00128 Image::PrintSupportedFormats(cout); 00129 exit(0); 00130 } 00131 00132 if (clr[0] < 0) 00133 bgColour = cWhite * grey; 00134 else 00135 bgColour = Colour(clr[0], clr[1], clr[2]); 00136 00137 if (defClr[0] >= 0) 00138 scPrimitive::sDefaultColour = Colour(defClr[0], defClr[1], defClr[2]); 00139 00140 if (xsize < 0) 00141 xsize = size; 00142 if (ysize < 0) 00143 ysize = size; 00144 } 00145 00146 Int RenderApp::Run() 00147 { 00148 if (numFiles < 1) 00149 return(1); 00150 00151 slInit(); 00152 00153 DrawScenes(); 00154 00155 return(0); 00156 } 00157 00158 Void RenderApp::DrawScenes() 00159 { 00160 Int i; 00161 RGBAImage image, outImage; 00162 FileName sceneFile, outFile; 00163 ofstream fout; 00164 MesaRenderer r; 00165 00166 scene = 0; 00167 if (super) 00168 image.SetSize(2 * xsize, 2 * ysize); 00169 else 00170 image.SetSize(xsize, ysize); 00171 00172 r.Init(image); 00173 r.SetHeadlight(!noLight); 00174 r.SetDoubleSided(showBF); 00175 if (wireFrame) 00176 scPrimitive::sRenderStyle = renLineLoop; 00177 r.SetBgColour(bgColour); 00178 00179 if (animFile) 00180 { 00181 anim = new Animation; 00182 00183 if (!anim->ParseAnimFile(animFile)) 00184 { 00185 cerr << "Couldn't open anim file" << endl; 00186 exit(1); 00187 } 00188 else 00189 cerr << "animation of " << anim->length << " seconds read" << endl; 00190 if (startFrame < 0) 00191 { 00192 startFrame = 1; 00193 if (endFrame < 0) 00194 endFrame = Int(anim->length * fps); 00195 } 00196 else if (endFrame < 0) 00197 endFrame = startFrame; 00198 00199 frame = startFrame; 00200 } 00201 00202 for (i = 0; i < numFiles; i++) 00203 { 00204 delete scene; 00205 sceneFile.SetPath(files[i]); 00206 00207 ReadScene(sceneFile); 00208 if (scene) 00209 { 00210 scene->Set(new scColour(scPrimitive::sDefaultColour)); 00211 itsCamera = (scCamera*) scene->FindFirst(aCamera); 00212 if (!itsCamera) 00213 itsCamera = new scCamera; 00214 scene->Set(itsCamera); 00215 00216 do 00217 { 00218 if (anim) 00219 SetAvarsFromAnim(scene); 00220 else 00221 SetAvars(scene); 00222 00223 r.Clear(); 00224 r.Draw(*scene); 00225 r.Show(); 00226 00227 if (output) 00228 outFile.SetPath(output); 00229 else 00230 { 00231 outFile = sceneFile; 00232 if (outFile.GetExtension() == "gz") 00233 outFile.RemoveExtension(); 00234 outFile.SetDir("."); 00235 outFile.SetExtension("tif"); 00236 } 00237 00238 if (frame >= 0) 00239 outFile.SetFile(outFile.GetFile() + String().Printf("-%04d", frame)); 00240 00241 if (super) 00242 { 00243 cout << "*** saving super-sampled image to " << outFile.GetPath() << endl; 00244 outImage.SetSize(xsize, ysize); 00245 image.DownSample(outImage); 00246 outImage.Save(outFile); 00247 } 00248 else 00249 { 00250 cout << "*** saving image to " << outFile.GetPath() << endl; 00251 image.Save(outFile); 00252 } 00253 } 00254 while (anim && frame++ <= endFrame); 00255 } 00256 } 00257 } 00258 00259 // ---------------------------------------------------------------------------- 00260 00261 Int main(Int argc, Char **argv) 00262 { 00263 RenderApp app; 00264 00265 app.SetOptions(argc, argv); 00266 00267 return(app.Run()); 00268 } 00269 00270 #else 00271 00272 #include <iostream.h> 00273 00274 int main(int argc, char **argv) 00275 { 00276 cout << "slrender: No MESA installed" << endl; 00277 return(-1); 00278 } 00279 00280 00281 #endif