// Is n a multiple of m? int multiple_of(int n, int m) { if (m < 0) { return multiple_of(n, -m); } if (n < 0) { return multiple_of(-n, m); // false } if (m == 0) { return 0; // false } if (n == 0) { return 1; // true } return multiple_of(n - m, m); }
int is_prime_(int n, int m) { if (n <= 1) { return 0; // false } if (n == 2) { return 1; // true } if (n > 2) { if (m <= 1) { return 1; // true } else { if (multiple_of(n, m) == 0) { return 0; // false } return is_prime_(n, m - 1); } } }
#include <string.h> #include "lame.h" #include "machine.h" #include "gain_analysis.h" /* for each filter: */ /* [0] 48 kHz, [1] 44.1 kHz, [2] 32 kHz, [3] 24 kHz, [4] 22050 Hz, [5] 16 kHz, [6] 12 kHz, [7] is 11025 Hz, [8] 8 kHz */ #ifdef WIN32 #pragma warning ( disable : 4305 ) #endif /*lint -save -e736 loss of precision */ static const Float_t ABYule[9][multiple_of(16, 2 * YULE_ORDER + 1)] = { /* 20 18 16 14 12 10 8 6 4 2 0 19 17 15 13 11 9 7 5 3 1 */ { 0.00288463683916, 0.00012025322027, 0.00306428023191, 0.00594298065125, -0.02074045215285, 0.02161526843274, -0.01655260341619, -0.00009291677959, -0.00123395316851, -0.02160367184185, 0.03857599435200, 0.13919314567432, -0.86984376593551, 2.75465861874613, -5.87257861775999, 9.48293806319790,-12.28759895145294, 13.05504219327545,-11.34170355132042, 7.81501653005538, -3.84664617118067}, {-0.00187763777362, 0.00674613682247, -0.00240879051584, 0.01624864962975, -0.02596338512915, 0.02245293253339, -0.00834990904936, -0.00851165645469, -0.00848709379851, -0.02911007808948, 0.05418656406430, 0.13149317958808, -0.75104302451432, 2.19611684890774, -4.39470996079559, 6.85401540936998, -8.81498681370155, 9.47693607801280, -8.54751527471874, 6.36317777566148, -3.47845948550071}, {-0.00881362733839, 0.00651420667831, -0.01390589421898, 0.03174092540049, 0.00222312597743, 0.04781476674921, -0.05588393329856, 0.02163541888798, -0.06247880153653, -0.09331049056315, 0.15457299681924, 0.02347897407020, -0.05032077717131, 0.16378164858596, -0.45953458054983, 1.00595954808547, -1.67148153367602, 2.23697657451713, -2.64577170229825, 2.84868151156327, -2.37898834973084}, {-0.02950134983287, 0.00205861885564, -0.00000828086748, 0.06276101321749, -0.00584456039913, -0.02364141202522, -0.00915702933434, 0.03282930172664, -0.08587323730772, -0.22613988682123, 0.30296907319327, 0.00302439095741, 0.02005851806501, 0.04500235387352, -0.22138138954925, 0.39120800788284, -0.22638893773906, -0.16276719120440, -0.25656257754070, 1.07977492259970, -1.61273165137247}, {-0.01760176568150, -0.01635381384540, 0.00832043980773, 0.05724228140351, -0.00589500224440, -0.00469977914380, -0.07834489609479, 0.11921148675203, -0.11828570177555, -0.25572241425570, 0.33642304856132, 0.02977207319925, -0.04237348025746, 0.08333755284107, -0.04067510197014, -0.12453458140019, 0.47854794562326, -0.80774944671438, 0.12205022308084, 0.87350271418188, -1.49858979367799}, { 0.00541907748707, -0.03193428438915, -0.01863887810927, 0.10478503600251, 0.04097565135648, -0.12398163381748, 0.04078262797139, -0.01419140100551, -0.22784394429749, -0.14351757464547, 0.44915256608450, 0.03222754072173, 0.05784820375801, 0.06747620744683, 0.00613424350682, 0.22199650564824, -0.42029820170918, 0.00213767857124, -0.37256372942400, 0.29661783706366, -0.62820619233671}, {-0.00588215443421, -0.03788984554840, 0.08647503780351, 0.00647310677246, -0.27562961986224, 0.30931782841830, -0.18901604199609, 0.16744243493672, 0.16242137742230, -0.75464456939302, 0.56619470757641, 0.01807364323573, 0.01639907836189, -0.04784254229033, 0.06739368333110, -0.33032403314006, 0.45054734505008, 0.00819999645858, -0.26806001042947, 0.29156311971249, -1.04800335126349}, {-0.00749618797172, -0.03721611395801, 0.06920467763959, 0.01628462406333, -0.25344790059353, 0.15558449135573, 0.02377945217615, 0.17520704835522, -0.14289799034253, -0.53174909058578, 0.58100494960553, 0.01818801111503, 0.02442357316099, -0.02505961724053, -0.05246019024463, -0.23313271880868, 0.38952639978999, 0.14728154134330, -0.20256413484477, -0.31863563325245, -0.51035327095184}, {-0.02217936801134, 0.04788665548180, -0.04060034127000, -0.11202315195388, -0.02459864859345, 0.14590772289388, -0.10214864179676, 0.04267842219415, -0.00275953611929, -0.42163034350696, 0.53648789255105, 0.04704409688120, 0.05477720428674, -0.18823009262115, -0.17556493366449, 0.15113130533216, 0.26408300200955, -0.04678328784242, -0.03424681017675, -0.43193942311114, -0.25049871956020} }; static const Float_t ABButter[9][multiple_of(16, 2 * BUTTER_ORDER + 1)] = { /* 5 4 3 2 1 */ {0.98621192462708, 0.97261396931306, -1.97242384925416, -1.97223372919527, 0.98621192462708},
void Scene::render_directional_shadowmap(DirectionalLight* dirlight) { Matrix4 light_mat,camera_mat; Vec3<float> ldir_norm = dirlight->get_direction(); ldir_norm.normalize(); // vectors defining the light-space plan to project points onto it (for pixel-size only transitions) Vec3<float> lsp_y(0,ldir_norm.z/ldir_norm.y,-1); lsp_y.normalize(); Vec3<float> lsp_x=ldir_norm.cross(lsp_y); lsp_x.normalize(); Vec3<float> eye_norm = eye_vector; eye_norm.normalize(); //shadowing depth and resolution calculations int cascaded_depth=8; float shadow_min_range = dirlight->get_shadow_min_range(), shadow_max_range=dirlight->get_shadow_max_range(); if(shadow_max_range==-1) shadow_max_range=scene_far; if(shadow_min_range==-1) shadow_min_range=scene_near; float shadow_range = shadow_max_range-shadow_min_range; //resolution at the nearest distance of the camera. Then go as planned float min_shadow_resolution = (log2(((shadow_min_range-scene_near)/SHADOWING_NEAR_RANGE)+1)+1)*SHADOWING_NEAR_RANGE; uniform_cascaded_shading_zdelta->set_value(min_shadow_resolution); float act_shadow_distance=0; for(int i=1;i<=SHADOWING_MAX_LAYERS;i++) { act_shadow_distance+=pow(2,i-1)*min_shadow_resolution; if(act_shadow_distance>shadow_range) { cascaded_depth=i; break; } } // calculation of the general projection matrix (global light) to calculate subfrustum borders Matrix4 global_light_projection; Vec3<float> global_cam_pos = camera_pos + eye_norm*(shadow_min_range+shadow_range*0.5); float gf = tanf(scene_fov_rad/2)*shadow_max_range; float vgf = gf / disp->get_ratio(); float global_radius = sqrt(gf*gf + vgf*vgf + (shadow_min_range+shadow_range)*(shadow_min_range+shadow_range)*0.25); camera_mat.camera((global_cam_pos-ldir_norm*scene_far),global_cam_pos,lsp_y); global_light_projection.perspective_ortho(global_radius*2,scene_near,scene_far*2,1); global_light_projection = global_light_projection * camera_mat; Matrix4 global_light_projection_inv = global_light_projection; global_light_projection_inv.invert(); // vector containing subfrustum positions near the camera (each iteration put their far pos into this for the next iteration) Vec3<float> subfrustum_near_position[4]; float sf = tanf(scene_fov_rad/2)*shadow_min_range; float vsf = sf / disp->get_ratio(); Vec3<float> mid_position = camera_pos + eye_norm*shadow_min_range; Vec3<float> third_eye_vector = eye_vector.cross(up_vector); third_eye_vector.normalize(); subfrustum_near_position[0] = mid_position + up_vector*vsf + third_eye_vector*sf; subfrustum_near_position[1] = mid_position + up_vector*vsf - third_eye_vector*sf; subfrustum_near_position[2] = mid_position - up_vector*vsf + third_eye_vector*sf; subfrustum_near_position[3] = mid_position - up_vector*vsf - third_eye_vector*sf; // projection of the 3D subfrustum positions into the 2D light-view plane. for(int i=0;i<4;i++) { subfrustum_near_position[i] = global_light_projection*subfrustum_near_position[i]; } for(int cascaded_layer=0;cascaded_layer<cascaded_depth;cascaded_layer++) { float zmax=(pow(2,cascaded_layer+1)-1)*min_shadow_resolution+shadow_min_range; if(cascaded_layer==SHADOWING_MAX_LAYERS-1) { zmax=shadow_max_range; } float f = tanf(scene_fov_rad/2)*zmax; float vf = f / disp->get_ratio(); // calculate the 4 zmax points and project them into 2D light_space Vec3<float> subfrustum_far_position[4]; Vec3<float> far_position = camera_pos + eye_norm*zmax; subfrustum_far_position[0] = far_position + up_vector*vf + third_eye_vector*f; subfrustum_far_position[1] = far_position + up_vector*vf - third_eye_vector*f; subfrustum_far_position[2] = far_position - up_vector*vf + third_eye_vector*f; subfrustum_far_position[3] = far_position - up_vector*vf - third_eye_vector*f; for(int i=0;i<4;i++) { subfrustum_far_position[i] = global_light_projection*subfrustum_far_position[i]; } float optimal_radius; Vec3<float> optimal_center = calculate_shadowing_optimal_point(subfrustum_near_position,subfrustum_far_position,optimal_radius); // multiply by global radius, as the optimal radius is only valid in the projected 2D space between 0 and 1 optimal_radius*=(global_radius); optimal_radius=multiple_of(optimal_radius,2); // switch near to far positions for the next layer for(int i=0;i<4;i++) { subfrustum_near_position[i] = subfrustum_far_position[i]; } float camera_height=DIRECTIONAL_LIGHT_HEIGHT; //clamping camera coordonnates to move depth_test pixel by depth_test pixel to avoid shadow glitters Vec3<float> cam_pos = global_light_projection_inv*optimal_center-ldir_norm*camera_height; // projection of the camera position onto the light-view plane float X=cam_pos.scalar(lsp_x), Y=cam_pos.scalar(lsp_y), Z=cam_pos.scalar(ldir_norm); X=multiple_of(X,optimal_radius/DEPTH_TEXTURE_SIZE); Y=multiple_of(Y,optimal_radius/DEPTH_TEXTURE_SIZE); cam_pos=lsp_x*X+lsp_y*Y+ldir_norm*Z; Vec3<float> cam_pointing_pos=cam_pos+ldir_norm*camera_height; // light-view transformation matrix calculation camera_mat.camera(cam_pos,cam_pointing_pos,lsp_x); light_mat.perspective_ortho(optimal_radius,scene_near,camera_height*2,1); light_mat = light_mat*camera_mat; // matrix send to pixel shader (phong) std::stringstream uniform_name; uniform_name<<"matrix"<<(cascaded_layer+1); uniform_light->set_value(light_mat,uniform_name.str()); fbo_shadows.attach_texture(dirlight->get_depth_texture(),FBO_DEPTH,cascaded_layer); if(fbo_shadows.iscomplete()) { fbo_shadows.bind(); uniform_light_projection->set_value(light_mat,"matrix"); frustum.orthogonal_frustum(cam_pos,ldir_norm,lsp_y,optimal_radius,1,camera_height*2); draw_scene(); fbo_shadows.unbind(); } else { Logger::log()<<"FBO incomplete for the render of directional light "<<std::endl; } } uniform_light_sampler->set_value(dirlight->get_depth_texture()); }