glMultMatrix在glBegin()内部不起作用 [英] glMultMatrix doesn't work inside of glBegin()

查看:96
本文介绍了glMultMatrix在glBegin()内部不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a program that allows me to plot points in 3 space, connects them using a Catmull-Rom Spline, and then draws a cylinder around the Spline. I am using GL_TRIANGLES_STRIP to connect circles of points drawn around the Spline at short intervals, to hopefully connect them all together into a Cylinder around the Spline.

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

I have managed to draw complete circles of points at these intervals, using GL_POINTS, and orientate them correctly to the line with regards to a Frenet Frame. Unfortunately, to use GL_TRIANGLE_STRIP, I believe I need to plot the points one at a time between a set of two circles of points.

我遇到的问题是,在glBegin 中时,glMultMatrix似乎不起作用.下面的代码将绘制一个圆点,但在原点处,而我用来平移和定向圆点的glMultMatrix似乎在glbegin中时并不适用.有解决办法吗?

The problem I am having, is that the glMultMatrix doesn't seem to work when inside a glBegin. The code below will draw a circle of points, but at the origin, and the glMultMatrix, which I use to translate and orientate the circle of points, doesn't seem to apply when inside the glbegin. Is there a solution to this?

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

推荐答案

我遇到的问题是,在glBegin中时,glMultMatrix似乎不起作用

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

毫不奇怪:

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

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.

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

Or apply the transforms client-side and hand OpenGL a big block 'o vertices to render in one go.

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

EDIT: Or pull those matrix multiplies outside the loop entirely:

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

这篇关于glMultMatrix在glBegin()内部不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆