簡體   English   中英

同時使用openGL繪制兩個對象

[英]draw two objects using openGL in a same time

我有幾個功能:

  1. drawGrid() - >在地面上繪制網格;
  2. drawAxes() - >使用gluCylinder繪制軸
  3. 箭頭() - >在drawAxes中用於繪制錐體作為箭頭;

我無法理解為什么我不能同時調用drawGrid和drawAxes; 在這種情況下,輸出就像這個鏈接: http//www.4shared.com/photo/8YTsc17s/wrong.html

如果我評論drawGrid()我可以看到軸很好; 但我想同時畫它們; http://www.4shared.com/photo/KI3DER-5/Axis.html

這是我使用的代碼:

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();
}

箭頭

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM