如何一次显示一个对象?

时间:2015-05-26 12:39:34

标签: opengl

我想绘制两个对象,当我按下按钮只显示一个时。当我再次按下显示下一个。

GLUquadricObj *newQuad = gluNewQuadric();
glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity ();
glColor3f(1.0,0.0,0.0);
glRotatef(50,1,0,0);

glPushMatrix();
    glTranslatef(-200,80,0);
    gluQuadricDrawStyle(newQuad,GLU_LINE);
gluSphere(newQuad,30,30,10);
glPopMatrix();

glPushMatrix();
glTranslatef(-130, 80, 0.0);
gluQuadricDrawStyle(newQuad,GLU_LINE);
gluCylinder( newQuad,20,20,60,20,10);
glPopMatrix();                      //cylinder

glPushMatrix();
glTranslatef(-60, 80, 0);
gluQuadricDrawStyle(newQuad,GLU_LINE);
gluCylinder(newQuad,20,0,60,20,10);   //cone
glPopMatrix();

这是C ++

1 个答案:

答案 0 :(得分:0)

当您想要显示一个对象时,创建一个布尔变量并将其设置为true。当输入发生时,将变量从true翻转到false,反之亦然。

//Variable
boolean showOneObject;

//Constructor
public MyClass()
{
    //...
    showOneObject = false;
}

//Inside input
public void input()
{
    //Flip variable
    showOneObject = !showOneObject;
}

现在当你画画时这样做

//If showOneObject == false, draw first object, otherwise only the second one
if (!showOneObject)
{
    glPushMatrix();
    glTranslatef(-130, 80, 0.0);
    gluQuadricDrawStyle(newQuad,GLU_LINE);
    gluCylinder( newQuad,20,20,60,20,10);
    glPopMatrix();     //cylinder
}                 

glPushMatrix();
glTranslatef(-60, 80, 0);
gluQuadricDrawStyle(newQuad,GLU_LINE);
gluCylinder(newQuad,20,0,60,20,10);   //cone
glPopMatrix();