///****************************************************************************** //* //* Projeto: Visualizador em Tempo Real //* //* Modulo: lua_scene.cpp //* //* Objetivo: A simple solar system Sun+Planet+Moon using the concept of scene //* graph. //* //* Versao Original: 1.0 Data: October 2001 //* //* Autor: Ismael H F Santos //* //* Email: ismael@cenpes.petrobras.com.br, //* ismael@tecgraf.puc-rio.br, //* ismaelh@uol.com.br //* //******************************************************************************* //* //* Historico de Alteracoes: //* //* DATA AUTOR COMENTARIO //* ---- ----- ---------- //* xx/yy/00 .... .... //* //*******************************************************************************/ #include #ifdef WIN32 #include #endif #include "GL/gl.h" #include "GL/glu.h" #include "GL/glut.h" /* define variable to control the current time */ static double time = 0.0; /* in hours */ /* define numerical constants */ static const double time_step = 1; /* time step in hours */ static const double one_day = 24; /* 24 hours in one day */ static const double one_year = 352; /* 50 days in one year */ static const double one_hour = 1; /* 60 minutos em uma hora */ static const double sun_radius = 10; /* sun radius */ static const double pl_radius = 5; /* planet radius */ static const double distance = 60; /* planet distance from the sun */ static const double moon_radius = 2;/* moon radius */ static const double moon_distance = 9;/* moon distance from the planet */ /*---------------------------------------------------------------------*/ /* render moon */ /*---------------------------------------------------------------------*/ static void drawMoon( void ) { /*----------------------*/ /* define Moon material */ /*----------------------*/ { GLfloat color[] = {0,1,1,1}; glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,color); } /*-----------*/ /* draw moon */ /*-----------*/ { double teta_moon = 0; // 360.*time/(one_day*one_hour); glPushMatrix(); glRotated(teta_moon,0,1,0); drawSphere(moon_radius); glPopMatrix(); } /*------------------*/ /* restore material */ /*------------------*/ { GLfloat none[] = {0,0,0,1}; glMaterialfv(GL_FRONT,GL_EMISSION,none); } } static void renderMoon(void) { glPushMatrix(); /*-----------*/ /* draw moon */ /*-----------*/ drawMoon(); /*--------------------------------------*/ /* Apply transform for sub-tree of moon */ /*--------------------------------------*/ // { double angle_xx = ....; // // } // // renderXX(); // glPopMatrix(); } /*---------------------------------------------------------------------*/ /* render planet */ /*---------------------------------------------------------------------*/ static void drawPlanet(void) { /*------------------------*/ /* define planet material */ /*------------------------*/ { GLfloat color[] = {0,0,1,1}; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,color); } /*-------------*/ /* draw planet */ /*-------------*/ { double teta_plan = 0; //360.*time/(one_day*one_hour); glPushMatrix(); glRotated(teta_plan,0,1,0); drawSphere(pl_radius); glPopMatrix(); } /*------------------*/ /* restore material */ /*------------------*/ { GLfloat none[] = {0,0,0,1}; glMaterialfv(GL_FRONT,GL_EMISSION,none); } } static void renderPlanet(void) { glPushMatrix(); /*-------------*/ /* draw planet */ /*-------------*/ drawPlanet(); /*----------------------------------------*/ /* Apply transform for sub-tree of Planet */ /*----------------------------------------*/ { double angle_day = 360.*time/(10.*one_day); // Divide by 10. to smooth moon´s movement !!! glRotated(angle_day,0,1,1); glTranslated(moon_distance,0,0); } renderMoon(); glPopMatrix(); } static void renderPlanetOrbit(void) { /*---------------------*/ /* define sun material */ /*---------------------*/ { GLfloat color[] = {1,1,1,1}; glMaterialfv(GL_FRONT,GL_EMISSION,color); /* it is a source of light */ } /*-----------------------*/ /* draw Orbit for Planet */ /*-----------------------*/ { glPushMatrix(); glRotated(90,1,0,0); drawDisk(distance-0.1, distance+0.1); glPopMatrix(); } /*------------------*/ /* restore material */ /*------------------*/ { GLfloat none[] = {0,0,0,1}; glMaterialfv(GL_FRONT,GL_EMISSION,none); } } /*---------------------------------------------------------------------*/ /* render sun -- fixed at origin */ /*---------------------------------------------------------------------*/ static void renderSun(void) { /*---------------------*/ /* position light at origin */ /*---------------------*/ { GLfloat pos[] = {0,0,0,1}; glLightfv(GL_LIGHT0,GL_POSITION,pos); } /*---------------------*/ /* define sun material */ /*---------------------*/ { GLfloat color[] = {1,1,0.2F,1}; glMaterialfv(GL_FRONT,GL_EMISSION,color); /* it is a source of light */ } /*----------*/ /* draw Sun */ /*----------*/ { double teta_sol = 0; //360.*time/(one_day*one_year); glPushMatrix(); glRotated(teta_sol,0,1,0); drawSphere(sun_radius); glPopMatrix(); } /*------------------*/ /* restore material */ /*------------------*/ { GLfloat none[] = {0,0,0,1}; glMaterialfv(GL_FRONT,GL_EMISSION,none); } } /*---------------------------------------------------------------------*/ /* renderSolarSystem */ /*---------------------------------------------------------------------*/ static void renderSolarSystem(void) { glPushMatrix(); /*----------*/ /* draw sun */ /*----------*/ renderSun(); renderPlanetOrbit(); /*----------------------------------------*/ /* Apply transform for sub-tree of planet */ /*----------------------------------------*/ { double angle_year = 360.*time/(one_day*one_year); glRotated(angle_year,0,1,0); glTranslated(distance,0,0); } renderPlanet(); glPopMatrix(); } /*---------------------------------------------------------------------*/ /* frustum and camera definition */ /*---------------------------------------------------------------------*/ static void setupCamera(void) { /* position camera */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45,1,distance,10*distance); } static void placeCamera(void) { /* position camera */ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0,-1.5*distance,4*distance,0,0,0,0,1,0); } //////////////////////////////////////////////// // GLU Functions //////////////////////////////////////////////// /*-----------------------*/ /* draw sphere using GLU */ /*-----------------------*/ static void drawSphere( double radius ) { static GLUquadricObj* qobj = NULL; if (!qobj) qobj = gluNewQuadric(); gluSphere(qobj,radius,32,32); } /*---------------------*/ /* draw disk using GLU */ /*---------------------*/ static void drawDisk( double inerRadius, double outerRadius ) { static GLUquadricObj* qobj = NULL; if (!qobj) qobj = gluNewQuadric(); gluDisk(qobj,inerRadius,outerRadius,32,32); } /*------------------------------*/ /* move and render solar-system */ /*------------------------------*/ static void display(void) { /* clear buffers */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* set projection */ setupCamera(); /* position camera */ placeCamera(); /* render Solar System */ renderSolarSystem(); time += time_step; /* update time */ glutSwapBuffers(); } /*---------------------*/ /* function redraw */ /*---------------------*/ static void redraw (int width, int height) { /* specify black as background */ glClearColor(0,0,0,1); /* enable z-buffer test for hiddern surface removal */ glEnable(GL_DEPTH_TEST); /* enable Lights - representing the sun light */ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); /* representing the sun light */ display(); } /*---------------------*/ /* keyboard callback */ /*---------------------*/ static void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; } } /*---------------------*/ /* GL main program */ /*---------------------*/ int main (int argc, char **argv) { /* GLUT - Initialization */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("CG2001-T1"); /* Registrando callbacks */ glutDisplayFunc(display); glutReshapeFunc(redraw); // glutMouseFunc(mouseCall); // glutMotionFunc(motionCall); glutKeyboardFunc(keyboard); glutIdleFunc(display); /* GLUT main loop */ glutMainLoop(); return 0; }