| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 424 人关注过本帖, 1 人收藏
标题:关于摄像机类绕人物旋转的问题?
取消只看楼主 加入收藏
zkang5
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2011-5-2
结帖率:100%
收藏(1)
 问题点数:0 回复次数:0 
关于摄像机类绕人物旋转的问题?
一个摄像机类如下:
.h 文件
#ifndef CAMERA_H
#define CAMERA_H

#include <d3dx9.h>

enum eCameraType { LAND_OBJECT, AIR_CRAFT };//LAND_OBJECT

class cCamera
{
public:
    eCameraType        m_camera_type;
    D3DXVECTOR3        m_right;
    D3DXVECTOR3        m_up;
    D3DXVECTOR3        m_look;   


public:
    D3DXVECTOR3        m_pos;


public:
    cCamera();
    cCamera(eCameraType camera_type);
    ~cCamera() { };

    void strafe(float units);   
    void fly(float units);        
    void walk(float units);        

    void pitch(float angle);
    void yaw(float angle);        
    void roll(float angle);        

    void get_view_matrix(D3DXMATRIX* v);
    void set_camera_type(eCameraType camera_type);//视图
   
    void get_right(D3DXVECTOR3* right);
    void get_up(D3DXVECTOR3* up);
    void get_look(D3DXVECTOR3* look);
    void SetTransfrom();
};
#endif
.cpp文件
#include "camera.h"


cCamera::cCamera()
{

    m_camera_type = LAND_OBJECT;

    m_pos   = D3DXVECTOR3(0.0f, -25.0f, -60.0f);

    m_right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
    m_up    = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
    m_look  = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
}

cCamera::cCamera(eCameraType camera_type)
{
    m_camera_type = camera_type;

    m_pos   = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
    m_right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
    m_up    = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
    m_look  = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
}

void cCamera::get_right(D3DXVECTOR3* right)
{
    *right = m_right;
}

void cCamera::get_up(D3DXVECTOR3* up)
{
    *up = m_up;
}

void cCamera::get_look(D3DXVECTOR3* look)
{
    *look = m_look;
}

void cCamera::walk(float units)
{
   
    if(m_camera_type == LAND_OBJECT)
        m_pos += D3DXVECTOR3(m_look.x, 0.0f, m_look.z) * units;
    else   
        m_pos += m_look * units;
}

void cCamera::strafe(float units)
{
   
    if(m_camera_type == LAND_OBJECT)
        m_pos += D3DXVECTOR3(m_right.x, 0.0f, m_right.z) * units;
    else   
        m_pos += m_right * units;
}

void cCamera::fly(float units)
{
   
    if(m_camera_type == LAND_OBJECT)
        m_pos.y += units;
    else
        m_pos += m_up * units;
}

void cCamera::pitch(float angle)
{
    D3DXMATRIX transform_matrix;
    D3DXMatrixRotationAxis(&transform_matrix, &m_right, angle);


    D3DXVec3TransformCoord(&m_up,   &m_up,   &transform_matrix);
    D3DXVec3TransformCoord(&m_look, &m_look, &transform_matrix);
}

void cCamera::yaw(float angle)
{



    D3DXMATRIX transform_matrix;
      
    if(m_camera_type == LAND_OBJECT)
        //D3DXMatrixRotationY(&transform_matrix, angle);//angle*D3DX_PI/1000.0f
    //else   
    D3DXMatrixRotationAxis(&transform_matrix, &m_up, angle);
    D3DXMatrixRotationY(&transform_matrix, angle);
   
    D3DXVec3TransformCoord(&m_right, &m_right, &transform_matrix);
    D3DXVec3TransformCoord(&m_look,  &m_look,  &transform_matrix);

}

void cCamera::roll(float angle)
{

    if(m_camera_type == AIR_CRAFT)
    {
        D3DXMATRIX transform_matrix;
        D3DXMatrixRotationAxis(&transform_matrix, &m_look, angle);

   
        D3DXVec3TransformCoord(&m_up,     &m_up,        &transform_matrix);
        D3DXVec3TransformCoord(&m_right, &m_right,  &transform_matrix);
    }
}

void cCamera::get_view_matrix(D3DXMATRIX* v)
{

    D3DXVec3Normalize(&m_look, &m_look);

    D3DXVec3Cross(&m_up, &m_look, &m_right);
    D3DXVec3Normalize(&m_up, &m_up);

    D3DXVec3Cross(&m_right, &m_up, &m_look);
    D3DXVec3Normalize(&m_right, &m_right);

    float x = -D3DXVec3Dot(&m_right, &m_pos);
    float y = -D3DXVec3Dot(&m_up,    &m_pos);
    float z = -D3DXVec3Dot(&m_look,  &m_pos);

    (*v)(0, 0) = m_right.x;        (*v)(0, 1) = m_up.x;    (*v)(0, 2) = m_look.x;        (*v)(0, 3) = 0.0f;
    (*v)(1, 0) = m_right.y;        (*v)(1, 1) = m_up.y;    (*v)(1, 2) = m_look.y;        (*v)(1, 3) = 0.0f;
    (*v)(2, 0) = m_right.z;        (*v)(2, 1) = m_up.z;    (*v)(2, 2) = m_look.z;        (*v)(2, 3) = 0.0f;
    (*v)(3, 0) = x;                (*v)(3, 1) = y;            (*v)(3, 2) = z;                (*v)(3, 3) = 1.0f;
}

void cCamera::set_camera_type(eCameraType camera_type)
{
    m_camera_type = camera_type;
}
然后人物的位置绑定摄像机的位置,现在有个问题就是如何修改摄像机的旋转矩阵,通过键盘控制让摄像机可以绕人物的Y轴旋转,我感觉应该修改这里面的(void cCamera::yaw(float angle))但是不知道该如何修改, 求帮忙解决一下,谢谢   源码太大,没发上传了, 我qq  519006994    想要我可以发给你们
搜索更多相关主题的帖子: 摄像机 
2011-05-02 19:14
快速回复:关于摄像机类绕人物旋转的问题?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017206 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved