1

The program should simulate a planet rotating around another planet.
I use gltranslatef to let the planet move around the bigger planet, but the problem is that the planet should hide when is over the bigger planet, because dz is -0.5.
But if I test the program I always see the red planet over the blue one.
Another problem I have: the planet rotates too fast, how do I slow it?

#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include "utility.h"

GLfloat dx=0.0;
GLfloat dz=-0.5;
bool plus=true;

void init()
{
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glLoadIdentity();
    glOrtho(-1, 1, -1, 1, -1, 1);
    glEnable(GLUT_DEPTH);
}

void render()
{
    glClearColor(BLACK);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor4f(BLUE);
    glutWireSphere(0.25, 100, 100);
    glPushMatrix();
    glLoadIdentity();
    glTranslatef(-0.5+dx, 0.0, -dz);
    glColor4f(RED);
    glutWireSphere(0.05, 100, 100);
    glPopMatrix();
    glFlush();
}


void idle()
{
    if(plus)
    {
        dx+=0.05;
    }
    else
    {
        dx-=0.05;
    }
    if(dx>=1.0)
    {
        dx=0.5;
        plus=false;
    }
    else if(dx<=-0.0)
    {
        dx=0.0;
        plus=true;
    }
    glutPostRedisplay();
}



int main(int argc, const char * argv[])
{
    glutInit(&argc, (char**)argv);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(150, 150);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Simple");
    glutIdleFunc(idle);
    init();
    glutDisplayFunc(render);
    glutMainLoop();
    return 0;
}

I haven't understood well how does the idle function work, why it gets called so many times? Can't I choose a time interval with which the idle function gets called?

More info: RED and BLUE are RGB floats, defined in the utility.h header file.
plus is a bool that is used to know if I have to decrease or increase dx.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187

1 Answers1

3

Give this a shot:

#include <GL/glut.h>

double GetSeconds()
{
    return glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
}

void render()
{
    glClearColor(0,0,0,0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glColor3ub(0,0,255);
    glutWireSphere(0.25, 100, 100);

    glPushMatrix();
    glLoadIdentity();

    static double prv = GetSeconds();
    double cur = GetSeconds();
    double delta = cur - prv;
    prv = cur;

    const float DEG_PER_SEC = 60.0f;
    static float angle = 0.0f;
    angle += DEG_PER_SEC * delta;
    while( angle > 360 ) angle -= 360;

    glPushMatrix();
    glRotatef( angle, 0, 1, 0 );
    glTranslatef( 0.5, 0, 0);
    glColor3ub(255,0,0);
    glutWireSphere(0.05, 100, 100);
    glPopMatrix();

    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1, 1, -1, 1, -1, 1);
}

void timer(int extra)
{
    glutPostRedisplay();
    glutTimerFunc(16, timer, 0);
}

int main(int argc, const char * argv[])
{
    glutInit(&argc, (char**)argv);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(150, 150);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Simple");
    glutReshapeFunc(reshape);
    glutTimerFunc(0, timer, 0);
    glutDisplayFunc(render);

    glEnable( GL_DEPTH_TEST );

    glutMainLoop();
    return 0;
}

Important parts:

  • Explicit glMatrixMode() calls

  • Calling glutInitDisplayMode() before glutCreateWindow()

  • Double-buffering requires glutSwapBuffers()

  • Clearing the depth buffer via GL_DEPTH_BUFFER_BIT

  • glEnable( GL_DEPTH_TEST )

  • glRotatef() for planet rotation

  • Timer-based animation

genpfault
  • 51,148
  • 11
  • 85
  • 139