Thread: OpenGL Spaceship

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    OpenGL Spaceship

    Hello people !

    I am starting to work on a game where you fly around and kill as many things as you can. Quite simple. But I have 3 questions.

    1) Since you fly around in a spaceship , I need sort of a cockpit view , to stay in front of the camera at all times. Is there any way I can do this ?

    2) If I use texture mapping to create the landscape , would the player need to have them textures stored on their computer ?

    3) well , I've forgotten what 3 was , but it'll come back to me.


    Any help will be greatly appriciated.
    Thanks.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >1) Since you fly around in a spaceship , I need sort of a cockpit view , to stay in
    >front of the camera at all times. Is there any way I can do this ?
    yes.

    >2) If I use texture mapping to create the landscape , would the player need to
    >have them textures stored on their computer ?
    You would have to distribute the texture map with your application.

    >3) well , I've forgotten what 3 was , but it'll come back to me
    I get the feeling like there will be a lot more than 3

    There's a sticky at the top of this board with some links you may find usefull. Particularly http://nehe.gamedev.net for opengl.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Yeah , I go to that site alot , but thanks anyway.

    I remembered 3 !

    3) Is there a way to have the camera ( ship ) constantly moving , so that the user can change the speed ?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    3) Is there a way ...
    There are ways to make most things you want, only you have some imagination when coding it...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    There is a way to do virtually anything. Your imagination is the limit.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you are using a cockpit view then really all you do is stick the camera in the cockpit of the 3D model. Now you will have to have a more detailed cockpit model surrounding the player because you won't be able to get good results by just using a low res poly model. So you have 2 models. One for the ship and one for the cockpit of the ship. The ship model would show the cockpit supports, and the flat surfaces where the more detailed models will be. The detailed model would then overlay on top of the ship model to create your final cockpit.

    The camera needs to be translated to the cockpit area and then everything needs to be drawn relative to the camera. Note that in an actual plane, car, etc, the center of rotation for the object is NOT in the cockpit and NOT in the driver's seat. So this works out just like it does in real life.

    In Direct3D this will translate your camera, if you have a camera class already setup. I'm assuming your 3 vectors are correct and orthogonal. I normally use Look, Right, and Up for all objects including cameras.


    Code:
    //Place camera in cockpit
    
    //Camera translation matrix
    D3DXMATRIX CameraTrans;
    
    //Camera view matrix
    D3DXMATRIX CameraView;
    
    //CameraWorldView -> with translation
    D3DXMATRIX CameraWorldView;
    
    //Build matrix to ranslate camera to cockpit position in model space
    D3DXMatrixTranslation(CameraTrans,Cockpit.Pos.x,Cockpit.Pos.y,Cockpit.Pos.z);
    
    //Get current view matrix
    Camera.GetViewMatrix(&CameraView);
    
    //Translate camera
    CameraWorldView=CameraView*CameraTrans;
    
    //Set camera view matrix
    Camera.SetViewMatrix(&CameraWorldView);
    
    //Set view transform
    Device->SetTransform(D3DTS_VIEW,&CameraWorldView);
    I may have missed a step. You might have to compute the inverse of the camera view matrix and then multiply the translation by that - then set the transform to this matrix.

    To place the camera behind the ship translate the camera by the negative ship look vector.
    Last edited by VirtualAce; 03-13-2005 at 02:30 AM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    So , when I rotate the camera , I also rotate the ship. Correct ?

    Anyway , how do I set the co-ordinates for where the camera starts off ?

    Thanks

  8. #8
    Slow Learner Prophet-ex's Avatar
    Join Date
    Jan 2005
    Posts
    9
    I don't think you do, just draw the shapes relative to where you want you camera to start off, just leave room for movement etc.
    I'm not sure though, I'm just new

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Since everything in an engine is transformed according to matrices, if you set the view matrix for the camera to be the view matrix for the engine, all vertices will be correctly transformed.

    Here are the transforms:

    World transformation
    * Local/Model space to World space
    * Translates object to it's position in the world
    * Rotates object around it's center, and/or around a specific center of rotation
    * Optionally may scale the object using a scaling matrix

    World to View/Camera Space transformation
    * World space to Camera/View/Eye space.
    * Moves the camera to the origin of the world (0,0,0) and draws and transforms all object relative to this position -> draws the world from the camera perspective

    View/Camera Space to Clip space Transformation
    * Clip space is a simple intermediary space between View and Screen space
    * This space transforms the W in an XYZW quad so that all points that satisfy
    -w<x<w
    -w<y<w
    -w<z<w
    are in the current view frustrum. All objects whose vertices and/or position (depending on how the engine culls objects - some use vertices, some just use position with bounding volume) do not satisfy those inequalities are not inside the view frustrum.

    Screen space
    * Transforms vertices from clip space to screen space.
    * Performs 3D to 2D projection using pre-calculated projection matrix based on HFOV, VFOV, far clip plane, near clip plane, screen width, and screen height. VFOV is usually a function of HFOV and screen height in pixels.
    * Actual transformation occurs when division by W happens. The projection matrix is a matrix that prepares the vertices for division by W.

    And here are some common matrices used in 3D graphics. I'm not including rotation here because...well I can't exactly recall each one from memory.

    Identity matrix
    1,0,0,0
    0,1,0,0
    0,0,1,0
    0,0,0,1

    Scaling matrix - scale factors of sx,sy,sz
    sx,0,0,0
    0,sy,0,0
    0,0,sz,0
    0,0,0,1

    Translation matrix - translation amounts dx,dy,dz
    1,0,0,0
    0,1,0,0
    0,0,1,0
    dx,dy,dz,1

    WorldTransformationMatrix=LocalRotation*Scaling*Tr anslation*WorldRotation;

    WorldRotation is an optional matrix that will rotate the entire object around a point instead of rotating the object around it's center.

    LocalRotation - earth spinning on its axis.
    WorldRotation - earth orbiting the sun.

    For more information, I suggest reading a book.

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Bubba
    Translation matrix - translation amounts dx,dy,dz
    1,0,0,0
    0,1,0,0
    0,0,1,0
    dx,dy,dz,1
    Hmm, so the matrices are aligned this way in 3d programming? In projective geometry we always multiplied the matrices with a column-matrix with the point coordinates. That way the translation matrices look like this:
    1,0,0,dx
    0,1,0,dy
    0,0,1,dz
    0,0,0,1

    Not a big change though. Just the transpose.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    The way i learned it is the same as Sang-drax mentions. Also, you can have a scale/projection factor in there too IIRC.

    1,0,0,0
    0,1,0,0
    0,0,1,0
    0,0,w,1

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The projection matrix is a bit more involved than what - I didn't post it to save confusion. I also did not post several other matrices namely rotation, axis angle rotation, reflection, skew, clip, etc. I tried to keep it simple.

    As far as the order goes, it just depends on how you order your matrices. But you are correct, I stuck the dx,dy,dz in the wrong place for what I normally use. they should be at (3,0),(3,1),(3,2),(3,3). There are other configurations like what I posted but my matrix was a typo.

    This is correct:

    1,0,0,dx
    0,1,0,dy
    0,0,1,dz
    0,0,0,1
    Last edited by VirtualAce; 03-14-2005 at 03:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  3. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM