void SpecialCallBack(int key, int x, int y) { bool needRedisplay = true; switch (key) { case GLUT_KEY_LEFT: { animCamera = false; cl_float3 t = scene->cam.viewCenter; vecSub(t, scene->cam.eye); t.s[0] = t.s[0] * cosf(-ROTATE_STEP) - t.s[2] * sinf(-ROTATE_STEP); t.s[2] = t.s[0] * sinf(-ROTATE_STEP) + t.s[2] * cosf(-ROTATE_STEP); vecAdd(t, scene->cam.eye); camLookAt(scene->cam, t); break; } case GLUT_KEY_RIGHT: { animCamera = false; cl_float3 t = scene->cam.viewCenter; vecSub(t, scene->cam.eye); t.s[0] = t.s[0] * cosf(ROTATE_STEP) - t.s[2] * sinf(ROTATE_STEP); t.s[2] = t.s[0] * sinf(ROTATE_STEP) + t.s[2] * cosf(ROTATE_STEP); vecAdd(t, scene->cam.eye); camLookAt(scene->cam, t); break; } case GLUT_KEY_PAGE_UP: { animCamera = false; cl_float3 t; vecInit(t, 0.f, MOVE_STEP / 4.f, 0.f); vecAdd(scene->cam.viewCenter, t); camUpdate(scene->cam); break; } case GLUT_KEY_PAGE_DOWN: { animCamera = false; cl_float3 t; vecInit(t, 0.f, -MOVE_STEP / 4.f, 0.f); vecAdd(scene->cam.viewCenter, t); camUpdate(scene->cam); break; } default: needRedisplay = false; break; } if (needRedisplay) glutPostRedisplay(); }
void updateCamera(Scene* scene) { double time = WallClockTime() - sceneTimeOffset; double rtime = (time / 21.35634); // camera rotation time rtime = rtime - floor(rtime); double btime = (time / 59.8752); // camera bobbing time btime = btime - floor(btime); double ltime = (time / 98.7654); // light rotation time ltime = ltime - floor(ltime); double lbtime = (time / 92.764); // light bobbing time lbtime = lbtime - floor(lbtime); camInit(scene->cam); cl_float3 pos; cl_float3 center; vecInit(center, 14.9f, 9.7f, -14.85f); vecInit(pos, 33.0f * sin(rtime * M_PI * 2.0), 8.0f * sin(btime * M_PI * 2.0 + 0.3), 33.0f * (cos(rtime * M_PI * 2.0) - 0.11)); vecAdd(pos, center); camMove(scene->cam, pos); camLookAt(scene->cam, center); vecInit(pos, 5.0f * sin(-ltime * M_PI * 2.0), 8.0f + 6.0f * sin(lbtime * M_PI * 2.0), 5.0f * cos(ltime * M_PI * 2.0)); vecNormalize(pos); scene->lightDir = pos; }
/********************************************* Reset cam orientation, zoom, rotation **********************************************/ void App::resetCam(){ for(int i=0;i<3;i++){ fRot[i] = 0.0f; } fRot[0] = 20; fZoom = 0.0f; dragVel = Vector2(0,0); endDrag(); camSetPos(0, -SLAB_SIZE/3, SLAB_SIZE); camLookAt(0, 0, 0); }
CameraManager::CameraManager() { m_pos = D3DXVECTOR3(0.0f, 0.0f, -10.0f); m_target = Vec3f(0.0f, 0.0f, 0.0f); m_oldTarget = Vec3f(0.0f, 0.0f, 0.0f); m_up = D3DXVECTOR3(0.0f, 1.0f, 0.0f); m_miniMapPos = D3DXVECTOR3(0.0f, 20.0f, -10.0f); m_vel = D3DXVECTOR3(0.0f, 0.0f, 0.0f); m_angleV = M_PI_2; m_angleH = -M_PI; m_mouseLeftDown = false; m_rotAngle = 0; m_zoom = 0.0f; m_zoomVel = 0.0f; m_xRot = 0; m_yRot = 0; m_zRot = 0; m_moveAcc = 0.0f; m_moveVel = 0.0f; m_wKeyDown = false; m_aKeyDown = false; m_sKeyDown = false; m_dKeyDown = false; m_pgUpKeyDown = false; m_pgDownKeyDown = false; m_lMouseButtonDown = false; m_rMouseButtonDown = false; m_mouseWheelDelta = 0.0f; TheEventPoller::Instance()->AddHandler(this); m_camera.position = Vec3f(-0.5, 10.0f, 19.5); Vec3f camLookAt(-0.5, 7.0f, 10.0f); m_camera.direction = camLookAt - m_camera.position; m_camera.direction.Normalise(); m_camera.right.CrossProduct(Vec3f(0.0f, 1.0f, 0.0f), m_camera.direction); m_camera.right.Normalise(); m_camera.down.CrossProduct(m_camera.right, m_camera.direction); m_spaceBarDown = false; m_ctrlDown = false; m_camWasMoved = true; m_acceleration = Vec3f(0.05f, 0.05f, 0.05f); }
void motion(SDL_MouseMotionEvent motion) { //fprintf(stderr, "debug: motion()\n"); // Le WarpMouse(320, 320) génère également un évènement // motion qu'il ne faut pas prendre en compte. if (doMotion && (motion.x != 320 || motion.y != 320)) { camTourneEtLeve(&camera, motion.xrel, motion.yrel); camLookAt(&camera); SDL_WarpMouse(320, 320); } else { // La première fois xrel et yrel ne sont pas pertinents, // on ne déplace donc pas la caméra doMotion = 1; } }
// initilize scene // load obj files, set lights, camera void initScene(const std::string& textureName) { // for loading triangle mesh Matrix4x4 matrix; loadObj(objName.c_str(), vertices, normals, texcoord, mesh); matrix.m[2][3] = -9.f; matrix.m[1][3] = 0.f; Transform* tr = new Transform(matrix); trfList.push_back(tr); Mesh* newmesh = new Mesh(tr, &vertices, &normals, &mesh); objList.push_back(newmesh); // object initialize, for sphere /*Matrix4x4 mat; mat.m[2][3] = -16.f; Transform* tr2 = new Transform(mat); trfList.push_back(tr2); Sphere *sphere1 = new Sphere(tr2, 6.f); objList.push_back(sphere1);*/ // initialize image plane intensity = new float[WINX * WINY * 3]; memset(intensity, 0, WINX*WINY * 3 * sizeof(float)); // set up the camera Point camCenter, camLookAt(0.f, 0.f, -1.f); Vector camUp(0.f, 1.f, 0.f); float fovy = 60.f; camera = new PerspectiveCamera(camCenter, camLookAt, camUp, fovy); // set up the projectionLight Point e(-1.5f, 3.f, 15.f), gaze(-1.2f, 1.3f, 0.f); Vector up(0.f, 0.9f, 0.43589f); /* For sphere ray tracing test */ /*Point e(0.f, 0.f, 7.f), gaze(.0f, 0.f, -1.f); Vector up(0.f, 1.f, 0.f);*/ Transform light2world = LookAt(e, gaze, up); //string texname("grid.jpg"); float projFovy = 20.0; ProjectionLight *projector = new ProjectionLight(Inverse(light2world), textureName, projFovy); // set up the sampler sampler = new SimpleSampler(WINX, WINY); scene = new Scene(sampler, camera, &objList, projector, WINX, WINY); }
void right() { camPasDeCote(&camera, speed); camLookAt(&camera); }
void left() { camPasDeCote(&camera, -speed); camLookAt(&camera); }
void dezoom() { camAvance(&camera, -speed); camLookAt(&camera); }
void zoom() { camAvance(&camera, speed); camLookAt(&camera); }
void reinitCam() { camFixePosition(&camera, 0, 0, 50, 0, 0, 0, 0, 1, 0); camLookAt(&camera); }
void setupAnim(Scene* scene, int imgWidth, int imgHeight) { // Positions Everybody! scene->numSpheres = NUMSPHERES; // three mirrored juggling spheres for (int i = 0; i <= 2; i++) { scene->spheres[i].radius = 1.4f; vecInit(scene->spheres[i].color, 0.5f, 0.5f, 0.5f); scene->spheres[i].ambient = 0.1f; scene->spheres[i].diffuse = 0.3f; scene->spheres[i].highlight = 0.8f; scene->spheres[i].roughness = 0.05f; scene->spheres[i].reflection = 0.8f; } vecInit(scene->spheres[0].center, 11.0f, 0.0f, 0.0f); vecInit(scene->spheres[1].center, 11.0f, 0.0f, 0.0f); vecInit(scene->spheres[2].center, 11.0f, 0.0f, 0.0f); // torso: seven spheres for (int i = 3; i <= 10; i++) { float fraction = float(i - 3) / 7.0f; scene->spheres[i].radius = 1.6f + 0.4f * fraction; vecInit(scene->spheres[i].center, 15.1f, 8.5f + 3.2f * fraction, -15.1f); vecInit(scene->spheres[i].color, 0.843f, 0.12f, 0.11f); scene->spheres[i].ambient = 0.3f; scene->spheres[i].diffuse = 0.8f; scene->spheres[i].highlight = 1.0f; scene->spheres[i].roughness = 0.1f; scene->spheres[i].reflection = 0.0f; } // head scene->spheres[11].radius = 1.4; vecInit(scene->spheres[11].center, 15.1f, 15.5f, -15.1f); vecInit(scene->spheres[11].color, 0.95f, 0.64f, 0.63f); scene->spheres[11].ambient = 0.25f; scene->spheres[11].diffuse = 0.9f; scene->spheres[11].highlight = 1.0f; scene->spheres[11].roughness = 0.1f; scene->spheres[11].reflection = 0.0f; // neck scene->spheres[12].radius = 0.5; vecInit(scene->spheres[12].center, 15.1f, 14.0f, -15.1f); vecInit(scene->spheres[12].color, 0.95f, 0.64f, 0.63f); scene->spheres[12].ambient = 0.25f; scene->spheres[12].diffuse = 0.9f; scene->spheres[12].highlight = 1.0f; scene->spheres[12].roughness = 0.1f; scene->spheres[12].reflection = 0.0f; // outer limb parts for (int i = 13; i <= 20; i++) { for (int j = 0; j < 4; j++) { scene->spheres[i + 17 * j].radius = 0.25f + 0.25f * float(i - 13) / 7.0f; vecInit(scene->spheres[i + 17 * j].center, 0.0f, 0.0f, 0.0f); vecInit(scene->spheres[i + 17 * j].color, 0.95f, 0.64f, 0.63f); scene->spheres[i + 17 * j].ambient = 0.25f; scene->spheres[i + 17 * j].diffuse = 0.9f; scene->spheres[i + 17 * j].highlight = 1.0f; scene->spheres[i + 17 * j].roughness = 0.1f; scene->spheres[i + 17 * j].reflection = 0.0f; } } // inner limb parts for (int i = 21; i <= 29; i++) { for (int j = 0; j < 4; j++) { scene->spheres[i + 17 * j].radius = 0.5f; vecInit(scene->spheres[i + 17 * j].center, 0.0f, 0.0f, 0.0f); vecInit(scene->spheres[i + 17 * j].color, 0.95f, 0.64f, 0.63f); scene->spheres[i + 17 * j].ambient = 0.25f; scene->spheres[i + 17 * j].diffuse = 0.9f; scene->spheres[i + 17 * j].highlight = 1.0f; scene->spheres[i + 17 * j].roughness = 0.1f; scene->spheres[i + 17 * j].reflection = 0.0f; } } // eyes scene->spheres[81].radius = 0.4; vecInit(scene->spheres[81].center, 14.2f, 15.4f, -14.4f); vecInit(scene->spheres[81].color, 0.121f, 0.105f, 0.58f); scene->spheres[81].ambient = 0.25f; scene->spheres[81].diffuse = 0.9f; scene->spheres[81].highlight = 1.0f; scene->spheres[81].roughness = 0.1f; scene->spheres[81].reflection = 0.0f; scene->spheres[82].radius = 0.4; vecInit(scene->spheres[82].center, 14.2f, 15.4f, -14.4f); vecInit(scene->spheres[82].color, 0.121f, 0.105f, 0.58f); scene->spheres[82].ambient = 0.25f; scene->spheres[82].diffuse = 0.9f; scene->spheres[82].highlight = 1.0f; scene->spheres[82].roughness = 0.1f; scene->spheres[82].reflection = 0.0f; // hair scene->spheres[83].radius = 1.4; vecInit(scene->spheres[83].center, 15.2f, 15.4f, -15.1f); vecInit(scene->spheres[83].color, 0.15f, 0.066f, 0.09f); scene->spheres[83].ambient = 0.25f; scene->spheres[83].diffuse = 0.9f; scene->spheres[83].highlight = 1.0f; scene->spheres[83].roughness = 0.1f; scene->spheres[83].reflection = 0.0f; // Lights! vecInit(scene->lightDir, -56.4f, 68.6f, 34.7f); vecNormalize(scene->lightDir); // Camera! camInit(scene->cam); cl_float3 tmp; vecInit(tmp, -7.8f, 10.0f, 7.8f); camMove(scene->cam, tmp); vecInit(tmp, 80.0f, 10.7f, -100.0f); camLookAt(scene->cam, tmp); scene->cam.imgWidth = imgWidth; scene->cam.imgHeight = imgHeight; // Action! animatePositions(scene, true); }