如何使用 OpenGL 使地球绕太阳旋转?
请原谅我的问题
我发现了我的错误,而且这是一个愚蠢的错误。一切正常。那么我怎样才能删除这个问题呢?
我似乎无法使用 OpenGL 让地球绕太阳旋转,我也不知道为什么会这样。
这是(我认为是)相关源代码(C++ 语言)的精简版本。
这是 Init()
函数,在调用任何其他 OpenGL 函数之前被调用一次:
void Init() {
glMatrixMode(GL_MODELVIEW);
gluLookAt (
0, 1, -3, // Where am I
0, 0, 0, // Where am I looking at
0, 1, 0 // Direction of 'my head'
);
// Going to Projection mode ...
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// ... to set up perspective viewing.
glFrustum (-0.5, 0.5,
-0.5, 0.5,
1.0, 100.0);
// Going back to Model view since drawing
// is done entierly in Modelview.
glMatrixMode(GL_MODELVIEW);
}
然后这是 Draw()
函数,它是重复调用以便“动画”场景:
void Draw() {
glClear(
GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT
);
// Draw yellow Sun
glColor3d(1.0, 1.0, 0.0);
sphere(radius_sun);
// draw the path of the Earth around
// the Sun. Assume distance Earth to Sun = 1
glColor4d( 0.0, 0.0, 1.0, 0.5);
glBegin(GL_LINE_LOOP);
for (double q=0; q<=1; q+=0.01) {
double rad = q * M_PI * 2.0;
glVertex3d(std::cos(rad), 0, std::sin(rad));
}
glEnd();
// the Earth
glColor3d(1.0, 1.0, 1.0);
glPushMatrix();
// Assign a value to t dependant on the
// time already passed.
double t = IncreasingDouble();
double angle = t * M_PI * 2.0;
// Rotate scene angle degres about y-axis:
glRotated (angle, 0, 1, 0);
// And then translate coordinate system
glTranslated(1, 0, 0);
sphere(radius_earth);
// Restore matrix for next call
glPopMatrix();
// Swap Buffers and the more snipped..
}
我没有发现我的错误,但地球正在螺旋式地远离太阳越来越远尽管场景的其余部分(太阳和地球的轨道-路径)绘制正确。
Please excuse my question
I spotted my misstake, and it is a dumb one. Everything is working ok. So how can I delete the question?
I can't seem to make an Earth rotate about a Sun with OpenGL and I have no idea as to why that is.
Here's a stripped down version of the (what I believe to be) relevant source code (in c++).
This is the Init()
function that is called once before any other OpenGL function is called:
void Init() {
glMatrixMode(GL_MODELVIEW);
gluLookAt (
0, 1, -3, // Where am I
0, 0, 0, // Where am I looking at
0, 1, 0 // Direction of 'my head'
);
// Going to Projection mode ...
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// ... to set up perspective viewing.
glFrustum (-0.5, 0.5,
-0.5, 0.5,
1.0, 100.0);
// Going back to Model view since drawing
// is done entierly in Modelview.
glMatrixMode(GL_MODELVIEW);
}
Then this is the Draw()
function that is called repeadetly in order to "animate" the scene:
void Draw() {
glClear(
GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT
);
// Draw yellow Sun
glColor3d(1.0, 1.0, 0.0);
sphere(radius_sun);
// draw the path of the Earth around
// the Sun. Assume distance Earth to Sun = 1
glColor4d( 0.0, 0.0, 1.0, 0.5);
glBegin(GL_LINE_LOOP);
for (double q=0; q<=1; q+=0.01) {
double rad = q * M_PI * 2.0;
glVertex3d(std::cos(rad), 0, std::sin(rad));
}
glEnd();
// the Earth
glColor3d(1.0, 1.0, 1.0);
glPushMatrix();
// Assign a value to t dependant on the
// time already passed.
double t = IncreasingDouble();
double angle = t * M_PI * 2.0;
// Rotate scene angle degres about y-axis:
glRotated (angle, 0, 1, 0);
// And then translate coordinate system
glTranslated(1, 0, 0);
sphere(radius_earth);
// Restore matrix for next call
glPopMatrix();
// Swap Buffers and the more snipped..
}
I don't spot my mistake but the earth is moving spirally away from the sun further and further although the rest of the scene (Sun and Earth's orbit-path) are drawn correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题一定出在其他地方,也许在你的 sphere() 方法内部。我正在按照与您相同的步骤操作,但我的有效。这是我得到的,它按照你想要的方式工作(一个球体围绕另一个球体平滑旋转)(另外,请注意我的 gluLookAt 和 glFrustum 是不同的,但这应该没有什么区别。我只是这样做,这样我就可以使用我的鼠标改变我的观察点):
The problem must be elsewhere, maybe inside your sphere() method. I'm following the same steps as you, but mine works. Here's what I've got, and it's working the way you want (one sphere rotates smoothly around another) (Also, note my gluLookAt and glFrustum are different, but that should make no difference. I just do it this way so I can use my mouse to change my viewing point):