我想做一个小动画,一部分是一个圆柱体,其绕自身的轴线旋转.
另外一部分是一个类似机械手臂的东西.该手臂可以绕其一端
旋转,也可以平移.
我这么做的
{ SetTimerOn();
plPushMatrix();
glRotatef(RotateAngle,0,0,1);
DrawPipe();
plPushMatrix();
Drawtoolmove();
glPopMatrix();
glPopMatrix();
}
drawtoomove()
{
glPushMatrix();
glRotatef(dC,0,1,0);
glTranslatef(dx,0,dz);
glPopMatrix();
Drawtool(x0, y0,z0,x0,y0);
}
但是一运行 就死机,希望大家帮忙看看 谢谢
另外一部分是一个类似机械手臂的东西.该手臂可以绕其一端
旋转,也可以平移.
我这么做的
{ SetTimerOn();
plPushMatrix();
glRotatef(RotateAngle,0,0,1);
DrawPipe();
plPushMatrix();
Drawtoolmove();
glPopMatrix();
glPopMatrix();
}
drawtoomove()
{
glPushMatrix();
glRotatef(dC,0,1,0);
glTranslatef(dx,0,dz);
glPopMatrix();
Drawtool(x0, y0,z0,x0,y0);
}
但是一运行 就死机,希望大家帮忙看看 谢谢
在drawpips()后面
为glPushMatrix();笔误
{
SetTimer(1,10,NULL);}
void CMyJobView::OnTimer(UINT nIDEvent)
{
CView::OnTimer(nIDEvent);
switch(nIDEvent)
{case 0:
break;
case 1:
RotateAngle+=10;
break;
default:
{}
}
if(RotateAngle>360)
{KillTimer(1);
}
InvalidateRect(NULL,FALSE);
}
改成
SetTimer(1,1000,NULL);
{
glPushMatrix();
glRotatef(dC,0,1,0);
glTranslatef(dx,0,dz);
Drawtool(x0, y0,z0,x0,y0);
glPopMatrix(); }
该函数先作平移,后旋转,opengl矩阵运算是从后往前;
因为你是部分程序,死机有很多原因,从这儿也看不出来。
SetTimerOn();
glPushMatrix();
glRotatef(RotateAngle,0,0,1);
Draw2PipesNodeTool();
glPopMatrix();
glPushMatrix();
CalSpeed(4*y0);//计算移动速度 速度存在Speed.txt中
FILE *PSpeed;
PSpeed=fopen("Speed.txt","r");
if(PSpeed==NULL)
{
MessageBox("打开速度文件夹出错,请查看输入数据"); }
else
{
while(!feof(PSpeed))
{ fscanf(PSpeed,"%lf %lf %lf",&dC,&dx,&dz);
toolMove( dC, dx, dz);
}
}
fclose(PSpeed);
glPopMatrix(); }
//手臂的运动函数
toolMove( dC, dx, dz);
{
glPushMatrix();
glRotatef(dC,0,1,0);
glTranslatef(dx,0,dz);
Drawtool(y0,x0, z0,x0,y0);//画手臂
glPopMatrix();
}
{ fscanf(PSpeed,"%lf %lf %lf",&dC,&dx,&dz);
toolMove( dC, dx, dz);
}
}
一直在写文件,造成了死循环
帖出你的ondraw函数的程序。
你去掉循环试一下。
drawtool(double x, double y, double z,double c,double l)
{// glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//设置变量
double Normal[3];
double a[1600],b[1600];
double m=0;
const double pi=3.1415926; int i=0;
//做手臂的上下面需要的点
for(m=0;m<=2*pi;m=m+pi/500)
{ a[i]=l*cos(m)/8;
b[i]=l*sin(m)/8;
i=i+1;
}
//做手臂 该手臂由三部分组成
while(i>0)
{ double point3d1[]= {x ,y ,z };
double point3d2[]={x+l/2 ,y+b[i],z+a[i]};
double point3d3[]={x+l/2,y+b[i+1],z+a[i+1]};
double point3d4[]={x+4*l/2,y+b[i],z+a[i]};
double point3d5[]={x+4*l/2,y+b[i+1],z+a[i+1]};
double point3d6[]={x+4*l/2 ,y,z };
//部分一
CalculateNormal(point3d1 ,point3d2 ,point3d3 ,Normal );//计算法向量
glColor3ub(200,0,255);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_TRIANGLE_FAN);
glNormal3dv(Normal );
glVertex3d(x ,y ,z);
glVertex3d(x+l/2 ,y+b[i],z+a[i]);
glVertex3d (x+l/2,y+b[i+1],z+a[i+1]);
glEnd();
// 部分二
CalculateNormal(point3d2 ,point3d4 ,point3d3 ,Normal );
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_TRIANGLE_FAN);
glNormal3dv(Normal );
glVertex3d(x+l/2 ,y+b[i],z+a[i]);
glVertex3d(x+4*l/2,y+b[i],z+a[i]);
glVertex3d (x+l/2,y+b[i+1],z+a[i+1]);
glEnd();
CalculateNormal(point3d2 ,point3d5 ,point3d4 ,Normal );
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_TRIANGLE_FAN);
glNormal3dv(Normal );
glVertex3d(x+l/2 ,y+b[i],z+a[i]);
glVertex3d(x+4*l/2,y+b[i+1],z+a[i+1]);
glVertex3d (x+4*l/2,y+b[i],z+a[i]);
glEnd();
//的三部分
CalculateNormal(point3d4 ,point3d5 ,point3d6 ,Normal );
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_TRIANGLE_FAN);
glNormal3dv(Normal );
glVertex3d(x+4*l/2,y+b[i],z+a[i]);
glVertex3d(x+4*l/2,y+b[i+1],z+a[i+1]);
glVertex3d (x+4*l/2 ,y,z);
glEnd();
i=i-1;
}
glFlush();
}
Draw2PipesNodeTool();
{
double Normal[3];
double x[10000],y[10000] ,z[10000];
double xg[10000],yg[10000] ,zg[10000];
const double pi=3.1415926;
FILE *pI, *pG;
int i=0;//设置参数 控制循环变量
pI=fopen("Intersection.txt","r");//读取文件数据作图
pG=fopen("Groove.txt","r");
// 如果不能打开文件 退出程序 if( pIntersection==NULL||pGroove==NULL)
{ MessageBox("不能打开文件"); exit(1); } //从文件中读取数据 并存入到 数组中
while((!feof(pIntersection))&&(!feof(pGroove)))
{
fscanf(pIntersection,"%lf %lf %lf %lf %lf %lf ",&x[i],&y[i],&z[i], &x[i+1],&y[i+1],&z[i+1]);
fscanf(pGroove,"%lf %lf %lf %lf %lf %lf ",
&xg[i],&yg[i] ,&zg[i], &xg[i+1],&yg[i+1],&zg[i+1] );
q=max(max(x[i],y[i]),z[i]);
i=i+1;
}
fclose(pIntersection);
fclose(pGroove); i=i-1;
for(i;i>0;i=i-1)
{
double point3d1[]= {x[i],y[i],z[i]};
double point3d3[]={x[i+1],y[i+1],z[i+1]}; double point3d2[]={x[i],y[i],4*q};
double point3d4[]={x[i+1],y[i+1],4*q};
double point3dg1[]={xg[i],yg[i],zg[i]}; double point3dg3[]={xg[i+1],yg[i+1],zg[i+1] }; double point3dg2[]={xg[i],yg[i],4*q};
double point3dg4[]={xg[i+1],yg[i+1],4*q}; //做整个柱体
{
glColor3ub(125,125,125);
CalculateNormal(point3d1 ,point3d2 ,point3d3 ,Normal );
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_TRIANGLE_FAN);
glNormal3dv(Normal );
glVertex3d(x[i] ,y[i] ,z[i] );
glVertex3d(x[i] ,y[i] ,4*q );
glVertex3d (x[i+1],y[i+1],z[i+1]);
glEnd();
CalculateNormal(point3d3 ,point3d2 ,point3d4 ,Normal );
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_TRIANGLE_FAN);
glNormal3dv(Normal);
glVertex3d(x[i+1],y[i+1],z[i+1]);
glVertex3d(x[i] ,y[i] ,4*q );
glVertex3d (x[i+1],y[i+1],4*q);
glEnd();
//画出圆管外壁
CalculateNormal(point3dg3 ,point3dg2 ,point3dg4 ,Normal );
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_TRIANGLE_FAN);
glNormal3dv(Normal);
glVertex3d(xg[i],yg[i],zg[i]);
glVertex3d(xg[i] ,yg[i] ,4*q);
glVertex3d(xg[i+1],yg[i+1],zg[i+1]);
glEnd();
CalculateNormal(point3dg1 ,point3dg2 ,point3dg3 ,Normal);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_TRIANGLE_FAN);
glNormal3dv(Normal );
glVertex3d(xg[i],yg[i],4*q);
glVertex3d(xg[i+1],yg[i+1],4*q);
glVertex3d(xg[i+1],yg[i+1],zg[i+1]);
glEnd();
}
//一段 口面
if(drawgrooveloop)
{ glColor3ub(200,200,200);
CalculateNormal(point3d1 ,point3dg1 ,point3d3 ,Normal );
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_TRIANGLE_FAN);
glNormal3dv(Normal );
glVertex3d(x[i],y[i],z[i]);
glVertex3d(xg[i],yg[i],zg[i] );
glVertex3d (x[i+1],y[i+1],z[i+1]);
glEnd(); CalculateNormal(point3dg1 ,point3dg3 ,point3d3 ,Normal );
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_TRIANGLE_FAN);
glNormal3dv(Normal );
glVertex3d(xg[i],yg[i],zg[i]);
glVertex3d(xg[i+1] ,yg[i+1] ,zg[i+1]);
glVertex3d(x[i+1],y[i+1],z[i+1]);
glEnd();
glColor3ub(200,200,200);
CalculateNormal(point3d1 ,point3dg1 ,point3d3 ,Normal );
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_TRIANGLE_FAN);
glNormal3dv(Normal );
glVertex3d(x[i],y[i],4*q);
glVertex3d(xg[i],yg[i],4*q );
glVertex3d (x[i+1],y[i+1],4*q);
glEnd(); CalculateNormal(point3dg1 ,point3dg3 ,point3d3 ,Normal );
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_TRIANGLE_FAN);
glNormal3dv(Normal );
glVertex3d(xg[i],yg[i],4*q);
glVertex3d(xg[i+1] ,yg[i+1] ,4*q);
glVertex3d(x[i+1],y[i+1],4*q);
glEnd(); }
}
x0=xg[0];
y0=yg[0];
z0=zg[0];
}
}
可能是这个地方出错,查查看
改成
SetTimer(1,1000,NULL);
1、你读文件应该放在onDraw外面,读进数组里面;
2、你读的文件好象是文本文件,应该这样:
pI=fopen("Intersection.txt","rt");//读取文件数据作图
....
fscanf(PSpeed,"%lf %lf %lf\n",&dC,&dx,&dz);
3、一定要单步跟踪调试,以找出错误所在。
否则出错看看你文档的格式
还有 我单步 调试的时候, 出现这样的提示 symbol "this" not fond, 但是程序还是能运行.
这个"this"是不是其他地方的错误?
还有 好像我的 killsettimmer(1) 没有起作用
那你好好找找。
数据是不是太大了?
在你的机器上可以运行么 ?
只是简单的画图了,我现在就应该把S`ETTIMER()gen跟画机器手臂 连起来.