TCompIsoReg::TCompIsoReg(double Reg, dVector Mass, dVector RC, dVector Eff) { stPolar Convert2Polar; fRegimen = Reg; fCorrectedMass = Mass; fCompRatio = RC; fEfficiency = Eff; CurvaRC = new Hermite_interp(fCorrectedMass, fCompRatio); CurvaEf = new Hermite_interp(fCorrectedMass, fEfficiency); fAngulo.resize(fCorrectedMass.size()); fModulo.resize(fCorrectedMass.size()); int k = 0; for(int i = fCorrectedMass.size() - 1; i >= 0; i--) { Convert2Polar(fCorrectedMass[i], fCompRatio[i] - 1.); fAngulo[k] = Convert2Polar.Ang; if(k > 0) { if(fAngulo[k] < atan((fCompRatio[i] - fCompRatio[i - 1]) / (fCorrectedMass[i] - fCorrectedMass[i - 1]))) { std::cout << "error interpolacion polar" << std::endl; } } fModulo[k] = Convert2Polar.Mod; k++; } fAngMax = MaxComponent(fAngulo); fAngMin = MaxComponent(fAngulo); CurvaPol = new Hermite_interp(fAngulo, fModulo); }
sEnvironmentSettings GetSettings() { sEnvironmentSettings s; CmpPtr<ICmpWaterManager> cmpWaterManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY); ENSURE(cmpWaterManager); s.waterheight = cmpWaterManager->GetExactWaterLevel(0, 0) / (65536.f * HEIGHT_SCALE); WaterManager* wm = g_Renderer.GetWaterManager(); s.watertype = wm->m_WaterType; s.waterwaviness = wm->m_Waviness; s.watermurkiness = wm->m_Murkiness; s.windangle = wm->m_WindAngle; // CColor colors #define COLOR(A, B) A = Color((int)(B.r*255), (int)(B.g*255), (int)(B.b*255)) COLOR(s.watercolor, wm->m_WaterColor); COLOR(s.watertint, wm->m_WaterTint); #undef COLOR float sunrotation = g_LightEnv.GetRotation(); if (sunrotation > (float)M_PI) sunrotation -= (float)M_PI*2; s.sunrotation = sunrotation; s.sunelevation = g_LightEnv.GetElevation(); s.posteffect = g_Renderer.GetPostprocManager().GetPostEffect(); s.skyset = g_Renderer.GetSkyManager()->GetSkySet(); s.fogfactor = g_LightEnv.m_FogFactor; s.fogmax = g_LightEnv.m_FogMax; s.brightness = g_LightEnv.m_Brightness; s.contrast = g_LightEnv.m_Contrast; s.saturation = g_LightEnv.m_Saturation; s.bloom = g_LightEnv.m_Bloom; // RGBColor (CVector3D) colors #define COLOR(A, B) A = Color((int)(B.X*255), (int)(B.Y*255), (int)(B.Z*255)) s.sunoverbrightness = MaxComponent(g_LightEnv.m_SunColor); // clamp color to [0..1] before packing into u8 triplet if(s.sunoverbrightness > 1.0f) g_LightEnv.m_SunColor *= 1.0/s.sunoverbrightness; // (there's no operator/=) // no component was above 1.0, so reset scale factor (don't want to darken) else s.sunoverbrightness = 1.0f; COLOR(s.suncolor, g_LightEnv.m_SunColor); COLOR(s.terraincolor, g_LightEnv.m_TerrainAmbientColor); COLOR(s.unitcolor, g_LightEnv.m_UnitsAmbientColor); COLOR(s.fogcolor, g_LightEnv.m_FogColor); #undef COLOR return s; }
int direction(Point3 *v) { Point3 a = v[0]-v[2]; Point3 b = v[1]-v[0]; Point3 n = CrossProd(a,b); switch(MaxComponent(n)) { case 0: return (n.x<0)?NEGX:POSX; case 1: return (n.y<0)?NEGY:POSY; case 2: return (n.z<0)?NEGZ:POSZ; } return 0; }
static int WhichDir(Point3 &p) { int j = MaxComponent(p); // the component with the maximum abs value return (p[j]<0.0f) ? 2*j+1 : 2*j; }
bool Triangle::IntersectP(const Ray &ray, bool testAlphaTexture) const { ProfilePhase p(Prof::TriIntersectP); ++nTests; // Get triangle vertices in _p0_, _p1_, and _p2_ const Point3f &p0 = mesh->p[v[0]]; const Point3f &p1 = mesh->p[v[1]]; const Point3f &p2 = mesh->p[v[2]]; // Perform ray--triangle intersection test // Transform triangle vertices to ray coordinate space // Translate vertices based on ray origin Point3f p0t = p0 - Vector3f(ray.o); Point3f p1t = p1 - Vector3f(ray.o); Point3f p2t = p2 - Vector3f(ray.o); // Permute components of triangle vertices and ray direction int kz = MaxDimension(Abs(ray.d)); int kx = kz + 1; if (kx == 3) kx = 0; int ky = kx + 1; if (ky == 3) ky = 0; Vector3f d = Permute(ray.d, kx, ky, kz); p0t = Permute(p0t, kx, ky, kz); p1t = Permute(p1t, kx, ky, kz); p2t = Permute(p2t, kx, ky, kz); // Apply shear transformation to translated vertex positions Float Sx = -d.x / d.z; Float Sy = -d.y / d.z; Float Sz = 1.f / d.z; p0t.x += Sx * p0t.z; p0t.y += Sy * p0t.z; p1t.x += Sx * p1t.z; p1t.y += Sy * p1t.z; p2t.x += Sx * p2t.z; p2t.y += Sy * p2t.z; // Compute edge function coefficients _e0_, _e1_, and _e2_ Float e0 = p1t.x * p2t.y - p1t.y * p2t.x; Float e1 = p2t.x * p0t.y - p2t.y * p0t.x; Float e2 = p0t.x * p1t.y - p0t.y * p1t.x; // Fall back to double precision test at triangle edges if (sizeof(Float) == sizeof(float) && (e0 == 0.0f || e1 == 0.0f || e2 == 0.0f)) { double p2txp1ty = (double)p2t.x * (double)p1t.y; double p2typ1tx = (double)p2t.y * (double)p1t.x; e0 = (float)(p2typ1tx - p2txp1ty); double p0txp2ty = (double)p0t.x * (double)p2t.y; double p0typ2tx = (double)p0t.y * (double)p2t.x; e1 = (float)(p0typ2tx - p0txp2ty); double p1txp0ty = (double)p1t.x * (double)p0t.y; double p1typ0tx = (double)p1t.y * (double)p0t.x; e2 = (float)(p1typ0tx - p1txp0ty); } // Perform triangle edge and determinant tests if ((e0 < 0 || e1 < 0 || e2 < 0) && (e0 > 0 || e1 > 0 || e2 > 0)) return false; Float det = e0 + e1 + e2; if (det == 0) return false; // Compute scaled hit distance to triangle and test against ray $t$ range p0t.z *= Sz; p1t.z *= Sz; p2t.z *= Sz; Float tScaled = e0 * p0t.z + e1 * p1t.z + e2 * p2t.z; if (det < 0 && (tScaled >= 0 || tScaled < ray.tMax * det)) return false; else if (det > 0 && (tScaled <= 0 || tScaled > ray.tMax * det)) return false; // Compute barycentric coordinates and $t$ value for triangle intersection Float invDet = 1 / det; Float b0 = e0 * invDet; Float b1 = e1 * invDet; Float b2 = e2 * invDet; Float t = tScaled * invDet; // Ensure that computed triangle $t$ is conservatively greater than zero // Compute $\delta_z$ term for triangle $t$ error bounds Float maxZt = MaxComponent(Abs(Vector3f(p0t.z, p1t.z, p2t.z))); Float deltaZ = gamma(3) * maxZt; // Compute $\delta_x$ and $\delta_y$ terms for triangle $t$ error bounds Float maxXt = MaxComponent(Abs(Vector3f(p0t.x, p1t.x, p2t.x))); Float maxYt = MaxComponent(Abs(Vector3f(p0t.y, p1t.y, p2t.y))); Float deltaX = gamma(5) * (maxXt + maxZt); Float deltaY = gamma(5) * (maxYt + maxZt); // Compute $\delta_e$ term for triangle $t$ error bounds Float deltaE = 2 * (gamma(2) * maxXt * maxYt + deltaY * maxXt + deltaX * maxYt); // Compute $\delta_t$ term for triangle $t$ error bounds and check _t_ Float maxE = MaxComponent(Abs(Vector3f(e0, e1, e2))); Float deltaT = 3 * (gamma(3) * maxE * maxZt + deltaE * maxZt + deltaZ * maxE) * std::abs(invDet); if (t <= deltaT) return false; // Test shadow ray intersection against alpha texture, if present if (testAlphaTexture && (mesh->alphaMask || mesh->shadowAlphaMask)) { // Compute triangle partial derivatives Vector3f dpdu, dpdv; Point2f uv[3]; GetUVs(uv); // Compute deltas for triangle partial derivatives Vector2f duv02 = uv[0] - uv[2], duv12 = uv[1] - uv[2]; Vector3f dp02 = p0 - p2, dp12 = p1 - p2; Float determinant = duv02[0] * duv12[1] - duv02[1] * duv12[0]; if (determinant == 0) { // Handle zero determinant for triangle partial derivative matrix CoordinateSystem(Normalize(Cross(p2 - p0, p1 - p0)), &dpdu, &dpdv); } else { Float invdet = 1 / determinant; dpdu = (duv12[1] * dp02 - duv02[1] * dp12) * invdet; dpdv = (-duv12[0] * dp02 + duv02[0] * dp12) * invdet; } // Interpolate $(u,v)$ parametric coordinates and hit point Point3f pHit = b0 * p0 + b1 * p1 + b2 * p2; Point2f uvHit = b0 * uv[0] + b1 * uv[1] + b2 * uv[2]; SurfaceInteraction isectLocal(pHit, Vector3f(0, 0, 0), uvHit, -ray.d, dpdu, dpdv, Normal3f(0, 0, 0), Normal3f(0, 0, 0), ray.time, this); if (mesh->alphaMask && mesh->alphaMask->Evaluate(isectLocal) == 0) return false; if (mesh->shadowAlphaMask && mesh->shadowAlphaMask->Evaluate(isectLocal) == 0) return false; } ++nHits; return true; }
bool Triangle::Intersect(const Ray &ray, Float *tHit, SurfaceInteraction *isect, bool testAlphaTexture) const { ProfilePhase p(Prof::TriIntersect); ++nTests; // Get triangle vertices in _p0_, _p1_, and _p2_ const Point3f &p0 = mesh->p[v[0]]; const Point3f &p1 = mesh->p[v[1]]; const Point3f &p2 = mesh->p[v[2]]; // Perform ray--triangle intersection test // Transform triangle vertices to ray coordinate space // Translate vertices based on ray origin Point3f p0t = p0 - Vector3f(ray.o); Point3f p1t = p1 - Vector3f(ray.o); Point3f p2t = p2 - Vector3f(ray.o); // Permute components of triangle vertices and ray direction int kz = MaxDimension(Abs(ray.d)); int kx = kz + 1; if (kx == 3) kx = 0; int ky = kx + 1; if (ky == 3) ky = 0; Vector3f d = Permute(ray.d, kx, ky, kz); p0t = Permute(p0t, kx, ky, kz); p1t = Permute(p1t, kx, ky, kz); p2t = Permute(p2t, kx, ky, kz); // Apply shear transformation to translated vertex positions Float Sx = -d.x / d.z; Float Sy = -d.y / d.z; Float Sz = 1.f / d.z; p0t.x += Sx * p0t.z; p0t.y += Sy * p0t.z; p1t.x += Sx * p1t.z; p1t.y += Sy * p1t.z; p2t.x += Sx * p2t.z; p2t.y += Sy * p2t.z; // Compute edge function coefficients _e0_, _e1_, and _e2_ Float e0 = p1t.x * p2t.y - p1t.y * p2t.x; Float e1 = p2t.x * p0t.y - p2t.y * p0t.x; Float e2 = p0t.x * p1t.y - p0t.y * p1t.x; // Fall back to double precision test at triangle edges if (sizeof(Float) == sizeof(float) && (e0 == 0.0f || e1 == 0.0f || e2 == 0.0f)) { double p2txp1ty = (double)p2t.x * (double)p1t.y; double p2typ1tx = (double)p2t.y * (double)p1t.x; e0 = (float)(p2typ1tx - p2txp1ty); double p0txp2ty = (double)p0t.x * (double)p2t.y; double p0typ2tx = (double)p0t.y * (double)p2t.x; e1 = (float)(p0typ2tx - p0txp2ty); double p1txp0ty = (double)p1t.x * (double)p0t.y; double p1typ0tx = (double)p1t.y * (double)p0t.x; e2 = (float)(p1typ0tx - p1txp0ty); } // Perform triangle edge and determinant tests if ((e0 < 0 || e1 < 0 || e2 < 0) && (e0 > 0 || e1 > 0 || e2 > 0)) return false; Float det = e0 + e1 + e2; if (det == 0) return false; // Compute scaled hit distance to triangle and test against ray $t$ range p0t.z *= Sz; p1t.z *= Sz; p2t.z *= Sz; Float tScaled = e0 * p0t.z + e1 * p1t.z + e2 * p2t.z; if (det < 0 && (tScaled >= 0 || tScaled < ray.tMax * det)) return false; else if (det > 0 && (tScaled <= 0 || tScaled > ray.tMax * det)) return false; // Compute barycentric coordinates and $t$ value for triangle intersection Float invDet = 1 / det; Float b0 = e0 * invDet; Float b1 = e1 * invDet; Float b2 = e2 * invDet; Float t = tScaled * invDet; // Ensure that computed triangle $t$ is conservatively greater than zero // Compute $\delta_z$ term for triangle $t$ error bounds Float maxZt = MaxComponent(Abs(Vector3f(p0t.z, p1t.z, p2t.z))); Float deltaZ = gamma(3) * maxZt; // Compute $\delta_x$ and $\delta_y$ terms for triangle $t$ error bounds Float maxXt = MaxComponent(Abs(Vector3f(p0t.x, p1t.x, p2t.x))); Float maxYt = MaxComponent(Abs(Vector3f(p0t.y, p1t.y, p2t.y))); Float deltaX = gamma(5) * (maxXt + maxZt); Float deltaY = gamma(5) * (maxYt + maxZt); // Compute $\delta_e$ term for triangle $t$ error bounds Float deltaE = 2 * (gamma(2) * maxXt * maxYt + deltaY * maxXt + deltaX * maxYt); // Compute $\delta_t$ term for triangle $t$ error bounds and check _t_ Float maxE = MaxComponent(Abs(Vector3f(e0, e1, e2))); Float deltaT = 3 * (gamma(3) * maxE * maxZt + deltaE * maxZt + deltaZ * maxE) * std::abs(invDet); if (t <= deltaT) return false; // Compute triangle partial derivatives Vector3f dpdu, dpdv; Point2f uv[3]; GetUVs(uv); // Compute deltas for triangle partial derivatives Vector2f duv02 = uv[0] - uv[2], duv12 = uv[1] - uv[2]; Vector3f dp02 = p0 - p2, dp12 = p1 - p2; Float determinant = duv02[0] * duv12[1] - duv02[1] * duv12[0]; if (determinant == 0) { // Handle zero determinant for triangle partial derivative matrix CoordinateSystem(Normalize(Cross(p2 - p0, p1 - p0)), &dpdu, &dpdv); } else { Float invdet = 1 / determinant; dpdu = (duv12[1] * dp02 - duv02[1] * dp12) * invdet; dpdv = (-duv12[0] * dp02 + duv02[0] * dp12) * invdet; } // Compute error bounds for triangle intersection Float xAbsSum = (std::abs(b0 * p0.x) + std::abs(b1 * p1.x) + std::abs(b2 * p2.x)); Float yAbsSum = (std::abs(b0 * p0.y) + std::abs(b1 * p1.y) + std::abs(b2 * p2.y)); Float zAbsSum = (std::abs(b0 * p0.z) + std::abs(b1 * p1.z) + std::abs(b2 * p2.z)); Vector3f pError = gamma(7) * Vector3f(xAbsSum, yAbsSum, zAbsSum); // Interpolate $(u,v)$ parametric coordinates and hit point Point3f pHit = b0 * p0 + b1 * p1 + b2 * p2; Point2f uvHit = b0 * uv[0] + b1 * uv[1] + b2 * uv[2]; // Test intersection against alpha texture, if present if (testAlphaTexture && mesh->alphaMask) { SurfaceInteraction isectLocal(pHit, Vector3f(0, 0, 0), uvHit, -ray.d, dpdu, dpdv, Normal3f(0, 0, 0), Normal3f(0, 0, 0), ray.time, this); if (mesh->alphaMask->Evaluate(isectLocal) == 0) return false; } // Fill in _SurfaceInteraction_ from triangle hit *isect = SurfaceInteraction(pHit, pError, uvHit, -ray.d, dpdu, dpdv, Normal3f(0, 0, 0), Normal3f(0, 0, 0), ray.time, this); // Override surface normal in _isect_ for triangle isect->n = isect->shading.n = Normal3f(Normalize(Cross(dp02, dp12))); if (mesh->n || mesh->s) { // Initialize _Triangle_ shading geometry // Compute shading normal _ns_ for triangle Normal3f ns; if (mesh->n) { ns = (b0 * mesh->n[v[0]] + b1 * mesh->n[v[1]] + b2 * mesh->n[v[2]]); if (ns.LengthSquared() > 0) ns = Normalize(ns); else ns = isect->n; } else ns = isect->n; // Compute shading tangent _ss_ for triangle Vector3f ss; if (mesh->s) { ss = (b0 * mesh->s[v[0]] + b1 * mesh->s[v[1]] + b2 * mesh->s[v[2]]); if (ss.LengthSquared() > 0) ss = Normalize(ss); else ss = Normalize(isect->dpdu); } else ss = Normalize(isect->dpdu); // Compute shading bitangent _ts_ for triangle and adjust _ss_ Vector3f ts = Cross(ss, ns); if (ts.LengthSquared() > 0.f) { ts = Normalize(ts); ss = Cross(ts, ns); } else CoordinateSystem((Vector3f)ns, &ss, &ts); // Compute $\dndu$ and $\dndv$ for triangle shading geometry Normal3f dndu, dndv; if (mesh->n) { // Compute deltas for triangle partial derivatives of normal Vector2f duv02 = uv[0] - uv[2]; Vector2f duv12 = uv[1] - uv[2]; Normal3f dn1 = mesh->n[v[0]] - mesh->n[v[2]]; Normal3f dn2 = mesh->n[v[1]] - mesh->n[v[2]]; Float determinant = duv02[0] * duv12[1] - duv02[1] * duv12[0]; if (determinant == 0) dndu = dndv = Normal3f(0, 0, 0); else { Float invDet = 1 / determinant; dndu = (duv12[1] * dn1 - duv02[1] * dn2) * invDet; dndv = (-duv12[0] * dn1 + duv02[0] * dn2) * invDet; } } else dndu = dndv = Normal3f(0, 0, 0); isect->SetShadingGeometry(ss, ts, dndu, dndv, true); } // Ensure correct orientation of the geometric normal if (mesh->n) isect->n = Faceforward(isect->n, isect->shading.n); else if (reverseOrientation ^ transformSwapsHandedness) isect->n = isect->shading.n = -isect->n; *tHit = t; ++nHits; return true; }