Sketching objects near each other [closed]

2020-04-11 07:40发布

问题:

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 7 years ago.

I want to sketch the below graph on the screen:

             |----|    sphere
             |----|
             / /
            / /
           / /         cylinder
          / /
         / / angle = 45
         | |
         | |
         | |           cylinder
         | |
         | |
         | |
 -----------           cylinder
 -----------

My output:

             / /
            / /
           / /         cylinder
          / /                        |-----|  sphere
         / / angle = 45              |-----|

I will sketch the top part namely sphere with a cylinder. My code is below, please look and say what is wrong.

I have tried to find the error why my primitives do not near to each other. But, I could not find. I have tried to change parameters of translate, but it does not work. Please, help

void object(void) {
    GLUquadraticObj *t = gluNewQuadratic();

    glTranslatef(-2.0f, -1.0f, 0.0f);
    gluCylinder(t, 0.1f, 0.1f, 0.3f, 32,32);
    gluSphere(t, 0.2f, 26, 13);

}

void display(void) {
    glLoadIdentity();
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    object();
    glPopMAtrix();
    glPopMatrix();
    glutSwapBuffers();
    glFlush();        
}

void reshape(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0f, w/h, 1.0, 500.0f);
    glLoadIdentity();
}

回答1:

You need to translate in between the calls to gluCylinder and gluSphere. In the current code, they will both have the same transformations, i.e. they will be in the same position/orientation. Try:

gluCylinder ( t, 0.1f, 0.1f, 0.3f, 32,32);
glTranslatef ( -2.0f, -1.0f, 0.0f );
gluSphere ( t, 0.2f, 26, 13 ) ;

Also you call PopMatrix() more times than you call PushMatrix().