如何解决使用 opengl 进行 2D 轨道移动
你好,我正试图在一条轨道路径上绕太阳运行地球。到目前为止,我所做的绘制了两个圆,一个是太阳,另一个是地球。地球在椭圆上。尝试使用 glutTimerFunc() 创建一个动画但没有结果,这是代码。如何以椭圆路径使地球绕太阳运行。
#include <gl/glut.h>
#include <math.h>
#define PI 3.14159
#define circlePoints 256
#define ellipsePoints 256
int i;
/*void myWireSphere(GLfloat radius,int slices,int stacks) {
glPushmatrix();
glrotatef(-90.0,1.0,0.0,0.0);
glutWireSphere(radius,slices,stacks);
glPopMatrix();
}*/
void circle(){
glColor3f(1,1,0);
GLfloat angleStep=2*PI/(float)circlePoints;
gluint pointsPerQuarter=circlePoints/4;
GLfloat x[circlePoints];
GLfloat y[circlePoints];
GLfloat radius=3;
for(i=0;i<pointsPerQuarter/2;i++)
{
//Define points in first quadrant
x[i]=radius*cos(i*angleStep);
y[i]=radius*sin(i*angleStep);
x[pointsPerQuarter-1-i]=y[i];
y[pointsPerQuarter-1-i]=x[i];
//Define points in second quadrant
x[pointsPerQuarter+i]=-y[i];
y[pointsPerQuarter+i]=x[i];
x[2*pointsPerQuarter-1-i]=-x[i];
y[2*pointsPerQuarter-1-i]=y[i];
//Define points in third quadrant
x[2*pointsPerQuarter+i]=-x[i];
y[2*pointsPerQuarter+i]=-y[i];
x[3*pointsPerQuarter-1-i]=-y[i];
y[3*pointsPerQuarter-1-i]=-x[i];
//Define points in fourth quadrant
x[3*pointsPerQuarter+i]=y[i];
y[3*pointsPerQuarter+i]=-x[i];
x[4*pointsPerQuarter-1-i]=x[i];
y[4*pointsPerQuarter-1-i]=-y[i];
}
glBegin(GL_LINE_LOOP);
for (i=0;i<circlePoints;i++)
{
glVertex2f(x[i],y[i]);
}
glEnd();
}
void circlearth(){
glColor3f(0,1);
GLfloat angleStep=2*PI/(float)circlePoints;
gluint pointsPerQuarter=circlePoints/4;
GLfloat x[circlePoints];
GLfloat y[circlePoints];
GLfloat radius=1;
for(i=0;i<pointsPerQuarter/2;i++)
{
//Define points in first quadrant
x[i]=radius*cos(i*angleStep);
y[i]=radius*sin(i*angleStep);
x[pointsPerQuarter-1-i]=y[i];
y[pointsPerQuarter-1-i]=x[i];
//Define points in second quadrant
x[pointsPerQuarter+i]=-y[i];
y[pointsPerQuarter+i]=x[i];
x[2*pointsPerQuarter-1-i]=-x[i];
y[2*pointsPerQuarter-1-i]=y[i];
//Define points in third quadrant
x[2*pointsPerQuarter+i]=-x[i];
y[2*pointsPerQuarter+i]=-y[i];
x[3*pointsPerQuarter-1-i]=-y[i];
y[3*pointsPerQuarter-1-i]=-x[i];
//Define points in fourth quadrant
x[3*pointsPerQuarter+i]=y[i];
y[3*pointsPerQuarter+i]=-x[i];
x[4*pointsPerQuarter-1-i]=x[i];
y[4*pointsPerQuarter-1-i]=-y[i];
}
glBegin(GL_LINE_LOOP);
for (i=0;i<circlePoints;i++)
{
glVertex2f(x[i],y[i]);
}
glEnd();
}
void ellipse(){
glColor3f(0,0);
GLfloat angleStep=2*PI/(float)ellipsePoints;
gluint pointsPerQuarter=ellipsePoints;///4;
GLfloat x[ellipsePoints];
GLfloat y[ellipsePoints];
GLfloat rx=15;
GLfloat ry=10;
glBegin(GL_LINE_LOOP);
for(i=0;i<pointsPerQuarter;i++)
{
x[i]=rx*cos(i*angleStep);
y[i]=ry*sin(i*angleStep);
}
for(i=0;i<ellipsePoints;i++)
{
glVertex2f(x[i],y[i]);
}
glEnd();
}
void reshape(int w,int h)
{
glViewport(0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-32,32,-24,24);
glMatrixMode(GL_MODELVIEW);
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(500,timer,0);
}
void init(void)
{
glClearColor(1,0);
glColor3f(0,0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
gllinewidth(3);
ellipse();
glPushmatrix();
glTranslatef(-7.0,0.0);
circle();
glPopMatrix();
glPushmatrix();
glTranslatef(15.0,0.0);
circlearth();
glPopMatrix();
glFlush();
glutSwapBuffers(); // double buffer
}
int main(int argc,char** argv) {
glutinit(&argc,argv);
glutinitwindowPosition(50,50);
glutinitdisplayMode(gluT_SINGLE | gluT_RGB);
glutinitwindowSize(640,480);
glutCreateWindow("Earth-Sun");
glMatrixMode(GL_PROJECTION);
init();
glutdisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0000,0);
glutMainLoop();
return 0;
}
解决方法
我制作了全局变量:
double xea = 12,yea = 0;
double ang = 0;
这个glutIdleFunc:
void idle() {
xea = 20 * sin(ang);
yea = 10 * cos(ang);
ang += 0.0001;
glutPostRedisplay();
}
然后glutIdleFunc(idle);
注册。
并在显示功能中更改了地球的位置:
glTranslatef(xea,yea,0.0);
而<gl/glut.h>
想要<GL/glut.h>
。嗯,谁是对的?
现在地球平稳地移动,或多或少在椭圆上。
但是:我注意到与带有 glDrawArrays()
和动画的简单“现代”OpenGL 程序相比,它的功耗很高(风扇立即处于活动状态)。
使用键盘回调中的手动角度增量(而不是空闲回调中的自动 60 Hz),这应该不是问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。