opengl导入stl文件的物体 转动物体后显示异常并且中断 希望大神帮帮忙

weixin_34241718 2017-04-18 10:55:15

代码:
void getstlmodel()
{
int max=0;
bool isbegin=false;
long size=0;
int nlines=0;
int count1=0;
int count2=0;
FILE* file=fopen("MR.stl","r");
fseek(file,0L,SEEK_END);
size=ftell(file);
fclose(file);
file=fopen("MR.stl","r");
for (int i=0;i<size;i++)
{
if(getc(file)=='\n')
{
nlines++;
}
}
num=nlines/7;
rewind(file);
while (getc(file) != '\n');
verts=new float[9*num];
vnorms=new float[9*num];
for (int i=0;i<num;i++)
{
char x[9999]="";
char y[9999]="";
char z[9999]="";
if(3!=fscanf(file,"%*s %*s %80s %80s %80s\n",x,y,z))
{
break;
}
vnorms[count1]=vnorms[count1+3]=vnorms[count1+6]=atof(x);
count1++;
vnorms[count1]=vnorms[count1+3]=vnorms[count1+6]=atof(y);
count1++;
vnorms[count1]=vnorms[count1+3]=vnorms[count1+6]=atof(z);
count1+=7;
fscanf(file,"%*s %*s");
if (3!=fscanf(file,"%*s %80s %80s %80s\n",x,y,z))
{
break;
}
if (isbegin==false)
{
isbegin=true;
max=atof(z);
}
verts[count2]=atof(x);
count2++;
verts[count2]=atof(y);
count2++;
verts[count2]=atof(z);
count2++;
if (3!=fscanf(file,"%*s %80s %80s %80s\n",x,y,z))
{
break;
}
verts[count2]=atof(x);
count2++;
verts[count2]=atof(y);
count2++;
verts[count2]=atof(z);
count2++;
if (3!=fscanf(file,"%*s %80s %80s %80s\n",x,y,z))
{
break;
}
verts[count2]=atof(x);
count2++;
verts[count2]=atof(y);
count2++;
verts[count2]=atof(z);
count2++;
fscanf(file,"%*s");
fscanf(file,"%*s");
}
}
void display(void)
{
/*getstlmodel();*/
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable (GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glLoadIdentity();
glPushMatrix();
glRotatef(angle,0.0,1.0,0.0);
for(int i=0;i<=9*num;i=i+9,p=p+9)
{
glBegin(GL_TRIANGLES);
glColor4f(1,1,1,1);
glNormal3d(vnorms[p],vnorms[p+1],vnorms[p+2]);
glVertex3d(verts[i]/10,verts[i+1]/10,verts[i+2]/10);
glVertex3d(verts[i+3]/10,verts[i+4]/10,verts[i+5]/10);
glVertex3d(verts[i+6]/10,verts[i+7]/10,verts[i+8]/10);
glEnd();

}
glPopMatrix();
glFlush;
glutSwapBuffers();


}

void init(void)
{

GLfloat light_position [ ] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
glClearColor ( 0.0, 0.0, 0.0, 0.0 ); //设置背景色为黑色
glShadeModel ( GL_SMOOTH );
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glLightfv ( GL_LIGHT0, GL_POSITION, light_position);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);

}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ( );
if (w <= h)
glOrtho (-1.5, 1.5, -1.5 * ( GLfloat ) h / ( GLfloat ) w,
1.5* ( GLfloat ) h / ( GLfloat ) w, -10.0, 10.0 ); //创建平行视景体
else
glOrtho (-1.5 * ( GLfloat ) w / ( GLfloat ) h,1.5 * ( GLfloat )
w/( GLfloat ) h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( ) ;
}
static void key(unsigned char key, int x, int y)
{
switch (key)
{
case 27 :
case 'q':
exit(0);
break;

case '+':
angle++;
break;

case '-':
angle--;
break;
}

glutPostRedisplay();
}
void show()
{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glLoadIdentity();
glEnable (GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glLoadIdentity();
glPushMatrix();
glRotatef(angle,0.0,1.0,0.0);
for(int i=0;i<=9*num;i=i+9,p=p+9)
{
glBegin(GL_TRIANGLES);
glColor4f(1,1,1,1);
glNormal3d(vnorms[p],vnorms[p+1],vnorms[p+2]);
glVertex3d(verts[i]/10,verts[i+1]/10,verts[i+2]/10);
glVertex3d(verts[i+3]/10,verts[i+4]/10,verts[i+5]/10);
glVertex3d(verts[i+6]/10,verts[i+7]/10,verts[i+8]/10);
glEnd();

}
glPopMatrix();
glFlush;
glutSwapBuffers();
}

int main(int argc, char *argv[])
{
getstlmodel();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(200, 300);
glutInitWindowSize(640, 480);
glutCreateWindow("读取");
init();
glutReshapeFunc (&reshape );
// glutSpecialFunc(SpecialKeys);
glutKeyboardFunc(key);
glutDisplayFunc(&show);
glutMainLoop();
return 0;
delete[]verts;
delete[]vnorms;
}
...全文
332 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-04-19
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧