主题:[讨论]OpenGL阴影贴图问题,急!!!(牛仔请进!)
我按照eastcowboy的教程,绘制了一个动画场景,太阳地球月亮,外加三个平面。
遇到了这个的问题:不知道如何产生shadow ,就是地球和月亮的阴影,我是初学者,请您多指教!万分感激!
以下是源代码:
#include <GL/glut.h>
#include <GL/glaux.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#pragma comment(lib, "glaux.lib")
// sun,earth and moon
// assume that each month contains 30 days
// 12 months make up a year
static int day=0; //day varies from 0 to 359
GLUquadricObj* qobj[2];//quadric object
GLuint texID[2];//texture object
GLfloat light_pos[] = {0.0f, 0.0f, 0.0f, 1.0f};
void init()
{
qobj[0]=qobj[1]=gluNewQuadric(); //create quadric object
AUX_RGBImageRec *TextureImage[2];
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
//texture of earth
TextureImage[0]=auxDIBImageLoad("earth_map.bmp");
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1,&texID[0]);
glBindTexture(GL_TEXTURE_2D,texID[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, TextureImage[0]->sizeX, TextureImage[0]->sizeY,0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
//texture of moon
TextureImage[1]=auxDIBImageLoad("moon_map.bmp");
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1,&texID[1]);
glBindTexture(GL_TEXTURE_2D,texID[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, TextureImage[1]->sizeX, TextureImage[1]->sizeY,0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);
}
void light_source()
{// define light source (white)
{
//light_position[] = {0.0f, 59600000.0f, 0.0f, 1.0f};
GLfloat light_ambient[] = {0.5f, 0.5f, 0.5f, 1.0f};
GLfloat light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);//choose the light model:both sides receive light
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
}
}
void draw_sun()
{ // define the material of sun and draw sun
{
GLfloat sun_mat_ambient[] = {1.0f, 0.0f, 0.0f, 1.0f};
GLfloat sun_mat_diffuse[] = {1.0f, 0.0f, 0.0f, 1.0f};
GLfloat sun_mat_specular[] = {1.0f, 0.0f, 0.0f, 1.0f};
GLfloat sun_mat_emission[] = {1.0f, 0.0f, 0.0f, 1.0f};// sun launches red light
GLfloat sun_mat_shininess = 0.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, sun_mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, sun_mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, sun_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, sun_mat_emission);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, sun_mat_shininess);
}
//glRotatef(day/360.0*360.0, 0.0, 1.0, 0.0);
glutSolidSphere(49600000,60, 60);
}
void draw_earth()
{// define th material of earth and draw earth
{
GLfloat earth_mat_ambient[] = {0.4f, 0.4f, 0.8f, 1.0f};
GLfloat earth_mat_diffuse[] = {0.4f, 0.4f, 0.8f, 1.0f};
GLfloat earth_mat_specular[] = {0.4f, 0.4f, 0.8f, 1.0f};
GLfloat earth_mat_emission[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat earth_mat_shininess = 30.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, earth_mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, earth_mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, earth_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, earth_mat_emission);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, earth_mat_shininess);
}
glEnable(GL_TEXTURE_2D);
//glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_ADD);//replace the color by texture color
glBindTexture(GL_TEXTURE_2D,texID[0]);
gluQuadricTexture(qobj[0],GL_TRUE);//generate the texture coordinates for the quadric surface
glRotatef(day/360.0*360.0, 0.0, 1.0, 0.0);//earth revolution
glTranslatef(150000000, 0.0, 0.0);// earth revolution radius
glPushMatrix();
glRotatef(day/360.0*360.0, 0.0, 1.0, 0.0);//earth rotation
gluSphere(qobj[0],25945000, 60, 60);//draw earth
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
void draw_moon()
{// define the material of moon and draw moon
{
GLfloat moon_mat_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat moon_mat_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat moon_mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat moon_mat_emission[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat moon_mat_shininess = 60.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, moon_mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, moon_mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, moon_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, moon_mat_emission);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, moon_mat_shininess);
}
glEnable(GL_TEXTURE_2D);
//glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
glBindTexture(GL_TEXTURE_2D,texID[1]);
gluQuadricTexture(qobj[1],GL_TRUE);
glRotatef(day/30.0*360.0, 0.0, 1.0, 0.0);//moon rotates around the earth
glTranslatef(48000000, 0.0, 0.0);//moon revolution radius
gluSphere(qobj[1],8345000, 20, 20);//draw moon
glDisable(GL_TEXTURE_2D);
}
void draw_plane()
{
{// define material of plane and draw bottom plane:
{
GLfloat plane_mat_ambient[] = {0.0f, 0.4f, 0.6f, 1.0f};
GLfloat plane_mat_diffuse[] = {0.0f, 0.4f, 0.6f, 1.0f};
GLfloat plane_mat_specular[] = {0.0f, 0.4f, 0.6f, 1.0f};
GLfloat plane_mat_emission[] = {0.0f, 0.0f, 0.0f, 1.0f};//does not shine
GLfloat plane_mat_shininess = 0.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, plane_mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, plane_mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, plane_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, plane_mat_emission);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, plane_mat_shininess);
}
glBegin(GL_QUADS);
glVertex3f(320000000,-89600000,-320000000);
glVertex3f(-320000000,-89600000,-320000000);
glVertex3f(-320000000,-89600000,320000000);
glVertex3f(320000000,-89600000,320000000);
glEnd();
}
{// define material of plane and draw front plane:
{
GLfloat plane_mat_ambient[] = {0.0f, 1.0f, 0.0f, 1.0f};
GLfloat plane_mat_diffuse[] = {0.0f, 1.0f, 0.0f, 1.0f};
GLfloat plane_mat_specular[] = {0.0f, 1.0f, 0.0f, 1.0f};
GLfloat plane_mat_emission[] = {0.0f, 0.0f, 0.0f, 1.0f};//does not shine
GLfloat plane_mat_shininess = 0.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, plane_mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, plane_mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, plane_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, plane_mat_emission);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, plane_mat_shininess);
}
glBegin(GL_QUADS);
glVertex3f(320000000,-89600000,-320000000);
glVertex3f(-320000000,-89600000,-320000000);
glVertex3f(-320000000,89600000,-320000000);
glVertex3f(320000000,89600000,-320000000);
glEnd();
}
{//define material of plane and draw left plane:
{
GLfloat plane_mat_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat plane_mat_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat plane_mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat plane_mat_emission[] = {0.0f, 0.0f, 0.0f, 1.0f};//does not shine
GLfloat plane_mat_shininess = 0.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, plane_mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, plane_mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, plane_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, plane_mat_emission);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, plane_mat_shininess);
}
glBegin(GL_QUADS);
glVertex3f(-320000000,-89600000,-320000000);
glVertex3f(-320000000,-89600000,320000000);
glVertex3f(-320000000,89600000,320000000);
glVertex3f(-320000000,89600000,-320000000);
glEnd();
}
}
void myDisplay(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);//projection transform
glLoadIdentity();
gluPerspective(70, 1, 0, 400000000);//perspective projection
//fovy=70°,aspect=width/height=1.0,znear=0.0,zfar=200000000*2=400000000
glMatrixMode(GL_MODELVIEW);//model view transform
glLoadIdentity();
gluLookAt(150000000, 200000000, 400000000, 0, 0, 0, 0, 1, 0);
glPushMatrix();
light_source();//enable the defined light
draw_sun();
draw_earth();
draw_moon();
glPopMatrix();
draw_plane();//draw three planes
glutSwapBuffers();//exchange the rendering buffer and display buffer
Sleep(100);//sleep for 80ms
++day;
if(day>=360)
day=0;
}
void main(int argc, char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowPosition(400,0);
glutInitWindowSize(800,800);
glutCreateWindow("sun earth and moon");
init();
glutDisplayFunc(myDisplay);
glutIdleFunc(myDisplay);//call the function when CPU is idle
glutMainLoop();
}
遇到了这个的问题:不知道如何产生shadow ,就是地球和月亮的阴影,我是初学者,请您多指教!万分感激!
以下是源代码:
#include <GL/glut.h>
#include <GL/glaux.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#pragma comment(lib, "glaux.lib")
// sun,earth and moon
// assume that each month contains 30 days
// 12 months make up a year
static int day=0; //day varies from 0 to 359
GLUquadricObj* qobj[2];//quadric object
GLuint texID[2];//texture object
GLfloat light_pos[] = {0.0f, 0.0f, 0.0f, 1.0f};
void init()
{
qobj[0]=qobj[1]=gluNewQuadric(); //create quadric object
AUX_RGBImageRec *TextureImage[2];
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
//texture of earth
TextureImage[0]=auxDIBImageLoad("earth_map.bmp");
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1,&texID[0]);
glBindTexture(GL_TEXTURE_2D,texID[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, TextureImage[0]->sizeX, TextureImage[0]->sizeY,0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
//texture of moon
TextureImage[1]=auxDIBImageLoad("moon_map.bmp");
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1,&texID[1]);
glBindTexture(GL_TEXTURE_2D,texID[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, TextureImage[1]->sizeX, TextureImage[1]->sizeY,0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);
}
void light_source()
{// define light source (white)
{
//light_position[] = {0.0f, 59600000.0f, 0.0f, 1.0f};
GLfloat light_ambient[] = {0.5f, 0.5f, 0.5f, 1.0f};
GLfloat light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);//choose the light model:both sides receive light
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
}
}
void draw_sun()
{ // define the material of sun and draw sun
{
GLfloat sun_mat_ambient[] = {1.0f, 0.0f, 0.0f, 1.0f};
GLfloat sun_mat_diffuse[] = {1.0f, 0.0f, 0.0f, 1.0f};
GLfloat sun_mat_specular[] = {1.0f, 0.0f, 0.0f, 1.0f};
GLfloat sun_mat_emission[] = {1.0f, 0.0f, 0.0f, 1.0f};// sun launches red light
GLfloat sun_mat_shininess = 0.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, sun_mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, sun_mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, sun_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, sun_mat_emission);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, sun_mat_shininess);
}
//glRotatef(day/360.0*360.0, 0.0, 1.0, 0.0);
glutSolidSphere(49600000,60, 60);
}
void draw_earth()
{// define th material of earth and draw earth
{
GLfloat earth_mat_ambient[] = {0.4f, 0.4f, 0.8f, 1.0f};
GLfloat earth_mat_diffuse[] = {0.4f, 0.4f, 0.8f, 1.0f};
GLfloat earth_mat_specular[] = {0.4f, 0.4f, 0.8f, 1.0f};
GLfloat earth_mat_emission[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat earth_mat_shininess = 30.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, earth_mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, earth_mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, earth_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, earth_mat_emission);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, earth_mat_shininess);
}
glEnable(GL_TEXTURE_2D);
//glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_ADD);//replace the color by texture color
glBindTexture(GL_TEXTURE_2D,texID[0]);
gluQuadricTexture(qobj[0],GL_TRUE);//generate the texture coordinates for the quadric surface
glRotatef(day/360.0*360.0, 0.0, 1.0, 0.0);//earth revolution
glTranslatef(150000000, 0.0, 0.0);// earth revolution radius
glPushMatrix();
glRotatef(day/360.0*360.0, 0.0, 1.0, 0.0);//earth rotation
gluSphere(qobj[0],25945000, 60, 60);//draw earth
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
void draw_moon()
{// define the material of moon and draw moon
{
GLfloat moon_mat_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat moon_mat_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat moon_mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat moon_mat_emission[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat moon_mat_shininess = 60.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, moon_mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, moon_mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, moon_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, moon_mat_emission);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, moon_mat_shininess);
}
glEnable(GL_TEXTURE_2D);
//glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
glBindTexture(GL_TEXTURE_2D,texID[1]);
gluQuadricTexture(qobj[1],GL_TRUE);
glRotatef(day/30.0*360.0, 0.0, 1.0, 0.0);//moon rotates around the earth
glTranslatef(48000000, 0.0, 0.0);//moon revolution radius
gluSphere(qobj[1],8345000, 20, 20);//draw moon
glDisable(GL_TEXTURE_2D);
}
void draw_plane()
{
{// define material of plane and draw bottom plane:
{
GLfloat plane_mat_ambient[] = {0.0f, 0.4f, 0.6f, 1.0f};
GLfloat plane_mat_diffuse[] = {0.0f, 0.4f, 0.6f, 1.0f};
GLfloat plane_mat_specular[] = {0.0f, 0.4f, 0.6f, 1.0f};
GLfloat plane_mat_emission[] = {0.0f, 0.0f, 0.0f, 1.0f};//does not shine
GLfloat plane_mat_shininess = 0.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, plane_mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, plane_mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, plane_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, plane_mat_emission);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, plane_mat_shininess);
}
glBegin(GL_QUADS);
glVertex3f(320000000,-89600000,-320000000);
glVertex3f(-320000000,-89600000,-320000000);
glVertex3f(-320000000,-89600000,320000000);
glVertex3f(320000000,-89600000,320000000);
glEnd();
}
{// define material of plane and draw front plane:
{
GLfloat plane_mat_ambient[] = {0.0f, 1.0f, 0.0f, 1.0f};
GLfloat plane_mat_diffuse[] = {0.0f, 1.0f, 0.0f, 1.0f};
GLfloat plane_mat_specular[] = {0.0f, 1.0f, 0.0f, 1.0f};
GLfloat plane_mat_emission[] = {0.0f, 0.0f, 0.0f, 1.0f};//does not shine
GLfloat plane_mat_shininess = 0.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, plane_mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, plane_mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, plane_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, plane_mat_emission);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, plane_mat_shininess);
}
glBegin(GL_QUADS);
glVertex3f(320000000,-89600000,-320000000);
glVertex3f(-320000000,-89600000,-320000000);
glVertex3f(-320000000,89600000,-320000000);
glVertex3f(320000000,89600000,-320000000);
glEnd();
}
{//define material of plane and draw left plane:
{
GLfloat plane_mat_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat plane_mat_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat plane_mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat plane_mat_emission[] = {0.0f, 0.0f, 0.0f, 1.0f};//does not shine
GLfloat plane_mat_shininess = 0.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, plane_mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, plane_mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, plane_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, plane_mat_emission);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, plane_mat_shininess);
}
glBegin(GL_QUADS);
glVertex3f(-320000000,-89600000,-320000000);
glVertex3f(-320000000,-89600000,320000000);
glVertex3f(-320000000,89600000,320000000);
glVertex3f(-320000000,89600000,-320000000);
glEnd();
}
}
void myDisplay(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);//projection transform
glLoadIdentity();
gluPerspective(70, 1, 0, 400000000);//perspective projection
//fovy=70°,aspect=width/height=1.0,znear=0.0,zfar=200000000*2=400000000
glMatrixMode(GL_MODELVIEW);//model view transform
glLoadIdentity();
gluLookAt(150000000, 200000000, 400000000, 0, 0, 0, 0, 1, 0);
glPushMatrix();
light_source();//enable the defined light
draw_sun();
draw_earth();
draw_moon();
glPopMatrix();
draw_plane();//draw three planes
glutSwapBuffers();//exchange the rendering buffer and display buffer
Sleep(100);//sleep for 80ms
++day;
if(day>=360)
day=0;
}
void main(int argc, char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowPosition(400,0);
glutInitWindowSize(800,800);
glutCreateWindow("sun earth and moon");
init();
glutDisplayFunc(myDisplay);
glutIdleFunc(myDisplay);//call the function when CPU is idle
glutMainLoop();
}