Sketching objects near each other [closed]

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

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().



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!