Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualLexTehRex

Posted 07 May 2013 - 04:05 PM

Deleted


#2LexTehRex

Posted 07 May 2013 - 04:05 PM

Deleted


#1LexTehRex

Posted 07 May 2013 - 09:38 AM

Hi all.

 

I need to implement a 3rd person camera. I have a very simple first person camera set up at the moment, but I need my camera to follow a model. I have tried looking at it many times and Googling how to do it, but there doesn't seem to be very many (none from what I saw) resources. Here are the parts of my code that relate to the camera and to the md2 model. I need the A and D keys to rotate the model, and rotate the camera around the model, then W to follow the model where it walks.

 

// GLOBALS
 
// Initial model coords
GLfloat mx = 0.0f, my = 1.2f, mz = 0.0f;
 
// Model rotation
GLfloat mr = 0.0f;
 
// Camera rotation
GLfloat r = 0.0f;
 
glm::vec3 eye(mx, my, mz+5.0f);
glm::vec3 at(mx, my, mz);
glm::vec3 up(0.0f, 1.0f, 0.0f);
 
//...
 
glm::vec3 moveForward(glm::vec3 pos, GLfloat angle, GLfloat d) {
        return glm::vec3(pos.x + d*std::sin(r*DEG_TO_RADIAN), pos.y, pos.z - d*std::cos(r*DEG_TO_RADIAN));
}
 
glm::vec3 moveRight(glm::vec3 pos, GLfloat angle, GLfloat d) {
        return glm::vec3(pos.x + d*std::cos(r*DEG_TO_RADIAN), pos.y, pos.z + d*std::sin(r*DEG_TO_RADIAN));
}
 
void update(void) {
        Uint8 *keys = SDL_GetKeyboardState(NULL);
        currentAnim = 0;
        if ( keys[SDL_SCANCODE_W] ) 
        {
                eye = moveForward(eye, r, 0.1f);
                currentAnim = 1;
        }
        
        if ( keys[SDL_SCANCODE_A] ) 
        {
                eye = moveRight(eye,r,-0.1f);
                //r += -1.0f;
        }
 
        if ( keys[SDL_SCANCODE_D] )
        {
                eye = moveRight(eye,r,0.1f);
                //r -= -1.0f;
        }
}
 
void draw(SDL_Window * window) {
        // clear the screen
        glEnable(GL_CULL_FACE);
        glClearColor(0.5f,0.5f,0.5f,1.0f);
        glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
 
        glm::mat4 projection(1.0);
        projection = glm::perspective(60.0f,800.0f/600.0f,1.0f,50.0f);
        rt3d::setUniformMatrix4fv(shaderProgram, "projection", glm::value_ptr(projection));
        
        GLfloat scale(1.0f); // just to allow easy scaling of complete scene
        
        glm::mat4 modelview(1.0); // set base position for scene
        mvStack.push(modelview);
 
        at = moveForward(eye,r,1.0f);
        mvStack.top() = glm::lookAt(eye,at,up);
    
    //...
 
        // Animate the md2 model, and update the mesh with new vertex data
        tmpModel.Animate(currentAnim,0.1);
        3dlib::updateMesh(meshObjects[1],RT3D_VERTEX,tmpModel.getAnimVerts(),tmpModel.getVertDataSize());
 
        // Draw MD2 model
        glCullFace(GL_FRONT);
        glBindTexture(GL_TEXTURE_2D, textures[2]);
        3dlib::materialStruct tmpMaterial = material1;
        3dlib::setMaterial(shaderProgram, tmpMaterial);
        mvStack.push(mvStack.top());
        mvStack.top() = glm::translate(mvStack.top(), glm::vec3(mx, my, mz));
        mvStack.top() = glm::rotate(mvStack.top(), 90.0f, glm::vec3(-1.0f, 0.0f, 0.0f));
        mvStack.top() = glm::rotate(mvStack.top(), 90.0f, glm::vec3(0.0f, 0.0f, 1.0f));
        mvStack.top() = glm::rotate(mvStack.top(), mr, glm::vec3(0.0f, 0.0f, 1.0f));
        mvStack.top() = glm::scale(mvStack.top(),glm::vec3(scale*0.05, scale*0.05, scale*0.05));
        3dlib::setUniformMatrix4fv(shaderProgram, "modelview", glm::value_ptr(mvStack.top()));
        3dlib::drawMesh(meshObjects[1], tmpModel.getVertDataSize()/3, GL_TRIANGLES);
        mvStack.pop();
        glCullFace(GL_BACK);
 
 
        mvStack.pop();
        glDepthMask(GL_TRUE);
    
    SDL_GL_SwapWindow(window); 
}

 

It seems to have something, if not everything, to do with lookAt. I just don't understand how it can be changed to follow the coordinates of the model around.


PARTNERS