// C++ file for a pendulum moving under SHM -- pendulum_shm.cc // Author: Jorge Balbas // Date: 04/05/09 #include #include #include #include #include using namespace std; int main() { //////////////////////////////////////////////////////////////////////////// // variables //////////////////////////////////////////////////////////////////////////// long n = 0; // frame counter long frames; // number of frames double L = 7.0; // length of pendulum double rod_r = 0.02; // radius of pendulum rod double bob_r = 0.4; // radius of pendulum bob double g = 9.81; // gravity double pi = 4.0*atan2(1.0,1.0); // pi double w = ...; // pendulum frequency -- COMPLETE double P = ...; // pendulum Period -- COMPLETE double T0 = ...; // initial displacement -- COMPLETE double w0 = ...; // initial angular velocity -- COMPLETE double T = ...; // T - displacement of the pendulum wrt // equlibrium -- COMPLETE double T2; // angular displacement of the rotating disk // underneat the pendulum double t = 0.0; // time variable double dt = 1.0/24.0; // interval betweeen frames (24 per second) double t_final = P; // final simulation time double A =...; // amplitud of SHM -- COMPLETE double phi = ...; // pendulum phase -- COMPLETE /////////////////////////////////////////////////////////////////////////// // lights /////////////////////////////////////////////////////////////////////////// double y_light = -2.5; // position of lights along y-axis double r_light = sqrt(2.5*L*2.5*L - (2.5*L + y_light)*(2.5*L + y_light)) - 0.1; // radius of a(n imaginary) ring // holding the lights double x_light = 0.5*r_light*sqrt(2.0) - 0.1; // x-coord of ligths (+/-) double z_light = 0.5*r_light*sqrt(2.0) - 0.1; // z-coord of ligths (+/-) char shm_file[25]; // holds name of frame file // RM type Float variables -- light intensity, fov, etc. static RtFloat fov = 40, intensity0 = 0.4, intensity1 = 0.7, intensity2 = 0.7, intensity3 = 0.7, intensity4 = 0.7; // Light direction static RtPoint from1 = {-1.0*x_light, y_light, -1.0*z_light}; static RtPoint to1 = {x_light, -1.0*L, z_light}; static RtPoint from2 = {-1.0*x_light, y_light, z_light}; static RtPoint to2 = {x_light, -1.0*L, -1.0*z_light}; static RtPoint from3 = {x_light, y_light, -z_light}; static RtPoint to3 = {-1.0*x_light, -1.0*L, z_light}; static RtPoint from4 = {x_light, y_light, z_light}; static RtPoint to4 = {-1.0*x_light, -1.0*L, -1.0*z_light}; // points on plane used for walls, ceiling and floor static RtPoint fpoints[4] = {-2.5*L, -1.3*L, -2.5*L, 2.5*L, -1.3*L, -2.5*L, 2.5*L, -1.3*L, 2.5*L, -2.5*L, -1.3*L, 2.5*L}; /////////////////////////////////////////////////////////////////////////// // other variables /////////////////////////////////////////////////////////////////////////// // colors static RtColor bob_color = {0.7, 0.8, 0.0}; static RtColor rod_color = {1.0, 1.0, 1.0}; static RtColor disk_color = {0.9, 0.5, 0.2}; static RtColor disk_sm_color = {0.1, 0.9, 0.1}; static RtColor plane_color = {0.3, 0.3, 0.8}; static RtColor dome_color = {0.9, 0.1, 0.7}; static RtColor white = {1.0, 1.0, 1.0}; static RtColor green = {0.0, 1.0, 0.0}; static RtColor Os = {0.5, 0.5, 0.5}; // Object handles RtObjectHandle rod, bob, disk, disk_sm; // number of frames frames = ceil(t_final/dt); // Time loop for (n = 0; n < frames + 1; n++) { // To keep right order of frames if (n < 10) sprintf(shm_file, "shm_files/shm_00%d.tiff", n); else { if (n < 100) sprintf(shm_file, "shm_files/shm_0%d.tiff", n); else sprintf(shm_file, "shm_files/shm_%d.tiff", n); } // environment and format set up RiBegin (RI_NULL); RiProjection ("perspective", "fov", &fov, RI_NULL); RiDisplay (shm_file, "file", "rgb", RI_NULL); RiFormat (600, 400, 1); // Object definitions // pendulum rod rod = RiObjectBegin(); RiTransformBegin(); RiRotate (-90.0, 1, 0, 0); RiCylinder (rod_r, -L, 0.0, 360.0, RI_NULL); RiTransformEnd(); RiObjectEnd(); // pendulum bob bob = RiObjectBegin(); RiTransformBegin(); RiTranslate (0.0, -L, 0.0); RiSphere (bob_r, -1.0*bob_r, bob_r, 360.0, RI_NULL); RiTransformEnd(); RiObjectEnd(); // rotating disk disk = RiObjectBegin(); RiTransformBegin(); RiTranslate(0.0, -1.3*L, 0.0); RiRotate(90.0, 1, 0, 0); RiDisk (-0.1, 3.7, 360); RiTransformEnd(); RiObjectEnd(); // small disk (dot) on larger disk disk_sm = RiObjectBegin(); RiTransformBegin(); RiTranslate(3.4, -1.3*L, 0.0); RiRotate(90.0, 1, 0, 0); RiDisk (-0.11, 0.2, 360); RiTransformEnd(); RiObjectEnd(); // FrameBegin RiFrameBegin(1); // WorldBegin RiWorldBegin(); RiTranslate (0.0, 5.5, 17.0); // Lights RiLightSource ("ambientlight", "intensity", &intensity0, RI_NULL); RiLightSource ("pointlight", "intensity", &intensity1, "from", from1, RI_NULL); RiLightSource ("distantlight", "intensity", &intensity1, "from", from1, "to", to1, RI_NULL); RiLightSource ("distantlight", "intensity", &intensity2, "from", from2, "to", to2, RI_NULL); RiLightSource ("distantlight", "intensity", &intensity3, "from", from3, "to", to3, RI_NULL); RiLightSource ("distantlight", "intensity", &intensity4, "from", from4, "to", to4, RI_NULL); // pendulum's position T = ... ; // COMPLETE T *= ...; // convert to degrees T2 = ...; // COMPLETE T2 *= ...; // convert to degrees t += dt; // pendulum RiTransformBegin(); // rotate pendulum by T rad along the z-asix RiRotate(T, 0, 0, 1); // pendulum's bob RiAttributeBegin(); RiSurface("matte", RI_NULL); RiColor(bob_color); RiObjectInstance(bob); RiAttributeEnd(); // pendulum's rod RiAttributeBegin(); RiSurface("metal", RI_NULL); RiColor(rod_color); RiObjectInstance(rod); RiAttributeEnd(); RiTransformEnd(); // DIsk RiTransformBegin(); // Disk rotaion RiRotate(T2,0,-1,0); //Disk RiAttributeBegin(); RiSurface("matte", RI_NULL); RiColor(disk_color); RiObjectInstance(disk); RiAttributeEnd(); // Small disk (dot) RiAttributeBegin(); RiSurface("matte", RI_NULL); RiColor(disk_sm_color); RiObjectInstance(disk_sm); RiAttributeEnd(); RiTransformEnd(); // Floor RiAttributeBegin(); RiTransformBegin(); RiScale(2.0, 1.0, 2.0); RiSurface("matte", RI_NULL); RiColor(plane_color); RiPolygon(4, RI_P, (RtPointer)fpoints, RI_NULL); RiTransformEnd(); RiAttributeEnd(); // Small hemisphere holding the pendulum at the top RiAttributeBegin(); RiTransformBegin(); RiRotate(90.0, 1, 0, 0); RiColor(plane_color); RiSphere (0.5*bob_r, 0.0, 0.5*bob_r, 360.0, RI_NULL); RiTransformEnd(); RiAttributeEnd(); RiWorldEnd(); // RiWorldEnd RiFrameEnd(); // RiFrameEnd RiEnd (); } return 0; }