我画了一个机器人,并模拟其飞行,飞行过程中图像发生了变形,我对其视点进行了变换可是达不到我想要的效果,哪位帮我看看,不胜感激。
void CRobotView::drawrobot()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
gluLookAt(m_xView,m_yView,m_zView,m_xView,m_yView,-10.0,0.0,1.0,0.0); //在这里设置视点变化,m_xView代表视点此时的位置,
robot(); //绘制飞机
glFlush();
}void CRobotView::robot() //绘制飞机
{
//绘制一个椭圆代表机身
glPushMatrix();
glTranslatef(m_xPos,m_yPos,m_zPos); //实现飞机的运动
glPushMatrix();
glScalef(1.0,0.5,1.0);
glutSolidSphere(3.0,100,100);
glPopMatrix(); //绘制三个脚
glPushMatrix();
glTranslatef(0.0,-2.0,2.0);
glRotatef(40.0,1.0,0.0,0.0);
auxSolidBox(0.4,0.2,4.0);
glTranslatef(0.0,0.0,2.0);
glutSolidSphere(0.3,20,30);
glPopMatrix(); glPushMatrix();
glRotatef(120.0,0.0,1.0,0.0);
glTranslatef(0.0,-2.0,3.5);
glRotatef(40.0,1.0,0.0,0.0);
auxSolidBox(0.4,0.2,4.0);
glTranslatef(0.0,0.0,2.0);
glutSolidSphere(0.3,20,30);
glPopMatrix(); glPushMatrix();
glRotatef(240.0,0.0,1.0,0.0);
glTranslatef(0.0,-2.0,3.5);
glRotatef(40.0,1.0,0.0,0.0);
auxSolidBox(0.4,0.2,4.0);
glTranslatef(0.0,0.0,2.0);
glutSolidSphere(0.3,20,30);
glPopMatrix(); //绘制脖颈
glPushMatrix();
glTranslatef(0.0,1.3,0.0);
auxSolidCylinder(0.35,0.8);
glTranslatef(0.0,1.1,0.0);
glutSolidSphere(0.45,20,20);
//绘制机翼
glPushMatrix();
glRotatef(-90.0,0.0,0.0,1.0);
glRotatef(35.0+rotate,1.0,0.0,0.0);
glTranslatef(0.0,2.6,0.0);
auxSolidCylinder(0.1,4.0);
glPopMatrix();
glPushMatrix();
glRotatef(-90.0,0.0,0.0,1.0);
glRotatef(155.0+rotate,1.0,0.0,0.0);
glTranslatef(0.0,2.6,0.0);
auxSolidCylinder(0.1,4.0);
glPopMatrix();
glPushMatrix();
glRotatef(-90.0,0.0,0.0,1.0);
glRotatef(250.0+rotate,1.0,0.0,0.0);
glTranslatef(0.0,2.6,0.0);
auxSolidCylinder(0.1,4.0);
glPopMatrix();
glPopMatrix(); //绘制脖颈
glPushMatrix();
glTranslatef(0.0,2.5,0.0);
auxSolidCylinder(0.35,0.8);
glTranslatef(0.0,1.1,0.0);
glutSolidSphere(0.45,20,20);
//绘制机翼
glPushMatrix();
glRotatef(-90.0,0.0,0.0,1.0);
glRotatef(-35.0-rotate,1.0,0.0,0.0);
glTranslatef(0.0,2.6,0.0);
auxSolidCylinder(0.1,4.0);
glPopMatrix();
glPushMatrix();
glRotatef(-90.0,0.0,0.0,1.0);
glRotatef(-155.0-rotate,1.0,0.0,0.0);
glTranslatef(0.0,2.6,0.0);
auxSolidCylinder(0.1,4.0);
glPopMatrix();
glPushMatrix();
glRotatef(-90.0,0.0,0.0,1.0);
glRotatef(-275.0-rotate,1.0,0.0,0.0);
glTranslatef(0.0,2.6,0.0);
auxSolidCylinder(0.1,4.0);
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
void CRobotView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call defaultif(nIDEvent==1)
{
if(v<2000.0&&m_xPos==0.0) //起飞前机翼的旋转过程,加速旋转
{
t+=1;
v=v+a*t;
rotate=v;
}
else
{
if(m_xPos==0.0&&m_yPos<5.0) //起飞过程,飞机匀速上升
{
m_yPos=m_yPos+0.1;
rotate=rotate+v;
m_yView=m_yPos; //我想使视点移动的距离与飞机 移动的距离相等。
}
else
{
if(m_yPos>=5.0&&m_xPos<8.0) //飞翔过程,飞机水平方向上 匀速加一,视点的位置也跟着加一。
{
m_xPos=m_xPos+0.1;
rotate=rotate+v;
m_xView=m_xPos;
}
else
{
if(m_xPos>=8.0&&m_yPos>0.0) //飞机降落过程,y轴方向上匀速减一,视点在y方向上也匀速减一。
{
m_yPos=m_yPos-0.1;
rotate=rotate+v;
m_yView=m_yPos
}
else
{
if(m_yPos<=0.0&&rotate>0.0) //机翼停止旋转
{
rotate=(int)(rotate)%360-5;
}
else
{
OnStop() ;
}
}
}
}
}
}
PostMessage(WM_PAINT);
CView::OnTimer(nIDEvent);
}开始时m_xView,m_yView,m_zView,m_xPos,m_yPos,m_zPos初始值均为0。我想飞机移动了多少距离让视点跟着移动相应的距离就可以了,但是并不是我想象的那样,是不是我对矩阵变换还没有理解,希望各位高手给我一点儿建议。
void CRobotView::drawrobot()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
gluLookAt(m_xView,m_yView,m_zView,m_xView,m_yView,-10.0,0.0,1.0,0.0); //在这里设置视点变化,m_xView代表视点此时的位置,
robot(); //绘制飞机
glFlush();
}void CRobotView::robot() //绘制飞机
{
//绘制一个椭圆代表机身
glPushMatrix();
glTranslatef(m_xPos,m_yPos,m_zPos); //实现飞机的运动
glPushMatrix();
glScalef(1.0,0.5,1.0);
glutSolidSphere(3.0,100,100);
glPopMatrix(); //绘制三个脚
glPushMatrix();
glTranslatef(0.0,-2.0,2.0);
glRotatef(40.0,1.0,0.0,0.0);
auxSolidBox(0.4,0.2,4.0);
glTranslatef(0.0,0.0,2.0);
glutSolidSphere(0.3,20,30);
glPopMatrix(); glPushMatrix();
glRotatef(120.0,0.0,1.0,0.0);
glTranslatef(0.0,-2.0,3.5);
glRotatef(40.0,1.0,0.0,0.0);
auxSolidBox(0.4,0.2,4.0);
glTranslatef(0.0,0.0,2.0);
glutSolidSphere(0.3,20,30);
glPopMatrix(); glPushMatrix();
glRotatef(240.0,0.0,1.0,0.0);
glTranslatef(0.0,-2.0,3.5);
glRotatef(40.0,1.0,0.0,0.0);
auxSolidBox(0.4,0.2,4.0);
glTranslatef(0.0,0.0,2.0);
glutSolidSphere(0.3,20,30);
glPopMatrix(); //绘制脖颈
glPushMatrix();
glTranslatef(0.0,1.3,0.0);
auxSolidCylinder(0.35,0.8);
glTranslatef(0.0,1.1,0.0);
glutSolidSphere(0.45,20,20);
//绘制机翼
glPushMatrix();
glRotatef(-90.0,0.0,0.0,1.0);
glRotatef(35.0+rotate,1.0,0.0,0.0);
glTranslatef(0.0,2.6,0.0);
auxSolidCylinder(0.1,4.0);
glPopMatrix();
glPushMatrix();
glRotatef(-90.0,0.0,0.0,1.0);
glRotatef(155.0+rotate,1.0,0.0,0.0);
glTranslatef(0.0,2.6,0.0);
auxSolidCylinder(0.1,4.0);
glPopMatrix();
glPushMatrix();
glRotatef(-90.0,0.0,0.0,1.0);
glRotatef(250.0+rotate,1.0,0.0,0.0);
glTranslatef(0.0,2.6,0.0);
auxSolidCylinder(0.1,4.0);
glPopMatrix();
glPopMatrix(); //绘制脖颈
glPushMatrix();
glTranslatef(0.0,2.5,0.0);
auxSolidCylinder(0.35,0.8);
glTranslatef(0.0,1.1,0.0);
glutSolidSphere(0.45,20,20);
//绘制机翼
glPushMatrix();
glRotatef(-90.0,0.0,0.0,1.0);
glRotatef(-35.0-rotate,1.0,0.0,0.0);
glTranslatef(0.0,2.6,0.0);
auxSolidCylinder(0.1,4.0);
glPopMatrix();
glPushMatrix();
glRotatef(-90.0,0.0,0.0,1.0);
glRotatef(-155.0-rotate,1.0,0.0,0.0);
glTranslatef(0.0,2.6,0.0);
auxSolidCylinder(0.1,4.0);
glPopMatrix();
glPushMatrix();
glRotatef(-90.0,0.0,0.0,1.0);
glRotatef(-275.0-rotate,1.0,0.0,0.0);
glTranslatef(0.0,2.6,0.0);
auxSolidCylinder(0.1,4.0);
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
void CRobotView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call defaultif(nIDEvent==1)
{
if(v<2000.0&&m_xPos==0.0) //起飞前机翼的旋转过程,加速旋转
{
t+=1;
v=v+a*t;
rotate=v;
}
else
{
if(m_xPos==0.0&&m_yPos<5.0) //起飞过程,飞机匀速上升
{
m_yPos=m_yPos+0.1;
rotate=rotate+v;
m_yView=m_yPos; //我想使视点移动的距离与飞机 移动的距离相等。
}
else
{
if(m_yPos>=5.0&&m_xPos<8.0) //飞翔过程,飞机水平方向上 匀速加一,视点的位置也跟着加一。
{
m_xPos=m_xPos+0.1;
rotate=rotate+v;
m_xView=m_xPos;
}
else
{
if(m_xPos>=8.0&&m_yPos>0.0) //飞机降落过程,y轴方向上匀速减一,视点在y方向上也匀速减一。
{
m_yPos=m_yPos-0.1;
rotate=rotate+v;
m_yView=m_yPos
}
else
{
if(m_yPos<=0.0&&rotate>0.0) //机翼停止旋转
{
rotate=(int)(rotate)%360-5;
}
else
{
OnStop() ;
}
}
}
}
}
}
PostMessage(WM_PAINT);
CView::OnTimer(nIDEvent);
}开始时m_xView,m_yView,m_zView,m_xPos,m_yPos,m_zPos初始值均为0。我想飞机移动了多少距离让视点跟着移动相应的距离就可以了,但是并不是我想象的那样,是不是我对矩阵变换还没有理解,希望各位高手给我一点儿建议。
#define KEY_DOWN(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
//移动镜头函数
float rad_xz;
static float eyex=0.0,eyez=0.0,lookx=0.0,lookz=-500.0;
static float g_Angle=0.0;
BOOL DisplayScene()
{
float speed=1.2f; if (KEY_DOWN(VK_SHIFT))speed=speed*4; //按shift加速 if (KEY_DOWN('A')) g_Angle+=speed*2; //按A左转
if (KEY_DOWN('D')) g_Angle-=speed*2; //按D右转
rad_xz = float (3.13149* g_Angle/180.0f);
if (KEY_DOWN('W')) //按W前进
{
eyex-=sin(rad_xz)*speed;
eyez-=cos(rad_xz)*speed;
int i=0;
}
if (KEY_DOWN('S')) //按S后退
{
eyex+=sin(rad_xz)*speed;
eyez+=cos(rad_xz)*speed;
}
lookx=eyex-1000*sin(rad_xz);
lookz=eyez-1000*cos(rad_xz);
gluLookAt(eyex,0.0,eyez,lookx,0.0,lookz,0.0,1.0,0.0);
return TRUE;
}