同时使用openGL绘制两个对象

时间:2013-11-25 08:42:50

标签: c++ visual-studio-2010 opengl windows-7-x64

我有几个功能:

  1. drawGrid() - >在地上画一个网格;
  2. drawAxes() - >这是使用gluCylinder绘制轴
  3. 箭头() - >在drawAxes中用于绘制锥形箭头;
  4. 我无法理解为什么我不能同时调用drawGrid和drawAxes;在这种情况下,输出就像这个链接: http://www.4shared.com/photo/8YTsc17s/wrong.html

    如果我评论drawGrid()我可以看到轴很好;但我想同时画它们; http://www.4shared.com/photo/KI3DER-5/Axis.html

    这是我使用的代码:

    1。 drawGrid

    void Golsa::drawGrid(float size, float step)
    {
        // disable lighting
        glDisable(GL_LIGHTING);
    
        glBegin(GL_LINES);
    
        glColor3f(0.3f, 0.3f, 0.3f);
        for(float i=step; i <= size; i+= step)
        {
            glVertex3f(-size, 0,  i);   // lines parallel to X-axis
            glVertex3f( size, 0,  i);
            glVertex3f(-size, 0, -i);   // lines parallel to X-axis
            glVertex3f( size, 0, -i);
    
            glVertex3f( i, 0, -size);   // lines parallel to Z-axis
            glVertex3f( i, 0,  size);
            glVertex3f(-i, 0, -size);   // lines parallel to Z-axis
            glVertex3f(-i, 0,  size);
        }
    
    }
    

    2。 drawAxes:

    void Golsa:: drawAxes(double length)
    
    {
        glPushMatrix();
        glTranslatef(-length,0,0);
        Arrow(0,0,0, 2*length,0,0,0.2);
        glPopMatrix();
    
        glPushMatrix();
        glTranslatef(0,-length,0);
        Arrow(0,0,0, 0,2*length,0,0.2);
        glPopMatrix();
    
        glPushMatrix();
        glTranslatef(0,0,-length);
        Arrow(0,0,0, 0,0,2*length,0.2);
        glPopMatrix();
    }
    

    3。箭头

    void Golsa::Arrow(GLdouble x1,GLdouble y1,GLdouble z1,GLdouble x2,GLdouble y2,GLdouble z2,GLdouble D)
    {
      double x=x2-x1;
      double y=y2-y1;
      double z=z2-z1;
      double L=sqrt(x*x+y*y+z*z);
    
        GLUquadricObj *quadObj;
        GLUquadric* cyl = gluNewQuadric();
        GLUquadric* cy2 = gluNewQuadric();
        GLUquadric* cy3 = gluNewQuadric();
        glPushMatrix ();
    
          glTranslated(x1,y1,z1);
    
          if((x!=0.)||(y!=0.)) {
            glRotated(atan2(y,x)/RADPERDEG,0.,0.,1.);
            glRotated(atan2(sqrt(x*x+y*y),z)/RADPERDEG,0.,1.,0.);
          } else if (z<0){
            glRotated(180,1.,0.,0.);
          }
    
        //glTranslatef(0,0,L-4*D);
    
          gluQuadricDrawStyle(cyl, GLU_FILL);
          gluQuadricNormals(cyl, GLU_SMOOTH);
    
          glTranslatef(0,0,0);
          glColor3f(1,1,1);
          gluQuadricDrawStyle(cyl, GLU_FILL);
          //gluQuadricNormals(cyl, GLU_SMOOTH);
          gluCylinder(cyl, 0.1, 0.1,4.0, 12,1);
    
          //glColor3f (1,1,1); 
          glColor3f(1,1,1);
          glTranslatef(0.0,0.0,4);
          glColor3f(1,1,1);
          gluQuadricNormals(cyl, GLU_SMOOTH);
          gluCylinder(cy2, 0.2, 0,0.4, 12,1);
          gluDeleteQuadric(cyl);
    
        glPopMatrix();
    }
    

    此函数调用其他人:

    void Golsa::drawSub()
    {
        float  Xangle, Yangle, Zangle;
        float Xposition, Yposition, Zposition;
        /*Mat    rvec1i = Mat(3,1,CV_64FC1,Scalar::all(0));
        Mat  tvec1i = Mat(3,1,CV_64FC1,Scalar::all(0));*/
        // set bottom viewport (perspective)
        glViewport(0, 0, windowWidth, windowHeight);
        glScissor(0, 0, windowWidth, windowHeight);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(FOV_Y, windowWidth/(windowHeight/2.0f), 1, 1000);
    
        // switch to modelview matrix
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        // clear buffer
        glClearColor(bgColor[0], bgColor[1], bgColor[2], bgColor[3]);   // background color
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    
        glPushMatrix();
    
        // First, transform the camera (viewing matrix) from world space to eye space
        glTranslatef(0, 0, -cameraDistance);
        glRotatef(cameraAngleX, 1, 0, 0); // pitch
        glRotatef(cameraAngleY, 0, 1, 0); // heading
    
        // draw grid
    
        drawGrid(10, 1);
    
    
        FindingCameraPosition(Xposition,Yposition,Zposition,Xangle,Yangle,Zangle);
    
        glPopMatrix();
    

    }

1 个答案:

答案 0 :(得分:2)

将我的评论转化为答案。

drawGrid()打电话给glBegin(),但没有与glEnd()匹配的电话。