OpenGL:绘制2个圆柱体

时间:2014-08-21 15:27:16

标签: c++ opengl

我试图绘制两个圆柱体,两者都填充了相同的纹理,但我只设法绘制了第一个圆柱体,这里是我的绘制功能:

void display(void)
{
    // Projection plane
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //a->draw(mouseY);
    //draw first cylinder WORKING
    glPushMatrix();
        glRotatef(mouseY, 0, 0, 1); 
        quadratic = gluNewQuadric();
        gluQuadricNormals(quadratic, GLU_SMOOTH);
        gluQuadricTexture(quadratic, GL_TRUE);
        glMatrixMode(GL_TEXTURE);
            glLoadIdentity();
            glRotatef(90,0,0,1);
            glTranslatef(0,-1.0,0);
        glMatrixMode(GL_PROJECTION);
        gluCylinder(quadratic,fh_cyl,fh_cyl,fw_cyl,50,1000);
        if(!exec) //used only one time to get a visible cylinder
        {
            glTranslatef(fX_cylpos,fY_cylpos,0);
            glRotatef(90, 0.0, 1, 0.0); //rotate object 90° on Y axis
            glRotatef(fangle_cyl, 1.0, 0.0, 0.0); //0° = vertical / 90° = horizontal
            exec=true;
        }
    glPopMatrix();
    glPushMatrix();
        //draw second cylinder NOT WORKING
        glRotatef(mouseY, 0, 0, 1);
        quadratic2 = gluNewQuadric();
        gluQuadricNormals(quadratic2, GLU_SMOOTH);
        gluQuadricTexture(quadratic2, GL_TRUE);
        gluCylinder(quadratic2,fh_cyl,fh_cyl,fw_cyl,50,1000);
        if(!exec2) 
        {
            glTranslatef(fX_cylpos+200,fY_cylpos,0);
            glRotatef(90, 0.0, 1, 0.0); //rotate object 90° on Y axis
            glRotatef(fangle_cyl, 1.0, 0.0, 0.0);  //0° = vertical / 90° = horizontal      
            exec2=true;
        }
    glPopMatrix();
   //b->draw(mouseY);
   glutSwapBuffers();
}

这是重塑的功能:

void reshape(int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
   glLoadIdentity();
   glMatrixMode(GL_PROJECTION);
   //glOrtho(-1000.0, 1000.0*(screen_h/screen_w), -1000.0, 1000.0*(screen_h/screen_w), 1000.0, -110.0);
   glOrtho(fOrtoXinit, fOrtoXend, fOrtoYinit, fOrtoYend , 1000, -1000.0);
   glMatrixMode(GL_MODELVIEW);
}

if语句中的代码用于绘制圆柱体垂直,如下图所示,并且使用鼠标沿x轴旋转以滚动数字:preview

感谢Reto Koradi的回复,我设法画了2个圆柱,同时我还有两个问题: 我不知道要改变什么来把一个圆柱体放在另一个旁边; 2.我认为我使用if语句绘制垂直圆柱体的解决方案不是一个好方法......

以下是编辑过的代码:

if(!exec) //basically IF is not used anymore
{
    glTranslatef(fX_cylpos,fY_cylpos,0);
    glRotatef(90, 0.0, 1, 0.0); //rotate object 90° on Y axis
    glRotatef(fangle_cyl, 1.0, 0.0, 0.0); //0° = vertical / 90° = horizontal
    //exec=true;
}
gluCylinder(quadratic,fh_cyl,fh_cyl,fw_cyl,50,1000);            
glPopMatrix();

glPushMatrix();
//draw second cylinder NOT WORKING
glRotatef(mouseY, 0, 0, 1);
quadratic2 = gluNewQuadric();
gluQuadricNormals(quadratic2, GLU_SMOOTH);
gluQuadricTexture(quadratic2, GL_TRUE);
if(!exec2) 
{
    glTranslatef(fX_cylpos+200,fY_cylpos,20);
    glRotatef(90, 0.0, 1, 0.0); //rotate object 90° on Y axis
    glRotatef(fangle_cyl, 1.0, 0.0, 0.0);  //0° = vertical / 90° = horizontal      
    exec2=true;
}
gluCylinder(quadratic2,fh_cyl,fh_cyl,fw_cyl,50,1000);

glPopMatrix();

这是用鼠标移动滚筒后的结果 preview

1 个答案:

答案 0 :(得分:0)

这是错字吗?

    gluQuadricTexture(quadratic2, GL_TRUE);
    gluCylinder(quadratic,fh_cyl,fh_cyl,fw_cyl,50,1000);

- >

    gluQuadricTexture(quadratic2, GL_TRUE);
    gluCylinder(quadratic2,fh_cyl,fh_cyl,fw_cyl,50,1000);