00001 #ifndef CAMERAANIMATOR_H
00002 #define CAMERAANIMATOR_H
00003
00004 #include <vector>
00005 #include <fstream>
00006
00007 #include "defines.h"
00008 #include "CameraInstructions.h"
00009 #include "PerspectiveCamera.h"
00010 #include "SceneBuilderFactories.h"
00011
00012 class Camera;
00013
00017 class CameraAnimator
00018 {
00019 friend class CameraInstructionMove;
00020 friend class CameraInstructionSet;
00021 friend class CameraInstructionNormal;
00022 friend class CameraInstructionStay;
00023
00024 public:
00027 CameraAnimator(std::string filename)
00028 :mActCam(0),
00029 mPC(0)
00030 {
00031
00032 parse(filename);
00033
00034
00035 }
00036
00039 ~CameraAnimator()
00040 {
00041 for(unsigned int i=0;i<mCameras.size();i++)
00042 delete mCameras.at(i);
00043 for (unsigned int i=0; i<mInstructions.size(); i++)
00044 delete mInstructions[i];
00045 }
00046
00049 Camera* animate()
00050 {
00051 LOG(mPC);
00052 assert(mPC<mInstructions.size() );
00053 assert(mInstructions.at(mPC)!= 0);
00054
00055 mInstructions.at(mPC)->execute();
00056 assert(mActCam<mCameras.size() );
00057 return mCameras.at(mActCam);
00058
00059 }
00060
00061
00062 private:
00065 void parse(std::string filename)
00066 {
00067 std::ifstream file(filename.c_str());
00068 std::string definition;
00069 file >> definition;
00070
00071
00072 if(definition!="definition:")
00073 {
00074 std::cout << "need a camera definition in front" << std::endl;
00075 assert(false);
00076 }
00077
00078
00079 file >> std::ws;
00080 DepthOfFieldCameraFactory df;
00081 PerspectiveCameraFactory pf;
00082
00083 std::string line;
00084 getline(file,line);
00085 while(line[0]!='m' && line[0]!='#')
00086 {
00087 LOG("parsing line:\"" << line << "\"");
00088
00089 if(line[0]!='#' && line!="")
00090 {
00091 std::stringstream linestream(line);
00092 std::string identifier;
00093 linestream >> identifier;
00094
00095 if(identifier=="perspectivecamera")
00096 {
00097 mCameras.push_back( pf.create(identifier, linestream));
00098 }
00099 else
00100 {
00101 mCameras.push_back( df.create(identifier, linestream));
00102 }
00103 }
00104 getline(file, line);
00105
00106 }
00107 while(line!="movesection:" && !file.eof())
00108 getline(file,line);
00109 if(file.eof())
00110 {
00111 std::cout << "move section expected" << std::endl;
00112 assert(false);
00113 }
00114
00115 getline(file, line);
00116
00117 if(file.eof() )
00118 LOG("EOF");
00119 while(!file.eof() )
00120 {
00121 LOG("parsing movesection");
00122 std::stringstream linestream;
00123 linestream << line;
00124 std::string identifier;
00125 linestream >> identifier;
00126 linestream >> std::ws;
00127
00128 if(identifier=="move")
00129 {
00130 std::string tmp;
00131
00132 unsigned int camnum;
00133 Vec3f newpos;
00134 Vec3f newdir;
00135 Vec3f newup;
00136 float newaperture;
00137 float newdistance;
00138 unsigned int framenumber;
00139 unsigned int depthnumber;
00140 linestream >> camnum;
00141 linestream >> std::ws;
00142 linestream >> newpos;
00143 linestream >> std::ws;
00144 linestream >> newdir;
00145 linestream >> std::ws;
00146 linestream >> newup;
00147 linestream >> std::ws;
00148 linestream >> newdistance;
00149 linestream >> std::ws;
00150 linestream >> newaperture;
00151 linestream >> std::ws;
00152 linestream >> depthnumber;
00153 linestream >> std::ws;
00154 linestream >> framenumber;
00155
00156 framenumber--;
00157
00158 if(mInstructions.size() == 0)
00159 {
00160
00161 mInstructions.push_back(new CameraInstructionNormal(this, camnum) );
00162 }
00163
00164 LOG("parsed with Vec: " << newpos << " newdir: " << newdir << " newup:" << newup << "aperture: " << newaperture << "distance " << newdistance << "depthnumber: " << depthnumber << " framenumber: " << framenumber << " camnum" << camnum);
00165 mInstructions.push_back(new CameraInstructionMove(this, newpos, newdir,newup, newaperture, newdistance, depthnumber, framenumber,camnum));
00166 }
00167 else if(identifier=="set")
00168 {
00169 linestream >> std::ws;
00170 unsigned int camnum;
00171 linestream >> camnum;
00172 assert(camnum<mCameras.size());
00173 mInstructions.push_back(new CameraInstructionSet(this, camnum));
00174 mInstructions.push_back(new CameraInstructionNormal(this, camnum));
00175 }
00176 else if(identifier=="stay")
00177 {
00178 linestream >> std::ws;
00179 unsigned int frames;
00180 unsigned int camnum;
00181 linestream >> camnum;
00182 linestream >> std::ws;
00183 linestream >> frames;
00184 LOG("frames parsed " << frames);
00185 mInstructions.push_back(new CameraInstructionStay(this,frames, camnum) );
00186 }
00187
00188 getline(file,line);
00189
00190 }
00191
00192 if(mInstructions.size()==0)
00193 LOG("WARNING, PLEASE ADD A NEWLINE AT THE END OF THE LINE, othwerise this will be recognized as empty");
00194 mInstructions.push_back(new CameraInstructionStay(this, 0, INT_MAX));
00195 }
00196
00197
00198 unsigned int mActCam;
00199 unsigned int mPC;
00200 std::vector<CameraInstructions*> mInstructions;
00201 std::vector<Camera*> mCameras;
00202 };
00203
00204 #endif