Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8709573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:23:39+00:00 2026-06-13T04:23:39+00:00

The program should simulate a planet rotating around another planet. I use gltranslatef to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:23:41+00:00Added an answer on June 13, 2026 at 4:23 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a program in XNA 4.0 that should simulate an assembly line process.
my program should print a message on the screen if the formula that the
I have this program that should execute a piece of code base on the
Im writing a program that should read input via stdin, so I have the
I'm working on a Java program that should repeatedly kill two processes as soon
I managed to write simple program which should simulate passing control in system consisting
Problem: Create an algorithm for a program that simulates a police radar gun. The
I wrote a program that simulate the bash command in Linux, in C. It
This program should calculate the value of 2^1+2^2 + ... + 2^10: #include <stdio.h>
What the program should do: After the user clicks the delete button with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.