用OpenGL绘制小球,按给定的坐标点沿直线运动。实现问题。
答案:2 悬赏:80 手机版
解决时间 2021-03-28 12:48
- 提问者网友:你独家记忆
- 2021-03-27 19:29
用OpenGL绘制小球,按给定的坐标点沿直线运动。实现问题。
最佳答案
- 五星知识达人网友:長槍戰八方
- 2021-03-27 20:42
#include
#include
void myinit(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glDepthRange(0.0, 1.0);
}
GLfloat tx = 0.0f, ty = 0.0f, tz = 0.0f;
void drawsphere(GLenum mode)
{
glPushMatrix();
glTranslatef( tx, ty, tz-90.0f);
glutWireSphere(1.0,10,10);
glPopMatrix();
}
void idle(void)
{
if( tx < 20.0f )
tx += 0.2f;
else if( ty < 20.0f )
ty += 0.2f;
else if ( tx < 40.0f && ty < 40.0f )
{
tx += 0.2f;
ty += 0.2f;
}
else
{
tx = ty = 0.0f;
}
glutPostRedisplay();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0f,1.0f,1.0f);
glPushMatrix();
glBegin(GL_LINES);
glVertex3f( -50.0f,0.0f,-80.0f);
glVertex3f( 50.0f, 0.0f, -80.0f);
glVertex3f(0.0f, -50.0f,-80.0f);
glVertex3f(0.0f,50.0f, -80.0f);
glEnd();
glPopMatrix();
drawsphere(GL_RENDER);
glutSwapBuffers();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat) 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (-3.0, -3.0, -8.0);
}
static void
key(unsigned char k, int x, int y)
{
switch (k) {
case 27:
exit(0);
break;
default:
return;
}
glutPostRedisplay();
}
int
main(int argc, char **argv)
{
glutInitWindowSize(200, 200);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInit(&argc, argv);
glutCreateWindow(argv[0]);
myinit();
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(idle);
glutKeyboardFunc(key);
glutMainLoop();
return 0;
}追问谢谢,这是一种思路了。如果我的轨迹点很多,怎么用循环写呢?追答你说的问题不是重复绘制吗,关键是每一帧获得新的位置,而不要把绘制放到循环里。就算你一下子算出所有的位置,也是每一帧取下一个位置用。
#include
void myinit(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glDepthRange(0.0, 1.0);
}
GLfloat tx = 0.0f, ty = 0.0f, tz = 0.0f;
void drawsphere(GLenum mode)
{
glPushMatrix();
glTranslatef( tx, ty, tz-90.0f);
glutWireSphere(1.0,10,10);
glPopMatrix();
}
void idle(void)
{
if( tx < 20.0f )
tx += 0.2f;
else if( ty < 20.0f )
ty += 0.2f;
else if ( tx < 40.0f && ty < 40.0f )
{
tx += 0.2f;
ty += 0.2f;
}
else
{
tx = ty = 0.0f;
}
glutPostRedisplay();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0f,1.0f,1.0f);
glPushMatrix();
glBegin(GL_LINES);
glVertex3f( -50.0f,0.0f,-80.0f);
glVertex3f( 50.0f, 0.0f, -80.0f);
glVertex3f(0.0f, -50.0f,-80.0f);
glVertex3f(0.0f,50.0f, -80.0f);
glEnd();
glPopMatrix();
drawsphere(GL_RENDER);
glutSwapBuffers();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat) 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (-3.0, -3.0, -8.0);
}
static void
key(unsigned char k, int x, int y)
{
switch (k) {
case 27:
exit(0);
break;
default:
return;
}
glutPostRedisplay();
}
int
main(int argc, char **argv)
{
glutInitWindowSize(200, 200);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInit(&argc, argv);
glutCreateWindow(argv[0]);
myinit();
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(idle);
glutKeyboardFunc(key);
glutMainLoop();
return 0;
}追问谢谢,这是一种思路了。如果我的轨迹点很多,怎么用循环写呢?追答你说的问题不是重复绘制吗,关键是每一帧获得新的位置,而不要把绘制放到循环里。就算你一下子算出所有的位置,也是每一帧取下一个位置用。
全部回答
- 1楼网友:逐風
- 2021-03-27 21:07
斯蒂芬各地方感到十分敢死队风格
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯