void Camera::move(int direction){// direction = 1 to front, direction = -1 to backwards i[0] += speed * direction * getCos(); i[1] += speed * direction * d[1]; i[2] += speed * direction * getSin(); refreshDirection(); refreshLookAt(); }
Vector3r Cell::shearAlignedExtents(const Vector3r& perpExtent) const{ if(!hasShear()) return perpExtent; Vector3r ret(perpExtent); const Vector3r& cos=getCos(); for(short ax:{0,1,2}){ short ax1=(ax+1)%3, ax2=(ax+2)%3; ret[ax1]+=.5*perpExtent[ax1]*(1/cos[ax]-1); ret[ax2]+=.5*perpExtent[ax2]*(1/cos[ax]-1); } return ret; }
void Body::applyForce(Vec2 f, Point p) { if (length(f) == 0) return; if (p != getMassCenter() && !fixedAngle) { Vec2 n = getMassCenter()-p; force += getOrientedVec(n, length(f)*getCos(f, n)); momentium += getSin(f,n)*length(f)*distance(p, getMassCenter()); } else { force += f; } }
void update() { position[3][0] = distance * getCos(); position[3][2] = distance * getSin(); rotation = rotate(glm::mat4(), (float)(-glutGet(GLUT_ELAPSED_TIME) * 0.025f * orbitRadians * SPEED), glm::vec3(0.0f, 1.0f, 0.0f)); orientation = position * rotation; return; }
bool PathCorrector::isLine(QList<QPoint> const & path) { const int minDifference = 8; const double minCos = 0.8; int difference = path.size(); while(difference >= minDifference) { for (int i = 0; i < path.size() / difference; i++) { double cos = getCos(path[i * difference], path[((2 * i + 1) * difference - 1) / 2], path[(i + 1) * difference - 1]); if (cos < minCos) return false; } difference /= 2; } return true; }
int getCurrentSinValue(void){ static uint8_t state = GETXSIN; int sinval; switch(state){ case GETXSIN: slopepos_current = slopepos[sinindex]; sinval = (int)getSin(sinpos_current = sinpos[sinindex])+xoffs; break; case GETYSIN: sinval = (int)getCos(sinpos_current)+yoffs; default: break; } /*if (NOSINSTATE == ++state){ state = GETXSIN; }*/ //only two states. toggle them state ^= 1; return sinval; }
double getAngle(Vec2 a, Vec2 b) { if (det(a,b) > 0) return acos(getCos(a,b)); return M_PI+acos(getCos(a,b)); }
Vector3d getRayColor(Vector3d p0, Vector3d ray, int rec) { ray.normalize(); if (rec == 0) { return Vector3d(0,0,0); } // 最小のt double tmin = -1; Vector3d P, N, color; // レイを飛ばして球と交差するか求める for(int i = 0; i < sphere_n; i++) { double t_sphere = sphere[i].getIntersec(p0, ray); if( (t_sphere > 0) && (t_sphere < tmin || tmin == -1) ) { // 球との交点がある tmin = t_sphere; // ★前回の課題を参考に、球体の表面の色を計算で求め、colorVecに設定する double Is = 0; // 鏡面反射光 double Id = 0; // 拡散反射光 // // 拡散反射光を計算する // // 球と視点ベクトルの交点座標 P P = p0 + ray * t_sphere; // 球の中心座標 C Vector3d C = sphere[i].center; // 法線ベクトル N = P - C N = P - C; // Nを正規化する N.normalize(); // Lambertの反射(拡散反射光) Id = -Iin * Kd * getCos(N, lightDirection); Id = Id > 0 ? Id : 0; // // 鏡面反射光を計算する // // 反射光 R=2(L・N)N-L Vector3d R = reflect(-lightDirection, N); // Phongの反射(鏡面反射光) Is = Iin * Ks * pow2(getCos(R, ray), Ns); Is = Is > 0 ? Is : 0; double I = Id + Is + Ia; double r = I * sphere[i].cR; double g = I * sphere[i].cG; double b = I * sphere[i].cB; color.set(r,g,b); } } // レイを飛ばして床と交差するか求める double t_board = board.getIntersec(p0, ray); if( (t_board > 0) && (t_board < tmin || tmin == -1) ) { // 床との交点がある tmin = t_board; // ★床の表面の色を設定する P = p0 + ray * t_board; color = board.getColorVec(P.x, P.z); // ★球の影になる場合は、RGBの値をそれぞれ0.5倍する Vector3d ray2 = -lightDirection; for(int i = 0; i < sphere_n; i++) { double t = sphere[i].getIntersec(P, ray2); if(t>0) { color = color * 0.5; } } N.set(0,1,0); } if (tmin != -1) { return color + getRayColor(P, reflect(ray,N), rec-1) * 0.5; } else { // 何とも交差しない return Vector3d(0,0,0); } }
// 反射ベクトルを求める Vector3d reflect(Vector3d L, Vector3d N) { //return N * getCos(L, N) * 2 - L; return L - N * getCos(L, N) * 2; }
void Camera::refreshDirection(){ d[0] = getCos() + i[0]; d[2] = getSin() + i[2]; }