bool Sphere::intersect(Ray ray, float* t, vec3f* intersectionPoint) const noexcept { const vec3f k{implementation->position - ray.position}; const float a = ray.direction.dot(k); const float D = a * a - (k.dot(k) - implementation->radius * implementation->radius); if (D < 0.0F) return false; const float sqrtD = std::sqrt(D); const float t1 = a - sqrtD; const float t2 = a + sqrtD; const float minT = std::min(t1, t2); const float maxT = std::max(t1, t2); const float fineT = minT >= 0.0F ? minT : maxT; if (fineT < 0.0F) return false; if (t) *t = fineT; if (intersectionPoint) *intersectionPoint = ray.position + ray.direction * fineT; return true; }
vec3f vec3f::random(){ static vec3f rnd; rnd.x=random_double()*2-1; rnd.y=random_double()*2-1; rnd.z=random_double()*2-1; rnd.normalize(); return rnd; }
bool Camera::InView(const vec3f& p, float radius) const { const vec3f t = (p - pos); return (t.dot3D(frustumR) < radius) && (t.dot3D(frustumL) < radius) && (t.dot3D(frustumB) < radius) && (t.dot3D(frustumT) < radius); }
camera() { V.make_identity(); P.make_identity(); R.make_identity(); position.set_value( vec3f(0,0,100) ); euler_rotation.set_value( vec3f(0,0,0) ); rotation.set_value( vec3f(0,1,0), 0); }
// Given a point and a plane (defined by a coplanar point and a normal), compute the closest point // in the plane. (The plane is unbounded.) vec3f NearestPointInPlane(const vec3f &point, const vec3f &planePoint, const vec3f &planeNormal) { vec3f nearestPoint; vec3f pointDelta = point - planePoint; float delta = planeNormal.dot(pointDelta) / planeNormal.dot(planeNormal); nearestPoint = point - delta*planeNormal; return nearestPoint; }
color texture2D(vec3f coords, texture t){ int x = coords.x() * t.width; int y = t.height-(coords.y() * t.height); if(t.bytesperpixel == 4){ return t.data[y*t.height + x]; } uint8_t* data = (uint8_t*)t.data; int col= (int)(data[t.bytesperpixel*(y*t.height + x)]); return gammaCorrect((color){static_cast<uint8_t>(col>>24),static_cast<uint8_t>((col>>16) &255),static_cast<uint8_t>((col>>8)&255),255});
/** * LookTowards * * Note 1: Function will exit if front_vec is (close to) parallel to the Y-axis; supply your own up_vec if this is the case. * Note 2: Not fully tested, use at own risk... * * Example: * Make camera look at 'my_node'. Note that world space positions are used. * camera->lookAt( my_node->getWorldPosition() - camera->getWorldPosition() ); */ void Transformable::lookTowards( vec3f front_vec, vec3f up_vec ) { vec3f right; vec3f up; vec3f prev_up; front_vec.normalize(); up_vec.normalize(); if (abs(up_vec.dot(front_vec)) > 0.99999f) { return; } if (parent && parent->is_transformable) { mat3f mat; R = ((Transformable *) parent)->getWorldMatrix().rotationMatrix(); R.inv(); prev_up = up_vec; right = front_vec.cross(prev_up); up = right.cross(front_vec); right.normalize(); up.normalize(); mat.setCol(0, right); mat.setCol(1, up); mat.setCol(2, -front_vec); R = R * mat; } else { prev_up = up_vec; right = front_vec.cross(prev_up); up = right.cross(front_vec); right.normalize(); up.normalize(); R.setCol(0, right); R.setCol(1, up); R.setCol(2, -front_vec); } }
void Logstalgia::addGroup(std::string grouptitle, std::string groupregex, int percent, vec3f colour) { if(percent<0) return; int remainpc = (int) ( ((float) remaining_space/total_space) * 100); if(percent==0) { percent=remainpc; } if(remainpc<percent) return; int space = (int) ( ((float)percent/100) * total_space ); int top_gap = total_space - remaining_space; int bottom_gap = display.height - (total_space - remaining_space + space); //debugLog("group %s: regex = %s, remainpc = %d, space = %d, top_gap = %d, bottom_gap = %d\n", // grouptitle.c_str(), groupregex.c_str(), remainpc, space, top_gap, bottom_gap); Summarizer* summ = new Summarizer(fontSmall, paddle_x, top_gap, bottom_gap, update_rate, groupregex, grouptitle); // summ->showCount(true); if(colour.length2() > 0.01f) { summ->setColour(colour); } summGroups.push_back(summ); remaining_space -= space; }
double SynthScore::handOrientationScore(const DisembodiedObject &object, const mat4f &modelToWorld, const Agent &agent) { const vec3f diff = modelToWorld * (object.model->bbox.getCenter() + vec3f(object.agentFace.x, object.agentFace.y, 0.0f)) - modelToWorld * object.model->bbox.getCenter(); //const OBBf worldBBox = modelToWorld * OBBf(object.model->bbox); //vec3f agentFaceDir = (worldBBox.getCenter() - agent.handPos()); //agentFaceDir.z = 0.0f; //float dot = diff.getNormalized() | agentFaceDir.getNormalized(); float dot = diff.getNormalized() | agent.gazeDir; if (dot < 0.0f) return 0.0f; return dot; }
void matrix4x4f::rotate( const float &angle, vec3f &axis ) { float s = sin(DEGTORAD(angle)); float c = cos(DEGTORAD(angle)); axis.norm(); float ux = axis.x; float uy = axis.y; float uz = axis.z; m[0] = c + (1-c) * ux*ux; m[1] = (1-c) * ux*uy + s*uz; m[2] = (1-c) * ux*uz - s*uy; m[3] = 0; m[4] = (1-c) * uy*ux - s*uz; m[5] = c + (1-c) * pow(uy,2); m[6] = (1-c) * uy*uz + s*ux; m[7] = 0; m[8] = (1-c) * uz*ux + s*uy; m[9] = (1-c) * uz*uy - s*ux; m[10] = c + (1-c) * pow(uz,2); m[11] = 0; m[12] = 0; m[13] = 0; m[14] = 0; m[15] = 1; }
bool Camera::AABBInOriginPlane(const vec3f& plane, const vec3f& mins, const vec3f& maxs) const { vec3f fp; fp.x = (plane.x > 0.0f)? mins.x: maxs.x; fp.y = (plane.y > 0.0f)? mins.y: maxs.y; fp.z = (plane.z > 0.0f)? mins.z: maxs.z; return (plane.dot3D(fp - pos) < 0.0f); }
particle_intermediate make_particle_jet(int num, vec3f start, vec3f dir, float len, float angle, vec3f col, float speedmod) { std::vector<cl_float4> p1; std::vector<cl_float4> p2; std::vector<cl_uint> colours; for(uint32_t i = 0; i<num; i++) { float len_pos = randf_s(0, len); float len_frac = len_pos / len; vec3f euler = dir.get_euler(); euler = euler + (vec3f){0, randf_s(-angle, angle), randf_s(-angle, angle)}; vec3f rot_pos = (vec3f){0.f, len_pos, 0.f}.rot({0.f, 0.f, 0.f}, euler); vec3f final_pos = start + rot_pos; final_pos = final_pos + randf<3, float>(-len/40.f, len/40.f); float mod = speedmod; p1.push_back({final_pos.v[0], final_pos.v[1], final_pos.v[2]}); vec3f pos_2 = final_pos * mod; //p2.push_back({pos_2.v[0], pos_2.v[1], pos_2.v[2]}); col = clamp(col, 0.f, 1.f); colours.push_back(rgba_to_uint(col)); }
//****************************************************************** //FUNCTION: void COutdoorLightScattering::__getRaySphereIntersection(vec3f vRayOrigin, vec3f vRayDirection, vec3f vSphereCenter, float vSphereRadius, vec2f& voIntersection) { vRayOrigin -= vSphereCenter; float A = vRayDirection.dot(vRayDirection); float B = 2 * vRayOrigin.dot(vRayDirection); float C = vRayOrigin.dot(vRayOrigin) - vSphereRadius * vSphereRadius; float D = B * B - 4 * A * C; if (D < 0) { voIntersection = vec2f(-1); } else { D = sqrt(D); voIntersection = vec2f((-B - D) / 2 * A, (-B + D) / 2 * A); } }
vec3f interpolate(vec3f current, vec3f target, float timeDelta, float maxSpeed, float maxDistance) { float distance = current.getDistanceFrom(target); if(distance > maxDistance) return target; else { float d = std::min(distance, (distance/maxDistance)*maxSpeed*timeDelta); return current + (target-current).normalize()*d; } }
vec3f::vec3f(const vec3f& aVec) { float v[3]; aVec.get(v[0], v[1], v[2]); vec[X] = v[X]; vec[Y] = v[Y]; vec[Z] = v[Z]; }
void CSMFRenderer::DrawPotentiallyVisibleSquares(const Camera* cam, int, int) { const CReadMap* rm = readMap; const float* hm = rm->GetHeightmap(); // const vec3f* nm = &rm->facenormals[0]; // each square is drawn clockwise topside-up glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CW); // our normals are always unit-length glDisable(GL_NORMALIZE); glPolygonMode(GL_FRONT, GL_FILL); for (int i = 0; i < numSquares; i++) { Square& q = squares[i]; if (!SquareRowVisible(q.idx, rm, cam)) { // advance to start of next row i += (numSquaresX - (i % numSquaresX)); continue; } // note: slightly inefficient, would be better to use y-extremes of <q> vec3f minBounds = q.GetMinBounds(); minBounds.y = rm->currMinHeight; vec3f maxBounds = q.GetMaxBounds(); maxBounds.y = rm->currMaxHeight; const vec3f v = (cam->pos - q.mid); const float dSq = v.sqLen3D(); const bool draw = (dSq < viewRadiusSq && cam->InView(minBounds, maxBounds)); if (draw) { DrawSquare(q, v, rm, hm); } } glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_NORMALIZE); glDisable(GL_CULL_FACE); glFrontFace(GL_CCW); }
bool BBox::Overlap( const vec3f& p ) const { bool x = (pMax.x() >= p.x()) && (pMin.x() <= p.x()); bool y = (pMax.y() >= p.y()) && (pMin.y() <= p.y()); bool z = (pMax.z() >= p.z()) && (pMin.z() <= p.z()); return (x && y && z); }
bool Entity::accumulateForce(vec3f steeringToAdd) { float forceUsed = mSteeringForce.getLength(); float forceRemaining = mMaxForce - forceUsed; if (forceRemaining <= 0) return false; float forceToAdd = steeringToAdd.getLength(); if (forceToAdd < forceRemaining) { mSteeringForce += steeringToAdd; } else { steeringToAdd.normalize(); mSteeringForce += steeringToAdd * forceRemaining; } return true; }
vec2f Grid::coordsToMapPos(vec3f coords) { GLfloat width = d_cellWidth * d_rows; GLfloat height = d_cellHeight * d_cols; GLfloat x = floor(coords.x() / width); GLfloat y = floor(coords.z() / height); // Clamp the values into the bounds if (x > d_rows) { x = d_rows; } else if (x < 0) { x = 0; } if (y > d_cols) { y = d_cols; } else if (y < 0) { y = 0; } return vec2f(x, y); }
static void Spawn (CPlayerEntity *Player, vec3f Start, vec3f AimDir, int DamageMultiplier, int Speed) { vec3f dir = AimDir.ToAngles(); anglef angles = dir.ToVectors (); CProx *Prox = QNewEntityOf CProx; Prox->State.GetOrigin() = Start; Prox->Velocity = (AimDir * Speed) .MultiplyAngles (200 + crand() * 10.0f, angles.Up) .MultiplyAngles (crand() * 10.0f, angles.Right); Prox->State.GetAngles() = dir - vec3f(90, 0, 0); Prox->PhysicsType = PHYSICS_BOUNCE; Prox->GetSolid() = SOLID_BBOX; Prox->State.GetEffects() |= FX_GRENADE; Prox->GetClipmask() = CONTENTS_MASK_SHOT|CONTENTS_LAVA|CONTENTS_SLIME; Prox->State.GetRenderEffects() |= RF_IR_VISIBLE; Prox->GetMins().Set (-6, -6, -6); Prox->GetMaxs().Set (6, 6, 6); Prox->State.GetModelIndex() = ModelIndex ("models/weapons/g_prox/tris.md2"); Prox->SetOwner(Player); Prox->Firer = Player; Prox->Touchable = true; Prox->ThinkType = PROXTHINK_EXPLODE; Prox->Damage = PROX_DAMAGE * DamageMultiplier; Prox->ClassName = "prox"; switch (DamageMultiplier) { case 1: Prox->NextThink = Level.Frame + PROX_TIME_TO_LIVE; break; case 2: Prox->NextThink = Level.Frame + 300; break; case 4: Prox->NextThink = Level.Frame + 150; break; case 8: Prox->NextThink = Level.Frame + 100; break; default: Prox->NextThink = Level.Frame + PROX_TIME_TO_LIVE; break; } Prox->Link (); }
void clamp(vec3f & v, float min, float max) { if(v.peekx() > max) v.x() = max; if(v.peekx() < min) v.x() = min; if(v.peeky() > max) v.y() = max; if(v.peeky() < min) v.y() = min; if(v.peekz() > max) v.z() = max; if(v.peekz() < min) v.z() = min; }
void clamp(vec3f & v, float vmin, float vmax) { if(v.peekx() > vmax) v.x() = vmax; if(v.peekx() < vmin) v.x() = vmin; if(v.peeky() > vmax) v.y() = vmax; if(v.peeky() < vmin) v.y() = vmin; if(v.peekz() > vmax) v.z() = vmax; if(v.peekz() < vmin) v.z() = vmin; }
void mat4x4f::lookAt(const vec3f & vFrom, const vec3f & vTo, const vec3f & vUp) { vec3f vZ = Normalise(vFrom - vTo); //vZ.Dumpvec3f("vZ"); vec3f vX = Normalise(vUp.Cross(vZ)); //vX.Dumpvec3f("vX"); vec3f vY = vZ.Cross(vX); //vY.Dumpvec3f("vY"); elem[0][0] = vX.i; elem[0][1] = vY.i; elem[0][2] = vZ.i; elem[0][3] = 0; elem[1][0] = vX.j; elem[1][1] = vY.j; elem[1][2] = vZ.j; elem[1][3] = 0; elem[2][0] = vX.k; elem[2][1] = vY.k; elem[2][2] = vZ.k; elem[2][3] = 0; elem[3][0] = -vX.dot3(vFrom); elem[3][1] = -vY.dot3(vFrom); elem[3][2] = -vZ.dot3(vFrom); elem[3][3] = 1; }
bool baryCentricTriangle(vec2f p, vec3f v1, vec3f v2, vec3f v3, float &u, float &v, float &r) { float x1mx3 = v1.x() - v3.x(); float x2mx3 = v2.x() - v3.x(); float y1my3 = v1.y() - v3.y(); float y2my3 = v2.y() - v3.y(); float det = (x1mx3 * y2my3) - (y1my3*x2mx3); float pxmx3 = p.x() - v3.x(); float pymy3 = p.y() - v3.y(); if(det == 0.0 || det == -0.0) return false; u = (y2my3*pxmx3 + x2mx3*-1*pymy3)/det; v = (y1my3*-1*pxmx3 + x1mx3*pymy3)/det; r = 1-u-v; if (u > 1.0f || v > 1.0f || r > 1.0f) return false; if (u < 0.0f || v < 0.0f || r < 0.0f) return false; return true; }
void DepthBuffer::setCamera(const mat44f& viewProjection,float znear,float zfar,float fov,vec3f cameraPos,vec3f cameraDir,vec3f cameraUp){ viewProjection_ = viewProjection; znear_ = znear; zfar_ = zfar; fov_ = fov; float aspect = float(size_.x)/float(size_.y); cameraPos_ = cameraPos; cameraDir_ = cameraDir; vec3f cameraRight = cameraDir.cross(cameraUp).normalize(); cameraUp = cameraRight.cross(cameraDir); // auto upLength = tanf(fov/2.0f)*znear; auto rightLength = upLength * (aspect); cameraRight = cameraRight*rightLength; cameraUp = cameraUp*upLength; cameraRight_ = vec4f(cameraRight.x,cameraRight.y,cameraRight.z,1); cameraUp_ = vec4f(cameraUp.x,cameraUp.y,cameraUp.z,1); aabbFrontFaceMask = calculateBoxVisibleFaceMask(cameraDir); }
void OrbitCamera::Init(const vec3f& p, const vec3f& t) { const vec3f v = (t - p); const vec3f w = v.norm3D(); const float d = v.len3D(); // acosf() takes values in [-1.0, 1.0] const float e = RAD2DEG(acosf(v.len2D() / d)); const float r = RAD2DEG(acosf(w.dot2D(XVECf))); // when v is parallel to world z-axis, dot // with x-axis will be 0 and rotation then // is ambiguous (can be +90 or -90): check // the sign of v.z to determine if we are // looking down +z or -z distance = cDistance = d; elevation = cElevation = e; rotation = cRotation = (v.z > 0.0f)? 180.0f + r: 180.0f - r; cen = t; active = false; }
void Vizzer::init(ApplicationData &app) { assets.init(app.graphics); vec3f eye(0.5f, 0.2f, 0.5f); vec3f worldUp(0.0f, 1.0f, 0.0f); camera = Cameraf(-eye, eye, worldUp, 60.0f, (float)app.window.getWidth() / app.window.getHeight(), 0.01f, 10000.0f); camera = Cameraf("-0.774448 1.24485 -1.35404 0.999848 1.80444e-009 -0.0174517 0.0152652 -0.484706 0.874544 -0.00845866 -0.874677 -0.484632 0 1 0 60 1.25 0.01 10000"); //-0.774448 1.24485 -1.35404 0.999848 1.80444e-009 -0.0174517 0.0152652 -0.484706 0.874544 -0.00845866 -0.874677 -0.484632 0 1 0 60 1.25 0.01 10000 font.init(app.graphics, "Calibri"); state.bundler.loadSensorFile(constants::dataDir + "/sensors/sample.sensor"); state.bundler.computeKeypoints(); state.bundler.addCorrespondences(1); state.bundler.addCorrespondences(2); state.bundler.addCorrespondences(4); state.bundler.addCorrespondences(6); state.bundler.addCorrespondences(12); state.bundler.addCorrespondences(20); state.bundler.addCorrespondences(30); state.bundler.addCorrespondences(40); state.bundler.addCorrespondences(60); state.bundler.addCorrespondences(80); state.bundler.addCorrespondences(100); state.bundler.solve(); state.bundler.thresholdCorrespondences(0.01); state.bundler.solve(); //state.bundler.thresholdCorrespondences(0.005); //state.bundler.solve(); state.bundler.saveKeypointCloud(constants::debugDir + "result.ply"); state.bundler.saveResidualDistribution(constants::debugDir + "residuals.csv"); state.frameCloudsSmall.resize(state.bundler.frames.size()); state.frameCloudsBig.resize(state.bundler.frames.size()); for (auto &frame : iterate(state.bundler.frames)) { vector<TriMeshf> meshesSmall, meshesBig; auto makeColoredBox = [](const vec3f ¢er, const vec4f &color, float radius) { TriMeshf result = ml::Shapesf::box(radius, radius, radius); result.transform(mat4f::translation(center)); result.setColor(color); return result; }; const int stride = 5; for (auto &p : frame.value.depthImage) { vec2i coord((int)p.x, (int)p.y); const vec3f framePos = frame.value.localPos(coord); if (!framePos.isValid() || p.x % stride != 0 || p.y % stride != 0) continue; meshesSmall.push_back(makeColoredBox(frame.value.frameToWorld * framePos, vec4f(frame.value.colorImage(coord)) / 255.0f, 0.002f)); meshesBig.push_back(makeColoredBox(frame.value.frameToWorld * framePos, vec4f(frame.value.colorImage(coord)) / 255.0f, 0.005f)); } state.frameCloudsSmall[frame.index] = D3D11TriMesh(app.graphics, Shapesf::unifyMeshes(meshesSmall)); state.frameCloudsBig[frame.index] = D3D11TriMesh(app.graphics, Shapesf::unifyMeshes(meshesBig)); } state.selectedCamera = 0; }
vec3f RayTracer::traceRay( Scene *scene, const ray& r, const vec3f& thresh, int depth, isect& i, vector<const SceneObject*>& stack ) { if( depth>=0 && thresh[0] > threshold - RAY_EPSILON && thresh[1] > threshold - RAY_EPSILON && thresh[2] > threshold - RAY_EPSILON && scene->intersect( r, i ) ) { // YOUR CODE HERE // An intersection occured! We've got work to do. For now, // this code gets the material for the surface that was intersected, // and asks that material to provide a color for the ray. // This is a great place to insert code for recursive ray tracing. // Instead of just returning the result of shade(), add some // more steps: add in the contributions from reflected and refracted // rays. const Material& m = i.getMaterial(); vec3f color = m.shade(scene, r, i); //calculate the reflected ray vec3f d = r.getDirection(); vec3f position = r.at(i.t); vec3f direction = d - 2 * i.N * d.dot(i.N); ray newray(position, direction); if(!m.kr.iszero()) { vec3f reflect = m.kr.multiply(traceRay(scene, newray, thresh.multiply(m.kr), depth-1, stack).clamp()); color += reflect; } //calculate the refracted ray double ref_ratio; double sin_ang = d.cross(i.N).length(); vec3f N = i.N; //Decide going in or out const SceneObject *mi = NULL, *mt = NULL; int stack_idx = -1; vector<const SceneObject*>::reverse_iterator itr; //1 use the normal to decide whether to go in or out //0: travel through, 1: in, 2: out char travel = 0; if(i.N.dot(d) <= -RAY_EPSILON) { //from outer surface in //test whether the object has two face ray test_ray(r.at(i.t) + d * 2 * RAY_EPSILON, -d); isect test_i; if(i.obj->intersect(r, test_i) && test_i.N.dot(N) > -RAY_EPSILON) { //has interior travel = 1; } } else { travel = 2; } if(travel == 1) { if(!stack.empty()) { mi = stack.back(); } mt = i.obj; stack.push_back(mt); } else if(travel == 2) { //if it is in our stack, then we must pop it for(itr = stack.rbegin(); itr != stack.rend(); ++itr) { if(*itr == i.obj) { mi = *itr; vector<const SceneObject*>::iterator ii = itr.base() - 1; stack_idx = ii - stack.begin(); stack.erase(ii); break; } } if(!stack.empty()) { mt = stack.back(); } } if(N.dot(d) >= RAY_EPSILON) { N = -N; } ref_ratio = (mi?(mi->getMaterial().index):1.0) / (mt?(mt->getMaterial().index):1.0); if(!m.kt.iszero() && (ref_ratio < 1.0 + RAY_EPSILON || sin_ang < 1.0 / ref_ratio + RAY_EPSILON)) { //No total internal reflection //We do refraction now double c = N.dot(-d); direction = (ref_ratio * c - sqrt(1 - ref_ratio * ref_ratio * (1 - c * c))) * N + ref_ratio * d; newray = ray(position, direction); vec3f refraction = m.kt.multiply(traceRay(scene, newray, thresh.multiply(m.kt), depth-1, stack).clamp()); color += refraction; } if(travel == 1) { stack.pop_back(); } else if(travel == 2) { if(mi) { stack.insert(stack.begin() + stack_idx, mi); } } return color; } else { // No intersection. This ray travels to infinity, so we color // it according to the background color, which in this (simple) case // is just black. if(m_bBackground && bg) { double u, v; angleToSphere(r.getDirection(), u, v); //Scale to [0, 1]; u /= 2 * M_PI; v /= M_PI; int tx = int(u * bg_width), ty = bg_height - int(v * bg_height); return vec3f(bg[3 * (ty * bg_width + tx)] / 255.0, bg[3 * (ty * bg_width + tx) + 1] / 255.0, bg[3 * (ty * bg_width + tx) + 2] / 255.0); } else { return vec3f( 0.0, 0.0, 0.0 ); } } }
void Entity::setVelocity(vec3f velocity) { velocity.normalize(); mVelocity = velocity * mMaxSpeed; }
void Shader::setUniform(const std::string &name, const vec3f v, bool warn) { glUniform3f(uniform(name, warn), v.x(), v.y(), v.z()); }