void special(int key, int x, int y) { switch(key) { case GLUT_KEY_F1: //resolution = 1; //chemin = parking_actuel->solution(); break; case GLUT_KEY_F2: //resolution = 0; //load_next_level(); break; case GLUT_KEY_LEFT: angle = (angle + 5) % 360; break; case GLUT_KEY_RIGHT: angle = (angle - 5) % 360; break; case GLUT_KEY_UP: posY +=5; break; case GLUT_KEY_DOWN: posY -= 5; break; } positionCamera(); glutPostRedisplay(); }
/** This function handles the end camera. It adjusts the camera position * according to the current camera type, and checks if a switch to the * next camera should be made. * \param dt Time step size. */ void Camera::handleEndCamera(float dt) { // First test if the kart is close enough to the next end camera, and // if so activate it. if( m_end_cameras.size()>0 && m_end_cameras[m_next_end_camera].isReached(m_kart->getXYZ())) { m_current_end_camera = m_next_end_camera; if(m_end_cameras[m_current_end_camera].m_type ==EndCameraInformation::EC_STATIC_FOLLOW_KART) { m_camera->setPosition( m_end_cameras[m_current_end_camera].m_position.toIrrVector() ); } m_camera->setFOV(m_fov); m_next_end_camera++; if(m_next_end_camera>=(unsigned)m_end_cameras.size()) m_next_end_camera = 0; } EndCameraInformation::EndCameraType info = m_end_cameras.size()==0 ? EndCameraInformation::EC_AHEAD_OF_KART : m_end_cameras[m_current_end_camera].m_type; switch(info) { case EndCameraInformation::EC_STATIC_FOLLOW_KART: { // Since the camera has no parents, we can use the relative // position here (otherwise we need to call updateAbsolutePosition // after changing the relative position in order to get the right // position here). const core::vector3df &cp = m_camera->getPosition(); const Vec3 &kp = m_kart->getXYZ(); // Estimate the fov, assuming that the vector from the camera to // the kart and the kart length are orthogonal to each other // --> tan (fov) = kart_length / camera_kart_distance // In order to show a little bit of the surrounding of the kart // the kart length is multiplied by 6 (experimentally found) float fov = 6*atan2(m_kart->getKartLength(), (cp-kp.toIrrVector()).getLength()); m_camera->setFOV(fov); m_camera->setTarget(m_kart->getXYZ().toIrrVector()); break; } case EndCameraInformation::EC_AHEAD_OF_KART: { const KartProperties *kp=m_kart->getKartProperties(); float cam_angle = kp->getCameraBackwardUpAngle(); positionCamera(dt, /*above_kart*/0.75f, cam_angle, /*side_way*/0, 2.0f*m_distance, /*smoothing*/false); break; } default: break; } // switch } // handleEndCamera
// Window's event handler to process the control panel's sliders. afx_msg void BoidsWin::OnHScroll( UINT SBCode, UINT Pos, CScrollBar *SB ) { // Only update the correct slider. // Cast the pointer to the scroll bar - // - to a pointer to a void for the comparison. if ( static_cast< void * >( SB ) == &sliderNum ) { // Process the slider for the number of flyers. // Adjust the number of flyers. requiredFlyers = sliderNum.GetPos(); } // Process the slider for the object's size. if ( static_cast< void * >( SB ) == &sliderSize ) { for ( int index = 0; index < numFlyers; index++ ) { D3DVALUE posSize = ( sliderSize.GetPos() * SIZE_SCALE_FACTOR ); flyers[ index ] -> setSize( posSize ); } // If the camera is pointing to the boid, update it's position. // This is to prevent an internal view of the boid. if ( cameraView == CAM_BOID ) { positionCamera( ); } } // Process the first slider for the position of the camera. if ( static_cast< void * >( SB ) == &sliderCam ) { cameraPosition = sliderCam.GetPos( ); positionCamera( ); } // Process the second slider for the position of the camera. if ( static_cast< void * >( SB ) == &sliderCam2 ) { cameraPosition2 = sliderCam2.GetPos( ); positionCamera( ); } SetFocus( ); // Set the focus back to the main window. }
/** Called once per time frame to move the camera to the right position. * \param dt Time step. */ void Camera::update(float dt) { if (m_kart == NULL) return; // cameras not attached to kart must be positioned manually float above_kart, cam_angle, side_way, distance; bool smoothing; // The following settings give a debug camera which shows the track from // high above the kart straight down. if (UserConfigParams::m_camera_debug==1) { core::vector3df xyz = m_kart->getXYZ().toIrrVector(); m_camera->setTarget(xyz); xyz.Y = xyz.Y+55; xyz.Z -= 5.0f; m_camera->setPosition(xyz); // To view inside tunnels (FIXME 27>15 why??? makes no sense // - the kart should not be visible, but it works) m_camera->setNearValue(27.0); } else if (m_mode==CM_FINAL) { handleEndCamera(dt); } // If an explosion is happening, stop moving the camera, // but keep it target on the kart. else if (dynamic_cast<ExplosionAnimation*>(m_kart->getKartAnimation())) { getCameraSettings(&above_kart, &cam_angle, &side_way, &distance, &smoothing); // The camera target needs to be 'smooth moved', otherwise // there will be a noticable jump in the first frame // Aim at the usual same position of the kart (i.e. slightly // above the kart). core::vector3df wanted_target(m_kart->getXYZ().toIrrVector() +core::vector3df(0, above_kart, 0) ); core::vector3df current_target = m_camera->getTarget(); // Note: this code is replicated from smoothMoveCamera so that // the camera keeps on pointing to the same spot. current_target += ((wanted_target-current_target)*m_target_speed)*dt; m_camera->setTarget(current_target); } else { getCameraSettings(&above_kart, &cam_angle, &side_way, &distance, &smoothing); positionCamera(dt, above_kart, cam_angle, side_way, distance, smoothing); } if (UserConfigParams::m_graphical_effects && m_rain) { m_rain->setPosition( getCameraSceneNode()->getPosition() ); m_rain->update(dt); } // UserConfigParams::m_graphical_effects } // update
/** Called once per time frame to move the camera to the right position. * \param dt Time step. */ void CameraDebug::update(float dt) { Camera::update(dt); // To view inside tunnels in top mode, increase near value m_camera->setNearValue(m_default_debug_Type==CM_DEBUG_TOP_OF_KART ? 27.0f : 1.0f); float above_kart, cam_angle, side_way, distance; // The following settings give a debug camera which shows the track from // high above the kart straight down. if (m_default_debug_Type==CM_DEBUG_TOP_OF_KART) { core::vector3df xyz = m_kart->getXYZ().toIrrVector(); m_camera->setTarget(xyz); xyz.Y = xyz.Y+55; xyz.Z -= 5.0f; m_camera->setPosition(xyz); } else if (m_default_debug_Type==CM_DEBUG_SIDE_OF_KART) { core::vector3df xyz = m_kart->getXYZ().toIrrVector(); Vec3 offset(3, 0, 0); offset = m_kart->getTrans()(offset); m_camera->setTarget(xyz); m_camera->setPosition(offset.toIrrVector()); } // If an explosion is happening, stop moving the camera, // but keep it target on the kart. else if (dynamic_cast<ExplosionAnimation*>(m_kart->getKartAnimation())) { getCameraSettings(&above_kart, &cam_angle, &side_way, &distance); // The camera target needs to be 'smooth moved', otherwise // there will be a noticable jump in the first frame // Aim at the usual same position of the kart (i.e. slightly // above the kart). // Note: this code is replicated from smoothMoveCamera so that // the camera keeps on pointing to the same spot. core::vector3df current_target = (m_kart->getXYZ().toIrrVector() +core::vector3df(0, above_kart, 0)); m_camera->setTarget(current_target); } else { getCameraSettings(&above_kart, &cam_angle, &side_way, &distance); positionCamera(dt, above_kart, cam_angle, side_way, distance); } } // update
void Camera::setViewMatrix(const mat4& mvMat) { mat4 inv = glm::inverse(mvMat); // preserve the focallength float focallength = glm::length(focus_ - position_); // calculate world-coordinates vec4 pos = (inv * vec4(0.f, 0.f, 0.f, 1.f)); vec4 look = (inv * vec4(0.f, 0.f, -1.f, 0.f)); vec4 focus = pos + focallength * look; vec4 up = (inv * vec4(0.f, 1.f, 0.f, 0.f)); positionCamera(pos.xyz(), focus.xyz(), up.xyz()); updateVM(); }
void Camera::setViewMatrix(const mat4& mvMat) { mat4 inv; if (mvMat.invert(inv)) { // preserve the focallength float focallength = length(focus_ - position_); // calculate world-coordinates vec4 pos = (inv * vec4(0.f, 0.f, 0.f, 1.f)); vec4 look = (inv * vec4(0.f, 0.f, -1.f, 0.f)); vec4 focus = pos + focallength * look; vec4 up = (inv * vec4(0.f, 1.f, 0.f, 0.f)); positionCamera(pos.xyz(), focus.xyz(), up.xyz()); viewMatrix_ = mvMat; } }
int main(int argc, char ** argv) { SimpleApplication::OnPreInitialize(); timer1.Start(); timer2.Start(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); // | GLUT_STEREO glutInitWindowSize(1024, 768); glutCreateWindow("BiG Hour"); glutDisplayFunc(draw); glutIdleFunc(idle); glutReshapeFunc(reshape); glutSpecialFunc(special); glutMouseFunc(mouse); glutMotionFunc(motion); glutPassiveMotionFunc(motion); // Init GL glEnable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); creer_cube(); creer_plateau(); // Init Camera positionCamera(); SimpleApplication::OnPostInitialize(); glutMainLoop(); return 0; }
void drawBackFaceCulling () { glClearColor(1.f, 1.f, 1.f, 1.f); // set background colour glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear buffers glMatrixMode(GL_MODELVIEW); // set for model and viewing operations glLoadIdentity(); // reset drawing glEnable(GL_DEPTH_TEST); // enable basic depth sorting glEnable(GL_CULL_FACE); // Enable face-culling glFrontFace(GL_CCW); // Set winding to counter-clockwise positionCamera(); // Position camera glPushMatrix(); // Create central cube glRotatef(45.f, 0.f, 1.f, 0.f); // Rotate around y by 45 degrees glColor3f(0.5f, 0.5f, 0.5f); // Mid-gray glutSolidCube(25.f); // Create solid cube (has coored winding by default) glPopMatrix(); // restore rotation to 0 glColor3f(1.f, 0.5f, 0.5f); // Pale red glBegin(GL_QUADS); // Create far QUAD (wound counter clockwise) glVertex3f(-50.f, 50.f, -100.f); // top left glVertex3f(-50.f, -50.f, -100.f); // bottom left glVertex3f(50.f, -50.f, -100.f); // bottom right glVertex3f(50.f, 50.f, -100.f); // top right glEnd(); // This quad will be invisible initially (try rotating the camera around the scene using LEFT and RIGHT) glColor3f(0.5f, 0.5f, 1.f); // Pale blue glBegin(GL_QUADS); // Create near QUAD (wound clockwise) glVertex3f(-50.f, 50.f, 100.f); // top left glVertex3f(50.f, 50.f, 100.f); // top right glVertex3f(50.f, -50.f, 100.f); // bottom right glVertex3f(-50.f, -50.f, 100.f); // bottom left glEnd(); checkGLError(); // check any OpenGL errors glutSwapBuffers(); // Flush and swap buffers }
// Member function to update the rest of the simulation, - // - after removing a quantity of flyers. void BoidsWin::updateRemoveFlyers( ) { // Set the text of the static control to show the current number. CString numberObjText( "Number of Flyers: " ); char buffer[ 50 ]; // Temporary buffer for string conversion. numberObjText += _itoa( numFlyers, buffer, 10 ); staticNum.SetWindowText( numberObjText ); // Check if the view from a boid has been invalidated. if ( numFlyers == 0 && cameraView == CAM_BOID ) { // Remove the camera from a boid, if it is on one. if ( cameraOnBoid == true ) { scene -> AddChild( camera ); cameraOnBoid = false; } cameraView = CAM_ABOVE; positionCamera( ); } // Move the slider to the correct position. sliderNum.SetPos( numFlyers ); }
// Set up the necessary requirements to start the simulation. void BoidsWin::createScene( ) { // Create the root frame for the scene. d3drm -> CreateFrame( NULL, &scene ); srand( time( 0 ) ); // Initialise random number generation. int randomX, randomY, randomZ; int speed, hHeading, vHeading; // Create the flyers at random positions and velocities. BoidsFlyer *newObjPtr; for ( int number = 0; number < numFlyers; number++ ) { newObjPtr = new BoidsFlyer( ); flyers.push_back( newObjPtr ); flyers[ number ] -> build( this, d3drm, scene ); randomX = ( ( 1 + rand( ) % (int)( X_LIMIT * 2 ) ) - X_LIMIT ); randomY = ( ( 1 + rand( ) % 10 ) - 5 ); randomZ = ( ( 1 + rand( ) % (int)( Z_LIMIT * 2 ) ) - Z_LIMIT ); speed = ( ( 1 + rand( ) % (int)( maximumSpeed - minimumSpeed ) ) + minimumSpeed ); hHeading = ( ( 1 + rand( ) % 360 ) - 180 ); vHeading = ( ( 1 + rand( ) % 10 ) - 5 ); flyers[ number ] -> setPosition( randomX, randomY, randomZ ); flyers[ number ] -> setReqVelocity( speed, hHeading, vHeading ); } // All the flyers meshes are set to the right type to start with. flyerMeshesChanged = numFlyers; // Set all the object's sizes to the position of the size slider. for ( int index = 0; index < numFlyers; index++ ) { double posSize = ( sliderSize.GetPos() * SIZE_SCALE_FACTOR ); flyers[ index ] -> setSize( posSize ); } // Create the directional lighting. LPDIRECT3DRMLIGHT light; d3drm -> CreateLightRGB( D3DRMLIGHT_DIRECTIONAL, D3DVALUE( 1.0 ), D3DVALUE( 1.0 ), D3DVALUE( 1.0 ), &light ); // Create a frame for the lighting. LPDIRECT3DRMFRAME lightFrame; d3drm -> CreateFrame( scene, &lightFrame ); lightFrame -> AddLight( light ); lightFrame -> SetOrientation( scene, D3DVALUE( 0 ), D3DVALUE( -1 ), D3DVALUE( 1 ), D3DVALUE( 0 ), D3DVALUE( 1 ), D3DVALUE( 1 ) ); light -> Release( ); light = NULL; lightFrame -> Release( ); lightFrame = NULL; // Create some white ambient lighting and add it to the scene. d3drm -> CreateLightRGB( D3DRMLIGHT_AMBIENT, D3DVALUE( 0.4 ), D3DVALUE( 0.4 ), D3DVALUE( 0.4 ), &light ); scene -> AddLight( light ); light -> Release( ); light = NULL; // Create a viewport frame for the viewport called camera. d3drm -> CreateFrame( scene, &camera ); // Set the camera's position with reference to the sliderCam control. cameraPosition = sliderCam.GetPos(); cameraPosition2 = sliderCam2.GetPos(); positionCamera( ); // Create the viewport and attach it to the frame called camera. d3drm -> CreateViewport( device, camera, 0, 0, device -> GetWidth( ), device -> GetHeight( ), &viewPort ); // Set the range of vision, default is 100. viewPort -> SetBack( D3DVALUE( VIEWPORT_BACK_DISTANCE ) ); // Set the background colour to a light blue (sky colour). scene -> SetSceneBackground( RGB_BACKGROUND ); // Create the landscape. landscape.build( this, d3drm, scene, DEFAULT_LANDSCAPE_SOLID_STYLE ); }