if (g3d->BeginDraw(engine->GetBeginDrawFlags() | CSDRAW_3DGRAPHICS))
{
// First get elapsed time from the virtual clock.
csTicks elapsed_time = vc->GetElapsedTicks ();
// Now rotate the camera according to keyboard state
float speed = (elapsed_time / 1000.0) * (0.06 * 20);
iCamera* c = view->GetCamera();
// Some start stuff for me
csVector3 v;
int i, n, intmeshcount;
// Get current camera rotation
csMatrix3 rot = c-> GetTransform ().GetO2T ();
/* rotX = getM3xAngle( rot );
rotY = getM3yAngle( rot );
rotZ = getM3zAngle( rot );
*/
meshlist = engine->GetMeshes();
intmeshcount = meshlist->GetCount();
if (kbd->GetKeyState (CSKEY_SHIFT))
{
if (PlayerMeshName=="Camera") {
// If the user is holding down shift, the arrow keys will cause
// the camera to strafe up, down, left or right from it's
// current position.
if (kbd->GetKeyState (CSKEY_RIGHT))
c->Move (CS_VEC_RIGHT * 4 * speed);
if (kbd->GetKeyState (CSKEY_LEFT))
c->Move (CS_VEC_LEFT * 4 * speed);
if (kbd->GetKeyState (CSKEY_UP))
c->Move (CS_VEC_UP * 4 * speed);
if (kbd->GetKeyState (CSKEY_DOWN))
c->Move (CS_VEC_DOWN * 4 * speed);
} else {
// Get and move the Players Mesh
// Move the current mesh
playermesh = meshlist->FindByName ( PlayerMeshName );
movable = playermesh->GetMovable ();
if (kbd->GetKeyState (CSKEY_UP)) {
//rot = c->GetTransform().GetO2T();
movable->UpdateMove ();
}
} // PlayerMesh == Camera
} // Key_Shift
else
{
// left and right cause the camera to rotate on the global Y
// axis; page up and page down cause the camera to rotate on the
// _camera's_ X axis (more on this in a second) and up and down
// arrows cause the camera to go forwards and backwards.
if (PlayerMeshName=="Camera") {
// handle keyboard input..
if(kbd->GetKeyState(CSKEY_RIGHT))
c->GetTransform().RotateThis(CS_VEC_ROT_RIGHT, speed);
if(kbd->GetKeyState(CSKEY_LEFT))
c->GetTransform().RotateThis(CS_VEC_ROT_LEFT, speed);
if ( (kbd->GetKeyState(CSKEY_UP)) || (kbd->GetKeyState(int('w'))) )
c->GetTransform().RotateThis(CS_VEC_TILT_UP, speed);
if ( (kbd->GetKeyState(CSKEY_DOWN)) || (kbd->GetKeyState(int('s'))) )
c->GetTransform().RotateThis(CS_VEC_TILT_DOWN, speed);
if(kbd->GetKeyState(int('a')))
c->GetTransform().RotateThis(CS_VEC_TILT_LEFT, speed);
if(kbd->GetKeyState(int('d')))
c->GetTransform().RotateThis (CS_VEC_TILT_RIGHT, speed);
if(kbd->GetKeyState(CSKEY_PGUP)) c->Move(CS_VEC_FORWARD * 4 * speed);
if(kbd->GetKeyState(CSKEY_PGDN)) c->Move(CS_VEC_BACKWARD * 4 * speed);
} else {
// Move the current mesh
playermesh = meshlist->FindByName ( PlayerMeshName );
movable = playermesh->GetMovable ();
if (kbd->GetKeyState (CSKEY_UP)) {
v = movable->GetTransform().This2Other (csVector3 (0, 0, 1 * speed ) );
movable->SetPosition( v );
movable->UpdateMove ();
}
if (kbd->GetKeyState (CSKEY_DOWN)) {
v = movable->GetTransform().This2Other (csVector3 (0, 0, -1 * speed ) );
movable->SetPosition( v );
movable->UpdateMove ();
}
if (kbd->GetKeyState (CSKEY_LEFT)) {
v = movable->GetTransform().This2Other (csVector3 (0, -1 * speed , 0 ) );
movable->SetPosition( v );
movable->UpdateMove ();
}
if (kbd->GetKeyState (CSKEY_RIGHT)) {
v = movable->GetTransform().This2Other (csVector3 (0, 1 * speed, 0 ) );
movable->SetPosition( v );
movable->UpdateMove ();
}
if (kbd->GetKeyState (CSKEY_PGUP)) {
v = movable->GetTransform().This2Other (csVector3 ( 1 * speed, 0 , 0 ) );
movable->SetPosition( v );
movable->UpdateMove ();
}
if (kbd->GetKeyState (CSKEY_PGDN)) {
v = movable->GetTransform().This2Other (csVector3 ( - 1 * speed, 0 , 0 ) );
movable->SetPosition( v );
movable->UpdateMove ();
}
} // End move Camera/playermesh
// Make Tab my mesh Select Key
if (kbd->GetKeyState (CSKEY_TAB))
{// Get new mesh for player
if ( PlayerMeshName == "Camera" ) {
playermesh = meshlist->Get(0);
PlayerMeshName = playermesh->QueryObject ()->GetName();
} else {
for (i=0 ; i<intmeshcount;i++) {
playermesh = meshlist->Get(i);
if ( playermesh->QueryObject ()->GetName() == PlayerMeshName) {
// Get next mesh or if last go back to first
if ( (i+1)<intmeshcount ) {
playermesh = meshlist->Get(i+1); // get next
PlayerMeshName = playermesh->QueryObject ()->GetName();
i= intmeshcount;
} else {
PlayerMeshName = "Camera" ; // back to camera
i= intmeshcount;
} // end if next or first
} // end if current mesh
} // end for
} // end if get new mesh
kbd->Reset();
} // End if keyboard state TAB
} // GetKeyState
// We now assign a new rotation transformation to the camera. You
// can think of the rotation this way: starting from the zero
// position, you first rotate "rotY" radians on your Y axis to get
// the first rotation. From there you rotate "rotX" radians on the
// your X axis to get the final rotation. We multiply the
// individual rotations on each axis together to get a single
// rotation matrix. The rotations are applied in right to left
// order .
csVector3 up ; //(0, 1, 0);
up = c->GetTransform ().GetUp();
rot = csXRotMatrix3 (rotX) * csYRotMatrix3 (rotY) ;
csOrthoTransform ot (rot , c->GetTransform().GetOrigin ());
// ot.RotateThis ( csVector3(1,0,0), rotX );
// ot.RotateThis ( csVector3(0,1,0), rotY );
// ot.RotateThis ( csVector3(0,0,1), rotZ );
// c->SetTransform (ot);
// Need new way to change camer, Up and forward must allways be linked to up
// and forward in screen
// Tell 3D driver we're going to display 3D things.
if (!g3d->BeginDraw (engine->GetBeginDrawFlags () | CSDRAW_3DGRAPHICS))
return;
// Tell the camera to render into the frame buffer.
view->Draw ();
if (!g3d->BeginDraw (CSDRAW_2DGRAPHICS))
return;
// Output 2d Info on top of 3d info
iGraphics2D* g2d = g3d->GetDriver2D ();
font = g2d->GetFontServer()->LoadFont(CSFONT_LARGE);
// Box the selected mesh
if ( PlayerMeshName != "Camera" ) {
float x1,y1,x2,y2;
csVector2 v2;
csBox2 sBox;
csBox3 cBox;
csScreenBoxResult meshscrbox;
if ( playermesh->GetScreenBoundingBox ( c , sBox , cBox ) ) {
v2 = sBox.GetCorner(1);
x1 = v2.x;
y1 = g2d->GetHeight () - v2.y;
v2 = sBox.GetCorner(2);
x2 = v2.x;
y2 = g2d->GetHeight () - v2.y;
g2d->DrawLine ( x1 , y1 , x1 , y2 , g2d->FindRGB(255,0,0) );
g2d->DrawLine ( x1 , y1 , x2 , y1 , g2d->FindRGB(255,0,0) );
g2d->DrawLine ( x2 , y2 , x1 , y2 , g2d->FindRGB(255,0,0) );
g2d->DrawLine ( x2 , y2 , x2 , y1 , g2d->FindRGB(255,0,0) );
} // End braw box
// Print Current Mesh Loc
if ( PlayerMeshName != "Camera" ) {
movable = playermesh->GetMovable();
ot = movable->GetTransform();
csV3PrintCon ( 140, 40 , ot.GetO2TTranslation() );
}
// List loaded meshes
int x = 40;
int y = 50;
printcon( 255,0,0,"Meshs" , x - 40 , y );
printcon( 255,0,0, PlayerMeshName , x , y );
for (i=0 ; i<intmeshcount;i++) {
playermesh = meshlist->Get(i);
n=0;
if ( playermesh->QueryObject ()->GetName() == PlayerMeshName )
{
n=255;
} // End highlite current mesh
printcon( 255,n,0, playermesh->QueryObject ()->GetName() , x , y - (10*(i+1)) );
} // end for list meshes
} else { // end if disply non camera info
// Show solarsys name
//printcon( 255,0,0, solarsys.GetSysName() , 100 , 20 );
// Show camera pos
csV3PrintCon ( 140, 40 , c->GetTransform().GetOrigin() );
// Show vel
char buf1 [10];
sprintf(buf1, "%d", vel );
printcon( 255,0,0, "Vel:" , 10 , 50 );
printcon( 255,0,0, buf1 , 40 , 50 );
}
char buf [10];
sprintf(buf, "%f", rotX );
int x = g2d->GetHeight () - 10;
int y = 10;
printcon( 255,0,0, "RotX:" , 10 , x );
printcon( 255,0,0, buf , 60 , x );
x=x-10;
sprintf(buf, "%f", rotY );
printcon( 255,0,0, "RotY:" , 10 , x );
printcon( 255,0,0, buf , 60 , x );
x=x-10;
sprintf(buf, "%f", rotZ );
printcon( 255,0,0, "RotZ:" , 10 , x );
printcon( 255,0,0, buf , 60 , x );
// Show Camera up
x = g2d->GetHeight () - 10;
y = 100;
up = c->GetTransform ().GetUp();
ot = c->GetTransform ();
csV3PrintCon ( x , y , up );
}// end if g3d->BeginDraw