VoxelChunkCL::VoxelChunkCL(GLuint tex, GLuint btexture, Vector3 start, int id, float xOffset, float zOffset, module::Perlin perlinModule) { startOffset = start; //allocate memory for the blocks infoBlocksArray = new int[SIZE]; memset(infoBlocksArray, 0, SIZE); typeBlocksArray = new int[SIZE]; memset(typeBlocksArray, 0, SIZE); noiseArray = new float[SIZE_NOISE]; cube = Mesh::GenerateCube(); setTextureForCube(tex, btexture); this->id = id; arrayVertices = new Vector3[36]; arrayTexture = new Vector2[36]; arrayNormals = new Vector3[36]; arrayTangents = new Vector3[36]; gridSizeLog2[0] = 5; gridSizeLog2[1] = 5; gridSizeLog2[2] = 5; gridSizeLog2[3] = 0; gridSize[0] = 1 << gridSizeLog2[0]; gridSize[1] = 1 << gridSizeLog2[1]; gridSize[2] = 1 << gridSizeLog2[2]; gridSizeMask[0] = gridSize[0] - 1; gridSizeMask[1] = gridSize[1] - 1; gridSizeMask[2] = gridSize[2] - 1; gridSizeShift[0] = 0; gridSizeShift[1] = gridSizeLog2[0]; gridSizeShift[2] = gridSizeLog2[0] + gridSizeLog2[1]; maxVerts = gridSize[0] * gridSize[1] * 100; cube->BindBuffers(CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z * 36); setupOpenCL(zOffset, perlinModule); initializeMarchingCubes(); computeSurface(); }
void main(void *arg) { const color WHITE(1.0f); //----------------------------------------- const color Cs(i_color()); color diffuse_color(1.0f, 1.0f, 1.0f); scalar diffuse_weight = i_diffuse(); color specular_color(i_specularColor()); scalar specular_weight= 0.2f;//cosinePower scalar roughness = 0.0f; int specular_mode = 1;//[0,3] scalar glossiness = 1.0f; color reflection_color(i_reflectedColor()); scalar reflection_weight = 0.5f; color refraction_color(1.0f, 1.0f, 1.0f); scalar refraction_weight= 0.0f;//zero will lead a dark image scalar refraction_glossiness = 0.5f; scalar refraction_thickness= 2.0f;//surfaceThickness color translucency_color(i_transparency()); scalar translucency_weight = i_translucence(); scalar anisotropy = 1.0f; scalar rotation = 0.0f; scalar ior = 1.5f;//refractiveIndex bool fresnel_by_ior = eiTRUE; scalar fresnel_0_degree_refl = 0.2f; scalar fresnel_90_degree_refl = 1.0f; scalar fresnel_curve= 5.0f; bool is_metal = eiTRUE; int diffuse_samples = 8; int reflection_samples= 4; int refraction_samples= 4; scalar cutoff_threshold = LIQ_SCALAR_EPSILON; eiTag bump_shader = eiNULL_TAG; scalar bump_factor= 0.3f; if( liq_UserDefinedNormal() == 0 ) { i_normalCamera() = N; } //----------------------------------------- vector In = normalize( I ); normal Nn = normalize( i_normalCamera() ); normal Nf = ShadingNormal(Nn); vector V = -In; //----------------------------------------- // specular is the percentage of specular lighting // limit weights in range [0, 1] specular_weight = clamp(specular_weight, 0.0f, 1.0f); refraction_weight = clamp(refraction_weight, 0.0f, 1.0f); translucency_weight = clamp(translucency_weight, 0.0f, 1.0f); // automatically compute Kd, Ks, Kt in an energy conserving way diffuse_weight = clamp(diffuse_weight, 0.0f, 1.0f); reflection_weight = clamp(reflection_weight, 0.0f, 1.0f); // the energy balancing between reflection and refraction is // dominated by Fresnel law //color Kt(refraction_color * (spec * refr * (1.0f - trans)) * (is_metal?Cs:WHITE)); // this is a monolithic shader which also serves as shadow shader if (ray_type == EI_RAY_SHADOW) { main_shadow(arg, refraction_color * (specular_weight * refraction_weight * (1.0f - translucency_weight)), refraction_thickness, cutoff_threshold); return; } // for surface shader, we call bump shader eiTag shader = bump_shader; if (shader != eiNULL_TAG) { call_bump_shader(shader, bump_factor); } //color Kc(refraction_color * (spec * refr * trans) * (is_metal?Cs:WHITE)); // non-reflected energy is absorbed //color Ks(specular_color * spec * (1.0f - refl) * (is_metal?Cs:WHITE)); //color Kr(reflection_color * (spec * refl) * (is_metal?Cs:WHITE)); // surface color will impact specular for metal material //const color Cs(surface_color); // const color Kd( Cs *(1.0f - spec) * diff ); // const int spec_mode = clamp(specular_mode, 0, 3); computeSurface( i_color(),//outColor(),//out->Ci,// i_transparency(),//out->Oi,// i_matteOpacityMode(), i_matteOpacity(), o_outColor(),//out->Ci,// o_outTransparency()//out->Oi// ); out->Ci = o_outColor(); out->Oi = o_outTransparency(); // apply rotation scalar deg = rotation; if (deg != 0.0f) { dPdu = rotate_vector(dPdu, N, radians(deg)); dPdv = cross(dPdu, N); u_axis = normalize(dPdu); v_axis = normalize(dPdv); } // set the glossiness scale based on the chosen BSDF scalar glossiness_scale = 370.37f; if (specular_mode == 1) { glossiness_scale = 125.0f; } else if (specular_mode == 3) { // scale to make the same glossiness parameter // results in similar lobe for different BSDFs glossiness_scale = 22.88f; } // scalar aniso = anisotropy; int refl_samples = reflection_samples; int refr_samples = refraction_samples; // prepare outgoing direction in local frame const vector wo(normalize(to_local(V))); // construct BSDFs OrenNayar Rd(roughness); scalar shiny_u = glossiness; if (shiny_u < eiSCALAR_EPS) { shiny_u = eiSCALAR_EPS; refl_samples = 1; } shiny_u = max(0.0f, glossiness_scale / shiny_u); scalar shiny_v = max(0.0f, shiny_u * anisotropy); scalar IOR = ior; eiBool fresn_by_ior = fresnel_by_ior; scalar fresn_0_degree_refl = fresnel_0_degree_refl; scalar fresn_90_degree_refl = fresnel_90_degree_refl; scalar fresn_curve = fresnel_curve; union { eiByte by_ior[sizeof(FresnelByIOR)]; eiByte schlick[sizeof(FresnelSchlick)]; } F_storage; union { eiByte by_ior[sizeof(FresnelByIOR)]; eiByte schlick[sizeof(FresnelSchlick)]; } invF_storage; Fresnel *F = NULL; Fresnel *invF = NULL; if (fresn_by_ior) { F = new (F_storage.by_ior) FresnelByIOR(IOR); invF = new (invF_storage.by_ior) InvFresnelByIOR(IOR); } else { F = new (F_storage.schlick) FresnelSchlick( fresn_0_degree_refl, fresn_90_degree_refl, fresn_curve); invF = new (invF_storage.schlick) InvFresnelSchlick( fresn_0_degree_refl, fresn_90_degree_refl, fresn_curve); } union { eiByte ward[sizeof(Ward)]; eiByte phong[sizeof(StretchedPhong)]; eiByte blinn[sizeof(Blinn)]; eiByte cooktorrance[sizeof(CookTorrance)]; } Rs_storage; BSDF *Rs = NULL; switch (specular_mode) { case 0: Rs = new (Rs_storage.ward) Ward(F, shiny_u, shiny_v); break; case 1: Rs = new (Rs_storage.phong) StretchedPhong(F, shiny_u); break; case 2: Rs = new (Rs_storage.blinn) Blinn(F, shiny_u); break; case 3: Rs = new (Rs_storage.cooktorrance) CookTorrance(F, 1.0f / shiny_u); break; } SpecularReflection Rr(F); scalar refr_shiny_u = refraction_glossiness; if (refr_shiny_u < eiSCALAR_EPS) { refr_shiny_u = eiSCALAR_EPS; refr_samples = 1; } refr_shiny_u = max(0.0f, glossiness_scale / refr_shiny_u); scalar refr_shiny_v = max(0.0f, shiny_u * anisotropy); union { eiByte ward[sizeof(Ward)]; eiByte phong[sizeof(StretchedPhong)]; eiByte blinn[sizeof(Blinn)]; eiByte cooktorrance[sizeof(CookTorrance)]; } Rts_storage; BSDF *Rts = NULL; switch (specular_mode) { case 0: Rts = new (Rts_storage.ward) Ward(invF, refr_shiny_u, refr_shiny_v); break; case 1: Rts = new (Rts_storage.phong) StretchedPhong(invF, refr_shiny_u); break; case 2: Rts = new (Rts_storage.blinn) Blinn(invF, refr_shiny_u); break; case 3: Rts = new (Rts_storage.cooktorrance) CookTorrance(invF, 1.0f / refr_shiny_u); break; } scalar refr_thickness = refraction_thickness; // internal scale for refraction thickness, make it smaller BRDFtoBTDF Rt(Rts, IOR, refr_thickness * 0.1f, this); color Cdiffuse(0.0f); color Cspecular(0.0f); // don't integrate direct lighting if the ray hits the back face if (dot_nd < 0.0f) { // integrate direct lighting from the front side //out->Ci += integrate_direct_lighting(/*Kd*/diffuse(), Rd, wo); //out->Ci *= i_diffuse() * getDiffuse(Nf, eiFALSE, eiFALSE); Cdiffuse += i_diffuse() * getDiffuse(Nf, eiFALSE, eiFALSE); //out->Ci += integrate_direct_lighting(Ks, *Rs, wo); //out->Ci += i_specularColor() * getPhong (Nf, V, i_cosinePower(), eiFALSE, eiFALSE); Cspecular += i_specularColor() * getPhong (Nf, V, i_cosinePower(), eiFALSE, eiFALSE); } // integrate for translucency from the back side if (!almost_black( refraction_color * (specular_weight * refraction_weight * translucency_weight)*(is_metal?Cs:WHITE) ) && //almost_black(Kc) (refr_thickness > 0.0f || dot_nd > 0.0f)) { vector old_dPdu(dPdu); vector old_dPdv(dPdv); vector old_u_axis(u_axis); vector old_v_axis(v_axis); normal old_N(N); vector new_wo(wo); if (dot_nd < 0.0f) { dPdu = old_dPdv; dPdv = old_dPdu; u_axis = old_v_axis; v_axis = old_u_axis; N = -N; new_wo = vector(wo.y, wo.x, -wo.z); } // integrate direct lighting from the back side //out->Ci += Kc * integrate_direct_lighting(/*Kd*/i_diffuse(), Rd, new_wo); //out->Ci += i_diffuse() * getDiffuse(Nf, eiFALSE, eiFALSE); Cdiffuse += i_diffuse() * getDiffuse(Nf, eiFALSE, eiFALSE); //out->Ci += Kc * integrate_direct_lighting(Ks, *Rs, new_wo); //out->Ci += i_specularColor() * getPhong (Nf, V, i_cosinePower(), eiFALSE, eiFALSE); Cspecular += i_specularColor() * getPhong (Nf, V, i_cosinePower(), eiFALSE, eiFALSE); N = old_N; u_axis = old_u_axis; v_axis = old_v_axis; dPdu = old_dPdu; dPdv = old_dPdv; } scalar cutoff_thresh = cutoff_threshold; color CReflectSpecular(0.0f); // integrate indirect specular lighting if (!almost_black( specular_color * (specular_weight * (1.0f - reflection_weight))*(is_metal?Cs:WHITE) ) && dot_nd < 0.0f)//almost_black(Ks) { IntegrateOptions opt; opt.ray_type = EI_RAY_REFLECT_GLOSSY; opt.min_samples = opt.max_samples = refl_samples; opt.cutoff_threshold = cutoff_thresh; CReflectSpecular = integrate(wo, *Rs, opt); } color CSpecularReflection(0.0f); // integrate perfect specular reflection if (!almost_black(reflection_color * (specular_weight * reflection_weight) * (is_metal?Cs:WHITE)) && dot_nd < 0.0f)//almost_black(Kr) { IntegrateOptions opt; opt.ray_type = EI_RAY_REFLECT_GLOSSY; opt.min_samples = opt.max_samples = 1; // force one sample for reflection opt.cutoff_threshold = cutoff_thresh; // the direct lighting of this BRDF is not accounted, // so we trace lights here to compensate opt.trace_lights = eiTRUE; CSpecularReflection = integrate(wo, Rr, opt); } color CReflectDiffuse(0.0f); // integrate indirect diffuse lighting (color bleeding) if (!almost_black( Cs *(1.0f - specular_weight) * diffuse_weight ) && dot_nd < 0.0f)//almost_black(Kd) { IntegrateOptions opt; opt.ray_type = EI_RAY_REFLECT_DIFFUSE; opt.min_samples = opt.max_samples = diffuse_samples; opt.cutoff_threshold = cutoff_thresh; CReflectDiffuse = integrate(wo, Rd, opt); } color CRefraction(0.0f); // integrate refraction if ( !almost_black(refraction_color * specular_weight * refraction_weight * (1.0f-translucency_weight)*(is_metal?Cs:WHITE)) ) //almost_black(Kt) { IntegrateOptions opt; opt.ray_type = EI_RAY_REFRACT_GLOSSY; if (IOR == 1.0f) { opt.ray_type = EI_RAY_TRANSPARENT; } opt.min_samples = opt.max_samples = refr_samples; opt.cutoff_threshold = cutoff_thresh; // account for refractive caustics opt.trace_lights = eiTRUE; CRefraction = integrate(wo, Rt, opt); } out->Ci *= (Cdiffuse + CReflectDiffuse * Cs *(1.0f - specular_weight) * diffuse_weight//Kd ); out->Ci += (Cspecular + CReflectSpecular* specular_color * specular_weight * (1.0f - reflection_weight)*(is_metal?Cs:WHITE)//Ks + CSpecularReflection* reflection_color * specular_weight * reflection_weight * (is_metal?Cs:WHITE)//Kr + CRefraction* refraction_color * specular_weight * refraction_weight * (1.0f-translucency_weight) * (is_metal?Cs:WHITE)//Kt ); #ifdef USE_AOV_aov_ambient aov_ambient() += ( i_ambientColor() *(CReflectDiffuse * Cs *(1.0f - specular_weight) * diffuse_weight//Kd ) ) * (1.0f - o_outTransparency()); #endif #ifdef USE_AOV_aov_diffuse aov_diffuse() += ( Cdiffuse * i_color() ) * (1.0f - o_outTransparency()); #endif #ifdef USE_AOV_aov_specular aov_specular() += (Cspecular + CReflectSpecular* specular_color * specular_weight * (1.0f - reflection_weight)*(is_metal?Cs:WHITE)//Ks + CSpecularReflection* reflection_color * specular_weight * reflection_weight * (is_metal?Cs:WHITE)//Kr + CRefraction* refraction_color * specular_weight * refraction_weight * (1.0f-translucency_weight) * (is_metal?Cs:WHITE)//Kt ); #endif if ( ! less_than( &i_transparency(), LIQ_SCALAR_EPSILON ) ) {//transparent out->Ci = out->Ci * ( 1.0f - i_transparency() ) + trace_transparent() * i_transparency(); }//else{ opacity } Rs->~BSDF(); Rts->~BSDF(); }