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 9150953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:41:01+00:00 2026-06-17T11:41:01+00:00

I’ve been able to use modelview matrix and functions like glTranslatef() and gluLookAt() to

  • 0

I’ve been able to use modelview matrix and functions like glTranslatef() and gluLookAt() to transform an object or the perspective of the entire scene, but when I try to do both I run into problems.

It seems that regardless of the parameters I use for gluLookAt(), the objects I draw are displayed from the same angle. I’ve tried calling functions in different others different uses of glPushMatrix() and glPopMatrix based on what I’ve read in other threads with no luck. Here is what I have now. The objects move they are supposed to, but I can only get them to appear from one viewpoint.

For instance, since they should be moving in the xy plane, and are circling counterclockwise, I would think that changing z_0 to -30 would make them appear to be moving clockwise, but it doesn’t seem to make any difference. There is a long list of constants preceding it that I’m leaving out. The eventual goal is to make a simple model of our solar system.

    GLfloat time = 0.0;
GLfloat inc = 0.01;

// viewing parameters
GLint winWidth = 600, winHeight = 600;
GLfloat x_0 = 0.0, y_0 = 0.0, z_0 = 30.0;
GLfloat xref = 0.0, yref = 0.0, zref = 0.0;
GLfloat Vx = 0.0, Vy= 1.0, Vz = 0.0;
GLfloat xwMin = -50.0, ywMin = -50.0, xwMax = 50.0, ywMax = 50.0;
GLfloat dnear = .3, dfar = 10.0;

void view(void) {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(x_0, y_0, z_0, xref, yref, zref, Vx, Vy, Vz);
}

void init(void) {
    view();
    glEnable(GL_DEPTH_TEST);
}

void drawPlanet(GLfloat maj, GLfloat min, GLfloat x0, GLfloat theta,
        GLfloat size) {
    GLfloat x = maj * cosf(theta) + x0;
    GLfloat y = min * sinf(theta);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glTranslatef(x, y, 0);
    glutSolidSphere(size, 25, 25);
    glPopMatrix();
}

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(1.0, 1.0, 1.0);

    drawPlanet(MERCURY_MAJOR, MERCURY_MINOR, MERCURY_CENTER,
        time / MERCURY_YEAR, MERCURY_SIZE);

    drawPlanet(MARS_MAJOR, MARS_MINOR, MARS_CENTER,
        time / MARS_YEAR, MARS_SIZE);

    drawPlanet(URANUS_MAJOR, URANUS_MINOR, URANUS_CENTER,
        time / URANUS_YEAR, URANUS_SIZE);

    glFlush();
}

void idle(void) {
    time += inc;
    display();
    glutSwapBuffers();
}

int main (int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(0,0);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
    glutCreateWindow("");

    init();

    glutDisplayFunc(display);
    glutIdleFunc(idle);

    glutMainLoop();
}

If anyone sees what I’m missing I’d really appreaciate an explanation or direction to some additional reading that will help me fix the problem.

  • 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-17T11:41:02+00:00Added an answer on June 17, 2026 at 11:41 am

    You make the typical OpenGL newbie mistake and scatter your matrix manipulations all around the place. You never initialize OpenGL matrix state. Always set it up fresh when entering the display function. This makes things much easier to spot.

    Also you may mistake OpenGL for a scene graph with glPush/PopMatrix “magically” establishing a transformation hierachy.

    glPushMatrix – as you probably know – makes a copy of the current top of the stack matrix and pushes it on the stack. The idea is to make temporary copies which further transformations can be multiplied onto.

    Now let’s look at your code in slightly different form:

    void display(void) {
        glViewport(…);
    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(x_0, y_0, z_0, xref, yref, zref, Vx, Vy, Vz);
    
        glColor3f(1.0, 1.0, 1.0);
    
        {
            GLfloat x = maj * cosf(theta) + x0;
            GLfloat y = min * sinf(theta);
            glMatrixMode(GL_MODELVIEW);
            glPushMatrix();
            glLoadIdentity(); // <<<<<<<<<
    
            glTranslatef(x, y, 0);
            glutSolidSphere(size, 25, 25);
            glPopMatrix();
        }
    

    You see what’s happening here? You’re resetting your modelview matrix to identity, then translate that. The look-at matrix you create previously is completely discarded, and has no effect on the sphere drawing.

    Remove that glLoadIdentity() I marked and things should work just fine.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I am confused How to use looping for Json response Array in another Array.
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:

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.