OpenGL(太陽、地球、月+太陽系八大惑星)
OpenGLは太陽、地球、月を描きます
太陽系八大惑星
#include
#include
#include
#define GL_PI 3.1415f
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
GLfloat whiteLight[]={0.2f,0.2f,0.2f,1.0f};
GLfloat lightPos[]={0.0f,0.0f,0.0f,1.0f};
void RenderScene(void)
{
static float fMoonRot = 0.0f;
static float fEarthRot= 0.0f;
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0f,0.0f,-300.0f);
//
glColor3ub(255,255,0);
glDisable(GL_LIGHTING); //
glutSolidSphere(20.0f,20.0f,20.0f);
glEnable(GL_LIGHTING); //
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
//
glColor3ub(0,0,255);
glRotatef(fEarthRot,0.0f,1.0f,0.0f);
glTranslatef(105.0f,0.0f,0.0f);
glutSolidSphere(15.0f,15.0f,15.0f); // , ,
// ,
glColor3ub(200,200,200);
glRotatef(fMoonRot,0.0f,1.0f,0.0f);
glTranslatef(30.0f,0.0f,0.0f);
fMoonRot += 15.0f;
if(fMoonRot >= 365.0f)
fMoonRot = 0.0f;
glutSolidSphere(6.0f,15.0f,15.0f); // 。
glPopMatrix();
fEarthRot += 5.0f;
if(fEarthRot>=365.0f)
fEarthRot=0.0f;
glutSwapBuffers();
}
void ChangeSize(GLsizei w,GLsizei h)
{
GLfloat fAspect;
if(h==0) h=1;
glViewport(0,0,w,h);
fAspect = (GLfloat)w/(GLfloat)h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f,fAspect ,1.0,4000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void SetupRC(void)
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glClearColor(0.0f,0.0f,0.0f,1.0f);
glEnable(GL_LIGHTING); //
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight); // whiteLght
glLightfv(GL_LIGHT0,GL_POSITION,lightPos); //0
glEnable(GL_LIGHT0);
}
void SpecialKeys(int key ,int x, int y)
{
if(key==GLUT_KEY_UP)
xRot -= 5.0f;
if(key==GLUT_KEY_DOWN)
xRot +=5.0f;
if(key == GLUT_KEY_LEFT)
yRot -=5.0f;
if(key == GLUT_KEY_RIGHT)
yRot +=5.0f;
xRot = (GLfloat)((const int)xRot %360);
yRot = (GLfloat)((const int)yRot %360);
glutPostRedisplay();
}
void TimerFunc(int value)
{
glutPostRedisplay();
glutTimerFunc(100,TimerFunc,1);
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(800,600);
glutCreateWindow("Orthographic Projection");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);//
SetupRC();
glutTimerFunc(250,TimerFunc,1); // ,
glutMainLoop();
return 0;
}
太陽系八大惑星
#include
#include
#include
#define GL_PI 3.1415f
float fMoonRot = 0.0f;
float fAdamRot= 0.0f;
float fEarthRot= 0.0f;
float fMarsRot = 0.0f;
float fMarsatellite1 =0.0f;
float fMarsatellite2 =0.0f;
float fHesperRot = 0.0f;
float fJupiterRot = 0.0f;
float fSaturnRot = 0.0f;
float fSaturn1 = 0.0f;
float fUranusRot = 0.0f;
float fNeptuneRot = 0.0f;
GLfloat whiteLight[]={0.2f,0.2f,0.2f,1.0f};
GLfloat lightPos[]={0.0f,0.0f,0.0f,1.0f};
void sun()
{
//
glColor3ub(255,50,0);
glDisable(GL_LIGHTING); //
glutSolidSphere(25.0f,200.0f,200.0f);
glEnable(GL_LIGHTING); //
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
}
void Adam()
{
//
glPushMatrix();
glColor3ub(0,0,255);
glRotatef(fAdamRot,0.0f,1.0f,0.0f);
glTranslatef(30.0f,0.0f,0.0f);
glutSolidSphere(2.0f,200.0f,200.0f); // 40% 2
fAdamRot += 4.152f; // 24.08% , 4.152
if(fAdamRot>=365.0f)
fAdamRot=0.0f;
glPopMatrix();
}
void Hesper()
{
//
glPushMatrix();
glColor3ub(255,215,0);
glRotatef(fHesperRot,0.0f,1.0f,0.0f);
glTranslatef(40.0f,0.0f,0.0f);
glutSolidSphere(4.75f,200.0f,200.0f); // 95% 4.75
fHesperRot += 1.62f; // 61.56% , 1.62
if(fHesperRot>=365.0f)
fHesperRot=0.0f;
glPopMatrix();
}
void Earth_Moon()
{
// ,
glPushMatrix();
glColor3ub(0,0,255);
glRotatef(fEarthRot,0.0f,1.0f,0.0f);
glTranslatef(55.0f,0.0f,0.0f); // 365
glutSolidSphere(5.0f,20.0f,20.0f); // 5
// ,
glColor3ub(200,200,200);
glRotatef(fMoonRot,0.0f,1.0f,0.0f);
glTranslatef(5.0f,0.0f,0.0f); //
fMoonRot += 13.3f; // 27.3 365/27.3==13.3
if(fMoonRot >= 365.0f) // 3/11 1.363
fMoonRot = 0.0f;
glutSolidSphere(1.363f,200.0f,200.0f);
glPopMatrix(); // ,
fEarthRot += 1.0f;
if(fEarthRot>=365.0f)
fEarthRot=0.0f;
}
void Mars()
{
//
glPushMatrix();
glColor3ub(255,0,0);
glRotatef(fMarsRot,0.0f,1.0f,0.0f);
glTranslatef(65.0f,0.0f,0.0f);
glutSolidSphere(2.65f,200.0f,200.0f); // 53% 2.65
glPushMatrix();
// ,
glColor3ub(255,100,100);
glRotatef(fMarsatellite1,0.0f,1.0f,0.0f);
glTranslatef(2.0f,0.0f,2.0f);
fMarsatellite1 += 13.3f;
if(fMarsatellite1 >= 365.0f)
fMarsatellite1 = 0.0f;
glutSolidSphere(0.963f,200.0f,200.0f);
glPopMatrix();
glPushMatrix();
// ,
glColor3ub(255,200,200);
glRotatef(fMarsatellite2,0.0f,1.0f,0.0f);
glTranslatef(-3.0f,0.0f,-3.0f);
fMarsatellite2 += 13.3f;
if(fMarsatellite2 >= 365.0f)
fMarsatellite2 = 0.0f;
glutSolidSphere(1.20f,200.0f,200.0f);
glPopMatrix();
fMarsRot += 0.5f; // 2
if(fMarsRot>=365.0f)
fMarsRot=0.0f;
glPopMatrix();
}
void Jupiter()
{
//
glPushMatrix();
glColor3ub(200,100,0);
glRotatef(fJupiterRot,0.0f,1.0f,0.0f);
glTranslatef(100.0f,0.0f,0.0f);
glutSolidSphere(17.0f,200.0f,200.0f); // 11.21 56.05 , 17
glPushMatrix();
glColor3ub(250,180,0);
glRotatef(70.0f,1.0f,0.0f,0.0f); // 70
glutSolidTorus(1.42,20,10,100); // ,
glPopMatrix();
fJupiterRot += 0.15f; // 11.87 , 0.084 , 0.15
if(fJupiterRot>=365.0f)
fJupiterRot=0.0f;
glPopMatrix();
}
void Saturn()
{
//
glPushMatrix();
glColor3ub(73,60,32);
glRotatef( fSaturnRot,0.0f,1.0f,0.0f);
glTranslatef(148.0f,0.0f,0.0f);
glutSolidSphere(15.0f,200.0f,200.0f); // 9.45 47.25 , 15
//
glPushMatrix();
glColor3ub(255,200,200);
glRotatef(fSaturn1,0.0f,1.0f,0.0f);
glTranslatef(-17.0f,0.0f,-17.0f);
fSaturn1 += 6.4f;
if(fSaturn1 >= 365.0f)
fSaturn1 = 0.0f;
glutSolidSphere(1.20f,200.0f,200.0f);
glPopMatrix();
glPushMatrix();
glColor3ub(200,200,100);
glRotatef(70.0f,1.0f,0.0f,0.0f); // 70
glutSolidTorus(1.42,25,10,100); // ,
glPopMatrix();
fSaturnRot += 0.03f; // 29.47 , 0.03
if( fSaturnRot>=365.0f)
fSaturnRot=0.0f;
glPopMatrix();
}
void UranusRot()
{
//
glPushMatrix();
glColor3ub(0,180,100);
glRotatef( fUranusRot,0.0f,1.0f,0.0f);
glTranslatef(202.0f,0.0f,0.0f);
glutSolidSphere(15.0f,200.0f,200.0f); // 4.01, 15
glPushMatrix();
glColor3ub(0,100,0);
glRotatef(150.0f,1.0f,0.0f,0.0f); //
glutSolidTorus(1.0,20,10,100); // ,
glPopMatrix();
fUranusRot += 0.03f; // 84.06 , 0.0124
if( fUranusRot>=365.0f)
fUranusRot=0.0f;
glPopMatrix();
}
void Neptune()
{
//
glPushMatrix();
glColor3ub(0,0,215);
glRotatef( fNeptuneRot,0.0f,1.0f,0.0f);
glTranslatef(240.0f,0.0f,0.0f);
glutSolidSphere(19.45f,200.0f,200.0f); // 3.89, 19.45
fNeptuneRot += 2.17f; // 46% , 2.17
if( fUranusRot>=365.0f)
fNeptuneRot=0.0f;
glPopMatrix();
}
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0f,0.0f,-300.0f);
sun();
Adam();
Hesper();
Earth_Moon();
Mars();
Jupiter();
Saturn();
UranusRot();
Neptune();
glPopMatrix();
glutSwapBuffers();
}
void ChangeSize(GLsizei w,GLsizei h)
{
GLfloat fAspect;
if(h==0) h=1;
glViewport(0,0,w,h);
fAspect = (GLfloat)w/(GLfloat)h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f,fAspect ,1.0,4000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void SetupRC(void)
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glClearColor(0.0f,0.0f,0.0f,1.0f);
glEnable(GL_LIGHTING); //
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight); // whiteLght
glLightfv(GL_LIGHT0,GL_POSITION,lightPos); //0
glEnable(GL_LIGHT0);
}
void TimerFunc(int value)
{
glutPostRedisplay();
glutTimerFunc(100,TimerFunc,1);
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(800,600);
glutCreateWindow("Orthographic Projection");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
SetupRC();
glutTimerFunc(250,TimerFunc,1); // ,
glutMainLoop();
return 0;
}