其他分享
首页 > 其他分享> > c-glMultMatrix在glBegin()内部不起作用

c-glMultMatrix在glBegin()内部不起作用

作者:互联网

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

我设法使用GL_POINTS在这些间隔处绘制了完整的圆点,并根据Frenet框架将它们正确地定向到了直线上.不幸的是,要使用GL_TRIANGLE_STRIP,我相信我需要在一组两个点的圆之间一次绘制一个点.

我遇到的问题是,在glBegin中时,glMultMatrix似乎不起作用.下面的代码将绘制一个点的圆,但是在原点处,而我用来平移和定向该点的圆的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();

解决方法:

The problem I am having, is that the glMultMatrix doesn’t seem to work when inside a glBegin

Unsurprising:

Only a subset of GL commands can be used between glBegin and glEnd.
The commands are
glVertex,
glColor,
glSecondaryColor,
glIndex,
glNormal,
glFogCoord,
glTexCoord,
glMultiTexCoord,
glVertexAttrib,
glEvalCoord,
glEvalPoint,
glArrayElement,
glMaterial, and
glEdgeFlag.
Also,
it is acceptable to use
glCallList or
glCallLists to execute
display lists that include only the preceding commands.
If any other GL command is executed between glBegin and glEnd,
the error flag is set and the command is ignored.

glBegin()之前的glMultMatrix():

//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传递一个大块的’o顶点以一次性完成渲染.

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

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

标签:c,opengl,qt-creator,opengl-1-x
来源: https://codeday.me/bug/20191013/1904875.html