这是我目前的代码:
void originAxis();
void sineWave();
void idle();
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
originAxis();
sineWave();
printf("%s\n", gluErrorString(glGetError()));
glutSwapBuffers();
}
void sineWave()
{
float wavelength = 2;
float amplitude = 1;
float inc = 0.05;
float k, x, y;
glBegin(GL_LINES);
glColor3f(1,1,1);
for(x=-1;x<=1;x+=inc){
k = 2 * 3.14 / wavelength;
y = amplitude * sin(k * x);
glVertex3f(x, y, 0);
}
glEnd();
}
void idle()
{
float wavelength = 2;
float amplitude = 1;
float inc = 0.05;
float k, x, y;
for (x = -1; x <= 1; x += inc){
glBegin(GL_POINTS);
glPointSize(200);
glColor3f(0, 1, 0);
k = 2 * 3.14 / wavelength;
y = amplitude * sin(k * x);
glVertex3f(x, y, 0);
glEnd();
}
glutPostRedisplay();
}
void originAxis()
{
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(1, 0, 0);
glColor3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(0, 1, 0);
glColor3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(0, 0, 1);
glEnd();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Tutorial 2");
glOrtho(-1, 1, -1, 1, -1, 1);
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutIdleFunc(idle);
glutMainLoop();
return EXIT_SUCCESS;
}
这里是我的输出:
http://imgur.com/bXct28P.png
问题:
为什么我的正弦波是断的而不是连续的?
我也得到零动画,为什么?
最佳答案
正弦波断开是因为GL_线在您提供的两个连续点之间画一条线,然后在下两个点之间画一条线,依此类推。
要绘制一条连接您提供的所有点的线,请使用GL_line_STRIP
还有~zero animation~因为你每一帧都在画同样的东西(没有真正理解你的怀疑)
这是一个我喜欢的来自Google图片的图片,它的原语类型是
http://3dgep.com/wp-content/uploads/2011/02/OpenGL-Primitives.png
关于c - 在OpenGL中创建带有动画点的正弦波?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22892791/