本文整理汇总了C++中Vec3f::Normalize方法的典型用法代码示例。如果您正苦于以下问题:C++ Vec3f::Normalize方法的具体用法?C++ Vec3f::Normalize怎么用?C++ Vec3f::Normalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Vec3f
的用法示例。
在下文中一共展示了Vec3f::Normalize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: intersect
bool Sphere::intersect(const Ray &r, Hit &h, float tmin)
{
Vec3f v = center - r.getOrigin();
float tp = v.Dot3(r.getDirection());
float det = tp*tp - v.Dot3(v) + radius*radius;
//intersect
if(det > 0)
{
//t'
det = sqrtf(det);
float t1 = tp - det;
float t2 = tp + det;
if(t1 > tmin && t1 < h.getT())
{
Vec3f normal = (r.pointAtParameter(t1) - center);
normal /= radius;
normal.Normalize();
h.set(t1,material,normal,r);
return 1;
}
else if(t2 > tmin && t2 < h.getT())
{
//sphere's normal
Vec3f normal = (r.pointAtParameter(t2) - center);
normal /= radius;
normal.Normalize();
h.set(t2,material,normal,r);
return 1;
}
}
return 0;
}
示例2: TracePhoton
void PhotonMapping::TracePhoton(const Vec3f &position, const Vec3f &direction,
const Vec3f &energy, int iter) {
if(iter>args->num_bounces){
return;
}
Hit h = Hit();
Ray R = Ray(position, direction);
bool intersect = raytracer->CastRay(R, h, false);
if(!intersect){
return;
}
Material *m = h.getMaterial();
Vec3f normal = h.getNormal();
Vec3f point = R.pointAtParameter(h.getT());
Vec3f opDirec = direction;
opDirec.Negate();
opDirec.Normalize();
Vec3f diffuse = m->getDiffuseColor(), reflec = m->getReflectiveColor();
double diffuseAnswer = diffuse.x()+diffuse.y()+diffuse.z();
double reflecAnswer = reflec.x()+reflec.y()+reflec.z();
double total = reflecAnswer+diffuseAnswer;
diffuseAnswer /= total;
reflecAnswer /= total;
double seed = GLOBAL_mtrand.rand();
if(seed <= diffuseAnswer && seed >= 0){
Vec3f newEnergy = energy * diffuse;
Vec3f newPosition = point;
Vec3f newDirection = Vec3f(GLOBAL_mtrand.rand(),GLOBAL_mtrand.rand(),GLOBAL_mtrand.rand());
newDirection.Normalize();
Photon answer = Photon(point,opDirec,newEnergy,iter+1);
kdtree->AddPhoton(answer);
TracePhoton(newPosition, newDirection, newEnergy, iter+1);
}
else if(seed>diffuseAnswer && seed <= 1){
Vec3f newEnergy = energy * reflec;
Vec3f newPosition = point;
Vec3f newDirection = direction - 2 * direction.Dot3(normal) * normal;
Photon answer = Photon(point,opDirec,newEnergy,iter+1);
kdtree->AddPhoton(answer);
TracePhoton(newPosition, newDirection, newEnergy, iter+1);
}
// ==============================================
// ASSIGNMENT: IMPLEMENT RECURSIVE PHOTON TRACING
// ==============================================
// Trace the photon through the scene. At each diffuse or
// reflective bounce, store the photon in the kd tree.
// One optimization is to *not* store the first bounce, since that
// direct light can be efficiently computed using classic ray
// tracing.
}
示例3: getRight
Vec3f cCameraManager::getRight()
{
Vec3f right;
Vec3f lookingVec = m_vecCamPos - m_vecCamLookAt;
lookingVec.Normalize();
Vec3f::Cross3(right, lookingVec, m_vecCamUp);
right.Normalize();
return right;
}
示例4:
PerspectiveCamera::PerspectiveCamera(Vec3f cer, Vec3f &direction, Vec3f &up, float angle)
{
this->center = cer;
direction.Normalize();
this->dir = direction;
up.Normalize();
this->up = up;
Vec3f::Cross3(this->hor, this->dir, this->up);
this->hor.Normalize();
this->angle = angle;
float theta = angle / 2.0f;
this->dis = 1.0f / (sin(theta) * 2.0f);
}
示例5: mirrorDirection
Vec3f RayTracer::mirrorDirection(const Vec3f &normal, const Vec3f &incoming) const
{
Vec3f reflectionDir;
reflectionDir = incoming - normal * (incoming.Dot3(normal)) * 2;
reflectionDir.Normalize();
return reflectionDir;
}
示例6: intersect
bool Triangle::intersect(const Ray &r, Hit &h, float tmin)
{
Vec3f r0 = r.getOrigin();
Vec3f rd = r.getDirection();
Vec3f E1 = a - b;
Vec3f E2 = a - c;
Vec3f S = a - r0;
//参数写错,rd写成r0了……
float de = det3x3(rd.x(), rd.y(), rd.z(), E1.x(), E1.y(), E1.z(), E2.x(), E2.y(), E2.z());
if (de == 0.0f)
return false;
float t = det3x3(S.x(), S.y(), S.z(), E1.x(), E1.y(), E1.z(), E2.x(), E2.y(), E2.z())/de;
float belta = det3x3(rd.x(), rd.y(), rd.z(), S.x(), S.y(), S.z(), E2.x(), E2.y(), E2.z()) / de;
float lamda = det3x3(rd.x(), rd.y(), rd.z(), E1.x(), E1.y(), E1.z(), S.x(), S.y(), S.z()) / de;
Vec3f normal;
Vec3f::Cross3(normal, b - a, c - a);
normal.Normalize();
h.set(t, material, normal, r);
if (t >= tmin && belta > 0.0f && lamda > 0.0f && belta + lamda < 1.0f)
return true;
else
return false;
}
示例7: intersect
/*
The intersect routine will first transform the ray,
then delegate to the intersect routine of the contained
object. Make sure to correctly transform the resulting
normal according to the rule seen in lecture. You may
choose to normalize the direction of the transformed ray
or leave it un-normalized. If you decide not to normalize
the direction, you might need to update some of your intersection code.
*/
bool Transform::intersect(const Ray &r, Hit &h, float tmin)
{
Vec3f r0 = r.getOrigin();
Vec3f rd = r.getDirection();
Matrix inv;
matrix.Inverse(inv);
inv.Transform(r0);
inv.TransformDirection(rd);
if (object != NULL)
{
//这里的h是有问题的,作如下修改:
bool judge = object->intersect(Ray(r0,rd), h, tmin);
Vec3f normal = h.getNormal();
//这里很奇怪,normal的方向没有修正,然而结果却是对的
//改了之后反而是错的!!
//这里确定normal没有错,那么就是之后应用normal的
//问题
//好吧,就是这里的问题
//经过把图形摆正,发现求的法向量没有问题,但是没有单位化…………!
matrix.TransformDirection(normal);
normal.Normalize();
//or:
//Matrix change,res;
//matrix.Inverse(change);
//change.Transpose(res);
//res.TransformDirection(normal);
h.set(h.getT(), h.getMaterial(), normal, r);
return judge;
}
return false;
}
示例8: getIntersection
bool Scene::getIntersection(Line l,Vec3f& N,Vec3f& IntersectPoint,Vec3f& Color,Material& material,double len_limit) const{
double t,dis = len_limit;
bool haveIntersection = false;
Vec3f n;
for (int i = 0;i < models.size();i++)
if (models[i]->getIntersection(l,N,IntersectPoint,Color,material,dis)){
haveIntersection = true;
dis = (IntersectPoint - l.start_point).Len();
}
/*if (tri_tree.getIntersection(l,N,IntersectPoint,Color,material,len_limit)){
haveIntersection = true;
dis = (IntersectPoint - l.start_point).Len();
}*/
for (int i = 0;i < _objects.size();i++){
t = _objects[i]->getIntersection(l);
if (t > EPS && t < dis){
haveIntersection = true;
dis = t;
IntersectPoint = l.start_point + l.dir * t;
N = _objects[i]->getN(IntersectPoint);
material = _objects[i]->material;
Color =_objects[i]->getColor(IntersectPoint);
}
}
N.Normalize();
if (N * l.dir < 0){
N = - N;
material.refract_n = 1.0 / material.refract_n;
}
return haveIntersection;
}
示例9: rayCasting
void ElevatorSimRenderWindow::rayCasting(int x, int y) {
SimulationState& simState = SimulationState::acquire();
float fovX = (GLWindow_width/GLWindow_height) * 45.f;
float mx = (float)((x - GLWindow_width * 0.5) *
(1.0 / GLWindow_width) * fovX * 0.5);
float my = (float)((y - GLWindow_height * 0.5) *
(1.0 / GLWindow_width) * fovX * 0.5);
Vec3f dx = simState.getCameraManager().getRight() * mx;
Vec3f dy = simState.getCameraManager().GetCameraUp() * my;
Vec3f dir = simState.getCameraManager().GetCameraLookAt() + (dx + dy) * 2.0;
dir.Normalize();
const int eachFloorHeight = simState.getBuilding().gfxEachFloorHeight;
std::vector<Elevator*> & elevators = simState.getBuilding().getElevators();
std::for_each(
elevators.begin(),
elevators.end(),
[this, &eachFloorHeight] ( const Elevator* thisElev ) {
float pos = 1.0f + thisElev->getYVal() /
Floor::YVALS_PER_FLOOR * eachFloorHeight;
(void) pos;
});
}
示例10: mirrorDirection
Vec3f mirrorDirection(const Vec3f &normal, const Vec3f &incoming)
{
//(反射光方向:R = V - 2(V.N)N )
Vec3f reflectDir = incoming - 2 * (incoming.Dot3(normal))*normal;
reflectDir.Normalize();
return reflectDir;
}
示例11: hit
// Detects if ray intersects sphere.
// TODO: Manage special case for transformed spheres by transforming vector.
Intersection* Sphere::hit(const Ray& r)
{
// Transform vector to use inverse of sphere's transformation matrix
Vec3f e = Matrix4f::pntMult(Ti,r.e);
Vec3f d = Matrix4f::vecMult(Ti,r.d);
// Calculate coefficients of quadratic
//Isn't this always 1?
float a = Vec3f::Dot(d,d);
float b = Vec3f::Dot(2*d,e-cntr);
float c = Vec3f::Dot(e-cntr,e-cntr)-pow(rad,2);
float discriminant = b*b-4*a*c;
if (discriminant<0)
{
return NULL;
}
float t = RTMin((-b + sqrt(discriminant))/(2*a),(-b - sqrt(discriminant))/(2*a));
Pnt3f p = Matrix4f::pntMult(T,e+t*d); // transform point back to world coordinates
Vec3f n = Matrix4f::vecMult(Tti,(p-cntr)/rad); // transform normal back to world coordinates
n.Normalize();
if (!SameDirection(d,n)) return NULL; // Backface culling
if (t > r.max || t < r.min) return NULL; // check range
return new Intersection(t, p, n, parent);
}
示例12: Intersect
bool Transform::Intersect(const Ray &r, Hit &h, float tmin) const
{
bool result = false;
Matrix m = m_matrix;
if ( m.Inverse() )
{
Vec3f org = r.getOrigin();
Vec3f dir = r.getDirection();
m.Transform(org);
m.TransformDirection(dir);
Ray r2 (dir, org);
result = m_pObject->Intersect(r2, h, tmin);
if (result)
{
Matrix m1 = m;
m1.Transpose();
Vec3f n = h.getNormal();
m1.TransformDirection(n);
n.Normalize();
h.set(h.getT(), h.getMaterial(), n, r);
}
}
return result;
}
示例13: shadow
Vec3f RayTracer::shadow(const Vec3f &point,
const Vec3f &pointOnLight,
const Face *f,
const Ray &ray,
const Hit &hit) const
{
const Vec3f normal(hit.getNormal());
const Material *m = hit.getMaterial();
Vec3f dirToLight = pointOnLight - point;
dirToLight.Normalize();
/* If dot product < 0, surface is not facing light */
if (normal.Dot3(dirToLight) > 0) {
Ray rayToLight(point, dirToLight);
Hit hLight;
bool blocked = CastRay(rayToLight, hLight, false);
while (std::fabs(hLight.getT()) < SURFACE_EPSILON &&
std::fabs((pointOnLight - point).Length()) > SURFACE_EPSILON) {
rayToLight = Ray(rayToLight.pointAtParameter(SURFACE_EPSILON),
dirToLight);
blocked = CastRay(rayToLight, hLight, false);
}
if (hLight.getT() == FLT_MAX || hLight.getMaterial() != f->getMaterial()) {
return Vec3f(0, 0, 0);
}
const Vec3f lightColor = 0.2 * f->getMaterial()->getEmittedColor() * f->getArea();
return m->Shade(ray,hit,dirToLight,lightColor,args);
}
return Vec3f(0, 0, 0);
}
示例14: CalculateNormals
inline void Mesh::CalculateNormals(void) {
Vec3f* u = new Vec3f();
Vec3f* v = new Vec3f();
Vec3f* uv = new Vec3f();
Vec3f* faceNormal;
Vec3f* va;
Vec3f* vb;
Vec3f* vc;
uint32 vertexCount = vertices.Length(),
normalCount = normals.Length(),
a, b, c, i, il;
if (vertexCount < normalCount) {
for (i = vertexCount, il = normalCount; i > il; i--) {
Vec3f* normal = normals[i];
normals.Splice(i, 1);
delete normal;
}
} else {
i = vertexCount;
normalCount = normals.Length();
while(i-- > normalCount) normals.Push(new Vec3f());
for (i = 0, il = vertexCount; i < il; i++) normals[i]->Set(0.0f, 0.0f, 0.0f);
}
for (i = 0, il = indices.Length(); i < il; i += 3) {
a = indices[i];
b = indices[i + 1];
c = indices[i + 2];
va = vertices[a];
vb = vertices[b];
vc = vertices[c];
Vec3Sub<float32>(*vc, *vb, *u);
Vec3Sub<float32>(*va, *vb, *v);
Vec3Cross<float32>(*u, *v, *uv);
faceNormal = uv;
faceNormal->Normalize();
*(normals[a]) += *faceNormal;
*(normals[b]) += *faceNormal;
*(normals[c]) += *faceNormal;
}
for (i = 0, il = indices.Length(); i < il; i += 3) {
normals[indices[i]]->Normalize();
normals[indices[i + 1]]->Normalize();
normals[indices[i + 2]]->Normalize();
}
delete u;
delete v;
delete uv;
m_needsUpdate = true;
}
示例15: getNormal
Vec3f sphere::getNormal(Vec3f eye, Vec3f dir)
{
Vec3f normal;
normal = (eye + dir * testIntersection(eye, dir)) - center;
normal.Normalize();
return normal;
}