glMultMatrix在glBegin()内部不起作用

时间:2013-12-12 19:40:27

标签: c++ opengl qt-creator opengl-1.x

我正在创建一个程序,允许我在3个空间中绘制点,使用Catmull-Rom Spline连接它们,然后在Spline周围绘制一个圆柱体。我正在使用GL_TRIANGLES_STRIP以短间隔连接在样条曲线周围绘制的圆点,希望将它们全部连接到样条曲线周围的圆柱体中。

我已设法使用GL_POINTS以这些间隔绘制完整的圆点,并将它们正确定位到与Frenet帧相关的线上。不幸的是,要使用GL_TRIANGLE_STRIP,我相信我需要在一组两个点之间一次绘制一个点。

我遇到的问题是,glMultMatrix 内的glBegin似乎不起作用。下面的代码将绘制一个圆点,但在原点,glMultMatrix,我用来翻译和定位圆点,在glbegin内部似乎不适用。有解决方案吗?

  //The matrixes that are applied to the circle of points
  GLfloat M1[16]={
    N1.x(),N1.y(),N1.z(),0,
    B1.x(),B1.y(),B1.z(),0,
    T1.x(),T1.y(),T1.z(),0,
    fromPoint->x,fromPoint->y,fromPoint->z,1
  };

  GLfloat M2[16]={
    N2.x(),N2.y(),N2.z(),0,
    B2.x(),B2.y(),B2.z(),0,
    T2.x(),T2.y(),T2.z(),0,
    toPoint->x,toPoint->y,toPoint->z,1
  };

  glBegin(GL_TRIANGLE_STRIP);
  GLfloat x, y;
  GLfloat radius = 0.4f;
  GLint pointCount = 180;
  for (GLfloat theta = 0; theta < 2*M_PI; theta += (2*M_PI)/pointCount) {
    x = radius * cos(theta);
    y = radius * sin(theta);

    // Now push a matrix, multiply it, draw a point and pop the matrix
    glPushMatrix();
    glMultMatrixf(& M1[0]);
    // Draw the point here
    glVertex3f(x, y, 0);
    glPopMatrix();

    // Do the same again for the second section
    glPushMatrix();
    glMultMatrixf(& M2[0]);
    glVertex3f(x, y, 0);
    glPopMatrix();
  }
  glEnd();

2 个答案:

答案 0 :(得分:5)

  

我遇到的问题是,glBegin内部的glMultMatrix似乎不起作用

Unsurprising:

  

glBegin和glEnd之间只能使用GL命令的子集。               命令是               glVertex,               glColor,               glSecondaryColor,               glIndex,               glNormal,               glFogCoord,               glTexCoord,               glMultiTexCoord,               glVertexAttrib,               glEvalCoord,               glEvalPoint,               glArrayElement,               glMaterial,和               glEdgeFlag。               也,               可以接受使用               glCallList或               glCallLists执行               显示仅包含上述命令的列表。               如果在glBegin和glEnd之间执行任何其他GL命令,               设置错误标志并忽略该命令。

glMultMatrix() 之前 glBegin()

//The matrixes that are applied to the circle of points
GLfloat M1[16]=
{
    N1.x(),N1.y(),N1.z(),0,
    B1.x(),B1.y(),B1.z(),0,
    T1.x(),T1.y(),T1.z(),0,
    fromPoint->x,fromPoint->y,fromPoint->z,1
};

GLfloat M2[16]=
{
    N2.x(),N2.y(),N2.z(),0,
    B2.x(),B2.y(),B2.z(),0,
    T2.x(),T2.y(),T2.z(),0,
    toPoint->x,toPoint->y,toPoint->z,1
};

GLfloat x, y;
GLfloat radius = 0.4f;
GLint pointCount = 180;
for (GLfloat theta = 0; theta < 2*M_PI; theta += (2*M_PI)/pointCount) 
{
    x = radius * cos(theta);
    y = radius * sin(theta);

    // Now push a matrix, multiply it, draw a point and pop the matrix
    glPushMatrix();
    glMultMatrixf(& M1[0]);
    // Draw the point here
    glBegin(GL_POINTS);
    glVertex3f(x, y, 0);
    glEnd();
    glPopMatrix();

    // Do the same again for the second section
    glPushMatrix();
    glMultMatrixf(& M2[0]);
    glBegin(GL_POINTS);
    glVertex3f(x, y, 0);
    glEnd();
    glPopMatrix();
}

或者应用转换客户端并手动OpenGL一个大块的顶点来一次渲染。

编辑:或者完全将这些矩阵乘法拉出循环:

GLfloat x, y;
GLfloat radius = 0.4f;
GLint pointCount = 180;

glPushMatrix();
glMultMatrixf(& M1[0]);
glBegin(GL_POINTS);
for (GLfloat theta = 0; theta < 2*M_PI; theta += (2*M_PI)/pointCount) 
{
    x = radius * cos(theta);
    y = radius * sin(theta);

    // Draw the point here
    glVertex3f(x, y, 0);
}
glEnd();
glPopMatrix();

glPushMatrix();
glMultMatrixf(& M2[0]);
glBegin(GL_POINTS);
for (GLfloat theta = 0; theta < 2*M_PI; theta += (2*M_PI)/pointCount) 
{
    x = radius * cos(theta);
    y = radius * sin(theta);

    // Draw the point here
    glVertex3f(x, y, 0);
}
glEnd();
glPopMatrix();

答案 1 :(得分:0)

您还可以使用顶点数组来保存所有信息,然后将数据推送到openGL:

GLFloat[] points = new GLFloat[3*pointCount];

for (GLfloat theta = 0; theta < 2*M_PI; theta += (2*M_PI)/pointCount) 
{
    points[0+off] = radius * cos(theta);
    points[1+off] = radius * sin(theta);
    points[2+off] = 0;
    off+=3;
}

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, points);

glPushMatrix();
glMultMatrixf(& M1[0]);
glDrawArrays(GL_POINTS, 0, pointCount);
glPopMatrix();

glPushMatrix();
glMultMatrixf(& M2[0]);
glDrawArrays(GL_POINTS, 0, pointCount);
glPopMatrix();

glDisableClientState(GL_VERTEX_ARRAY);
delete[] points;