我有几个功能:


drawGrid()->在地面上绘制网格;
drawAxes()->使用gluCylinder绘制轴
Arrows()->在drawAxes中用于将圆锥体绘制为箭头;


我不明白为什么不能同时调用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();


}

最佳答案

把我的评论变成答案。

drawGrid()调用了glBegin(),但没有匹配的调用glEnd()

07-22 01:01