void LinearFilter::SeparableKernelConvolution() { Mat src = original_image_; Mat dst = processed_image_; Mat temp = src.clone(); float sum, x1, y1; for (int y = 0; y < src.rows; y++) { for (int x = 0; x < src.cols; x++) { sum = 0.0; for (int i = -radius_ / 2; i <= radius_ / 2; i++) { y1 = Reflect(src.rows, y - i); sum = sum + separable_kernel_x_[i + radius_ / 2] * src.at<uchar>(y1, x); } sum = (sum > 255) ? 255 : sum; sum = (sum < 0) ? 0 : sum; temp.at<uchar>(y, x) = sum; } } // along x - direction for (int y = 0; y < src.rows; y++) { for (int x = 0; x < src.cols; x++) { sum = 0.0; for (int i = -radius_ / 2; i <= radius_ / 2; i++) { x1 = Reflect(src.cols, x - i); sum = sum + separable_kernel_x_[i + radius_ / 2] * temp.at<uchar>(y, x1); } sum = (sum > 255) ? 255 : sum; sum = (sum < 0) ? 0 : sum; dst.at<uchar>(y, x) = sum; } } }
void CFruchtermanReingold::CalculateForces() { //Coulomb force for(int i = 0; i < vgc_nodes_num; ++i) { QVector2D force; if(!vgc_graph->vertice_exists(i)) continue; for(int j = 0; j < vgc_nodes_num; ++j) { if(i == j || !vgc_graph->vertice_exists(j)) continue; force += CoulombForce(i, j); } vgc_vertices[i].v_force = force; } //Hooke force for(int i = 0; i < vgc_nodes_num; ++i) { QVector2D force; for(int j = 0; j < vgc_nodes_num; ++j) { if(i == j) continue; else if(vgc_graph->edge_exists(i, j)) force += HookeForce(i, j); //TODO: oriented graph? } vgc_vertices[i].v_force += force; } //Barrier force for(int i = 0; i < vgc_nodes_num; ++i) Reflect(i); }
void TMercury::OnHit(value_type x1, state_type y1, value_type &x2, state_type &y2, int &polarisation, const double normal[3], solid *leaving, solid *entering, bool &trajectoryaltered, bool &traversed){ trajectoryaltered = false; traversed = true; Reflect(x1, y1, x2, y2, polarisation, normal, leaving, entering, trajectoryaltered, traversed); }
int Trace(float *p, float *v, int k) { if( k > MAX_STEPS ) { return -1; } int con = Intersect(p, v); if(con == -1) { return con; //nothing } else if(con < -1) { return con; //light source } else if (con >=0 ) // is object { Normal(con); Reflect(v); Phong(v, con); for(int i=0; i<3; i++) { color[i] += inters_c[i];; } Trace (inter, ref, k+1); } return 0; }
int Piano::Impact(Vettore *Pos,Vettore *Vel) { double Dist = Distance(Pos); if(Dist > Rad) return 0; Vettore PosS = ProjOnSurf(Pos); if(!IsOnSurf(&PosS)) return 0; Vettore Vel1 = Reflect(Vel); Vel->Copy(&Vel1); }
void CMA::GenCandidate() { myRandMVN.Factor(Cov); Az=myRandMVN.Rand(); for (int i=0;i<dim;i++) { NewState.x[i]=Reflect(BestState->x[i]+sigma*Az[i],pFunc->xmin[i],pFunc->xmax[i]); } }
virtual void particleCollision(const ParticleGeometryCollisionEventUnrecPtr ColE) { ParticleSystemRefPtr TheSystem= ColE->getSystem(); UInt32 ParticleIndex(ColE->getParticleIndex()); Vec3f Reflect(TheSystem->getVelocity(ParticleIndex).reflect(ColE->getHitNormal())); TheSystem->setVelocity(Reflect, ParticleIndex); TheSystem->setPosition(ColE->getHitPoint() + (0.00001f*Reflect), ParticleIndex); }
Spectrum MicrofacetReflection::Sample_f(const Vector3f &wo, Vector3f *wi, const Point2f &u, Float *pdf, BxDFType *sampledType) const { // Sample microfacet orientation $\wh$ and reflected direction $\wi$ Vector3f wh = distribution->Sample_wh(wo, u); *wi = Reflect(wo, wh); if (!SameHemisphere(wo, *wi)) return Spectrum(0.f); // Compute PDF of _wi_ for microfacet reflection *pdf = distribution->Pdf(wo, wh) / (4 * Dot(wo, wh)); return f(wo, *wi); }
Point3D RayScene::GetColor(Ray3D ray,int rDepth,Point3D cLimit){ if (rDepth == 0) {return Point3D();} if (cLimit[0] > 1 && cLimit[1] > 1 && cLimit[2] > 1) {return Point3D();} RayIntersectionInfo iInfo; double resp = group->intersect(ray, iInfo, -1); if (resp > 0) { //Calculating reflection values. Point3D reflectedDirection = Reflect(ray.direction, iInfo.normal); Ray3D reflectedRay(iInfo.iCoordinate+(reflectedDirection.unit()*0.0001), reflectedDirection.unit()); //Calculating and finding the refraction values. Point3D refractedDirection; double refIndex; if (ray.direction.dot(iInfo.normal) < 0) { refIndex = iInfo.material->refind; } else { refIndex = 1/iInfo.material->refind; } int refract = Refract(ray.direction.unit(), iInfo.normal.unit(), refIndex, refractedDirection); Ray3D refractedRay(iInfo.iCoordinate+(refractedDirection.unit()*0.0001), refractedDirection.unit()); Point3D refractedTransparency = iInfo.material->transparent; //Calculating light contributions to the point. Point3D diffuse = Point3D(0,0,0); Point3D specular = Point3D(0,0,0); for (int i=0; i<lightNum; i++) { RayIntersectionInfo iInfo2 = iInfo; int iSectCount = 0; Point3D diffuseResp = lights[i]->getDiffuse(camera->position, iInfo2); Point3D specularResp = lights[i]->getSpecular(camera->position, iInfo2); Point3D transparency = lights[i]->transparency(iInfo2, group, cLimit); diffuse += diffuseResp*transparency; specular += specularResp*transparency; } Point3D response = iInfo.material->ambient*ambient+iInfo.material->emissive + diffuse + specular + GetColor(reflectedRay, rDepth-1, cLimit/iInfo.material->specular) + GetColor(refractedRay, rDepth-1, cLimit/iInfo.material->specular)*refractedTransparency; for (int i = 0; i <3; i++) { if (response[i] < 0) { response[i] = 0; } if (response[i] > 1) { response[i] = 1; } } return response; } else if (ray.position[0]==camera->position[0] && ray.position[1]==camera->position[1] && ray.position[2]==camera->position[2]) { return background; } else return Point3D(); }
void LinearFilter::ReflectedIndexingConvolution() { //Mat src = original_image_; //Mat dst = processed_image_; float sum, x1, y1; for (int y = 0; y < original_image_.rows; y++){ for (int x = 0; x < original_image_.cols; x++){ sum = 0.0; for (int k = -radius_ / 2; k <= radius_ / 2; k++){ for (int j = -radius_ / 2; j <= radius_ / 2; j++){ x1 = Reflect(original_image_.cols, x - j); y1 = Reflect(original_image_.rows, y - k); sum = sum + kernel_[j + radius_ / 2][k + radius_ / 2] * original_image_.at<uchar>(y1, x1); } } sum = (sum > 255) ? 255 : sum; sum = (sum < 0) ? 0 : sum; processed_image_.at<uchar>(y, x) = sum; } } }
PixelShader::PixelShader(wstring path) { HRESULT result; auto shaderBuffer = Tools::ReadFileToVector(path); result = GetD3D11Device()->CreatePixelShader(&shaderBuffer[0], shaderBuffer.size(), nullptr, &m_Shader); Assert(result == S_OK); auto extensionIndex = path.find_last_of('.'); auto metadataBuffer = Tools::ReadFileToVector(path.substr(0, extensionIndex + 1) + L"shadermetadata"); Reflect(shaderBuffer, metadataBuffer); }
bool DielectricMaterial::Scatter( const Ray& ray, const HitRecord& record, Vector3& attenuation, Ray& scattered ) const { attenuation = Vector3( 1.f, 1.f, 1.f ); const float dirDotNormal = ray.Direction().Dot( record.normal ); Vector3 outwardNormal; float niOverNt; float cosine; if ( dirDotNormal > 0.f ) { outwardNormal = -record.normal; niOverNt = refractionIndex; //cosine = refractionIndex * dirDotNormal / ray.Direction().Length(); cosine = dirDotNormal / ray.Direction().Length(); cosine = std::sqrt( 1.f - refractionIndex * refractionIndex * ( 1.f - cosine * cosine ) ); } else { outwardNormal = record.normal; niOverNt = 1.f / refractionIndex; cosine = -dirDotNormal / ray.Direction().Length(); } const Vector3 reflected = Reflect( ray.Direction(), record.normal ); float reflectProbability; Vector3 refracted; if ( Refract( ray.Direction(), outwardNormal, niOverNt, refracted ) ) { reflectProbability = Schlick( cosine, refractionIndex ); } else { reflectProbability = 1.f; } if ( Rand01() < reflectProbability ) { scattered = Ray( record.point, reflected ); } else { scattered = Ray( record.point, refracted ); } return true; }
void Phong::Evaluate(const Vector3 &wi, const Vector3 &normal, const Vector3 &wo, const Vector2 st, Vector3 &contrib, Float &cosWo, Float &pdf, Float &revPdf) const { contrib.setZero(); pdf = Float(0.0); revPdf = Float(0.0); Float cosWi = Dot(normal, wi); Vector3 normal_ = normal; if (twoSided && cosWi < Float(0.0)) { cosWi = -cosWi; normal_ = -normal_; } cosWo = Dot(normal_, wo); if (cosWi <= c_CosEpsilon || cosWo <= c_CosEpsilon) { return; } if (KsWeight > Float(0.0)) { const Float alpha = fmax(Dot(Reflect(wi, normal_), wo), Float(0.0)); const Float expo = exponent->Eval(st)[0]; const Float weight = pow(alpha, expo) * c_INVTWOPI; const Float expoConst1 = (expo + Float(1.0)); const Float expoConst2 = (expo + Float(2.0)); if (weight > Float(1e-10)) { contrib = Ks->Eval(st) * (expoConst2 * weight); pdf = KsWeight * expoConst1 * weight; revPdf = pdf; } } if (KsWeight < Float(1.0)) { pdf += (Float(1.0) - KsWeight) * cosWo * c_INVPI; revPdf += (Float(1.0) - KsWeight) * cosWi * c_INVPI; contrib += Kd->Eval(st) * c_INVPI; } contrib *= cosWo; // Just for numerical stability if (contrib.maxCoeff() < Float(1e-10)) { contrib.setZero(); } }
Spectrum FresnelBlend::Sample_f(const Vector3f &wo, Vector3f *wi, const Point2f &uOrig, Float *pdf, BxDFType *sampledType) const { Point2f u = uOrig; if (u[0] < .5) { u[0] = 2 * u[0]; // Cosine-sample the hemisphere, flipping the direction if necessary *wi = CosineSampleHemisphere(u); if (wo.z < 0) wi->z *= -1; } else { u[0] = 2 * (u[0] - .5f); // Sample microfacet orientation $\wh$ and reflected direction $\wi$ Vector3f wh = distribution->Sample_wh(wo, u); *wi = Reflect(wo, wh); if (!SameHemisphere(wo, *wi)) return Spectrum(0.f); } *pdf = Pdf(wo, *wi); return f(wo, *wi); }
void particleCollision(ParticleGeometryCollisionEventDetails* const details) { ParticleSystemRefPtr TheSystem= details->getSystem(); UInt32 ParticleIndex(details->getParticleIndex()); Real32 phi= osgACos((-TheSystem->getVelocity(ParticleIndex).dot(details->getHitNormal()))/(TheSystem->getVelocity(ParticleIndex).length()*details->getHitNormal().length())); if( phi < osgDegree2Rad(80.0) ) { TheSystem->killParticle(ParticleIndex); } else { //Reflect the Particle Vec3f Reflect(TheSystem->getVelocity(ParticleIndex).reflect(details->getHitNormal())); TheSystem->setVelocity(Reflect, ParticleIndex); TheSystem->setPosition(details->getHitPoint() + (0.00001f*Reflect), ParticleIndex); } }
double Optimizer::dX_CauchyReflect(double x,double xmin,double xmax,double T) { double u=Rand::RandDouble(); double y=MathUtils::Sgn(u-0.5)*T*( pow(1.0+1.0/T,fabs(2.0*u-1.0))); return Reflect(x+y*(xmax-xmin),xmin,xmax);; }
void genSurfFile(Element *E, double *x, double *y, double *z, Curve *curve) { register int i; int info,l,lm[4]; int q1 = E->qa, q2 = E->qb,*vertid,fid, cnt, cnt1; int ntot = Snp*(Snp+1)/2; double **sj; static int *chkfid; if(!chkfid) { chkfid = ivector(0,Snface-1); izero(Snface,chkfid,1); } vertid = curve->info.file.vert; if(E->identify() == Nek_Prism) q2 = E->qc; // find face id; cnt = vertid[0] + vertid[1] + vertid[2]; for(i = 0; i < Snface; ++i) if(surfids[i][3] == cnt) { // just search vertices with same vertex id sum if(vertid[0] == surfids[i][0]) { if((vertid[1] == surfids[i][1])||(vertid[1] == surfids[i][2])) { chkfid[i]++; if(chkfid[i] > 1) // check form mutiple calls to same face fprintf(stderr,"gensurfFile: Error face %d is being " "operated on multiple times\n",i); if(vertid[1] == surfids[i][2]) { Rotate(i,1); Reflect(i); } fid = i; break; } } else if(vertid[0] == surfids[i][1]) { if((vertid[1] == surfids[i][0])||(vertid[1] == surfids[i][2])) { chkfid[i]++; if(chkfid[i] > 1) // check form mutiple calls to same face fprintf(stderr,"gensurfFile: Error face %d is being " "operated on multiple times\n",i); if(vertid[1] == surfids[i][0]) Reflect(i); else Rotate(i,2); fid = i; break; } } else if(vertid[0] == surfids[i][2]) { if((vertid[1] == surfids[i][0])||(vertid[1] == surfids[i][1])) { chkfid[i]++; if(chkfid[i] > 1) // check form mutiple calls to same face fprintf(stderr,"gensurfFile: Error face %d is being " "operated on multiple times\n",i); if(vertid[1] == surfids[i][1]) { Rotate(i,2); Reflect(i); } else Rotate(i,1); // set up to rotate Feisal to Nektar fid = i; break; } } } // invert basis dgetrs('T', ntot, 1, *CollMat,ntot,CollMatIpiv,SurXpts[fid],ntot,info); if(info) fprintf(stderr,"Trouble solve collocation X matrix\n"); dgetrs('T', ntot, 1, *CollMat,ntot,CollMatIpiv,SurYpts[fid],ntot,info); if(info) fprintf(stderr,"Trouble solve collocation Y matrix\n"); dgetrs('T', ntot, 1, *CollMat,ntot,CollMatIpiv,SurZpts[fid],ntot,info); if(info) fprintf(stderr,"Trouble solve collocation Z matrix\n"); // Take out require modes and Backward transformation // base it on LGmax at present although might cause problems with // trijbwd if LGmax > qa sj = dmatrix(0,2,0,LGmax*(LGmax+1)/2-1); dzero(3*LGmax*(LGmax+1)/2,sj[0],1); lm[0] = LGmax-2; lm[1] = LGmax-2; lm[2] = LGmax-2; lm[3] = max(LGmax-3,0); dcopy(3,SurXpts[fid],1,sj[0],1); dcopy(3,SurYpts[fid],1,sj[1],1); dcopy(3,SurZpts[fid],1,sj[2],1); cnt = cnt1 = 3; for(i=0;i<3;++i) { l = lm[i]; dcopy(min(Snp-2,l), SurXpts[fid]+cnt,1,sj[0]+cnt1,1); dcopy(min(Snp-2,l), SurYpts[fid]+cnt,1,sj[1]+cnt1,1); dcopy(min(Snp-2,l), SurZpts[fid]+cnt,1,sj[2]+cnt1,1); cnt += Snp-2; cnt1 += l; } l = lm[3]; for(i=0;i<l;++i) { dcopy(min(Snp-3,l)-i,SurXpts[fid]+cnt,1,sj[0]+cnt1,1); dcopy(min(Snp-3,l)-i,SurYpts[fid]+cnt,1,sj[1]+cnt1,1); dcopy(min(Snp-3,l)-i,SurZpts[fid]+cnt,1,sj[2]+cnt1,1); cnt += Snp-3-i; cnt1 += l-i; } JbwdTri(q1,q2,LGmax,lm,sj[0],x); JbwdTri(q1,q2,LGmax,lm,sj[1],y); JbwdTri(q1,q2,LGmax,lm,sj[2],z); free_dmatrix(sj,0,0); }
void SamplePhong(const bool adjoint, const ADFloat *buffer, const ADVector3 &wi, const ADVector3 &normal, const ADVector2 st, const ADVector2 rndParam, const ADFloat uDiscrete, const bool fixDiscrete, ADVector3 &wo, ADVector3 &contrib, ADFloat &cosWo, ADFloat &pdf, ADFloat &revPdf) { ADVector3 Kd; ADVector3 Ks; ADFloat exponent; ADFloat KsWeight; buffer = Deserialize(buffer, Kd); buffer = Deserialize(buffer, Ks); buffer = Deserialize(buffer, exponent); buffer = Deserialize(buffer, KsWeight); ADFloat cosWi = Dot(normal, wi); std::vector<CondExprCPtr> ret = CreateCondExprVec(4); BeginIf(Gt(cosWi, Float(0.0)), ret); { SetCondOutput({normal[0], normal[1], normal[2], cosWi}); } BeginElse(); { SetCondOutput({-normal[0], -normal[1], -normal[2], -cosWi}); } EndIf(); ADVector3 normal_(ret[0], ret[1], ret[2]); cosWi = ret[3]; ADVector3 R = Reflect(wi, normal_); ret = CreateCondExprVec(4); BeginIf(Gt(uDiscrete, KsWeight), ret); { ADVector3 localDir = SampleCosHemisphere(rndParam); ADVector3 b0; ADVector3 b1; CoordinateSystem(normal_, b0, b1); ADVector3 wo = localDir[0] * b0 + localDir[1] * b1 + localDir[2] * normal_; ADFloat factor = (Float(1.0) - KsWeight); SetCondOutput({wo[0], wo[1], wo[2], factor}); } BeginElse(); { ADFloat power = Float(1.0) / (exponent + Float(1.0)); ADFloat cosAlpha = pow(rndParam[1], power); // Ugly hack to avoid sqrt(0) which has undefined derivatives... ADFloat sinAlpha = sqrt(fmax(Float(1.0) - square(cosAlpha), Float(1e-6))); ADFloat phi = c_TWOPI * rndParam[0]; ADVector3 localDir = ADVector3(sinAlpha * cos(phi), sinAlpha * sin(phi), cosAlpha); ADVector3 b0; ADVector3 b1; CoordinateSystem(R, b0, b1); ADVector3 wo = localDir[0] * b0 + localDir[1] * b1 + localDir[2] * R; ADFloat factor = KsWeight; SetCondOutput({wo[0], wo[1], wo[2], factor}); } EndIf(); wo = ADVector3(ret[0], ret[1], ret[2]); ADFloat factor = ret[3]; cosWo = Dot(normal_, wo); BeginIf(Gt(KsWeight, Float(0.0)), ret); { ADFloat alpha = fmax(Dot(Reflect(wi, normal_), wo), Float(0.0)); ADFloat weight = pow(alpha, exponent) * c_INVTWOPI; ADFloat expoConst1 = (exponent + Float(1.0)); ADFloat expoConst2 = (exponent + Float(2.0)); std::vector<CondExprCPtr> ret = CreateCondExprVec(4); BeginIf(Gt(weight, Float(1e-10)), ret); { ADVector3 specContrib = Ks * (expoConst2 * weight); ADFloat specPdf = KsWeight * expoConst1 * weight; SetCondOutput({specContrib[0], specContrib[1], specContrib[2], specPdf}); } BeginElse(); { SetCondOutput({Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0)}); } EndIf(); ADVector3 specContrib = ADVector3(ret[0], ret[1], ret[2]); ADFloat specPdf = ret[3]; SetCondOutput({specContrib[0], specContrib[1], specContrib[2], specPdf}); } BeginElse(); { SetCondOutput( {Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0)}); } EndIf(); contrib[0] = ret[0]; contrib[1] = ret[1]; contrib[2] = ret[2]; pdf = ret[3]; revPdf = ret[3]; ret = CreateCondExprVec(5); BeginIf(Lt(KsWeight, Float(1.0)), ret); { ADVector3 diffContrib = Kd * Const<ADFloat>(c_INVPI); ADFloat tmp = (Float(1.0) - KsWeight) * c_INVPI; ADFloat diffPdf = tmp * cosWo; ADFloat revDiffPdf = tmp * cosWi; SetCondOutput({diffContrib[0], diffContrib[1], diffContrib[2], diffPdf, revDiffPdf}); } BeginElse(); { SetCondOutput({Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0)}); } EndIf(); contrib[0] += ret[0]; contrib[1] += ret[1]; contrib[2] += ret[2]; pdf += ret[3]; revPdf += ret[4]; contrib *= cosWo; contrib *= inverse(pdf); if (fixDiscrete) { contrib *= factor; } }
bool Phong::Sample(const Vector3 &wi, const Vector3 &normal, const Vector2 st, const Vector2 rndParam, const Float uDiscrete, Vector3 &wo, Vector3 &contrib, Float &cosWo, Float &pdf, Float &revPdf) const { Float cosWi = Dot(wi, normal); if (fabs(cosWi) < c_CosEpsilon) { return false; } Vector3 normal_ = normal; if (cosWi < Float(0.0)) { if (twoSided) { cosWi = -cosWi; normal_ = -normal_; } else { return false; } } const Float expo = exponent->Eval(st)[0]; const Vector3 R = Reflect(wi, normal_); Float g; Vector3 n; if (uDiscrete > KsWeight) { g = Float(1.0); n = normal_; } else { g = expo; n = R; } const Float power = Float(1.0) / (g + Float(1.0)); const Float cosAlpha = pow(rndParam[1], power); assert(cosAlpha >= Float(0.0) && cosAlpha <= Float(1.0)); const Float sinAlpha = sqrt(Float(1.0) - square(cosAlpha)); const Float phi = c_TWOPI * rndParam[0]; const Vector3 localDir = Vector3(sinAlpha * cos(phi), sinAlpha * sin(phi), cosAlpha); Vector3 b0; Vector3 b1; CoordinateSystem(n, b0, b1); wo = localDir[0] * b0 + localDir[1] * b1 + localDir[2] * n; cosWo = Dot(normal_, wo); if (cosWo < c_CosEpsilon) { return false; } contrib = Vector3::Zero(); pdf = Float(0.0); if (KsWeight > Float(0.0)) { const Float alpha = fmax(Dot(R, wo), Float(0.0)); const Float weight = pow(alpha, expo) * c_INVTWOPI; const Float expoConst1 = (expo + Float(1.0)); const Float expoConst2 = (expo + Float(2.0)); if (weight > Float(1e-10)) { contrib = Ks->Eval(st) * (expoConst2 * weight); pdf = KsWeight * expoConst1 * weight; } // Phong lobe sampling is symmetric revPdf = pdf; } if (KsWeight < Float(1.0)) { contrib += Kd->Eval(st) * c_INVPI; pdf += (Float(1.0) - KsWeight) * cosWo * c_INVPI; // reverse cosine hemisphere sampling is not symmetric revPdf += (Float(1.0) - KsWeight) * cosWi * c_INVPI; } contrib *= cosWo; if (pdf < Float(1e-10)) { return false; } assert(pdf > Float(0.0)); contrib *= inverse(pdf); return true; }
lean::com_ptr<const beCore::ReflectedComponent, lean::critical_ref> MeshCompound<Material>::GetReflectedComponent(uint4 idx) const { LEAN_ASSERT(idx < m_meshes.size()); return Reflect(m_materials[idx]); }
void display(void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Axes(); glColor3f (1.0, 0.0, 0.0); // Set the color to RED Draw(initial); glFlush(); matrixSetIdentity(theMatrix); //change begins switch(choice) { case 1: printf("Enter Translation along X, Y & Z\n=>"); scanf("%f%f%f",&TransDistX , &TransDistY , &TransDistZ); break; case 2: printf("Enter Scaling ratios along X, Y & Z\n=>"); scanf("%f%f%f",&ScaleX , &ScaleY , &ScaleZ); break; case 3: printf("Enter your choice for Rotation about axis:\n"); printf("1.parallel to X-axis.(y=B & z=C)\n2.parallel to Y-axis.(x=A & z=C)\n"); printf("3.parallel to Z-axis.(x=A & y=B)\n"); printf("4.Arbitrary line passing through (x1,y1,z1) & (x2,y2,z2)\n =>"); //Realign above line while execution scanf("%d",&choiceRot); switch(choiceRot) { case 1: printf("Enter B & C: "); scanf("%f %f",&B,&C); printf("Enter Rot. Angle Alpha: "); scanf("%f",&Alpha); break; case 2: printf("Enter A & C: "); scanf("%f %f",&A,&C); printf("Enter Rot. Angle Beta: "); scanf("%f",&Beta); break; case 3: printf("Enter A & B: "); scanf("%f %f",&A,&B); printf("Enter Rot. Angle degrees: "); scanf("%f",&Gamma); break; case 4: printf("Enter values of x1 ,y1 & z1:\n"); scanf("%f %f %f",&x1,&y11,&z1); printf("Enter values of x2 ,y2 & z2:\n"); scanf("%f %f %f",&x2,&y2,&z2); printf("Enter Rot. Angle Theta: "); scanf("%f",&Theta); break; } break; case 4: printf("Enter your choice for reflection about plane:\n1.X-Y\n2.Y-Z\n3.X-Z\n=>"); scanf("%d",&choiceRef); break; default: printf("Please enter a valid choice!!!\n"); //return 0; } //chenge ends switch(choice) { case 1: Translate(TransDistX , TransDistY ,TransDistZ); break; case 2: Scale(ScaleX, ScaleY, ScaleZ); break; case 3: switch(choiceRot) { case 1: DrawRotLine(); Translate(0,-B,-C); RotateX(Alpha); Translate(0,B,C); break; case 2: DrawRotLine(); Translate(-A,0,-C); RotateY(Beta); Translate(A,0,C); break; case 3: DrawRotLine(); Translate(-A,-B,0); RotateZ(Gamma); Translate(A,B,0); break; case 4: DrawRotLine(); float MOD =sqrt((x2-x1)*(x2-x1) + (y2-y11)*(y2-y11) + (z2-z1)*(z2-z1)); aa = (x2-x1)/MOD; bb = (y2-y11)/MOD; cc = (z2-z1)/MOD; Translate(-x1,-y11,-z1); float ThetaDash; ThetaDash = 1260*atan(bb/cc)/22; RotateX(ThetaDash); RotateY(1260*asin(-aa)/22); RotateZ(Theta); RotateY(1260*asin(aa)/22); RotateX(-ThetaDash); Translate(x1,y11,z1); break; } break; case 4: Reflect(); break; } //glFlush(); TransformPoints(); Draw(ptsFin); memcpy(initial, ptsFin, sizeof(ptsFin)); glFlush(); scanf("%d",&choice); glutPostRedisplay(); }
void Scene::BuildCornellBoxScene(std::vector<Shape*> &objects) { camera = new Camera( Point(-260, 0, 90), Point(-199, 0, 90) ); float lightEnergy = 35e8; lights.push_back(new SphereLight(Point(0, 0, 160), 3, Color(lightEnergy,lightEnergy,lightEnergy),1)); Color walls(0.1f,0.1f,0.1f); Material* white = new Material(Ambient(0.3,0.3,0.3),Diffuse(0.9, 0.9, 0.9),walls); AddObject("files\\cornell_box\\cornell_box_floor.3ds", new Material(Color(0.3,0.3,0.3),Color(0.9,0.9,0.9),walls, Reflect(0.2, 0.2, 0.2)), objects); AddObject("files\\cornell_box\\cornell_box_ceil.3ds", white, objects); AddObject("files\\cornell_box\\cornell_box_right_wall.3ds", new Material(Color(0.,0.1,0.),Color(0.4,1,0.4),walls), objects); AddObject("files\\cornell_box\\cornell_box_back.3ds", white, objects); AddObject("files\\cornell_box\\cornell_box_left_wall.3ds", new Material(Color(0.1,0.,0.),Color(1,0.4,0.4),walls), objects); AddObject("files\\cornell_box\\cornell_box_box1.3ds", white, objects); AddObject("files\\cornell_box\\cornell_box_box2.3ds", white, objects); objects.push_back(new Sphere(Point(-35,-45,20),15,new Material(Ambient(0,0,0),Diffuse(0,0,0),Specular(0,0,0), Reflect(0,0,0), Refract(1,1,1), 1.51, 1), Shape::GetUniqueID())); objects.push_back(new Sphere(Point(-50,50,15),15,new Material(Ambient(0,0,0),Diffuse(0,0,0),Specular(0,0,0), Reflect(1,1,1)), Shape::GetUniqueID() )); }
Point3D RayScene::GetColor(Ray3D ray,int rDepth,Point3D cLimit) { Point3D color; RayIntersectionInfo info; Ray3D reflection; Ray3D refraction; Ray3D reflectedRay; Point3D reflect, reflColor, refractColor; int shad; int numCrossed = 0; double dist; dist = group -> intersect(ray, info, -1); if (dist == -1) return background; color = ambient*info.material -> ambient + info.material -> emissive; if (ray.direction.dot(info.normal) < 0) { for (int j = 0; j < this->lightNum; j++) { shad = this->lights[j]->isInShadow(info, this->group, numCrossed); Point3D ts = lights[j]->transparency(info, group, cLimit); color += this->lights[j]->getDiffuse(ray.position, info) * ts; color += this->lights[j]->getSpecular(ray.position, info) * ts; } reflect = Reflect(ray.direction, info.normal); // Reflected ray reflectedRay = Ray3D(info.iCoordinate, reflect); reflectedRay.position = reflectedRay(0.0001); if (rDepth > 0 && (info.material->specular[0] > cLimit[0]) && (info.material->specular[1] > cLimit[1]) && (info.material->specular[2] > cLimit[2])) { reflColor = GetColor(reflectedRay, rDepth - 1, (cLimit / info.material->specular)); if (reflColor.p[0] == background.p[0] && reflColor.p[1] == background.p[1] && reflColor.p[2] == background.p[2]) reflColor = Point3D(); // reflected color added reflColor *= info.material -> specular; color += reflColor; } } // refraction if (this -> Refract(ray.direction, info.normal, info.material->refind, refraction.direction)) { refraction.position = info.iCoordinate; refraction.position = refraction(0.0001); refractColor = this -> GetColor(refraction, rDepth - 1, cLimit); if (refractColor.p[0] == background.p[0] && refractColor.p[1] == background.p[1] && refractColor.p[2] == background.p[2]) refractColor = Point3D(); refractColor *= info.material -> transparent; color += refractColor; } for (int i = 0; i < 3; i++) { if (color.p[i] > 1) color.p[i] = 1; } for (int i = 0; i < 3; i++) { if (color.p[i] < 0) color.p[i] = 0; } return color; }
// pertube a single component with reflection double Optimizer::dX_NormalReflect(double x,double xmin,double xmax,double stepsize) { double sigma=stepsize*(xmax-xmin); return Reflect(x+sigma*Rand::RandNorm(),xmin,xmax); }
void EvaluatePhong(const bool adjoint, const ADFloat *buffer, const ADVector3 &wi, const ADVector3 &normal, const ADVector3 &wo, const ADVector2 st, ADVector3 &contrib, ADFloat &cosWo, ADFloat &pdf, ADFloat &revPdf) { ADVector3 Kd; ADVector3 Ks; ADFloat exponent; ADFloat KsWeight; buffer = Deserialize(buffer, Kd); buffer = Deserialize(buffer, Ks); buffer = Deserialize(buffer, exponent); buffer = Deserialize(buffer, KsWeight); ADFloat cosWi = Dot(normal, wi); std::vector<CondExprCPtr> ret = CreateCondExprVec(4); BeginIf(Gt(cosWi, Float(0.0)), ret); { SetCondOutput({normal[0], normal[1], normal[2], cosWi}); } BeginElse(); { SetCondOutput({-normal[0], -normal[1], -normal[2], -cosWi}); } EndIf(); ADVector3 normal_(ret[0], ret[1], ret[2]); cosWi = ret[3]; cosWo = Dot(normal_, wo); ret = CreateCondExprVec(4); BeginIf(Gt(KsWeight, Float(0.0)), ret); { ADFloat alpha = Dot(Reflect(wi, normal_), wo); ADFloat weight = pow(alpha, exponent) * c_INVTWOPI; std::vector<CondExprCPtr> ret = CreateCondExprVec(4); BeginIf(Gt(weight, Float(1e-10)), ret); { ADFloat expoConst1 = (exponent + Float(1.0)); ADFloat expoConst2 = (exponent + Float(2.0)); ADVector3 specContrib = Ks * (expoConst2 * weight); ADFloat specPdf = KsWeight * expoConst1 * weight; SetCondOutput({specContrib[0], specContrib[1], specContrib[2], specPdf}); } BeginElse(); { SetCondOutput({Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0)}); } EndIf(); ADVector3 specContrib = ADVector3(ret[0], ret[1], ret[2]); ADFloat specPdf = ret[3]; SetCondOutput({specContrib[0], specContrib[1], specContrib[2], specPdf}); } BeginElse(); { SetCondOutput( {Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0)}); } EndIf(); contrib[0] = ret[0]; contrib[1] = ret[1]; contrib[2] = ret[2]; pdf = ret[3]; revPdf = ret[3]; ret = CreateCondExprVec(5); BeginIf(Lt(KsWeight, Float(1.0)), ret); { ADVector3 diffContrib = Kd * Const<ADFloat>(c_INVPI); ADFloat tmp = (Float(1.0) - KsWeight) * c_INVPI; ADFloat diffPdf = tmp * cosWo; ADFloat revDiffPdf = tmp * cosWi; SetCondOutput({diffContrib[0], diffContrib[1], diffContrib[2], diffPdf, revDiffPdf}); } BeginElse(); { SetCondOutput({Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0), Const<ADFloat>(0.0)}); } EndIf(); contrib[0] += ret[0]; contrib[1] += ret[1]; contrib[2] += ret[2]; pdf += ret[3]; revPdf += ret[4]; contrib *= cosWo; }