Kameraproblem



  • Hi,

    ich wollte vor kurzem eine bewegbare Kamera in meine 3D-Welt einbringen nur leider funktioniert sie nicht.
    Hier der Quellcode, den ich übrigends von der Seite www.gametutorials.com habe:
    Ich hoffe der Code ist nicht zu lang.

    #define kSpeed  1.03f 
    
    class CVector3 {
    
    public:                                                 // We want this data public so we don't have to use data access functions.  We could use a struct but I chose not to.
        float x, y, z;                                      // We just want a float for a X Y and Z.
    };
    
    class CCamera {
    
    public:
    
        CVector3 m_vPosition;                               // The camera's position
        CVector3 m_vView;                                   // The camera's View
        CVector3 m_vUpVector;                               // The camera's UpVector
    
        // Our camera constructor
        CCamera();  
    
        // This changes the position, view, and up vector of the camera.
        // This is primarily used for initialization
        void PositionCamera(float positionX, float positionY, float positionZ,
                             float viewX,     float viewY,     float viewZ,
                            float upVectorX, float upVectorY, float upVectorZ);
    
        // This rotates the camera's view around the position depending on the values passed in.
        // The rotation is done using axis-angle rotation. That means you give this function
        // a degree (in radians) and a axis rotate around.  
        void RotateView(float angle, float x, float y, float z);
    
        // This will move the camera forward or backward depending on the speed
        void MoveCamera(float speed);
    };
    
    CCamera::CCamera()
    {
        CVector3 vZero = {0.0, 0.0, 0.0};        // Init a vector to 0 0 0 for our position
        CVector3 vView = {0.0, 1.0, 0.5};        // Init a starting view
        CVector3 vUp   = {0.0, 0.0, 1.0};        // Init a standard up vector (Rarely ever changes)
    
        m_vPosition    = vZero;                  // Init the position to zero
        m_vView        = vView;                  // Init the view to a std starting view
        m_vUpVector    = vUp;                    // Init the UpVector
    }
    
    void CCamera::PositionCamera(float positionX, float positionY, float positionZ,
                                     float viewX,     float viewY,     float viewZ,
                                   float upVectorX, float upVectorY, float upVectorZ)
    {
        CVector3 vPosition    = {positionX, positionY, positionZ};
        CVector3 vView        = {viewX, viewY, viewZ};
        CVector3 vUpVector    = {upVectorX, upVectorY, upVectorZ};
    
        // The code above just makes it cleaner to set the variables.
        // Otherwise we would have to set each variable x y and z.
    
        m_vPosition = vPosition;                    // Assign the position
        m_vView = vView;                            // Assign the view
        m_vUpVector = vUpVector;                    // Assign the up vector
    }
    
    void CCamera::RotateView(float angle, float x, float y, float z)
    {
        CVector3 vNewView;
        CVector3 vView;    
    
        // Get our view vector (The direciton we are facing)
        vView.x = m_vView.x - m_vPosition.x;        // This gets the direction of the X    
        vView.y = m_vView.y - m_vPosition.y;        // This gets the direction of the Y
        vView.z = m_vView.z - m_vPosition.z;        // This gets the direction of the Z
    
        // Calculate the sine and cosine of the angle once
        float cosTheta = (float)cos(angle);
        float sinTheta = (float)sin(angle);
    
        // Find the new x position for the new rotated point
        vNewView.x  = (cosTheta + (1 - cosTheta) * x * x)        * vView.x;
        vNewView.x += ((1 - cosTheta) * x * y - z * sinTheta)    * vView.y;
        vNewView.x += ((1 - cosTheta) * x * z + y * sinTheta)    * vView.z;
    
        // Find the new y position for the new rotated point
        vNewView.y  = ((1 - cosTheta) * x * y + z * sinTheta)    * vView.x;
        vNewView.y += (cosTheta + (1 - cosTheta) * y * y)        * vView.y;
        vNewView.y += ((1 - cosTheta) * y * z - x * sinTheta)    * vView.z;
    
        // Find the new z position for the new rotated point
        vNewView.z  = ((1 - cosTheta) * x * z - y * sinTheta)    * vView.x;
        vNewView.z += ((1 - cosTheta) * y * z + x * sinTheta)    * vView.y;
        vNewView.z += (cosTheta + (1 - cosTheta) * z * z)        * vView.z;
    
        // Now we just add the newly rotated vector to our position to set
        // our new rotated view of our camera.
        m_vView.x = m_vPosition.x + vNewView.x;
        m_vView.y = m_vPosition.y + vNewView.y;
        m_vView.z = m_vPosition.z + vNewView.z;
    }
    
    void CCamera::MoveCamera(float speed)
    {
        CVector3 vVector={0};                    // Init a vector for our view
    
        // Get our view vector (The direciton we are facing)
        vVector.x = m_vView.x - m_vPosition.x;    // This gets the direction of the X    
        vVector.y = m_vView.y - m_vPosition.y;    // This gets the direction of the Y
        vVector.z = m_vView.z - m_vPosition.z;    // This gets the direction of the Z
    
        m_vPosition.x += vVector.x * speed;        // Add our acceleration to our position's X
        m_vPosition.z += vVector.z * speed;        // Add our acceleration to our position's Z
        m_vView.x += vVector.x * speed;            // Add our acceleration to our view's X
        m_vView.z += vVector.z * speed;            // Add our acceleration to our view's Z
    }
    
    ……………..
    
    int main(int argc, char **argv) 
    {  
      int done;
      SDL_Event event;
      Uint8 *keys;
    CCamera    g_Camera;
    
    //. . . . . . . . .
    
    //. . . . . . .
    
    g_Camera.PositionCamera(0, 0.5, 6,   0, 0.5, 0,   0, 1, 0 ); 
    
      done = 0;
      while ( ! done ) {
        DrawGLScene();
    
          while ( SDL_PollEvent(&event) ) {
    
    //. . . . . .
    
    }
    
    keys = SDL_GetKeyState(NULL);
    
    //………
    
    if(keys[SDLK_UP]) {                // If we hit the UP arrow key
            g_Camera.MoveCamera(kSpeed);            // Move our camera forward by a positive speed
        }
    
        if(keys[SDLK_DOWN]) {            // If we hit the DOWN arrow key
            g_Camera.MoveCamera(-kSpeed);            // Move our camera backward by a negative speed
        }
    
        if(keys[SDLK_LEFT]) {            // If we hit the LEFT arrow key
    
            // We want to rotate around the Y axis so we pass in (0, 1, 0) for the axis
            g_Camera.RotateView(kSpeed, 0, 1, 0);    // Rotate our camera LEFT by the positive speed
        }
    
        if(keys[SDLK_RIGHT]) {            // If we hit the RIGHT arrow key
    
            // Use a positive speed to rotate around the Y axis (0, 1, 0)
            g_Camera.RotateView(-kSpeed, 0, 1, 0);    // Rotate our camera RIGHT by the negative speed
        }
    
        }
    
      SDL_Quit();
      return 1;
    }
    

    Wisst ihr, warum der Code nicht funzt?

    Gruß Crabbe



  • Nö, verrätst du es? Irgdendwie sind die Codetags putt, kannst du ja mal richten!

    Bye, TGGC (Der Held ist zurück)



  • Nein ehrlich, das ist keine Preisfrage 😃 ich meine es ernst.
    Das mit den Codetags habe ich jetzt verbessert.

    Gruß Crabbe



  • nur leider funktioniert sie nicht.

    was ????

    vermute mal das du nach dem bewegen der camera es opengl nicht mitgeteilt hast

    // aus dem tut von gametutorials
    	glLoadIdentity();									// Reset The matrix
    
    	// Give openGL our camera position, then camera view, then camera up vector
    	gluLookAt(gCamera.mPosition.x, gCamera.mPosition.y, gCamera.mPosition.z,	
    			  gCamera.mView.x,	   gCamera.mView.y,     gCamera.mView.z,	
    			  gCamera.mUpVector.x, gCamera.mUpVector.y, gCamera.mUpVector.z);
    // .....
    


  • Danke, genau das war es!

    Gruß Crabbe


Anmelden zum Antworten