c++ - 同时使用 openGL 绘制两个对象

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

我有几个功能:

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

我不明白为什么我不能同时调用 drawGrid 和 drawAxes;在这种情况下,输出类似于此链接: http://www.4shared.com/photo/8YTsc17s/wrong.html

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

这是我使用的代码:

1。绘制网格

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。绘制轴:

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

关于c++ - 同时使用 openGL 绘制两个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20188045/

相关文章:

c++ - 具有方法指针参数默认值的模板

cocoa - 透明 NSWindow 上的 2D 绘图

java - 测试了一些 LWJGL OpenGL,不会生成盒子。我应该怎么办?

windows - Windows 中供应商的 OpenGL 驱动程序在哪里?

c++ - 二维 vector 中的 push_back(),正确的语法是什么?

c++ - 什么是内存碎片?

c++ - GTK 显示小部件

visual-studio-2010 - 测试挂起(Chutzpah TimeoutException)

c# - Visual Studio SOAP 服务引用客户端 : see XML message

visual-studio-2010 - Enter.、Backspace 和导航键在 Visual Studio 2010 w/Powertools/Resharper 中没有响应